blob: 46694cbdf8ca0c07062c02860fa265f3fe57ac13 [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"
Godbachb3016542014-12-17 16:32:05 +0800127 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100128
Willy Tarreau0f772532006-12-23 20:51:41 +0100129
130const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200131 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100132 [HTTP_ERR_400] = 400,
133 [HTTP_ERR_403] = 403,
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 :
Willy Tarreaubcd03362015-09-03 17:15:21 +0200364 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100365 */
366};
367
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100368const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100369 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
370 [HTTP_METH_GET] = { "GET", 3 },
371 [HTTP_METH_HEAD] = { "HEAD", 4 },
372 [HTTP_METH_POST] = { "POST", 4 },
373 [HTTP_METH_PUT] = { "PUT", 3 },
374 [HTTP_METH_DELETE] = { "DELETE", 6 },
375 [HTTP_METH_TRACE] = { "TRACE", 5 },
376 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
377};
378
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100379/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200380 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100381 * RFC2616 for those chars.
382 */
383
384const char http_is_spht[256] = {
385 [' '] = 1, ['\t'] = 1,
386};
387
388const char http_is_crlf[256] = {
389 ['\r'] = 1, ['\n'] = 1,
390};
391
392const char http_is_lws[256] = {
393 [' '] = 1, ['\t'] = 1,
394 ['\r'] = 1, ['\n'] = 1,
395};
396
397const char http_is_sep[256] = {
398 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
399 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
400 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
401 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
402 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
403};
404
405const char http_is_ctl[256] = {
406 [0 ... 31] = 1,
407 [127] = 1,
408};
409
410/*
411 * A token is any ASCII char that is neither a separator nor a CTL char.
412 * Do not overwrite values in assignment since gcc-2.95 will not handle
413 * them correctly. Instead, define every non-CTL char's status.
414 */
415const char http_is_token[256] = {
416 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
417 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
418 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
419 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
420 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
421 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
422 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
423 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
424 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
425 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
426 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
427 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
428 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
429 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
430 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
431 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
432 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
433 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
434 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
435 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
436 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
437 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
438 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
439 ['|'] = 1, ['}'] = 0, ['~'] = 1,
440};
441
442
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100443/*
444 * An http ver_token is any ASCII which can be found in an HTTP version,
445 * which includes 'H', 'T', 'P', '/', '.' and any digit.
446 */
447const char http_is_ver_token[256] = {
448 ['.'] = 1, ['/'] = 1,
449 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
450 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
451 ['H'] = 1, ['P'] = 1, ['T'] = 1,
452};
453
454
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100455/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100456 * Adds a header and its CRLF at the tail of the message's buffer, just before
457 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100458 * The header is also automatically added to the index <hdr_idx>, and the end
459 * of headers is automatically adjusted. The number of bytes added is returned
460 * on success, otherwise <0 is returned indicating an error.
461 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100462int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100463{
464 int bytes, len;
465
466 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200467 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100468 if (!bytes)
469 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100470 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100471 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
472}
473
474/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100475 * Adds a header and its CRLF at the tail of the message's buffer, just before
476 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100477 * the buffer is only opened and the space reserved, but nothing is copied.
478 * The header is also automatically added to the index <hdr_idx>, and the end
479 * of headers is automatically adjusted. The number of bytes added is returned
480 * on success, otherwise <0 is returned indicating an error.
481 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100482int http_header_add_tail2(struct http_msg *msg,
483 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100484{
485 int bytes;
486
Willy Tarreau9b28e032012-10-12 23:49:43 +0200487 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100488 if (!bytes)
489 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100490 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100491 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
492}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200493
494/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100495 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
496 * If so, returns the position of the first non-space character relative to
497 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
498 * to return a pointer to the place after the first space. Returns 0 if the
499 * header name does not match. Checks are case-insensitive.
500 */
501int http_header_match2(const char *hdr, const char *end,
502 const char *name, int len)
503{
504 const char *val;
505
506 if (hdr + len >= end)
507 return 0;
508 if (hdr[len] != ':')
509 return 0;
510 if (strncasecmp(hdr, name, len) != 0)
511 return 0;
512 val = hdr + len + 1;
513 while (val < end && HTTP_IS_SPHT(*val))
514 val++;
515 if ((val >= end) && (len + 2 <= end - hdr))
516 return len + 2; /* we may replace starting from second space */
517 return val - hdr;
518}
519
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200520/* Find the first or next occurrence of header <name> in message buffer <sol>
521 * using headers index <idx>, and return it in the <ctx> structure. This
522 * structure holds everything necessary to use the header and find next
523 * occurrence. If its <idx> member is 0, the header is searched from the
524 * beginning. Otherwise, the next occurrence is returned. The function returns
525 * 1 when it finds a value, and 0 when there is no more. It is very similar to
526 * http_find_header2() except that it is designed to work with full-line headers
527 * whose comma is not a delimiter but is part of the syntax. As a special case,
528 * if ctx->val is NULL when searching for a new values of a header, the current
529 * header is rescanned. This allows rescanning after a header deletion.
530 */
531int http_find_full_header2(const char *name, int len,
532 char *sol, struct hdr_idx *idx,
533 struct hdr_ctx *ctx)
534{
535 char *eol, *sov;
536 int cur_idx, old_idx;
537
538 cur_idx = ctx->idx;
539 if (cur_idx) {
540 /* We have previously returned a header, let's search another one */
541 sol = ctx->line;
542 eol = sol + idx->v[cur_idx].len;
543 goto next_hdr;
544 }
545
546 /* first request for this header */
547 sol += hdr_idx_first_pos(idx);
548 old_idx = 0;
549 cur_idx = hdr_idx_first_idx(idx);
550 while (cur_idx) {
551 eol = sol + idx->v[cur_idx].len;
552
553 if (len == 0) {
554 /* No argument was passed, we want any header.
555 * To achieve this, we simply build a fake request. */
556 while (sol + len < eol && sol[len] != ':')
557 len++;
558 name = sol;
559 }
560
561 if ((len < eol - sol) &&
562 (sol[len] == ':') &&
563 (strncasecmp(sol, name, len) == 0)) {
564 ctx->del = len;
565 sov = sol + len + 1;
566 while (sov < eol && http_is_lws[(unsigned char)*sov])
567 sov++;
568
569 ctx->line = sol;
570 ctx->prev = old_idx;
571 ctx->idx = cur_idx;
572 ctx->val = sov - sol;
573 ctx->tws = 0;
574 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
575 eol--;
576 ctx->tws++;
577 }
578 ctx->vlen = eol - sov;
579 return 1;
580 }
581 next_hdr:
582 sol = eol + idx->v[cur_idx].cr + 1;
583 old_idx = cur_idx;
584 cur_idx = idx->v[cur_idx].next;
585 }
586 return 0;
587}
588
Willy Tarreau68085d82010-01-18 14:54:04 +0100589/* Find the end of the header value contained between <s> and <e>. See RFC2616,
590 * par 2.2 for more information. Note that it requires a valid header to return
591 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200592 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100593char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200594{
595 int quoted, qdpair;
596
597 quoted = qdpair = 0;
598 for (; s < e; s++) {
599 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200600 else if (quoted) {
601 if (*s == '\\') qdpair = 1;
602 else if (*s == '"') quoted = 0;
603 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200604 else if (*s == '"') quoted = 1;
605 else if (*s == ',') return s;
606 }
607 return s;
608}
609
610/* Find the first or next occurrence of header <name> in message buffer <sol>
611 * using headers index <idx>, and return it in the <ctx> structure. This
612 * structure holds everything necessary to use the header and find next
613 * occurrence. If its <idx> member is 0, the header is searched from the
614 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100615 * 1 when it finds a value, and 0 when there is no more. It is designed to work
616 * with headers defined as comma-separated lists. As a special case, if ctx->val
617 * is NULL when searching for a new values of a header, the current header is
618 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200619 */
620int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100621 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200622 struct hdr_ctx *ctx)
623{
Willy Tarreau68085d82010-01-18 14:54:04 +0100624 char *eol, *sov;
625 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200626
Willy Tarreau68085d82010-01-18 14:54:04 +0100627 cur_idx = ctx->idx;
628 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200629 /* We have previously returned a value, let's search
630 * another one on the same line.
631 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200632 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200633 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100634 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200635 eol = sol + idx->v[cur_idx].len;
636
637 if (sov >= eol)
638 /* no more values in this header */
639 goto next_hdr;
640
Willy Tarreau68085d82010-01-18 14:54:04 +0100641 /* values remaining for this header, skip the comma but save it
642 * for later use (eg: for header deletion).
643 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200644 sov++;
645 while (sov < eol && http_is_lws[(unsigned char)*sov])
646 sov++;
647
648 goto return_hdr;
649 }
650
651 /* first request for this header */
652 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100653 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200654 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200655 while (cur_idx) {
656 eol = sol + idx->v[cur_idx].len;
657
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200658 if (len == 0) {
659 /* No argument was passed, we want any header.
660 * To achieve this, we simply build a fake request. */
661 while (sol + len < eol && sol[len] != ':')
662 len++;
663 name = sol;
664 }
665
Willy Tarreau33a7e692007-06-10 19:45:56 +0200666 if ((len < eol - sol) &&
667 (sol[len] == ':') &&
668 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100669 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200670 sov = sol + len + 1;
671 while (sov < eol && http_is_lws[(unsigned char)*sov])
672 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100673
Willy Tarreau33a7e692007-06-10 19:45:56 +0200674 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100675 ctx->prev = old_idx;
676 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200677 ctx->idx = cur_idx;
678 ctx->val = sov - sol;
679
680 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200681 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200682 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200683 eol--;
684 ctx->tws++;
685 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200686 ctx->vlen = eol - sov;
687 return 1;
688 }
689 next_hdr:
690 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100691 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200692 cur_idx = idx->v[cur_idx].next;
693 }
694 return 0;
695}
696
697int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100698 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200699 struct hdr_ctx *ctx)
700{
701 return http_find_header2(name, strlen(name), sol, idx, ctx);
702}
703
Willy Tarreau68085d82010-01-18 14:54:04 +0100704/* Remove one value of a header. This only works on a <ctx> returned by one of
705 * the http_find_header functions. The value is removed, as well as surrounding
706 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100707 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100708 * message <msg>. The new index is returned. If it is zero, it means there is
709 * no more header, so any processing may stop. The ctx is always left in a form
710 * that can be handled by http_find_header2() to find next occurrence.
711 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100712int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100713{
714 int cur_idx = ctx->idx;
715 char *sol = ctx->line;
716 struct hdr_idx_elem *hdr;
717 int delta, skip_comma;
718
719 if (!cur_idx)
720 return 0;
721
722 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200723 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100724 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200725 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100726 http_msg_move_end(msg, delta);
727 idx->used--;
728 hdr->len = 0; /* unused entry */
729 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100730 if (idx->tail == ctx->idx)
731 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100732 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau14acc652015-01-07 17:23:50 +0100733 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100734 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200735 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100736 return ctx->idx;
737 }
738
739 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200740 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
741 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100742 */
743
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200744 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200745 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200746 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100747 NULL, 0);
748 hdr->len += delta;
749 http_msg_move_end(msg, delta);
750 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200751 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100752 return ctx->idx;
753}
754
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100755/* This function handles a server error at the stream interface level. The
756 * stream interface is assumed to be already in a closed state. An optional
757 * message is copied into the input buffer, and an HTTP status code stored.
758 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100759 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200760 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200761static void http_server_error(struct session *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100762 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200763{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200764 channel_auto_read(si->ob);
765 channel_abort(si->ob);
766 channel_auto_close(si->ob);
767 channel_erase(si->ob);
768 channel_auto_close(si->ib);
769 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100770 if (status > 0 && msg) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200771 s->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200772 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200773 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200774 if (!(s->flags & SN_ERR_MASK))
775 s->flags |= err;
776 if (!(s->flags & SN_FINST_MASK))
777 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200778}
779
Willy Tarreau80587432006-12-24 17:47:20 +0100780/* This function returns the appropriate error location for the given session
781 * and message.
782 */
783
Willy Tarreau783f2582012-09-04 12:19:04 +0200784struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100785{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200786 if (s->be->errmsg[msgnum].str)
787 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100788 else if (s->fe->errmsg[msgnum].str)
789 return &s->fe->errmsg[msgnum];
790 else
791 return &http_err_chunks[msgnum];
792}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200793
Willy Tarreau53b6c742006-12-17 13:37:46 +0100794/*
Willy Tarreaubcd03362015-09-03 17:15:21 +0200795 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
796 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100797 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100798enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100799{
800 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100801 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100802
803 m = ((unsigned)*str - 'A');
804
805 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100806 for (h = http_methods[m]; h->len > 0; h++) {
807 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100808 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100809 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100810 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100811 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100812 }
Willy Tarreaubcd03362015-09-03 17:15:21 +0200813 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100814}
815
Willy Tarreau21d2af32008-02-14 20:25:24 +0100816/* Parse the URI from the given transaction (which is assumed to be in request
817 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
818 * It is returned otherwise.
819 */
820static char *
821http_get_path(struct http_txn *txn)
822{
823 char *ptr, *end;
824
Willy Tarreau9b28e032012-10-12 23:49:43 +0200825 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100826 end = ptr + txn->req.sl.rq.u_l;
827
828 if (ptr >= end)
829 return NULL;
830
831 /* RFC2616, par. 5.1.2 :
832 * Request-URI = "*" | absuri | abspath | authority
833 */
834
835 if (*ptr == '*')
836 return NULL;
837
838 if (isalpha((unsigned char)*ptr)) {
839 /* this is a scheme as described by RFC3986, par. 3.1 */
840 ptr++;
841 while (ptr < end &&
842 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
843 ptr++;
844 /* skip '://' */
845 if (ptr == end || *ptr++ != ':')
846 return NULL;
847 if (ptr == end || *ptr++ != '/')
848 return NULL;
849 if (ptr == end || *ptr++ != '/')
850 return NULL;
851 }
852 /* skip [user[:passwd]@]host[:[port]] */
853
854 while (ptr < end && *ptr != '/')
855 ptr++;
856
857 if (ptr == end)
858 return NULL;
859
860 /* OK, we got the '/' ! */
861 return ptr;
862}
863
William Lallemand65ad6e12014-01-31 15:08:02 +0100864/* Parse the URI from the given string and look for the "/" beginning the PATH.
865 * If not found, return NULL. It is returned otherwise.
866 */
867static char *
868http_get_path_from_string(char *str)
869{
870 char *ptr = str;
871
872 /* RFC2616, par. 5.1.2 :
873 * Request-URI = "*" | absuri | abspath | authority
874 */
875
876 if (*ptr == '*')
877 return NULL;
878
879 if (isalpha((unsigned char)*ptr)) {
880 /* this is a scheme as described by RFC3986, par. 3.1 */
881 ptr++;
882 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
883 ptr++;
884 /* skip '://' */
885 if (*ptr == '\0' || *ptr++ != ':')
886 return NULL;
887 if (*ptr == '\0' || *ptr++ != '/')
888 return NULL;
889 if (*ptr == '\0' || *ptr++ != '/')
890 return NULL;
891 }
892 /* skip [user[:passwd]@]host[:[port]] */
893
894 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
895 ptr++;
896
897 if (*ptr == '\0' || *ptr == ' ')
898 return NULL;
899
900 /* OK, we got the '/' ! */
901 return ptr;
902}
903
Willy Tarreau71241ab2012-12-27 11:30:54 +0100904/* Returns a 302 for a redirectable request that reaches a server working in
905 * in redirect mode. This may only be called just after the stream interface
906 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
907 * follow normal proxy processing. NOTE: this function is designed to support
908 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100909 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100910void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100911{
912 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100913 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100914 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200915 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100916
917 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100918 trash.len = strlen(HTTP_302);
919 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100920
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100921 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100922
Willy Tarreauefb453c2008-10-26 20:49:47 +0100923 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100924 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100925 return;
926
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100927 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100928 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100929 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
930 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100931 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100932
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200933 /* 3: add the request URI. Since it was already forwarded, we need
934 * to temporarily rewind the buffer.
935 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100936 txn = &s->txn;
Willy Tarreau211cdec2014-04-17 20:18:08 +0200937 b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200938
Willy Tarreauefb453c2008-10-26 20:49:47 +0100939 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200940 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 +0200941
Willy Tarreau9b28e032012-10-12 23:49:43 +0200942 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200943
Willy Tarreauefb453c2008-10-26 20:49:47 +0100944 if (!path)
945 return;
946
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100947 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100948 return;
949
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100950 memcpy(trash.str + trash.len, path, len);
951 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100952
953 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100954 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
955 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100956 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100957 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
958 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100959 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100960
961 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200962 si_shutr(si);
963 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100964 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100965 si->state = SI_ST_CLO;
966
967 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200968 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100969
970 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100971 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500972 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100973}
974
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100975/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100976 * that the server side is closed. Note that err_type is actually a
977 * bitmask, where almost only aborts may be cumulated with other
978 * values. We consider that aborted operations are more important
979 * than timeouts or errors due to the fact that nobody else in the
980 * logs might explain incomplete retries. All others should avoid
981 * being cumulated. It should normally not be possible to have multiple
982 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100983 * Note that connection errors appearing on the second request of a keep-alive
984 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100985 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100986void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100987{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100988 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100989
990 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100991 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200992 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100993 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100994 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100995 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
996 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100997 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100998 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200999 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001000 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001001 http_server_error(s, si, SN_ERR_SRVCL, 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_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001004 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001005 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1006 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001007 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001008 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001009 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001010 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001011 else if (err_type & SI_ET_CONN_RES)
1012 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001013 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1014 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001015 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001016 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001017 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001018}
1019
Willy Tarreau42250582007-04-01 01:30:43 +02001020extern const char sess_term_cond[8];
1021extern const char sess_fin_state[8];
1022extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001023struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001024struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001025struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001026
Willy Tarreau117f59e2007-03-04 18:17:17 +01001027/*
1028 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001029 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001030 */
1031void capture_headers(char *som, struct hdr_idx *idx,
1032 char **cap, struct cap_hdr *cap_hdr)
1033{
1034 char *eol, *sol, *col, *sov;
1035 int cur_idx;
1036 struct cap_hdr *h;
1037 int len;
1038
1039 sol = som + hdr_idx_first_pos(idx);
1040 cur_idx = hdr_idx_first_idx(idx);
1041
1042 while (cur_idx) {
1043 eol = sol + idx->v[cur_idx].len;
1044
1045 col = sol;
1046 while (col < eol && *col != ':')
1047 col++;
1048
1049 sov = col + 1;
1050 while (sov < eol && http_is_lws[(unsigned char)*sov])
1051 sov++;
1052
1053 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001054 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001055 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1056 if (cap[h->index] == NULL)
1057 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001058 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001059
1060 if (cap[h->index] == NULL) {
1061 Alert("HTTP capture : out of memory.\n");
1062 continue;
1063 }
1064
1065 len = eol - sov;
1066 if (len > h->len)
1067 len = h->len;
1068
1069 memcpy(cap[h->index], sov, len);
1070 cap[h->index][len]=0;
1071 }
1072 }
1073 sol = eol + idx->v[cur_idx].cr + 1;
1074 cur_idx = idx->v[cur_idx].next;
1075 }
1076}
1077
1078
Willy Tarreau42250582007-04-01 01:30:43 +02001079/* either we find an LF at <ptr> or we jump to <bad>.
1080 */
1081#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1082
1083/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1084 * otherwise to <http_msg_ood> with <state> set to <st>.
1085 */
1086#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1087 ptr++; \
1088 if (likely(ptr < end)) \
1089 goto good; \
1090 else { \
1091 state = (st); \
1092 goto http_msg_ood; \
1093 } \
1094 } while (0)
1095
1096
Willy Tarreaubaaee002006-06-26 02:48:02 +02001097/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001098 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001099 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1100 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1101 * will give undefined results.
1102 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1103 * and that msg->sol points to the beginning of the response.
1104 * If a complete line is found (which implies that at least one CR or LF is
1105 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1106 * returned indicating an incomplete line (which does not mean that parts have
1107 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1108 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1109 * upon next call.
1110 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001111 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001112 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1113 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001114 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001115 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001116const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001117 enum ht_state state, const char *ptr, const char *end,
1118 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001119{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001120 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001121
Willy Tarreau8973c702007-01-21 23:58:29 +01001122 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001123 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001124 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001125 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001126 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1127
1128 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001129 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001130 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1131 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001132 state = HTTP_MSG_ERROR;
1133 break;
1134
Willy Tarreau8973c702007-01-21 23:58:29 +01001135 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001136 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001137 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001138 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001139 goto http_msg_rpcode;
1140 }
1141 if (likely(HTTP_IS_SPHT(*ptr)))
1142 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1143 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001144 state = HTTP_MSG_ERROR;
1145 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001146
Willy Tarreau8973c702007-01-21 23:58:29 +01001147 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001148 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001149 if (likely(!HTTP_IS_LWS(*ptr)))
1150 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1151
1152 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001153 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001154 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1155 }
1156
1157 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001158 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001159 http_msg_rsp_reason:
1160 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001161 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001162 msg->sl.st.r_l = 0;
1163 goto http_msg_rpline_eol;
1164
Willy Tarreau8973c702007-01-21 23:58:29 +01001165 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001166 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001167 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001168 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001169 goto http_msg_rpreason;
1170 }
1171 if (likely(HTTP_IS_SPHT(*ptr)))
1172 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1173 /* so it's a CR/LF, so there is no reason phrase */
1174 goto http_msg_rsp_reason;
1175
Willy Tarreau8973c702007-01-21 23:58:29 +01001176 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001177 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001178 if (likely(!HTTP_IS_CRLF(*ptr)))
1179 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001180 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001181 http_msg_rpline_eol:
1182 /* We have seen the end of line. Note that we do not
1183 * necessarily have the \n yet, but at least we know that we
1184 * have EITHER \r OR \n, otherwise the response would not be
1185 * complete. We can then record the response length and return
1186 * to the caller which will be able to register it.
1187 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001188 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001189 return ptr;
1190
Willy Tarreau8973c702007-01-21 23:58:29 +01001191 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001192#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001193 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1194 exit(1);
1195#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001196 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001197 }
1198
1199 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001200 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001201 if (ret_state)
1202 *ret_state = state;
1203 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001204 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001205 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001206}
1207
Willy Tarreau8973c702007-01-21 23:58:29 +01001208/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001209 * This function parses a request line between <ptr> and <end>, starting with
1210 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1211 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1212 * will give undefined results.
1213 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1214 * and that msg->sol points to the beginning of the request.
1215 * If a complete line is found (which implies that at least one CR or LF is
1216 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1217 * returned indicating an incomplete line (which does not mean that parts have
1218 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1219 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1220 * upon next call.
1221 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001222 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001223 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1224 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001225 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001226 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001227const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001228 enum ht_state state, const char *ptr, const char *end,
1229 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001230{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001231 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001232
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001233 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001234 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001235 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 if (likely(HTTP_IS_TOKEN(*ptr)))
1237 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001238
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001240 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001241 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1242 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001243
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001244 if (likely(HTTP_IS_CRLF(*ptr))) {
1245 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001246 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001247 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001248 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001249 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001250 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001251 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001252 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001253 msg->sl.rq.v_l = 0;
1254 goto http_msg_rqline_eol;
1255 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001256 state = HTTP_MSG_ERROR;
1257 break;
1258
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001259 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001260 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001262 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001263 goto http_msg_rquri;
1264 }
1265 if (likely(HTTP_IS_SPHT(*ptr)))
1266 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1267 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1268 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001269
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001270 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001271 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001272 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001274
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001275 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001276 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001277 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1278 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001279
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001280 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001281 /* non-ASCII chars are forbidden unless option
1282 * accept-invalid-http-request is enabled in the frontend.
1283 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001284 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001285 if (msg->err_pos < -1)
1286 goto invalid_char;
1287 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001288 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001289 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1290 }
1291
1292 if (likely(HTTP_IS_CRLF(*ptr))) {
1293 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1294 goto http_msg_req09_uri_e;
1295 }
1296
1297 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001298 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001299 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001300 state = HTTP_MSG_ERROR;
1301 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001302
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001303 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001304 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001305 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001306 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001307 goto http_msg_rqver;
1308 }
1309 if (likely(HTTP_IS_SPHT(*ptr)))
1310 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1311 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1312 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001313
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001314 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001315 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001316 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001318
1319 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001320 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001321 http_msg_rqline_eol:
1322 /* We have seen the end of line. Note that we do not
1323 * necessarily have the \n yet, but at least we know that we
1324 * have EITHER \r OR \n, otherwise the request would not be
1325 * complete. We can then record the request length and return
1326 * to the caller which will be able to register it.
1327 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001328 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001329 return ptr;
1330 }
1331
1332 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001333 state = HTTP_MSG_ERROR;
1334 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001335
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001336 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001337#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001338 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1339 exit(1);
1340#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001341 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001342 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001343
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001344 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001345 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001346 if (ret_state)
1347 *ret_state = state;
1348 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001349 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001351}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001352
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001353/*
1354 * Returns the data from Authorization header. Function may be called more
1355 * than once so data is stored in txn->auth_data. When no header is found
1356 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001357 * searching again for something we are unable to find anyway. However, if
1358 * the result if valid, the cache is not reused because we would risk to
1359 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001360 */
1361
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001362/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1363 * set according to global.tune.bufsize.
1364 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001365char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001366
1367int
1368get_http_auth(struct session *s)
1369{
1370
1371 struct http_txn *txn = &s->txn;
1372 struct chunk auth_method;
1373 struct hdr_ctx ctx;
1374 char *h, *p;
1375 int len;
1376
1377#ifdef DEBUG_AUTH
1378 printf("Auth for session %p: %d\n", s, txn->auth.method);
1379#endif
1380
1381 if (txn->auth.method == HTTP_AUTH_WRONG)
1382 return 0;
1383
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001384 txn->auth.method = HTTP_AUTH_WRONG;
1385
1386 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001387
1388 if (txn->flags & TX_USE_PX_CONN) {
1389 h = "Proxy-Authorization";
1390 len = strlen(h);
1391 } else {
1392 h = "Authorization";
1393 len = strlen(h);
1394 }
1395
Willy Tarreau9b28e032012-10-12 23:49:43 +02001396 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001397 return 0;
1398
1399 h = ctx.line + ctx.val;
1400
1401 p = memchr(h, ' ', ctx.vlen);
1402 if (!p || p == h)
1403 return 0;
1404
1405 chunk_initlen(&auth_method, h, 0, p-h);
1406 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1407
1408 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1409
1410 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001411 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001412
1413 if (len < 0)
1414 return 0;
1415
1416
1417 get_http_auth_buff[len] = '\0';
1418
1419 p = strchr(get_http_auth_buff, ':');
1420
1421 if (!p)
1422 return 0;
1423
1424 txn->auth.user = get_http_auth_buff;
1425 *p = '\0';
1426 txn->auth.pass = p+1;
1427
1428 txn->auth.method = HTTP_AUTH_BASIC;
1429 return 1;
1430 }
1431
1432 return 0;
1433}
1434
Willy Tarreau58f10d72006-12-04 02:26:12 +01001435
Willy Tarreau8973c702007-01-21 23:58:29 +01001436/*
1437 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001438 * depending on the initial msg->msg_state. The caller is responsible for
1439 * ensuring that the message does not wrap. The function can be preempted
1440 * everywhere when data are missing and recalled at the exact same location
1441 * with no information loss. The message may even be realigned between two
1442 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001443 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001444 * fields. Note that msg->sol will be initialized after completing the first
1445 * state, so that none of the msg pointers has to be initialized prior to the
1446 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001447 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001448void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449{
Willy Tarreau3770f232013-12-07 00:01:53 +01001450 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001452 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001453
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001454 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001455 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001456 ptr = buf->p + msg->next;
1457 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001458
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 if (unlikely(ptr >= end))
1460 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001461
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001463 /*
1464 * First, states that are specific to the response only.
1465 * We check them first so that request and headers are
1466 * closer to each other (accessed more often).
1467 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001468 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001469 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001470 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001471 /* we have a start of message, but we have to check
1472 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001473 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001474 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001475 if (unlikely(ptr != buf->p)) {
1476 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001477 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001478 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001479 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001480 }
Willy Tarreau26927362012-05-18 23:22:52 +02001481 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001482 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001483 hdr_idx_init(idx);
1484 state = HTTP_MSG_RPVER;
1485 goto http_msg_rpver;
1486 }
1487
1488 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1489 goto http_msg_invalid;
1490
1491 if (unlikely(*ptr == '\n'))
1492 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1493 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1494 /* stop here */
1495
Willy Tarreau8973c702007-01-21 23:58:29 +01001496 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001497 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001498 EXPECT_LF_HERE(ptr, http_msg_invalid);
1499 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1500 /* stop here */
1501
Willy Tarreau8973c702007-01-21 23:58:29 +01001502 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001503 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001504 case HTTP_MSG_RPVER_SP:
1505 case HTTP_MSG_RPCODE:
1506 case HTTP_MSG_RPCODE_SP:
1507 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001508 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001509 state, ptr, end,
1510 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001511 if (unlikely(!ptr))
1512 return;
1513
1514 /* we have a full response and we know that we have either a CR
1515 * or an LF at <ptr>.
1516 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001517 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1518
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001519 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001520 if (likely(*ptr == '\r'))
1521 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1522 goto http_msg_rpline_end;
1523
Willy Tarreau8973c702007-01-21 23:58:29 +01001524 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001525 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001526 /* msg->sol must point to the first of CR or LF. */
1527 EXPECT_LF_HERE(ptr, http_msg_invalid);
1528 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1529 /* stop here */
1530
1531 /*
1532 * Second, states that are specific to the request only
1533 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001535 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001537 /* we have a start of message, but we have to check
1538 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001539 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001540 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001541 if (likely(ptr != buf->p)) {
1542 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001543 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001544 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001545 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 }
Willy Tarreau26927362012-05-18 23:22:52 +02001547 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001548 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001549 state = HTTP_MSG_RQMETH;
1550 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001552
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1554 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001555
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 if (unlikely(*ptr == '\n'))
1557 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1558 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001559 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001560
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001562 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 EXPECT_LF_HERE(ptr, http_msg_invalid);
1564 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001565 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001566
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001568 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 case HTTP_MSG_RQMETH_SP:
1570 case HTTP_MSG_RQURI:
1571 case HTTP_MSG_RQURI_SP:
1572 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001573 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001574 state, ptr, end,
1575 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001576 if (unlikely(!ptr))
1577 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001578
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 /* we have a full request and we know that we have either a CR
1580 * or an LF at <ptr>.
1581 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001583
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001584 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 if (likely(*ptr == '\r'))
1586 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001587 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001588
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001590 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001591 /* check for HTTP/0.9 request : no version information available.
1592 * msg->sol must point to the first of CR or LF.
1593 */
1594 if (unlikely(msg->sl.rq.v_l == 0))
1595 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001596
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 EXPECT_LF_HERE(ptr, http_msg_invalid);
1598 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001599 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001600
Willy Tarreau8973c702007-01-21 23:58:29 +01001601 /*
1602 * Common states below
1603 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001604 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001605 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001606 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 if (likely(!HTTP_IS_CRLF(*ptr))) {
1608 goto http_msg_hdr_name;
1609 }
1610
1611 if (likely(*ptr == '\r'))
1612 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1613 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001614
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001616 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001617 /* assumes msg->sol points to the first char */
1618 if (likely(HTTP_IS_TOKEN(*ptr)))
1619 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001620
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001621 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001622 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001623
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001624 if (likely(msg->err_pos < -1) || *ptr == '\n')
1625 goto http_msg_invalid;
1626
1627 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001628 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001629
1630 /* and we still accept this non-token character */
1631 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001632
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001633 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001634 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001635 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 if (likely(HTTP_IS_SPHT(*ptr)))
1637 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001638
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001640 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 if (likely(!HTTP_IS_CRLF(*ptr))) {
1643 goto http_msg_hdr_val;
1644 }
1645
1646 if (likely(*ptr == '\r'))
1647 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1648 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001649
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001650 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001651 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001652 EXPECT_LF_HERE(ptr, http_msg_invalid);
1653 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001654
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001655 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001656 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001657 if (likely(HTTP_IS_SPHT(*ptr))) {
1658 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001659 for (; buf->p + msg->sov < ptr; msg->sov++)
1660 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001661 goto http_msg_hdr_l1_sp;
1662 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001663 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001664 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001665 goto http_msg_complete_header;
1666
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001667 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001668 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001669 /* assumes msg->sol points to the first char, and msg->sov
1670 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 */
1672 if (likely(!HTTP_IS_CRLF(*ptr)))
1673 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001674
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001675 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 /* Note: we could also copy eol into ->eoh so that we have the
1677 * real header end in case it ends with lots of LWS, but is this
1678 * really needed ?
1679 */
1680 if (likely(*ptr == '\r'))
1681 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1682 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001683
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001684 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001685 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001686 EXPECT_LF_HERE(ptr, http_msg_invalid);
1687 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001688
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001690 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001691 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1692 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001693 for (; buf->p + msg->eol < ptr; msg->eol++)
1694 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001695 goto http_msg_hdr_val;
1696 }
1697 http_msg_complete_header:
1698 /*
1699 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001700 * Assumes msg->sol points to the first char, msg->sov points
1701 * to the first character of the value and msg->eol to the
1702 * first CR or LF so we know how the line ends. We insert last
1703 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001704 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001705 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001706 idx, idx->tail) < 0))
1707 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001708
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001709 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710 if (likely(!HTTP_IS_CRLF(*ptr))) {
1711 goto http_msg_hdr_name;
1712 }
1713
1714 if (likely(*ptr == '\r'))
1715 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1716 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001717
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001718 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001719 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001720 /* Assumes msg->sol points to the first of either CR or LF.
1721 * Sets ->sov and ->next to the total header length, ->eoh to
1722 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1723 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001724 EXPECT_LF_HERE(ptr, http_msg_invalid);
1725 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001726 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001727 msg->eoh = msg->sol;
1728 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001729 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001730 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001731 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001732
1733 case HTTP_MSG_ERROR:
1734 /* this may only happen if we call http_msg_analyser() twice with an error */
1735 break;
1736
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001737 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001738#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001739 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1740 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001741#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001742 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001743 }
1744 http_msg_ood:
1745 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001746 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001747 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001748 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001749
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001750 http_msg_invalid:
1751 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001752 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001753 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 return;
1755}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001756
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001757/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1758 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1759 * nothing is done and 1 is returned.
1760 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001761static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001762{
1763 int delta;
1764 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001765 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001766
1767 if (msg->sl.rq.v_l != 0)
1768 return 1;
1769
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001770 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1771 if (txn->meth != HTTP_METH_GET)
1772 return 0;
1773
Willy Tarreau9b28e032012-10-12 23:49:43 +02001774 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001775 delta = 0;
1776
1777 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001778 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1779 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001780 }
1781 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001782 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001783 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001784 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001785 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001786 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001787 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001788 NULL, NULL);
1789 if (unlikely(!cur_end))
1790 return 0;
1791
1792 /* we have a full HTTP/1.0 request now and we know that
1793 * we have either a CR or an LF at <ptr>.
1794 */
1795 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1796 return 1;
1797}
1798
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001799/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001800 * and "keep-alive" values. If we already know that some headers may safely
1801 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001802 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1803 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001804 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001805 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1806 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1807 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001808 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001809 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001810void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001811{
Willy Tarreau5b154472009-12-21 20:11:07 +01001812 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001813 const char *hdr_val = "Connection";
1814 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001815
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001816 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001817 return;
1818
Willy Tarreau88d349d2010-01-25 12:15:43 +01001819 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1820 hdr_val = "Proxy-Connection";
1821 hdr_len = 16;
1822 }
1823
Willy Tarreau5b154472009-12-21 20:11:07 +01001824 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001825 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001826 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001827 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1828 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001829 if (to_del & 2)
1830 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001831 else
1832 txn->flags |= TX_CON_KAL_SET;
1833 }
1834 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1835 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001836 if (to_del & 1)
1837 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001838 else
1839 txn->flags |= TX_CON_CLO_SET;
1840 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001841 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1842 txn->flags |= TX_HDR_CONN_UPG;
1843 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001844 }
1845
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001846 txn->flags |= TX_HDR_CONN_PRS;
1847 return;
1848}
Willy Tarreau5b154472009-12-21 20:11:07 +01001849
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001850/* Apply desired changes on the Connection: header. Values may be removed and/or
1851 * added depending on the <wanted> flags, which are exclusively composed of
1852 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1853 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1854 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001855void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001856{
1857 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001858 const char *hdr_val = "Connection";
1859 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001860
1861 ctx.idx = 0;
1862
Willy Tarreau88d349d2010-01-25 12:15:43 +01001863
1864 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1865 hdr_val = "Proxy-Connection";
1866 hdr_len = 16;
1867 }
1868
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001869 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001870 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001871 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1872 if (wanted & TX_CON_KAL_SET)
1873 txn->flags |= TX_CON_KAL_SET;
1874 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001875 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001876 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001877 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1878 if (wanted & TX_CON_CLO_SET)
1879 txn->flags |= TX_CON_CLO_SET;
1880 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001881 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001882 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001883 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001884
1885 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1886 return;
1887
1888 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1889 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001890 hdr_val = "Connection: close";
1891 hdr_len = 17;
1892 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1893 hdr_val = "Proxy-Connection: close";
1894 hdr_len = 23;
1895 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001896 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001897 }
1898
1899 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1900 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001901 hdr_val = "Connection: keep-alive";
1902 hdr_len = 22;
1903 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1904 hdr_val = "Proxy-Connection: keep-alive";
1905 hdr_len = 28;
1906 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001907 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001908 }
1909 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001910}
1911
Willy Tarreauc24715e2014-04-17 15:21:20 +02001912/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1913 * the first byte of data after the chunk size, so that we know we can forward
1914 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1915 * the chunk size. That way it is always possible to differentiate between the
1916 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001917 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001918 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001919 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001920static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001921{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001922 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001923 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001924 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001925 const char *end = buf->data + buf->size;
1926 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001927 unsigned int chunk = 0;
1928
1929 /* The chunk size is in the following form, though we are only
1930 * interested in the size and CRLF :
1931 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1932 */
1933 while (1) {
1934 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001935 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001936 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001937 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001938 if (c < 0) /* not a hex digit anymore */
1939 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001940 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001941 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001942 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001943 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001944 chunk = (chunk << 4) + c;
1945 }
1946
Willy Tarreaud98cf932009-12-27 22:54:55 +01001947 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001948 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001949 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001950
1951 while (http_is_spht[(unsigned char)*ptr]) {
1952 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001953 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001954 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001955 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001956 }
1957
Willy Tarreaud98cf932009-12-27 22:54:55 +01001958 /* Up to there, we know that at least one byte is present at *ptr. Check
1959 * for the end of chunk size.
1960 */
1961 while (1) {
1962 if (likely(HTTP_IS_CRLF(*ptr))) {
1963 /* we now have a CR or an LF at ptr */
1964 if (likely(*ptr == '\r')) {
1965 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001966 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001967 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001968 return 0;
1969 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001970
Willy Tarreaud98cf932009-12-27 22:54:55 +01001971 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001972 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001973 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001974 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001975 /* done */
1976 break;
1977 }
1978 else if (*ptr == ';') {
1979 /* chunk extension, ends at next CRLF */
1980 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001981 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001982 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001983 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001984
1985 while (!HTTP_IS_CRLF(*ptr)) {
1986 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001987 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001988 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001989 return 0;
1990 }
1991 /* we have a CRLF now, loop above */
1992 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001993 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001994 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001995 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001996 }
1997
Willy Tarreaud98cf932009-12-27 22:54:55 +01001998 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02001999 * which may or may not be present. We save that into ->next,
2000 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002001 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002002 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002003 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002004 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002005 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002006 msg->chunk_len = chunk;
2007 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002008 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002009 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002010 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002011 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002012 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002013}
2014
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002015/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002016 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002017 * the trailers is found, it is automatically scheduled to be forwarded,
2018 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2019 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002020 * except maybe msg->next if it could parse some lines, and returns zero.
2021 * If a parse error is encountered, the function returns < 0 and does not
2022 * change anything except maybe msg->next. Note that the message must
2023 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002024 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002025 * forwarding, and that msg->next exactly matches the length of trailers
2026 * already parsed and not forwarded. It is also important to note that this
2027 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002028 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002029static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002030{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002031 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002032
Willy Tarreaua458b672012-03-05 11:17:50 +01002033 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002034 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002035 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002036 const char *ptr = b_ptr(buf, msg->next);
2037 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002038 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002039
2040 /* scan current line and stop at LF or CRLF */
2041 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002042 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002043 return 0;
2044
2045 if (*ptr == '\n') {
2046 if (!p1)
2047 p1 = ptr;
2048 p2 = ptr;
2049 break;
2050 }
2051
2052 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002053 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002054 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002055 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002056 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002057 p1 = ptr;
2058 }
2059
2060 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002061 if (ptr >= buf->data + buf->size)
2062 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002063 }
2064
2065 /* after LF; point to beginning of next line */
2066 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002067 if (p2 >= buf->data + buf->size)
2068 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002069
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002070 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002071 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002072 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002073
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002074 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002075 /* LF/CRLF at beginning of line => end of trailers at p2.
2076 * Everything was scheduled for forwarding, there's nothing
2077 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002078 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002079 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002080 msg->msg_state = HTTP_MSG_DONE;
2081 return 1;
2082 }
2083 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002084 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002085 }
2086}
2087
Willy Tarreauc24715e2014-04-17 15:21:20 +02002088/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2089 * or a possible LF alone at the end of a chunk. It automatically adjusts
2090 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002091 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002092 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2093 * not enough data are available, the function does not change anything and
2094 * returns zero. If a parse error is encountered, the function returns < 0 and
2095 * does not change anything. Note: this function is designed to parse wrapped
2096 * CRLF at the end of the buffer.
2097 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002098static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002099{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002100 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002101 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002102 int bytes;
2103
2104 /* NB: we'll check data availabilty at the end. It's not a
2105 * problem because whatever we match first will be checked
2106 * against the correct length.
2107 */
2108 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002109 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002110 if (*ptr == '\r') {
2111 bytes++;
2112 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002113 if (ptr >= buf->data + buf->size)
2114 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002115 }
2116
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002117 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002118 return 0;
2119
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002120 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002121 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002122 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002123 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002124
2125 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002126 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002127 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002128 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002129 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002130 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2131 return 1;
2132}
Willy Tarreau5b154472009-12-21 20:11:07 +01002133
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002134/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2135 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2136 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2137 * Unparsable qvalues return 1000 as "q=1.000".
2138 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002139int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002140{
2141 int q = 1000;
2142
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002143 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002144 goto out;
2145 q = (*qvalue++ - '0') * 1000;
2146
2147 if (*qvalue++ != '.')
2148 goto out;
2149
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002150 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002151 goto out;
2152 q += (*qvalue++ - '0') * 100;
2153
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002154 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002155 goto out;
2156 q += (*qvalue++ - '0') * 10;
2157
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002158 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002159 goto out;
2160 q += (*qvalue++ - '0') * 1;
2161 out:
2162 if (q > 1000)
2163 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002164 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002165 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002166 return q;
2167}
William Lallemand82fe75c2012-10-23 10:25:10 +02002168
2169/*
2170 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002171 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002172int select_compression_request_header(struct session *s, struct buffer *req)
2173{
2174 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002175 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002176 struct hdr_ctx ctx;
2177 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002178 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002179
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002180 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2181 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002182 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2183 */
2184 ctx.idx = 0;
2185 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2186 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002187 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2188 (ctx.vlen < 31 ||
2189 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2190 ctx.line[ctx.val + 30] < '6' ||
2191 (ctx.line[ctx.val + 30] == '6' &&
2192 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2193 s->comp_algo = NULL;
2194 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002195 }
2196
William Lallemand82fe75c2012-10-23 10:25:10 +02002197 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002198 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 +01002199 int best_q = 0;
2200
William Lallemand82fe75c2012-10-23 10:25:10 +02002201 ctx.idx = 0;
2202 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002203 const char *qval;
2204 int q;
2205 int toklen;
2206
2207 /* try to isolate the token from the optional q-value */
2208 toklen = 0;
2209 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2210 toklen++;
2211
2212 qval = ctx.line + ctx.val + toklen;
2213 while (1) {
2214 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2215 qval++;
2216
2217 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2218 qval = NULL;
2219 break;
2220 }
2221 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002222
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002223 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2224 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002225
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002226 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2227 qval = NULL;
2228 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002229 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002230 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2231 break;
2232
2233 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2234 qval++;
2235 }
2236
2237 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002238 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002239
2240 if (q <= best_q)
2241 continue;
2242
2243 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2244 if (*(ctx.line + ctx.val) == '*' ||
2245 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2246 s->comp_algo = comp_algo;
2247 best_q = q;
2248 break;
2249 }
2250 }
2251 }
2252 }
2253
2254 /* remove all occurrences of the header when "compression offload" is set */
2255 if (s->comp_algo) {
2256 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2257 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2258 ctx.idx = 0;
2259 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2260 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002261 }
2262 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002263 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002264 }
2265
2266 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002267 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2268 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002269 if (comp_algo->add_data == identity_add_data) {
2270 s->comp_algo = comp_algo;
2271 return 1;
2272 }
2273 }
2274 }
2275
2276 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002277 return 0;
2278}
2279
2280/*
2281 * Selects a comression algorithm depending of the server response.
2282 */
2283int select_compression_response_header(struct session *s, struct buffer *res)
2284{
2285 struct http_txn *txn = &s->txn;
2286 struct http_msg *msg = &txn->rsp;
2287 struct hdr_ctx ctx;
2288 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002289
2290 /* no common compression algorithm was found in request header */
2291 if (s->comp_algo == NULL)
2292 goto fail;
2293
2294 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002295 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002296 goto fail;
2297
William Lallemandd3002612012-11-26 14:34:47 +01002298 /* 200 only */
2299 if (txn->status != 200)
2300 goto fail;
2301
William Lallemand82fe75c2012-10-23 10:25:10 +02002302 /* Content-Length is null */
2303 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2304 goto fail;
2305
2306 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002307 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002308 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2309 goto fail;
2310
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002311 /* no compression when Cache-Control: no-transform is present in the message */
2312 ctx.idx = 0;
2313 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2314 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2315 goto fail;
2316 }
2317
William Lallemand82fe75c2012-10-23 10:25:10 +02002318 comp_type = NULL;
2319
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002320 /* we don't want to compress multipart content-types, nor content-types that are
2321 * not listed in the "compression type" directive if any. If no content-type was
2322 * found but configuration requires one, we don't compress either. Backend has
2323 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002324 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002325 ctx.idx = 0;
2326 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2327 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2328 goto fail;
2329
2330 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2331 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002332 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002333 if (ctx.vlen >= comp_type->name_len &&
2334 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002335 /* this Content-Type should be compressed */
2336 break;
2337 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002338 /* this Content-Type should not be compressed */
2339 if (comp_type == NULL)
2340 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002341 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002342 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002343 else { /* no content-type header */
2344 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2345 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002346 }
2347
William Lallemandd85f9172012-11-09 17:05:39 +01002348 /* limit compression rate */
2349 if (global.comp_rate_lim > 0)
2350 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2351 goto fail;
2352
William Lallemand072a2bf2012-11-20 17:01:01 +01002353 /* limit cpu usage */
2354 if (idle_pct < compress_min_idle)
2355 goto fail;
2356
William Lallemand4c49fae2012-11-07 15:00:23 +01002357 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002358 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002359 goto fail;
2360
William Lallemandec3e3892012-11-12 17:02:18 +01002361 s->flags |= SN_COMP_READY;
2362
William Lallemand82fe75c2012-10-23 10:25:10 +02002363 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002364 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002365 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2366 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2367
2368 /* add Transfer-Encoding header */
2369 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2370 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2371
2372 /*
2373 * Add Content-Encoding header when it's not identity encoding.
2374 * RFC 2616 : Identity encoding: This content-coding is used only in the
2375 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2376 * header.
2377 */
2378 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002379 trash.len = 18;
2380 memcpy(trash.str, "Content-Encoding: ", trash.len);
2381 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2382 trash.len += s->comp_algo->name_len;
2383 trash.str[trash.len] = '\0';
2384 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002385 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002386 return 1;
2387
2388fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002389 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002390 return 0;
2391}
2392
Willy Tarreau2e47a3a2014-09-30 18:44:22 +02002393void http_adjust_conn_mode(struct session *s, struct http_txn *txn, struct http_msg *msg)
2394{
2395 int tmp = TX_CON_WANT_KAL;
2396
2397 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2398 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
2399 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2400 tmp = TX_CON_WANT_TUN;
2401
2402 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2403 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2404 tmp = TX_CON_WANT_TUN;
2405 }
2406
2407 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
2408 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2409 /* option httpclose + server_close => forceclose */
2410 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2411 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2412 tmp = TX_CON_WANT_CLO;
2413 else
2414 tmp = TX_CON_WANT_SCL;
2415 }
2416
2417 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
2418 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2419 tmp = TX_CON_WANT_CLO;
2420
2421 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2422 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2423
2424 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2425 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2426 /* parse the Connection header and possibly clean it */
2427 int to_del = 0;
2428 if ((msg->flags & HTTP_MSGF_VER_11) ||
2429 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2430 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
2431 to_del |= 2; /* remove "keep-alive" */
2432 if (!(msg->flags & HTTP_MSGF_VER_11))
2433 to_del |= 1; /* remove "close" */
2434 http_parse_connection_header(txn, msg, to_del);
2435 }
2436
2437 /* check if client or config asks for explicit close in KAL/SCL */
2438 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2439 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2440 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2441 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2442 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
2443 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
2444 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2445}
William Lallemand82fe75c2012-10-23 10:25:10 +02002446
Willy Tarreaud787e662009-07-07 10:14:51 +02002447/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2448 * processing can continue on next analysers, or zero if it either needs more
2449 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2450 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2451 * when it has nothing left to do, and may remove any analyser when it wants to
2452 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002453 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002454int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002455{
Willy Tarreau59234e92008-11-30 23:51:27 +01002456 /*
2457 * We will parse the partial (or complete) lines.
2458 * We will check the request syntax, and also join multi-line
2459 * headers. An index of all the lines will be elaborated while
2460 * parsing.
2461 *
2462 * For the parsing, we use a 28 states FSM.
2463 *
2464 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002465 * req->buf->p = beginning of request
2466 * req->buf->p + msg->eoh = end of processed headers / start of current one
2467 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002468 * msg->eol = end of current header or line (LF or CRLF)
2469 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002470 *
2471 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002472 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002473 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2474 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002475 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002476
Willy Tarreau59234e92008-11-30 23:51:27 +01002477 int cur_idx;
2478 struct http_txn *txn = &s->txn;
2479 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002480 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002481
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002482 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 +01002483 now_ms, __FUNCTION__,
2484 s,
2485 req,
2486 req->rex, req->wex,
2487 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002488 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002489 req->analysers);
2490
Willy Tarreau52a0c602009-08-16 22:45:38 +02002491 /* we're speaking HTTP here, so let's speak HTTP to the client */
2492 s->srv_error = http_return_srv_error;
2493
Willy Tarreau83e3af02009-12-28 17:39:57 +01002494 /* There's a protected area at the end of the buffer for rewriting
2495 * purposes. We don't want to start to parse the request if the
2496 * protected area is affected, because we may have to move processed
2497 * data later, which is much more complicated.
2498 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002499 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002500 if (txn->flags & TX_NOT_FIRST) {
2501 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002502 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002503 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002504 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002505 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002506 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002507 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002508 return 0;
2509 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002510 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2511 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2512 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002513 }
2514
Willy Tarreau065e8332010-01-08 00:30:20 +01002515 /* Note that we have the same problem with the response ; we
2516 * may want to send a redirect, error or anything which requires
2517 * some spare space. So we'll ensure that we have at least
2518 * maxrewrite bytes available in the response buffer before
2519 * processing that one. This will only affect pipelined
2520 * keep-alive requests.
2521 */
2522 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002523 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002524 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2525 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2526 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002527 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002528 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002529 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002530 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002531 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002532 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002533 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002534 return 0;
2535 }
2536 }
2537
Willy Tarreau9b28e032012-10-12 23:49:43 +02002538 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002539 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002540 }
2541
Willy Tarreau59234e92008-11-30 23:51:27 +01002542 /* 1: we might have to print this header in debug mode */
2543 if (unlikely((global.mode & MODE_DEBUG) &&
2544 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau8767b132014-10-21 19:36:09 +02002545 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002546 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002547
Willy Tarreau9b28e032012-10-12 23:49:43 +02002548 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002549 /* this is a bit complex : in case of error on the request line,
2550 * we know that rq.l is still zero, so we display only the part
2551 * up to the end of the line (truncated by debug_hdr).
2552 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002553 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002554 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002555
Willy Tarreau59234e92008-11-30 23:51:27 +01002556 sol += hdr_idx_first_pos(&txn->hdr_idx);
2557 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002558
Willy Tarreau59234e92008-11-30 23:51:27 +01002559 while (cur_idx) {
2560 eol = sol + txn->hdr_idx.v[cur_idx].len;
2561 debug_hdr("clihdr", s, sol, eol);
2562 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2563 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002564 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002565 }
2566
Willy Tarreau58f10d72006-12-04 02:26:12 +01002567
Willy Tarreau59234e92008-11-30 23:51:27 +01002568 /*
2569 * Now we quickly check if we have found a full valid request.
2570 * If not so, we check the FD and buffer states before leaving.
2571 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002572 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002573 * requests are checked first. When waiting for a second request
2574 * on a keep-alive session, if we encounter and error, close, t/o,
2575 * we note the error in the session flags but don't set any state.
2576 * Since the error will be noted there, it will not be counted by
2577 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002578 * Last, we may increase some tracked counters' http request errors on
2579 * the cases that are deliberately the client's fault. For instance,
2580 * a timeout or connection reset is not counted as an error. However
2581 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002582 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002583
Willy Tarreau655dce92009-11-08 13:10:58 +01002584 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002585 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002587 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002588 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002589 session_inc_http_req_ctr(s);
2590 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002591 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002592 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002593 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002594
Willy Tarreau59234e92008-11-30 23:51:27 +01002595 /* 1: Since we are in header mode, if there's no space
2596 * left for headers, we won't be able to free more
2597 * later, so the session will never terminate. We
2598 * must terminate it now.
2599 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002600 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002601 /* FIXME: check if URI is set and return Status
2602 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002603 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002604 session_inc_http_req_ctr(s);
2605 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002606 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002607 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002608 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002609 goto return_bad_req;
2610 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002611
Willy Tarreau59234e92008-11-30 23:51:27 +01002612 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002613 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002614 if (!(s->flags & SN_ERR_MASK))
2615 s->flags |= SN_ERR_CLICL;
2616
Willy Tarreaufcffa692010-01-10 14:21:19 +01002617 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002618 goto failed_keep_alive;
2619
Willy Tarreau1c3a6122015-05-01 15:37:53 +02002620 if (s->fe->options & PR_O_IGNORE_PRB)
2621 goto failed_keep_alive;
2622
Willy Tarreau59234e92008-11-30 23:51:27 +01002623 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002624 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002625 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002626 session_inc_http_err_ctr(s);
2627 }
2628
Willy Tarreaudc979f22012-12-04 10:39:01 +01002629 txn->status = 400;
2630 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002631 msg->msg_state = HTTP_MSG_ERROR;
2632 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002633
Willy Tarreauda7ff642010-06-23 11:44:09 +02002634 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002635 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002636 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002637 if (s->listener->counters)
2638 s->listener->counters->failed_req++;
2639
Willy Tarreau59234e92008-11-30 23:51:27 +01002640 if (!(s->flags & SN_FINST_MASK))
2641 s->flags |= SN_FINST_R;
2642 return 0;
2643 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002644
Willy Tarreau59234e92008-11-30 23:51:27 +01002645 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002646 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002647 if (!(s->flags & SN_ERR_MASK))
2648 s->flags |= SN_ERR_CLITO;
2649
Willy Tarreaufcffa692010-01-10 14:21:19 +01002650 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002651 goto failed_keep_alive;
2652
Willy Tarreau1c3a6122015-05-01 15:37:53 +02002653 if (s->fe->options & PR_O_IGNORE_PRB)
2654 goto failed_keep_alive;
2655
Willy Tarreau59234e92008-11-30 23:51:27 +01002656 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002657 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002658 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002659 session_inc_http_err_ctr(s);
2660 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002661 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002662 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002663 msg->msg_state = HTTP_MSG_ERROR;
2664 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002665
Willy Tarreauda7ff642010-06-23 11:44:09 +02002666 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002667 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002668 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002669 if (s->listener->counters)
2670 s->listener->counters->failed_req++;
2671
Willy Tarreau59234e92008-11-30 23:51:27 +01002672 if (!(s->flags & SN_FINST_MASK))
2673 s->flags |= SN_FINST_R;
2674 return 0;
2675 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002676
Willy Tarreau59234e92008-11-30 23:51:27 +01002677 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002678 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002679 if (!(s->flags & SN_ERR_MASK))
2680 s->flags |= SN_ERR_CLICL;
2681
Willy Tarreaufcffa692010-01-10 14:21:19 +01002682 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002683 goto failed_keep_alive;
2684
Willy Tarreau1c3a6122015-05-01 15:37:53 +02002685 if (s->fe->options & PR_O_IGNORE_PRB)
2686 goto failed_keep_alive;
2687
Willy Tarreau4076a152009-04-02 15:18:36 +02002688 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002689 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002690 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002691 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002692 msg->msg_state = HTTP_MSG_ERROR;
2693 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002694
Willy Tarreauda7ff642010-06-23 11:44:09 +02002695 session_inc_http_err_ctr(s);
2696 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002697 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002698 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002699 if (s->listener->counters)
2700 s->listener->counters->failed_req++;
2701
Willy Tarreau59234e92008-11-30 23:51:27 +01002702 if (!(s->flags & SN_FINST_MASK))
2703 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002704 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002705 }
2706
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002707 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002708 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2709 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002710#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002711 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 +01002712 /* We need more data, we have to re-enable quick-ack in case we
2713 * previously disabled it, otherwise we might cause the client
2714 * to delay next data.
2715 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002716 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002717 }
2718#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002719
Willy Tarreaufcffa692010-01-10 14:21:19 +01002720 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2721 /* If the client starts to talk, let's fall back to
2722 * request timeout processing.
2723 */
2724 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002725 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002726 }
2727
Willy Tarreau59234e92008-11-30 23:51:27 +01002728 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002729 if (!tick_isset(req->analyse_exp)) {
2730 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2731 (txn->flags & TX_WAIT_NEXT_RQ) &&
2732 tick_isset(s->be->timeout.httpka))
2733 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2734 else
2735 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2736 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002737
Willy Tarreau59234e92008-11-30 23:51:27 +01002738 /* we're not ready yet */
2739 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002740
2741 failed_keep_alive:
2742 /* Here we process low-level errors for keep-alive requests. In
2743 * short, if the request is not the first one and it experiences
2744 * a timeout, read error or shutdown, we just silently close so
2745 * that the client can try again.
2746 */
2747 txn->status = 0;
2748 msg->msg_state = HTTP_MSG_RQBEFORE;
2749 req->analysers = 0;
2750 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002751 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002752 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002753 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002754 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002755 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002756
Willy Tarreaud787e662009-07-07 10:14:51 +02002757 /* OK now we have a complete HTTP request with indexed headers. Let's
2758 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002759 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002760 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002761 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002762 * byte after the last LF. msg->sov points to the first byte of data.
2763 * msg->eol cannot be trusted because it may have been left uninitialized
2764 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002765 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002766
Willy Tarreauda7ff642010-06-23 11:44:09 +02002767 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002768 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2769
Willy Tarreaub16a5742010-01-10 14:46:16 +01002770 if (txn->flags & TX_WAIT_NEXT_RQ) {
2771 /* kill the pending keep-alive timeout */
2772 txn->flags &= ~TX_WAIT_NEXT_RQ;
2773 req->analyse_exp = TICK_ETERNITY;
2774 }
2775
2776
Willy Tarreaud787e662009-07-07 10:14:51 +02002777 /* Maybe we found in invalid header name while we were configured not
2778 * to block on that, so we have to capture it now.
2779 */
2780 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002781 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002782
Willy Tarreau59234e92008-11-30 23:51:27 +01002783 /*
2784 * 1: identify the method
2785 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002786 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002787
2788 /* we can make use of server redirect on GET and HEAD */
2789 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2790 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002791
Willy Tarreau59234e92008-11-30 23:51:27 +01002792 /*
2793 * 2: check if the URI matches the monitor_uri.
2794 * We have to do this for every request which gets in, because
2795 * the monitor-uri is defined by the frontend.
2796 */
2797 if (unlikely((s->fe->monitor_uri_len != 0) &&
2798 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002799 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002800 s->fe->monitor_uri,
2801 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002802 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002803 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002804 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002805 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002806
Willy Tarreau59234e92008-11-30 23:51:27 +01002807 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002808 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002809
Willy Tarreau59234e92008-11-30 23:51:27 +01002810 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002811 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002812 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002813
Willy Tarreau59234e92008-11-30 23:51:27 +01002814 ret = acl_pass(ret);
2815 if (cond->pol == ACL_COND_UNLESS)
2816 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002817
Willy Tarreau59234e92008-11-30 23:51:27 +01002818 if (ret) {
2819 /* we fail this request, let's return 503 service unavail */
2820 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002821 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002822 if (!(s->flags & SN_ERR_MASK))
2823 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002824 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002825 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002826 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002827
Willy Tarreau59234e92008-11-30 23:51:27 +01002828 /* nothing to fail, let's reply normaly */
2829 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002830 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002831 if (!(s->flags & SN_ERR_MASK))
2832 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002833 goto return_prx_cond;
2834 }
2835
2836 /*
2837 * 3: Maybe we have to copy the original REQURI for the logs ?
2838 * Note: we cannot log anymore if the request has been
2839 * classified as invalid.
2840 */
2841 if (unlikely(s->logs.logwait & LW_REQ)) {
2842 /* we have a complete HTTP request that we must log */
2843 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2844 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002845
Willy Tarreau59234e92008-11-30 23:51:27 +01002846 if (urilen >= REQURI_LEN)
2847 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002848 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002849 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002850
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002851 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002852 s->do_log(s);
2853 } else {
2854 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002855 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002856 }
Willy Tarreau06619262006-12-17 08:37:22 +01002857
Willy Tarreau59234e92008-11-30 23:51:27 +01002858 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002859 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002860 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002861
Willy Tarreau55645552015-05-01 13:26:00 +02002862 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2863 * exactly one digit "." one digit. This check may be disabled using
2864 * option accept-invalid-http-request.
2865 */
2866 if (!(s->fe->options2 & PR_O2_REQBUG_OK)) {
2867 if (msg->sl.rq.v_l != 8) {
2868 msg->err_pos = msg->sl.rq.v;
2869 goto return_bad_req;
2870 }
2871
2872 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2873 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2874 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2875 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2876 msg->err_pos = msg->sl.rq.v + 4;
2877 goto return_bad_req;
2878 }
2879 }
2880
Willy Tarreau5b154472009-12-21 20:11:07 +01002881 /* ... and check if the request is HTTP/1.1 or above */
2882 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002883 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2884 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2885 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002886 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002887
2888 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002889 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 +01002890
Willy Tarreau88d349d2010-01-25 12:15:43 +01002891 /* if the frontend has "option http-use-proxy-header", we'll check if
2892 * we have what looks like a proxied connection instead of a connection,
2893 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2894 * Note that this is *not* RFC-compliant, however browsers and proxies
2895 * happen to do that despite being non-standard :-(
2896 * We consider that a request not beginning with either '/' or '*' is
2897 * a proxied connection, which covers both "scheme://location" and
2898 * CONNECT ip:port.
2899 */
2900 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002901 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002902 txn->flags |= TX_USE_PX_CONN;
2903
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002904 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002905 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002906
Willy Tarreau59234e92008-11-30 23:51:27 +01002907 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002908 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002909 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002910 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002911
Willy Tarreau4db603d2015-05-01 10:05:17 +02002912 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2913 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002914 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002915 * The length of a message body is determined by one of the following
2916 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002917 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002918 * 1. Any response to a HEAD request and any response with a 1xx
2919 * (Informational), 204 (No Content), or 304 (Not Modified) status
2920 * code is always terminated by the first empty line after the
2921 * header fields, regardless of the header fields present in the
2922 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002923 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002924 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2925 * the connection will become a tunnel immediately after the empty
2926 * line that concludes the header fields. A client MUST ignore any
2927 * Content-Length or Transfer-Encoding header fields received in
2928 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002929 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002930 * 3. If a Transfer-Encoding header field is present and the chunked
2931 * transfer coding (Section 4.1) is the final encoding, the message
2932 * body length is determined by reading and decoding the chunked
2933 * data until the transfer coding indicates the data is complete.
2934 *
2935 * If a Transfer-Encoding header field is present in a response and
2936 * the chunked transfer coding is not the final encoding, the
2937 * message body length is determined by reading the connection until
2938 * it is closed by the server. If a Transfer-Encoding header field
2939 * is present in a request and the chunked transfer coding is not
2940 * the final encoding, the message body length cannot be determined
2941 * reliably; the server MUST respond with the 400 (Bad Request)
2942 * status code and then close the connection.
2943 *
2944 * If a message is received with both a Transfer-Encoding and a
2945 * Content-Length header field, the Transfer-Encoding overrides the
2946 * Content-Length. Such a message might indicate an attempt to
2947 * perform request smuggling (Section 9.5) or response splitting
2948 * (Section 9.4) and ought to be handled as an error. A sender MUST
2949 * remove the received Content-Length field prior to forwarding such
2950 * a message downstream.
2951 *
2952 * 4. If a message is received without Transfer-Encoding and with
2953 * either multiple Content-Length header fields having differing
2954 * field-values or a single Content-Length header field having an
2955 * invalid value, then the message framing is invalid and the
2956 * recipient MUST treat it as an unrecoverable error. If this is a
2957 * request message, the server MUST respond with a 400 (Bad Request)
2958 * status code and then close the connection. If this is a response
2959 * message received by a proxy, the proxy MUST close the connection
2960 * to the server, discard the received response, and send a 502 (Bad
2961 * Gateway) response to the client. If this is a response message
2962 * received by a user agent, the user agent MUST close the
2963 * connection to the server and discard the received response.
2964 *
2965 * 5. If a valid Content-Length header field is present without
2966 * Transfer-Encoding, its decimal value defines the expected message
2967 * body length in octets. If the sender closes the connection or
2968 * the recipient times out before the indicated number of octets are
2969 * received, the recipient MUST consider the message to be
2970 * incomplete and close the connection.
2971 *
2972 * 6. If this is a request message and none of the above are true, then
2973 * the message body length is zero (no message body is present).
2974 *
2975 * 7. Otherwise, this is a response message without a declared message
2976 * body length, so the message body length is determined by the
2977 * number of octets received prior to the server closing the
2978 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002979 */
2980
Willy Tarreau32b47f42009-10-18 20:55:02 +02002981 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002982 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaue77bc172015-05-01 10:06:30 +02002983 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002984 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002985 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2986 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau49efa262015-05-01 10:09:49 +02002987 /* chunked not last, return badreq */
2988 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002989 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002990 }
2991
Willy Tarreaudfa3d922015-04-30 10:57:51 +02002992 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002993 ctx.idx = 0;
Willy Tarreaudfa3d922015-04-30 10:57:51 +02002994 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2995 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
2996 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2997 }
Willy Tarreau49efa262015-05-01 10:09:49 +02002998 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002999 signed long long cl;
3000
Willy Tarreauad14f752011-09-02 20:33:27 +02003001 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003002 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003003 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003004 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003005
Willy Tarreauad14f752011-09-02 20:33:27 +02003006 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003007 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003008 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003009 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003010
Willy Tarreauad14f752011-09-02 20:33:27 +02003011 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003012 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003013 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003014 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003015
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003016 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003017 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003018 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003019 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003020
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003021 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003022 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003023 }
3024
Willy Tarreau49efa262015-05-01 10:09:49 +02003025 /* even bodyless requests have a known length */
3026 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003027
Willy Tarreau179085c2014-04-28 16:48:56 +02003028 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3029 * only change if both the request and the config reference something else.
3030 * Option httpclose by itself sets tunnel mode where headers are mangled.
3031 * However, if another mode is set, it will affect it (eg: server-close/
3032 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3033 * if FE and BE have the same settings (common). The method consists in
3034 * checking if options changed between the two calls (implying that either
3035 * one is non-null, or one of them is non-null and we are there for the first
3036 * time.
3037 */
3038 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau2e47a3a2014-09-30 18:44:22 +02003039 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
3040 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003041
Willy Tarreaud787e662009-07-07 10:14:51 +02003042 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003043 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003044 req->analyse_exp = TICK_ETERNITY;
3045 return 1;
3046
3047 return_bad_req:
3048 /* We centralize bad requests processing here */
3049 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3050 /* we detected a parsing error. We want to archive this request
3051 * in the dedicated proxy area for later troubleshooting.
3052 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003053 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003054 }
3055
3056 txn->req.msg_state = HTTP_MSG_ERROR;
3057 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003058 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003059
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003060 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003061 if (s->listener->counters)
3062 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003063
3064 return_prx_cond:
3065 if (!(s->flags & SN_ERR_MASK))
3066 s->flags |= SN_ERR_PRXCOND;
3067 if (!(s->flags & SN_FINST_MASK))
3068 s->flags |= SN_FINST_R;
3069
3070 req->analysers = 0;
3071 req->analyse_exp = TICK_ETERNITY;
3072 return 0;
3073}
3074
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003075
Willy Tarreau347a35d2013-11-22 17:51:09 +01003076/* This function prepares an applet to handle the stats. It can deal with the
3077 * "100-continue" expectation, check that admin rules are met for POST requests,
3078 * and program a response message if something was unexpected. It cannot fail
3079 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003080 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003081 * s->target which is supposed to already point to the stats applet. The caller
3082 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003083 */
3084int http_handle_stats(struct session *s, struct channel *req)
3085{
3086 struct stats_admin_rule *stats_admin_rule;
3087 struct stream_interface *si = s->rep->prod;
3088 struct http_txn *txn = &s->txn;
3089 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003090 struct uri_auth *uri_auth = s->be->uri_auth;
3091 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003092 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003093
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003094 appctx = si_appctx(si);
3095 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3096 appctx->st1 = appctx->st2 = 0;
3097 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3098 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003099 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3100 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003101
3102 uri = msg->chn->buf->p + msg->sl.rq.u;
3103 lookup = uri + uri_auth->uri_len;
3104
3105 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3106 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003107 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003108 break;
3109 }
3110 }
3111
3112 if (uri_auth->refresh) {
3113 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3114 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003115 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003116 break;
3117 }
3118 }
3119 }
3120
3121 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3122 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003123 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003124 break;
3125 }
3126 }
3127
3128 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3129 if (memcmp(h, ";st=", 4) == 0) {
3130 int i;
3131 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003132 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003133 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3134 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003135 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003136 break;
3137 }
3138 }
3139 break;
3140 }
3141 }
3142
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003143 appctx->ctx.stats.scope_str = 0;
3144 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003145 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3146 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3147 int itx = 0;
3148 const char *h2;
3149 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3150 const char *err;
3151
3152 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3153 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003154 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003155 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3156 itx++;
3157 h++;
3158 }
3159
3160 if (itx > STAT_SCOPE_TXT_MAXLEN)
3161 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003162 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003163
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003164 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003165 memcpy(scope_txt, h2, itx);
3166 scope_txt[itx] = '\0';
3167 err = invalid_char(scope_txt);
3168 if (err) {
3169 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003170 appctx->ctx.stats.scope_str = 0;
3171 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003172 }
3173 break;
3174 }
3175 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003176
3177 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003178 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003179 int ret = 1;
3180
3181 if (stats_admin_rule->cond) {
3182 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3183 ret = acl_pass(ret);
3184 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3185 ret = !ret;
3186 }
3187
3188 if (ret) {
3189 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003190 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003191 break;
3192 }
3193 }
3194
3195 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003196 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003197 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003198 /* we'll need the request body, possibly after sending 100-continue */
3199 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003200 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003201 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003202 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003203 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3204 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003205 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003206 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003207 else {
3208 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003209 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003210 }
3211
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003212 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003213 return 1;
3214}
3215
Lukas Tribus67db8df2013-06-23 17:37:13 +02003216/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3217 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3218 */
3219static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3220{
3221#ifdef IP_TOS
3222 if (from.ss_family == AF_INET)
3223 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3224#endif
3225#ifdef IPV6_TCLASS
3226 if (from.ss_family == AF_INET6) {
3227 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3228 /* v4-mapped addresses need IP_TOS */
3229 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3230 else
3231 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3232 }
3233#endif
3234}
3235
Sasha Pachev218f0642014-06-16 12:05:59 -06003236static int http_transform_header(struct session* s, struct http_msg *msg, const char* name, uint name_len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003237 char* buf, struct hdr_idx* idx, struct list *fmt, struct my_regex *re,
Sasha Pachev218f0642014-06-16 12:05:59 -06003238 struct hdr_ctx* ctx, int action)
3239{
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003240 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3241 struct hdr_idx *idx, struct hdr_ctx *ctx);
3242 struct chunk *replace = get_trash_chunk();
3243 struct chunk *output = get_trash_chunk();
3244
3245 replace->len = build_logline(s, replace->str, replace->size, fmt);
3246 if (replace->len >= replace->size - 1)
3247 return -1;
3248
Sasha Pachev218f0642014-06-16 12:05:59 -06003249 ctx->idx = 0;
3250
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003251 /* Choose the header browsing function. */
3252 switch (action) {
3253 case HTTP_REQ_ACT_REPLACE_VAL:
3254 case HTTP_RES_ACT_REPLACE_VAL:
3255 http_find_hdr_func = http_find_header2;
3256 break;
3257 case HTTP_REQ_ACT_REPLACE_HDR:
3258 case HTTP_RES_ACT_REPLACE_HDR:
3259 http_find_hdr_func = http_find_full_header2;
3260 break;
3261 default: /* impossible */
3262 return -1;
3263 }
3264
3265 while (http_find_hdr_func(name, name_len, buf, idx, ctx)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06003266 struct hdr_idx_elem *hdr = idx->v + ctx->idx;
3267 int delta;
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003268 char *val = ctx->line + ctx->val;
3269 char* val_end = val + ctx->vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003270
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003271 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch))
3272 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003273
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003274 output->len = exp_replace(output->str, output->size, val, replace->str, pmatch);
3275 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003276 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003277
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003278 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003279
3280 hdr->len += delta;
3281 http_msg_move_end(msg, delta);
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003282
3283 /* Adjust the length of the current value of the index. */
3284 ctx->vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003285 }
3286
3287 return 0;
3288}
3289
Willy Tarreau20b0de52012-12-24 15:45:22 +01003290/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003291 * transaction <txn>. Returns the verdict of the first rule that prevents
3292 * further processing of the request (auth, deny, ...), and defaults to
3293 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3294 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3295 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003296 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003297enum rule_result
Willy Tarreau96257ec2012-12-27 10:46:37 +01003298http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003299{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003300 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003301 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003302 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003303 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003304
Willy Tarreauff011f22011-01-06 17:51:27 +01003305 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003306 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003307 continue;
3308
Willy Tarreau96257ec2012-12-27 10:46:37 +01003309 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003310 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003311 int ret;
3312
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003313 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003314 ret = acl_pass(ret);
3315
Willy Tarreauff011f22011-01-06 17:51:27 +01003316 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003317 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003318
3319 if (!ret) /* condition not matched */
3320 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003321 }
3322
Willy Tarreau20b0de52012-12-24 15:45:22 +01003323
Willy Tarreau96257ec2012-12-27 10:46:37 +01003324 switch (rule->action) {
3325 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003326 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003327
3328 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003329 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003330
Willy Tarreauccbcc372012-12-27 12:37:57 +01003331 case HTTP_REQ_ACT_TARPIT:
3332 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003333 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003334
Willy Tarreau96257ec2012-12-27 10:46:37 +01003335 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003336 /* Auth might be performed on regular http-req rules as well as on stats */
3337 auth_realm = rule->arg.auth.realm;
3338 if (!auth_realm) {
3339 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3340 auth_realm = STATS_DEFAULT_REALM;
3341 else
3342 auth_realm = px->id;
3343 }
3344 /* send 401/407 depending on whether we use a proxy or not. We still
3345 * count one error, because normal browsing won't significantly
3346 * increase the counter but brute force attempts will.
3347 */
3348 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3349 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3350 stream_int_retnclose(&s->si[0], &trash);
3351 session_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003352 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003353
Willy Tarreau81499eb2012-12-27 12:19:02 +01003354 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003355 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3356 return HTTP_RULE_RES_BADREQ;
3357 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003358
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003359 case HTTP_REQ_ACT_SET_NICE:
3360 s->task->nice = rule->arg.nice;
3361 break;
3362
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003363 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003364 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003365 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003366 break;
3367
Willy Tarreau51347ed2013-06-11 19:34:13 +02003368 case HTTP_REQ_ACT_SET_MARK:
3369#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003370 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003371 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003372#endif
3373 break;
3374
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003375 case HTTP_REQ_ACT_SET_LOGL:
3376 s->logs.level = rule->arg.loglevel;
3377 break;
3378
Sasha Pachev218f0642014-06-16 12:05:59 -06003379 case HTTP_REQ_ACT_REPLACE_HDR:
3380 case HTTP_REQ_ACT_REPLACE_VAL:
3381 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3382 txn->req.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003383 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003384 return HTTP_RULE_RES_BADREQ;
3385 break;
3386
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003387 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003388 ctx.idx = 0;
3389 /* remove all occurrences of the header */
3390 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3391 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3392 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003393 }
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003394 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003395
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003396 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003397 case HTTP_REQ_ACT_ADD_HDR:
3398 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3399 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3400 trash.len = rule->arg.hdr_add.name_len;
3401 trash.str[trash.len++] = ':';
3402 trash.str[trash.len++] = ' ';
3403 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003404
3405 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3406 /* remove all occurrences of the header */
3407 ctx.idx = 0;
3408 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3409 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3410 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3411 }
3412 }
3413
Willy Tarreau96257ec2012-12-27 10:46:37 +01003414 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3415 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003416
3417 case HTTP_REQ_ACT_DEL_ACL:
3418 case HTTP_REQ_ACT_DEL_MAP: {
3419 struct pat_ref *ref;
3420 char *key;
3421 int len;
3422
3423 /* collect reference */
3424 ref = pat_ref_lookup(rule->arg.map.ref);
3425 if (!ref)
3426 continue;
3427
3428 /* collect key */
3429 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3430 key = trash.str;
3431 key[len] = '\0';
3432
3433 /* perform update */
3434 /* returned code: 1=ok, 0=ko */
3435 pat_ref_delete(ref, key);
3436
3437 break;
3438 }
3439
3440 case HTTP_REQ_ACT_ADD_ACL: {
3441 struct pat_ref *ref;
3442 char *key;
3443 struct chunk *trash_key;
3444 int len;
3445
3446 trash_key = get_trash_chunk();
3447
3448 /* collect reference */
3449 ref = pat_ref_lookup(rule->arg.map.ref);
3450 if (!ref)
3451 continue;
3452
3453 /* collect key */
3454 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3455 key = trash_key->str;
3456 key[len] = '\0';
3457
3458 /* perform update */
3459 /* add entry only if it does not already exist */
3460 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003461 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003462
3463 break;
3464 }
3465
3466 case HTTP_REQ_ACT_SET_MAP: {
3467 struct pat_ref *ref;
3468 char *key, *value;
3469 struct chunk *trash_key, *trash_value;
3470 int len;
3471
3472 trash_key = get_trash_chunk();
3473 trash_value = get_trash_chunk();
3474
3475 /* collect reference */
3476 ref = pat_ref_lookup(rule->arg.map.ref);
3477 if (!ref)
3478 continue;
3479
3480 /* collect key */
3481 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3482 key = trash_key->str;
3483 key[len] = '\0';
3484
3485 /* collect value */
3486 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3487 value = trash_value->str;
3488 value[len] = '\0';
3489
3490 /* perform update */
3491 if (pat_ref_find_elt(ref, key) != NULL)
3492 /* update entry if it exists */
3493 pat_ref_set(ref, key, value, NULL);
3494 else
3495 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003496 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003497
3498 break;
3499 }
William Lallemand73025dd2014-04-24 14:38:37 +02003500
3501 case HTTP_REQ_ACT_CUSTOM_CONT:
3502 rule->action_ptr(rule, px, s, txn);
3503 break;
3504
3505 case HTTP_REQ_ACT_CUSTOM_STOP:
3506 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003507 return HTTP_RULE_RES_DONE;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003508 }
3509 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003510
3511 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003512 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003513}
3514
Willy Tarreau71241ab2012-12-27 11:30:54 +01003515
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003516/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3517 * transaction <txn>. Returns the first rule that prevents further processing
3518 * of the response (deny, ...) or NULL if it executed all rules or stopped
3519 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3520 * rule.
3521 */
3522static struct http_res_rule *
3523http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3524{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003525 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003526 struct http_res_rule *rule;
3527 struct hdr_ctx ctx;
3528
3529 list_for_each_entry(rule, rules, list) {
3530 if (rule->action >= HTTP_RES_ACT_MAX)
3531 continue;
3532
3533 /* check optional condition */
3534 if (rule->cond) {
3535 int ret;
3536
3537 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3538 ret = acl_pass(ret);
3539
3540 if (rule->cond->pol == ACL_COND_UNLESS)
3541 ret = !ret;
3542
3543 if (!ret) /* condition not matched */
3544 continue;
3545 }
3546
3547
3548 switch (rule->action) {
3549 case HTTP_RES_ACT_ALLOW:
3550 return NULL; /* "allow" rules are OK */
3551
3552 case HTTP_RES_ACT_DENY:
3553 txn->flags |= TX_SVDENY;
3554 return rule;
3555
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003556 case HTTP_RES_ACT_SET_NICE:
3557 s->task->nice = rule->arg.nice;
3558 break;
3559
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003560 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003561 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003562 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003563 break;
3564
Willy Tarreau51347ed2013-06-11 19:34:13 +02003565 case HTTP_RES_ACT_SET_MARK:
3566#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003567 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003568 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003569#endif
3570 break;
3571
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003572 case HTTP_RES_ACT_SET_LOGL:
3573 s->logs.level = rule->arg.loglevel;
3574 break;
3575
Sasha Pachev218f0642014-06-16 12:05:59 -06003576 case HTTP_RES_ACT_REPLACE_HDR:
3577 case HTTP_RES_ACT_REPLACE_VAL:
3578 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3579 txn->rsp.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003580 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003581 return NULL; /* note: we should report an error here */
3582 break;
3583
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003584 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003585 ctx.idx = 0;
3586 /* remove all occurrences of the header */
3587 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3588 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3589 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3590 }
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003591 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003592
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003593 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003594 case HTTP_RES_ACT_ADD_HDR:
3595 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3596 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3597 trash.len = rule->arg.hdr_add.name_len;
3598 trash.str[trash.len++] = ':';
3599 trash.str[trash.len++] = ' ';
3600 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003601
3602 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3603 /* remove all occurrences of the header */
3604 ctx.idx = 0;
3605 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3606 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3607 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3608 }
3609 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003610 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3611 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003612
3613 case HTTP_RES_ACT_DEL_ACL:
3614 case HTTP_RES_ACT_DEL_MAP: {
3615 struct pat_ref *ref;
3616 char *key;
3617 int len;
3618
3619 /* collect reference */
3620 ref = pat_ref_lookup(rule->arg.map.ref);
3621 if (!ref)
3622 continue;
3623
3624 /* collect key */
3625 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3626 key = trash.str;
3627 key[len] = '\0';
3628
3629 /* perform update */
3630 /* returned code: 1=ok, 0=ko */
3631 pat_ref_delete(ref, key);
3632
3633 break;
3634 }
3635
3636 case HTTP_RES_ACT_ADD_ACL: {
3637 struct pat_ref *ref;
3638 char *key;
3639 struct chunk *trash_key;
3640 int len;
3641
3642 trash_key = get_trash_chunk();
3643
3644 /* collect reference */
3645 ref = pat_ref_lookup(rule->arg.map.ref);
3646 if (!ref)
3647 continue;
3648
3649 /* collect key */
3650 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3651 key = trash_key->str;
3652 key[len] = '\0';
3653
3654 /* perform update */
3655 /* check if the entry already exists */
3656 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003657 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003658
3659 break;
3660 }
3661
3662 case HTTP_RES_ACT_SET_MAP: {
3663 struct pat_ref *ref;
3664 char *key, *value;
3665 struct chunk *trash_key, *trash_value;
3666 int len;
3667
3668 trash_key = get_trash_chunk();
3669 trash_value = get_trash_chunk();
3670
3671 /* collect reference */
3672 ref = pat_ref_lookup(rule->arg.map.ref);
3673 if (!ref)
3674 continue;
3675
3676 /* collect key */
3677 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3678 key = trash_key->str;
3679 key[len] = '\0';
3680
3681 /* collect value */
3682 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3683 value = trash_value->str;
3684 value[len] = '\0';
3685
3686 /* perform update */
3687 if (pat_ref_find_elt(ref, key) != NULL)
3688 /* update entry if it exists */
3689 pat_ref_set(ref, key, value, NULL);
3690 else
3691 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003692 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003693
3694 break;
3695 }
William Lallemand73025dd2014-04-24 14:38:37 +02003696
3697 case HTTP_RES_ACT_CUSTOM_CONT:
3698 rule->action_ptr(rule, px, s, txn);
3699 break;
3700
3701 case HTTP_RES_ACT_CUSTOM_STOP:
3702 rule->action_ptr(rule, px, s, txn);
3703 return rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003704 }
3705 }
3706
3707 /* we reached the end of the rules, nothing to report */
3708 return NULL;
3709}
3710
3711
Willy Tarreau71241ab2012-12-27 11:30:54 +01003712/* Perform an HTTP redirect based on the information in <rule>. The function
3713 * returns non-zero on success, or zero in case of a, irrecoverable error such
3714 * as too large a request to build a valid response.
3715 */
3716static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3717{
3718 struct http_msg *msg = &txn->req;
3719 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003720 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003721
3722 /* build redirect message */
3723 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003724 case 308:
3725 msg_fmt = HTTP_308;
3726 break;
3727 case 307:
3728 msg_fmt = HTTP_307;
3729 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003730 case 303:
3731 msg_fmt = HTTP_303;
3732 break;
3733 case 301:
3734 msg_fmt = HTTP_301;
3735 break;
3736 case 302:
3737 default:
3738 msg_fmt = HTTP_302;
3739 break;
3740 }
3741
3742 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3743 return 0;
3744
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003745 location = trash.str + trash.len;
3746
Willy Tarreau71241ab2012-12-27 11:30:54 +01003747 switch(rule->type) {
3748 case REDIRECT_TYPE_SCHEME: {
3749 const char *path;
3750 const char *host;
3751 struct hdr_ctx ctx;
3752 int pathlen;
3753 int hostlen;
3754
3755 host = "";
3756 hostlen = 0;
3757 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003758 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003759 host = ctx.line + ctx.val;
3760 hostlen = ctx.vlen;
3761 }
3762
3763 path = http_get_path(txn);
3764 /* build message using path */
3765 if (path) {
3766 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3767 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3768 int qs = 0;
3769 while (qs < pathlen) {
3770 if (path[qs] == '?') {
3771 pathlen = qs;
3772 break;
3773 }
3774 qs++;
3775 }
3776 }
3777 } else {
3778 path = "/";
3779 pathlen = 1;
3780 }
3781
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003782 if (rule->rdr_str) { /* this is an old "redirect" rule */
3783 /* check if we can add scheme + "://" + host + path */
3784 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3785 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003786
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003787 /* add scheme */
3788 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3789 trash.len += rule->rdr_len;
3790 }
3791 else {
3792 /* add scheme with executing log format */
3793 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003794
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003795 /* check if we can add scheme + "://" + host + path */
3796 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3797 return 0;
3798 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003799 /* add "://" */
3800 memcpy(trash.str + trash.len, "://", 3);
3801 trash.len += 3;
3802
3803 /* add host */
3804 memcpy(trash.str + trash.len, host, hostlen);
3805 trash.len += hostlen;
3806
3807 /* add path */
3808 memcpy(trash.str + trash.len, path, pathlen);
3809 trash.len += pathlen;
3810
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003811 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003812 if (trash.len && trash.str[trash.len - 1] != '/' &&
3813 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3814 if (trash.len > trash.size - 5)
3815 return 0;
3816 trash.str[trash.len] = '/';
3817 trash.len++;
3818 }
3819
3820 break;
3821 }
3822 case REDIRECT_TYPE_PREFIX: {
3823 const char *path;
3824 int pathlen;
3825
3826 path = http_get_path(txn);
3827 /* build message using path */
3828 if (path) {
3829 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3830 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3831 int qs = 0;
3832 while (qs < pathlen) {
3833 if (path[qs] == '?') {
3834 pathlen = qs;
3835 break;
3836 }
3837 qs++;
3838 }
3839 }
3840 } else {
3841 path = "/";
3842 pathlen = 1;
3843 }
3844
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003845 if (rule->rdr_str) { /* this is an old "redirect" rule */
3846 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3847 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003848
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003849 /* add prefix. Note that if prefix == "/", we don't want to
3850 * add anything, otherwise it makes it hard for the user to
3851 * configure a self-redirection.
3852 */
3853 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3854 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3855 trash.len += rule->rdr_len;
3856 }
3857 }
3858 else {
3859 /* add prefix with executing log format */
3860 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3861
3862 /* Check length */
3863 if (trash.len + pathlen > trash.size - 4)
3864 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003865 }
3866
3867 /* add path */
3868 memcpy(trash.str + trash.len, path, pathlen);
3869 trash.len += pathlen;
3870
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003871 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003872 if (trash.len && trash.str[trash.len - 1] != '/' &&
3873 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3874 if (trash.len > trash.size - 5)
3875 return 0;
3876 trash.str[trash.len] = '/';
3877 trash.len++;
3878 }
3879
3880 break;
3881 }
3882 case REDIRECT_TYPE_LOCATION:
3883 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003884 if (rule->rdr_str) { /* this is an old "redirect" rule */
3885 if (trash.len + rule->rdr_len > trash.size - 4)
3886 return 0;
3887
3888 /* add location */
3889 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3890 trash.len += rule->rdr_len;
3891 }
3892 else {
3893 /* add location with executing log format */
3894 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003895
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003896 /* Check left length */
3897 if (trash.len > trash.size - 4)
3898 return 0;
3899 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003900 break;
3901 }
3902
3903 if (rule->cookie_len) {
3904 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3905 trash.len += 14;
3906 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3907 trash.len += rule->cookie_len;
3908 memcpy(trash.str + trash.len, "\r\n", 2);
3909 trash.len += 2;
3910 }
3911
3912 /* add end of headers and the keep-alive/close status.
3913 * We may choose to set keep-alive if the Location begins
3914 * with a slash, because the client will come back to the
3915 * same server.
3916 */
3917 txn->status = rule->code;
3918 /* let's log the request time */
3919 s->logs.tv_request = now;
3920
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003921 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003922 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3923 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3924 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3925 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3926 /* keep-alive possible */
3927 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3928 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3929 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3930 trash.len += 30;
3931 } else {
3932 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3933 trash.len += 24;
3934 }
3935 }
3936 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3937 trash.len += 4;
3938 bo_inject(txn->rsp.chn, trash.str, trash.len);
3939 /* "eat" the request */
3940 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02003941 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003942 msg->sov = 0;
3943 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3944 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3945 txn->req.msg_state = HTTP_MSG_CLOSED;
3946 txn->rsp.msg_state = HTTP_MSG_DONE;
3947 } else {
3948 /* keep-alive not possible */
3949 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3950 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3951 trash.len += 29;
3952 } else {
3953 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3954 trash.len += 23;
3955 }
3956 stream_int_retnclose(txn->req.chn->prod, &trash);
3957 txn->req.chn->analysers = 0;
3958 }
3959
3960 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003961 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003962 if (!(s->flags & SN_FINST_MASK))
3963 s->flags |= SN_FINST_R;
3964
3965 return 1;
3966}
3967
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003968/* This stream analyser runs all HTTP request processing which is common to
3969 * frontends and backends, which means blocking ACLs, filters, connection-close,
3970 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003971 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003972 * either needs more data or wants to immediately abort the request (eg: deny,
3973 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003974 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003975int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003976{
Willy Tarreaud787e662009-07-07 10:14:51 +02003977 struct http_txn *txn = &s->txn;
3978 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003979 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003980 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003981 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02003982
Willy Tarreau655dce92009-11-08 13:10:58 +01003983 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003984 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003985 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003986 return 0;
3987 }
3988
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003989 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 +02003990 now_ms, __FUNCTION__,
3991 s,
3992 req,
3993 req->rex, req->wex,
3994 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003995 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003996 req->analysers);
3997
Willy Tarreau65410832014-04-28 21:25:43 +02003998 /* just in case we have some per-backend tracking */
3999 session_inc_be_http_req_ctr(s);
4000
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004001 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004002 if (!LIST_ISEMPTY(&px->http_req_rules)) {
4003 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01004004
Willy Tarreau0b748332014-04-29 00:13:29 +02004005 switch (verdict) {
4006 case HTTP_RULE_RES_CONT:
4007 case HTTP_RULE_RES_STOP: /* nothing to do */
4008 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004009
Willy Tarreau0b748332014-04-29 00:13:29 +02004010 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4011 if (txn->flags & TX_CLTARPIT)
4012 goto tarpit;
4013 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004014
Willy Tarreau0b748332014-04-29 00:13:29 +02004015 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4016 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004017
Willy Tarreau0b748332014-04-29 00:13:29 +02004018 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004019 goto done;
4020
Willy Tarreau0b748332014-04-29 00:13:29 +02004021 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4022 goto return_bad_req;
4023 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004024 }
4025
Willy Tarreau52542592014-04-28 18:33:26 +02004026 /* OK at this stage, we know that the request was accepted according to
4027 * the http-request rules, we can check for the stats. Note that the
4028 * URI is detected *before* the req* rules in order not to be affected
4029 * by a possible reqrep, while they are processed *after* so that a
4030 * reqdeny can still block them. This clearly needs to change in 1.6!
4031 */
4032 if (stats_check_uri(s->rep->prod, txn, px)) {
4033 s->target = &http_stats_applet.obj_type;
4034 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
4035 txn->status = 500;
4036 s->logs.tv_request = now;
4037 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004038
Willy Tarreau52542592014-04-28 18:33:26 +02004039 if (!(s->flags & SN_ERR_MASK))
4040 s->flags |= SN_ERR_RESOURCE;
4041 goto return_prx_cond;
4042 }
4043
4044 /* parse the whole stats request and extract the relevant information */
4045 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004046 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4047 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004048
Willy Tarreau0b748332014-04-29 00:13:29 +02004049 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4050 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004051
Willy Tarreau0b748332014-04-29 00:13:29 +02004052 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4053 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004054 }
4055
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004056 /* evaluate the req* rules except reqadd */
4057 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004058 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004059 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004060
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004061 if (txn->flags & TX_CLDENY)
4062 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004063
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004064 if (txn->flags & TX_CLTARPIT)
4065 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004066 }
Willy Tarreau06619262006-12-17 08:37:22 +01004067
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004068 /* add request headers from the rule sets in the same order */
4069 list_for_each_entry(wl, &px->req_add, list) {
4070 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004071 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004072 ret = acl_pass(ret);
4073 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4074 ret = !ret;
4075 if (!ret)
4076 continue;
4077 }
4078
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004079 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004080 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004081 }
4082
Willy Tarreau52542592014-04-28 18:33:26 +02004083
4084 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004085 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004086 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004087 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
4088 s->fe->fe_counters.intercepted_req++;
4089
4090 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
4091 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
4092 if (!(s->flags & SN_FINST_MASK))
4093 s->flags |= SN_FINST_R;
4094
Willy Tarreau70730dd2014-04-24 18:06:27 +02004095 /* we may want to compress the stats page */
4096 if (s->fe->comp || s->be->comp)
4097 select_compression_request_header(s, req->buf);
4098
4099 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreaude8ca962014-11-20 22:23:10 +01004100 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004101 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004102 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004103
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004104 /* check whether we have some ACLs set to redirect this request */
4105 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004106 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004107 int ret;
4108
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004109 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004110 ret = acl_pass(ret);
4111 if (rule->cond->pol == ACL_COND_UNLESS)
4112 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004113 if (!ret)
4114 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004115 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004116 if (!http_apply_redirect_rule(rule, s, txn))
4117 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004118 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004119 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004120
Willy Tarreau2be39392010-01-03 17:24:51 +01004121 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4122 * If this happens, then the data will not come immediately, so we must
4123 * send all what we have without waiting. Note that due to the small gain
4124 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004125 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004126 * itself once used.
4127 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004128 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004129
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004130 done: /* done with this analyser, continue with next ones that the calling
4131 * points will have set, if any.
4132 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004133 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004134 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4135 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004136 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004137
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004138 tarpit:
4139 /* When a connection is tarpitted, we use the tarpit timeout,
4140 * which may be the same as the connect timeout if unspecified.
4141 * If unset, then set it to zero because we really want it to
4142 * eventually expire. We build the tarpit as an analyser.
4143 */
4144 channel_erase(s->req);
4145
4146 /* wipe the request out so that we can drop the connection early
4147 * if the client closes first.
4148 */
4149 channel_dont_connect(req);
4150 req->analysers = 0; /* remove switching rules etc... */
4151 req->analysers |= AN_REQ_HTTP_TARPIT;
4152 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4153 if (!req->analyse_exp)
4154 req->analyse_exp = tick_add(now_ms, 0);
4155 session_inc_http_err_ctr(s);
4156 s->fe->fe_counters.denied_req++;
4157 if (s->fe != s->be)
4158 s->be->be_counters.denied_req++;
4159 if (s->listener->counters)
4160 s->listener->counters->denied_req++;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004161 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004162
4163 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004164 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004165 txn->status = 403;
4166 s->logs.tv_request = now;
4167 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4168 session_inc_http_err_ctr(s);
4169 s->fe->fe_counters.denied_req++;
4170 if (s->fe != s->be)
4171 s->be->be_counters.denied_req++;
4172 if (s->listener->counters)
4173 s->listener->counters->denied_req++;
4174 goto return_prx_cond;
4175
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004176 return_bad_req:
4177 /* We centralize bad requests processing here */
4178 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4179 /* we detected a parsing error. We want to archive this request
4180 * in the dedicated proxy area for later troubleshooting.
4181 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004182 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004183 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004184
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004185 txn->req.msg_state = HTTP_MSG_ERROR;
4186 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004187 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004188
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004189 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004190 if (s->listener->counters)
4191 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004192
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004193 return_prx_cond:
4194 if (!(s->flags & SN_ERR_MASK))
4195 s->flags |= SN_ERR_PRXCOND;
4196 if (!(s->flags & SN_FINST_MASK))
4197 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004198
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004199 req->analysers = 0;
4200 req->analyse_exp = TICK_ETERNITY;
4201 return 0;
4202}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004203
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004204/* This function performs all the processing enabled for the current request.
4205 * It returns 1 if the processing can continue on next analysers, or zero if it
4206 * needs more data, encounters an error, or wants to immediately abort the
4207 * request. It relies on buffers flags, and updates s->req->analysers.
4208 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004209int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004210{
4211 struct http_txn *txn = &s->txn;
4212 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004213 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004214
Willy Tarreau655dce92009-11-08 13:10:58 +01004215 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004216 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004217 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004218 return 0;
4219 }
4220
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004221 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 +02004222 now_ms, __FUNCTION__,
4223 s,
4224 req,
4225 req->rex, req->wex,
4226 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004227 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004228 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004229
William Lallemand82fe75c2012-10-23 10:25:10 +02004230 if (s->fe->comp || s->be->comp)
4231 select_compression_request_header(s, req->buf);
4232
Willy Tarreau59234e92008-11-30 23:51:27 +01004233 /*
4234 * Right now, we know that we have processed the entire headers
4235 * and that unwanted requests have been filtered out. We can do
4236 * whatever we want with the remaining request. Also, now we
4237 * may have separate values for ->fe, ->be.
4238 */
Willy Tarreau06619262006-12-17 08:37:22 +01004239
Willy Tarreau59234e92008-11-30 23:51:27 +01004240 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004241 * If HTTP PROXY is set we simply get remote server address parsing
4242 * incoming request. Note that this requires that a connection is
4243 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004244 */
4245 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004246 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004247 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004248
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004249 /* Note that for now we don't reuse existing proxy connections */
4250 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004251 txn->req.msg_state = HTTP_MSG_ERROR;
4252 txn->status = 500;
4253 req->analysers = 0;
4254 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4255
4256 if (!(s->flags & SN_ERR_MASK))
4257 s->flags |= SN_ERR_RESOURCE;
4258 if (!(s->flags & SN_FINST_MASK))
4259 s->flags |= SN_FINST_R;
4260
4261 return 0;
4262 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004263
4264 path = http_get_path(txn);
4265 url2sa(req->buf->p + msg->sl.rq.u,
4266 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004267 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004268 /* if the path was found, we have to remove everything between
4269 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4270 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4271 * u_l characters by a single "/".
4272 */
4273 if (path) {
4274 char *cur_ptr = req->buf->p;
4275 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4276 int delta;
4277
4278 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4279 http_msg_move_end(&txn->req, delta);
4280 cur_end += delta;
4281 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4282 goto return_bad_req;
4283 }
4284 else {
4285 char *cur_ptr = req->buf->p;
4286 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4287 int delta;
4288
4289 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4290 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4291 http_msg_move_end(&txn->req, delta);
4292 cur_end += delta;
4293 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4294 goto return_bad_req;
4295 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004296 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004297
Willy Tarreau59234e92008-11-30 23:51:27 +01004298 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004299 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004300 * Note that doing so might move headers in the request, but
4301 * the fields will stay coherent and the URI will not move.
4302 * This should only be performed in the backend.
4303 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004304 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004305 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4306 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004307
Willy Tarreau59234e92008-11-30 23:51:27 +01004308 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004309 * 8: the appsession cookie was looked up very early in 1.2,
4310 * so let's do the same now.
4311 */
4312
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004313 /* It needs to look into the URI unless persistence must be ignored */
4314 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004315 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 +01004316 }
4317
William Lallemanda73203e2012-03-12 12:48:57 +01004318 /* add unique-id if "header-unique-id" is specified */
4319
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004320 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4321 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4322 goto return_bad_req;
4323 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004324 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004325 }
William Lallemanda73203e2012-03-12 12:48:57 +01004326
4327 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004328 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4329 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004330 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004331 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004332 goto return_bad_req;
4333 }
4334
Cyril Bontéb21570a2009-11-29 20:04:48 +01004335 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004336 * 9: add X-Forwarded-For if either the frontend or the backend
4337 * asks for it.
4338 */
4339 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004340 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004341 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004342 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4343 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004344 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004345 /* The header is set to be added only if none is present
4346 * and we found it, so don't do anything.
4347 */
4348 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004349 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004350 /* Add an X-Forwarded-For header unless the source IP is
4351 * in the 'except' network range.
4352 */
4353 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004354 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004355 != s->fe->except_net.s_addr) &&
4356 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004357 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004358 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004359 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004360 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004361 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004362
4363 /* Note: we rely on the backend to get the header name to be used for
4364 * x-forwarded-for, because the header is really meant for the backends.
4365 * However, if the backend did not specify any option, we have to rely
4366 * on the frontend's header name.
4367 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004368 if (s->be->fwdfor_hdr_len) {
4369 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004370 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004371 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004372 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004373 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004374 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004375 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 +01004376
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004377 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004378 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004379 }
4380 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004381 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004382 /* FIXME: for the sake of completeness, we should also support
4383 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004384 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004385 int len;
4386 char pn[INET6_ADDRSTRLEN];
4387 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004388 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004389 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004390
Willy Tarreau59234e92008-11-30 23:51:27 +01004391 /* Note: we rely on the backend to get the header name to be used for
4392 * x-forwarded-for, because the header is really meant for the backends.
4393 * However, if the backend did not specify any option, we have to rely
4394 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004395 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004396 if (s->be->fwdfor_hdr_len) {
4397 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004398 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004399 } else {
4400 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004401 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004402 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004403 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004404
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004405 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004406 goto return_bad_req;
4407 }
4408 }
4409
4410 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004411 * 10: add X-Original-To if either the frontend or the backend
4412 * asks for it.
4413 */
4414 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4415
4416 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004417 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004418 /* Add an X-Original-To header unless the destination IP is
4419 * in the 'except' network range.
4420 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004421 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004422
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004423 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004424 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004425 (((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 +02004426 != s->fe->except_to.s_addr) &&
4427 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004428 (((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 +02004429 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004430 int len;
4431 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004432 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004433
4434 /* Note: we rely on the backend to get the header name to be used for
4435 * x-original-to, because the header is really meant for the backends.
4436 * However, if the backend did not specify any option, we have to rely
4437 * on the frontend's header name.
4438 */
4439 if (s->be->orgto_hdr_len) {
4440 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004441 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004442 } else {
4443 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004444 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004445 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004446 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 +02004447
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004448 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004449 goto return_bad_req;
4450 }
4451 }
4452 }
4453
Willy Tarreau50fc7772012-11-11 22:19:57 +01004454 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4455 * If an "Upgrade" token is found, the header is left untouched in order not to have
4456 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4457 * "Upgrade" is present in the Connection header.
4458 */
4459 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4460 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004461 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4462 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004463 unsigned int want_flags = 0;
4464
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004465 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004466 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004467 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4468 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004469 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004470 want_flags |= TX_CON_CLO_SET;
4471 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004472 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004473 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4474 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004475 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004476 want_flags |= TX_CON_KAL_SET;
4477 }
4478
4479 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004480 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004481 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004482
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004483
Willy Tarreau522d6c02009-12-06 18:49:18 +01004484 /* If we have no server assigned yet and we're balancing on url_param
4485 * with a POST request, we may be interested in checking the body for
4486 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004487 */
4488 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4489 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004490 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004491 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004492 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004493 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004494
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004495 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004496 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004497#ifdef TCP_QUICKACK
4498 /* We expect some data from the client. Unless we know for sure
4499 * we already have a full request, we have to re-enable quick-ack
4500 * in case we previously disabled it, otherwise we might cause
4501 * the client to delay further data.
4502 */
4503 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004504 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004505 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004506 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004507 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004508#endif
4509 }
Willy Tarreau03945942009-12-22 16:50:27 +01004510
Willy Tarreau59234e92008-11-30 23:51:27 +01004511 /*************************************************************
4512 * OK, that's finished for the headers. We have done what we *
4513 * could. Let's switch to the DATA state. *
4514 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004515 req->analyse_exp = TICK_ETERNITY;
4516 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004517
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004518 /* if the server closes the connection, we want to immediately react
4519 * and close the socket to save packets and syscalls.
4520 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004521 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4522 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004523
Willy Tarreau59234e92008-11-30 23:51:27 +01004524 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004525 /* OK let's go on with the BODY now */
4526 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004527
Willy Tarreau59234e92008-11-30 23:51:27 +01004528 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004529 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004530 /* we detected a parsing error. We want to archive this request
4531 * in the dedicated proxy area for later troubleshooting.
4532 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004533 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004534 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004535
Willy Tarreau59234e92008-11-30 23:51:27 +01004536 txn->req.msg_state = HTTP_MSG_ERROR;
4537 txn->status = 400;
4538 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004539 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004540
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004541 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004542 if (s->listener->counters)
4543 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004544
Willy Tarreau59234e92008-11-30 23:51:27 +01004545 if (!(s->flags & SN_ERR_MASK))
4546 s->flags |= SN_ERR_PRXCOND;
4547 if (!(s->flags & SN_FINST_MASK))
4548 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004549 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004550}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004551
Willy Tarreau60b85b02008-11-30 23:28:40 +01004552/* This function is an analyser which processes the HTTP tarpit. It always
4553 * returns zero, at the beginning because it prevents any other processing
4554 * from occurring, and at the end because it terminates the request.
4555 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004556int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004557{
4558 struct http_txn *txn = &s->txn;
4559
4560 /* This connection is being tarpitted. The CLIENT side has
4561 * already set the connect expiration date to the right
4562 * timeout. We just have to check that the client is still
4563 * there and that the timeout has not expired.
4564 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004565 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004566 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004567 !tick_is_expired(req->analyse_exp, now_ms))
4568 return 0;
4569
4570 /* We will set the queue timer to the time spent, just for
4571 * logging purposes. We fake a 500 server error, so that the
4572 * attacker will not suspect his connection has been tarpitted.
4573 * It will not cause trouble to the logs because we can exclude
4574 * the tarpitted connections by filtering on the 'PT' status flags.
4575 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004576 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4577
4578 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004579 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004580 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004581
4582 req->analysers = 0;
4583 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004584
Willy Tarreau60b85b02008-11-30 23:28:40 +01004585 if (!(s->flags & SN_ERR_MASK))
4586 s->flags |= SN_ERR_PRXCOND;
4587 if (!(s->flags & SN_FINST_MASK))
4588 s->flags |= SN_FINST_T;
4589 return 0;
4590}
4591
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004592/* This function is an analyser which waits for the HTTP request body. It waits
4593 * for either the buffer to be full, or the full advertised contents to have
4594 * reached the buffer. It must only be called after the standard HTTP request
4595 * processing has occurred, because it expects the request to be parsed and will
4596 * look for the Expect header. It may send a 100-Continue interim response. It
4597 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4598 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4599 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004600 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004601int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004602{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004603 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004604 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004605
4606 /* We have to parse the HTTP request body to find any required data.
4607 * "balance url_param check_post" should have been the only way to get
4608 * into this. We were brought here after HTTP header analysis, so all
4609 * related structures are ready.
4610 */
4611
Willy Tarreau890988f2014-04-10 11:59:33 +02004612 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4613 /* This is the first call */
4614 if (msg->msg_state < HTTP_MSG_BODY)
4615 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004616
Willy Tarreau890988f2014-04-10 11:59:33 +02004617 if (msg->msg_state < HTTP_MSG_100_SENT) {
4618 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4619 * send an HTTP/1.1 100 Continue intermediate response.
4620 */
4621 if (msg->flags & HTTP_MSGF_VER_11) {
4622 struct hdr_ctx ctx;
4623 ctx.idx = 0;
4624 /* Expect is allowed in 1.1, look for it */
4625 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4626 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4627 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4628 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004629 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004630 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004631 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004632
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004633 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004634 * req->buf->p still points to the beginning of the message. We
4635 * must save the body in msg->next because it survives buffer
4636 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004637 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004638 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004639
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004640 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004641 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4642 else
4643 msg->msg_state = HTTP_MSG_DATA;
4644 }
4645
Willy Tarreau890988f2014-04-10 11:59:33 +02004646 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4647 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreau5b7f9242015-05-01 23:05:14 +02004648 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004649 goto missing_data;
4650
4651 /* OK we have everything we need now */
4652 goto http_end;
4653 }
4654
4655 /* OK here we're parsing a chunked-encoded message */
4656
Willy Tarreau522d6c02009-12-06 18:49:18 +01004657 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004658 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004659 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004660 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004661 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004662 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004663
Willy Tarreau115acb92009-12-26 13:56:06 +01004664 if (!ret)
4665 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004666 else if (ret < 0) {
4667 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004668 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004669 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004670 }
4671
Willy Tarreaud98cf932009-12-27 22:54:55 +01004672 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau5b7f9242015-05-01 23:05:14 +02004673 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4674 * for at least a whole chunk or the whole content length bytes after
4675 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004676 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004677 if (msg->msg_state == HTTP_MSG_TRAILERS)
4678 goto http_end;
4679
Willy Tarreau5b7f9242015-05-01 23:05:14 +02004680 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004681 goto http_end;
4682
4683 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004684 /* we get here if we need to wait for more data. If the buffer is full,
4685 * we have the maximum we can expect.
4686 */
4687 if (buffer_full(req->buf, global.tune.maxrewrite))
4688 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004689
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004690 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004691 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004692 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004693
4694 if (!(s->flags & SN_ERR_MASK))
4695 s->flags |= SN_ERR_CLITO;
4696 if (!(s->flags & SN_FINST_MASK))
4697 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004698 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004699 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004700
4701 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004702 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004703 /* Not enough data. We'll re-use the http-request
4704 * timeout here. Ideally, we should set the timeout
4705 * relative to the accept() date. We just set the
4706 * request timeout once at the beginning of the
4707 * request.
4708 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004709 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004710 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004711 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004712 return 0;
4713 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004714
4715 http_end:
4716 /* The situation will not evolve, so let's give up on the analysis. */
4717 s->logs.tv_request = now; /* update the request timer to reflect full request */
4718 req->analysers &= ~an_bit;
4719 req->analyse_exp = TICK_ETERNITY;
4720 return 1;
4721
4722 return_bad_req: /* let's centralize all bad requests */
4723 txn->req.msg_state = HTTP_MSG_ERROR;
4724 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004725 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004726
Willy Tarreau79ebac62010-06-07 13:47:49 +02004727 if (!(s->flags & SN_ERR_MASK))
4728 s->flags |= SN_ERR_PRXCOND;
4729 if (!(s->flags & SN_FINST_MASK))
4730 s->flags |= SN_FINST_R;
4731
Willy Tarreau522d6c02009-12-06 18:49:18 +01004732 return_err_msg:
4733 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004734 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004735 if (s->listener->counters)
4736 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004737 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004738}
4739
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004740/* send a server's name with an outgoing request over an established connection.
4741 * Note: this function is designed to be called once the request has been scheduled
4742 * for being forwarded. This is the reason why it rewinds the buffer before
4743 * proceeding.
4744 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004745int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004746
4747 struct hdr_ctx ctx;
4748
Mark Lamourinec2247f02012-01-04 13:02:01 -05004749 char *hdr_name = be->server_id_hdr_name;
4750 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004751 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004752 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004753 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004754
William Lallemandd9e90662012-01-30 17:27:17 +01004755 ctx.idx = 0;
4756
Willy Tarreau211cdec2014-04-17 20:18:08 +02004757 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004758 if (old_o) {
4759 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004760 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004761 txn->req.next += old_o;
4762 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004763 }
4764
Willy Tarreau9b28e032012-10-12 23:49:43 +02004765 old_i = chn->buf->i;
4766 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 -05004767 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004768 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004769 }
4770
4771 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004772 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004773 memcpy(hdr_val, hdr_name, hdr_name_len);
4774 hdr_val += hdr_name_len;
4775 *hdr_val++ = ':';
4776 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004777 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4778 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004779
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004780 if (old_o) {
4781 /* If this was a forwarded request, we must readjust the amount of
4782 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004783 * variations. Note that the current state is >= HTTP_MSG_BODY,
4784 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004785 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004786 old_o += chn->buf->i - old_i;
4787 b_adv(chn->buf, old_o);
4788 txn->req.next -= old_o;
4789 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004790 }
4791
Mark Lamourinec2247f02012-01-04 13:02:01 -05004792 return 0;
4793}
4794
Willy Tarreau610ecce2010-01-04 21:15:02 +01004795/* Terminate current transaction and prepare a new one. This is very tricky
4796 * right now but it works.
4797 */
4798void http_end_txn_clean_session(struct session *s)
4799{
Willy Tarreau068621e2013-12-23 15:11:25 +01004800 int prev_status = s->txn.status;
4801
Willy Tarreau610ecce2010-01-04 21:15:02 +01004802 /* FIXME: We need a more portable way of releasing a backend's and a
4803 * server's connections. We need a safer way to reinitialize buffer
4804 * flags. We also need a more accurate method for computing per-request
4805 * data.
4806 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004807
Willy Tarreau4213a112013-12-15 10:25:42 +01004808 /* unless we're doing keep-alive, we want to quickly close the connection
4809 * to the server.
4810 */
4811 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4812 !si_conn_ready(s->req->cons)) {
4813 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4814 si_shutr(s->req->cons);
4815 si_shutw(s->req->cons);
4816 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004817
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004818 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004819 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004820 if (unlikely(s->srv_conn))
4821 sess_change_server(s, NULL);
4822 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004823
4824 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4825 session_process_counters(s);
4826
4827 if (s->txn.status) {
4828 int n;
4829
4830 n = s->txn.status / 100;
4831 if (n < 1 || n > 5)
4832 n = 0;
4833
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004834 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004835 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004836 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004837 s->fe->fe_counters.p.http.comp_rsp++;
4838 }
Willy Tarreau24657792010-02-26 10:30:28 +01004839 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004840 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004841 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004842 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004843 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004844 s->be->be_counters.p.http.comp_rsp++;
4845 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004846 }
4847
4848 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004849 s->logs.bytes_in -= s->req->buf->i;
4850 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004851
4852 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004853 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004854 !(s->flags & SN_MONITOR) &&
4855 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4856 s->do_log(s);
4857 }
4858
Willy Tarreauc177ea72014-06-25 15:36:04 +02004859 /* stop tracking content-based counters */
4860 session_stop_content_counters(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004861 session_update_time_stats(s);
4862
Willy Tarreau610ecce2010-01-04 21:15:02 +01004863 s->logs.accept_date = date; /* user-visible date for logging */
4864 s->logs.tv_accept = now; /* corrected date for internal use */
4865 tv_zero(&s->logs.tv_request);
4866 s->logs.t_queue = -1;
4867 s->logs.t_connect = -1;
4868 s->logs.t_data = -1;
4869 s->logs.t_close = 0;
4870 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4871 s->logs.srv_queue_size = 0; /* we will get this number soon */
4872
Willy Tarreau9b28e032012-10-12 23:49:43 +02004873 s->logs.bytes_in = s->req->total = s->req->buf->i;
4874 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004875
4876 if (s->pend_pos)
4877 pendconn_free(s->pend_pos);
4878
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004879 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004880 if (s->flags & SN_CURR_SESS) {
4881 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004882 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004883 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004884 if (may_dequeue_tasks(objt_server(s->target), s->be))
4885 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004886 }
4887
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004888 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004889
Willy Tarreau4213a112013-12-15 10:25:42 +01004890 /* only release our endpoint if we don't intend to reuse the
4891 * connection.
4892 */
4893 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4894 !si_conn_ready(s->req->cons)) {
4895 si_release_endpoint(s->req->cons);
4896 }
4897
Willy Tarreau610ecce2010-01-04 21:15:02 +01004898 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004899 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004900 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004901 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004902 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreaub4d05092014-09-01 20:35:55 +02004903 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA);
4904 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|CF_WROTE_DATA);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004905 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 +01004906 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Cyril Bonté17f77072014-10-22 22:30:13 +02004907 s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004908
Willy Tarreau610ecce2010-01-04 21:15:02 +01004909 s->txn.meth = 0;
4910 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004911 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004912
4913 if (prev_status == 401 || prev_status == 407) {
4914 /* In HTTP keep-alive mode, if we receive a 401, we still have
4915 * a chance of being able to send the visitor again to the same
4916 * server over the same connection. This is required by some
4917 * broken protocols such as NTLM, and anyway whenever there is
4918 * an opportunity for sending the challenge to the proper place,
4919 * it's better to do it (at least it helps with debugging).
4920 */
4921 s->txn.flags |= TX_PREFER_LAST;
4922 }
4923
Willy Tarreauee55dc02010-06-01 10:56:34 +02004924 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004925 s->req->cons->flags |= SI_FL_INDEP_STR;
4926
Willy Tarreau96e31212011-05-30 18:10:30 +02004927 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004928 s->req->flags |= CF_NEVER_WAIT;
4929 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004930 }
4931
Willy Tarreau610ecce2010-01-04 21:15:02 +01004932 /* if the request buffer is not empty, it means we're
4933 * about to process another request, so send pending
4934 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004935 * Just don't do this if the buffer is close to be full,
4936 * because the request will wait for it to flush a little
4937 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004938 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004939 if (s->req->buf->i) {
4940 if (s->rep->buf->o &&
4941 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4942 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004943 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004944 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004945
4946 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004947 channel_auto_read(s->req);
4948 channel_auto_close(s->req);
4949 channel_auto_read(s->rep);
4950 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004951
Willy Tarreau27375622013-12-17 00:00:28 +01004952 /* we're in keep-alive with an idle connection, monitor it */
4953 si_idle_conn(s->req->cons);
4954
Willy Tarreau342b11c2010-11-24 16:22:09 +01004955 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004956 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004957}
4958
4959
4960/* This function updates the request state machine according to the response
4961 * state machine and buffer flags. It returns 1 if it changes anything (flag
4962 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4963 * it is only used to find when a request/response couple is complete. Both
4964 * this function and its equivalent should loop until both return zero. It
4965 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4966 */
4967int http_sync_req_state(struct session *s)
4968{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004969 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004970 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004971 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004972 unsigned int old_state = txn->req.msg_state;
4973
Willy Tarreau610ecce2010-01-04 21:15:02 +01004974 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4975 return 0;
4976
4977 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004978 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004979 * We can shut the read side unless we want to abort_on_close,
4980 * or we have a POST request. The issue with POST requests is
4981 * that some browsers still send a CRLF after the request, and
4982 * this CRLF must be read so that it does not remain in the kernel
4983 * buffers, otherwise a close could cause an RST on some systems
4984 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004985 * Note that if we're using keep-alive on the client side, we'd
4986 * rather poll now and keep the polling enabled for the whole
4987 * session's life than enabling/disabling it between each
4988 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004989 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004990 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4991 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4992 !(s->be->options & PR_O_ABRT_CLOSE) &&
4993 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004994 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004995
Willy Tarreau40f151a2012-12-20 12:10:09 +01004996 /* if the server closes the connection, we want to immediately react
4997 * and close the socket to save packets and syscalls.
4998 */
4999 chn->cons->flags |= SI_FL_NOHALF;
5000
Willy Tarreau610ecce2010-01-04 21:15:02 +01005001 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5002 goto wait_other_side;
5003
5004 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5005 /* The server has not finished to respond, so we
5006 * don't want to move in order not to upset it.
5007 */
5008 goto wait_other_side;
5009 }
5010
5011 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5012 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005013 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005014 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005015 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005016 goto wait_other_side;
5017 }
5018
5019 /* When we get here, it means that both the request and the
5020 * response have finished receiving. Depending on the connection
5021 * mode, we'll have to wait for the last bytes to leave in either
5022 * direction, and sometimes for a close to be effective.
5023 */
5024
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005025 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5026 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005027 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5028 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005029 }
5030 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5031 /* Option forceclose is set, or either side wants to close,
5032 * let's enforce it now that we're not expecting any new
5033 * data to come. The caller knows the session is complete
5034 * once both states are CLOSED.
5035 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005036 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5037 channel_shutr_now(chn);
5038 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005039 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005040 }
5041 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005042 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5043 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005044 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005045 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5046 channel_auto_read(chn);
5047 txn->req.msg_state = HTTP_MSG_TUNNEL;
5048 chn->flags |= CF_NEVER_WAIT;
5049 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005050 }
5051
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005052 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005053 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005054 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005055
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005056 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005057 txn->req.msg_state = HTTP_MSG_CLOSING;
5058 goto http_msg_closing;
5059 }
5060 else {
5061 txn->req.msg_state = HTTP_MSG_CLOSED;
5062 goto http_msg_closed;
5063 }
5064 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005065 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005066 }
5067
5068 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5069 http_msg_closing:
5070 /* nothing else to forward, just waiting for the output buffer
5071 * to be empty and for the shutw_now to take effect.
5072 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005073 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005074 txn->req.msg_state = HTTP_MSG_CLOSED;
5075 goto http_msg_closed;
5076 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005077 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005078 txn->req.msg_state = HTTP_MSG_ERROR;
5079 goto wait_other_side;
5080 }
5081 }
5082
5083 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5084 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005085 /* see above in MSG_DONE why we only do this in these states */
5086 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5087 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5088 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005089 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005090 goto wait_other_side;
5091 }
5092
5093 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005094 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005095}
5096
5097
5098/* This function updates the response state machine according to the request
5099 * state machine and buffer flags. It returns 1 if it changes anything (flag
5100 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5101 * it is only used to find when a request/response couple is complete. Both
5102 * this function and its equivalent should loop until both return zero. It
5103 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5104 */
5105int http_sync_res_state(struct session *s)
5106{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005107 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005108 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005109 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005110 unsigned int old_state = txn->rsp.msg_state;
5111
Willy Tarreau610ecce2010-01-04 21:15:02 +01005112 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5113 return 0;
5114
5115 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5116 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005117 * still monitor the server connection for a possible close
5118 * while the request is being uploaded, so we don't disable
5119 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005120 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005121 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005122
5123 if (txn->req.msg_state == HTTP_MSG_ERROR)
5124 goto wait_other_side;
5125
5126 if (txn->req.msg_state < HTTP_MSG_DONE) {
5127 /* The client seems to still be sending data, probably
5128 * because we got an error response during an upload.
5129 * We have the choice of either breaking the connection
5130 * or letting it pass through. Let's do the later.
5131 */
5132 goto wait_other_side;
5133 }
5134
5135 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5136 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005137 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005138 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005139 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005140 goto wait_other_side;
5141 }
5142
5143 /* When we get here, it means that both the request and the
5144 * response have finished receiving. Depending on the connection
5145 * mode, we'll have to wait for the last bytes to leave in either
5146 * direction, and sometimes for a close to be effective.
5147 */
5148
5149 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5150 /* Server-close mode : shut read and wait for the request
5151 * side to close its output buffer. The caller will detect
5152 * when we're in DONE and the other is in CLOSED and will
5153 * catch that for the final cleanup.
5154 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005155 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5156 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005157 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005158 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5159 /* Option forceclose is set, or either side wants to close,
5160 * let's enforce it now that we're not expecting any new
5161 * data to come. The caller knows the session is complete
5162 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005163 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005164 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5165 channel_shutr_now(chn);
5166 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005167 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005168 }
5169 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005170 /* The last possible modes are keep-alive and tunnel. Tunnel will
5171 * need to forward remaining data. Keep-alive will need to monitor
5172 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005173 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005174 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005175 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005176 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5177 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005178 }
5179
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005180 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005181 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005182 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005183 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5184 goto http_msg_closing;
5185 }
5186 else {
5187 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5188 goto http_msg_closed;
5189 }
5190 }
5191 goto wait_other_side;
5192 }
5193
5194 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5195 http_msg_closing:
5196 /* nothing else to forward, just waiting for the output buffer
5197 * to be empty and for the shutw_now to take effect.
5198 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005199 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005200 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5201 goto http_msg_closed;
5202 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005203 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005204 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005205 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005206 if (objt_server(s->target))
5207 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005208 goto wait_other_side;
5209 }
5210 }
5211
5212 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5213 http_msg_closed:
5214 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005215 bi_erase(chn);
5216 channel_auto_close(chn);
5217 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005218 goto wait_other_side;
5219 }
5220
5221 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005222 /* We force the response to leave immediately if we're waiting for the
5223 * other side, since there is no pending shutdown to push it out.
5224 */
5225 if (!channel_is_empty(chn))
5226 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005227 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005228}
5229
5230
5231/* Resync the request and response state machines. Return 1 if either state
5232 * changes.
5233 */
5234int http_resync_states(struct session *s)
5235{
5236 struct http_txn *txn = &s->txn;
5237 int old_req_state = txn->req.msg_state;
5238 int old_res_state = txn->rsp.msg_state;
5239
Willy Tarreau610ecce2010-01-04 21:15:02 +01005240 http_sync_req_state(s);
5241 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005242 if (!http_sync_res_state(s))
5243 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005244 if (!http_sync_req_state(s))
5245 break;
5246 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005247
Willy Tarreau610ecce2010-01-04 21:15:02 +01005248 /* OK, both state machines agree on a compatible state.
5249 * There are a few cases we're interested in :
5250 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5251 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5252 * directions, so let's simply disable both analysers.
5253 * - HTTP_MSG_CLOSED on the response only means we must abort the
5254 * request.
5255 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5256 * with server-close mode means we've completed one request and we
5257 * must re-initialize the server connection.
5258 */
5259
5260 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5261 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5262 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5263 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5264 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005265 channel_auto_close(s->req);
5266 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005267 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005268 channel_auto_close(s->rep);
5269 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005270 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005271 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5272 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005273 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005274 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005275 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005276 channel_auto_close(s->rep);
5277 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01005278 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005279 channel_abort(s->req);
5280 channel_auto_close(s->req);
5281 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005282 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005283 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005284 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5285 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005286 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005287 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5288 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5289 /* server-close/keep-alive: terminate this transaction,
5290 * possibly killing the server connection and reinitialize
5291 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005292 */
5293 http_end_txn_clean_session(s);
5294 }
5295
Willy Tarreau610ecce2010-01-04 21:15:02 +01005296 return txn->req.msg_state != old_req_state ||
5297 txn->rsp.msg_state != old_res_state;
5298}
5299
Willy Tarreaud98cf932009-12-27 22:54:55 +01005300/* This function is an analyser which forwards request body (including chunk
5301 * sizes if any). It is called as soon as we must forward, even if we forward
5302 * zero byte. The only situation where it must not be called is when we're in
5303 * tunnel mode and we want to forward till the close. It's used both to forward
5304 * remaining data and to resync after end of body. It expects the msg_state to
5305 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5306 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005307 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005308 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005309 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005310int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005311{
5312 struct http_txn *txn = &s->txn;
5313 struct http_msg *msg = &s->txn.req;
5314
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005315 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5316 return 0;
5317
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005318 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005319 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005320 /* Output closed while we were sending data. We must abort and
5321 * wake the other side up.
5322 */
5323 msg->msg_state = HTTP_MSG_ERROR;
5324 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005325 return 1;
5326 }
5327
Willy Tarreaud98cf932009-12-27 22:54:55 +01005328 /* Note that we don't have to send 100-continue back because we don't
5329 * need the data to complete our job, and it's up to the server to
5330 * decide whether to return 100, 417 or anything else in return of
5331 * an "Expect: 100-continue" header.
5332 */
5333
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005334 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005335 /* we have msg->sov which points to the first byte of message
5336 * body, and req->buf.p still points to the beginning of the
5337 * message. We forward the headers now, as we don't need them
5338 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005339 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005340 b_adv(req->buf, msg->sov);
5341 msg->next -= msg->sov;
5342 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005343
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005344 /* The previous analysers guarantee that the state is somewhere
5345 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5346 * msg->next are always correct.
5347 */
5348 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5349 if (msg->flags & HTTP_MSGF_TE_CHNK)
5350 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5351 else
5352 msg->msg_state = HTTP_MSG_DATA;
5353 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005354 }
5355
Willy Tarreau7ba23542014-04-17 21:50:00 +02005356 /* Some post-connect processing might want us to refrain from starting to
5357 * forward data. Currently, the only reason for this is "balance url_param"
5358 * whichs need to parse/process the request after we've enabled forwarding.
5359 */
5360 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5361 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5362 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005363 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005364 goto missing_data;
5365 }
5366 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5367 }
5368
Willy Tarreau80a92c02014-03-12 10:41:13 +01005369 /* in most states, we should abort in case of early close */
5370 channel_auto_close(req);
5371
Willy Tarreauefdf0942014-04-24 20:08:57 +02005372 if (req->to_forward) {
5373 /* We can't process the buffer's contents yet */
5374 req->flags |= CF_WAKE_WRITE;
5375 goto missing_data;
5376 }
5377
Willy Tarreaud98cf932009-12-27 22:54:55 +01005378 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005379 if (msg->msg_state == HTTP_MSG_DATA) {
5380 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005381 /* we may have some pending data starting at req->buf->p */
5382 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005383 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005384 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005385 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005386 msg->next += msg->chunk_len;
5387 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005388
5389 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005390 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005391 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005392 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005393 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005394 }
5395 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005396 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005397 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005398 * TRAILERS state.
5399 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005400 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005401
Willy Tarreau54d23df2012-10-25 19:04:45 +02005402 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005403 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005404 else if (ret < 0) {
5405 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005406 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005407 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005408 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005409 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005410 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005411 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005412 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005413 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005414 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005415
5416 if (ret == 0)
5417 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005418 else if (ret < 0) {
5419 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005420 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005421 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005422 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005423 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005424 /* we're in MSG_CHUNK_SIZE now */
5425 }
5426 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005427 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005428
5429 if (ret == 0)
5430 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005431 else if (ret < 0) {
5432 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005433 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005434 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005435 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005436 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005437 /* we're in HTTP_MSG_DONE now */
5438 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005439 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005440 int old_state = msg->msg_state;
5441
Willy Tarreau610ecce2010-01-04 21:15:02 +01005442 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005443
5444 /* we may have some pending data starting at req->buf->p
5445 * such as last chunk of data or trailers.
5446 */
5447 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005448 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005449 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005450 msg->next = 0;
5451
Willy Tarreau294e4672015-05-11 18:30:33 +02005452 /* we don't want to forward closes on DONE except in
5453 * tunnel mode.
5454 */
5455 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005456 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005457 if (http_resync_states(s)) {
5458 /* some state changes occurred, maybe the analyser
5459 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005460 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005461 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005462 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005463 /* request errors are most likely due to
5464 * the server aborting the transfer.
5465 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005466 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005467 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005468 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005469 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005470 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005471 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005472 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005473 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005474
5475 /* If "option abortonclose" is set on the backend, we
5476 * want to monitor the client's connection and forward
5477 * any shutdown notification to the server, which will
5478 * decide whether to close or to go on processing the
Willy Tarreau294e4672015-05-11 18:30:33 +02005479 * request. We only do that in tunnel mode, and not in
5480 * other modes since it can be abused to exhaust source
5481 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005482 */
5483 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005484 channel_auto_read(req);
Willy Tarreau294e4672015-05-11 18:30:33 +02005485 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5486 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5487 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005488 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005489 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005490 else if (s->txn.meth == HTTP_METH_POST) {
5491 /* POST requests may require to read extra CRLF
5492 * sent by broken browsers and which could cause
5493 * an RST to be sent upon close on some systems
5494 * (eg: Linux).
5495 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005496 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005497 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005498
Willy Tarreau610ecce2010-01-04 21:15:02 +01005499 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005500 }
5501 }
5502
Willy Tarreaud98cf932009-12-27 22:54:55 +01005503 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005504 /* we may have some pending data starting at req->buf->p */
5505 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005506 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005507 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5508
Willy Tarreaubed410e2014-04-22 08:19:34 +02005509 msg->next = 0;
5510 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5511
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005512 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005513 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005514 if (!(s->flags & SN_ERR_MASK))
5515 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005516 if (!(s->flags & SN_FINST_MASK)) {
5517 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5518 s->flags |= SN_FINST_H;
5519 else
5520 s->flags |= SN_FINST_D;
5521 }
5522
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005523 s->fe->fe_counters.cli_aborts++;
5524 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005525 if (objt_server(s->target))
5526 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005527
5528 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005529 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005530
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005531 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005532 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005533 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005534
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005535 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005536 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005537 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005538 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005539 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005540
Willy Tarreau5c620922011-05-11 19:56:11 +02005541 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005542 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005543 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005544 * modes are already handled by the stream sock layer. We must not do
5545 * this in content-length mode because it could present the MSG_MORE
5546 * flag with the last block of forwarded data, which would cause an
5547 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005548 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005549 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005550 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005551
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005552 return 0;
5553
Willy Tarreaud98cf932009-12-27 22:54:55 +01005554 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005555 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005556 if (s->listener->counters)
5557 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005558
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005559 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005560 /* we may have some pending data starting at req->buf->p */
5561 b_adv(req->buf, msg->next);
5562 msg->next = 0;
5563
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005564 txn->req.msg_state = HTTP_MSG_ERROR;
5565 if (txn->status) {
5566 /* Note: we don't send any error if some data were already sent */
5567 stream_int_retnclose(req->prod, NULL);
5568 } else {
5569 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005570 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005571 }
5572 req->analysers = 0;
5573 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005574
5575 if (!(s->flags & SN_ERR_MASK))
5576 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005577 if (!(s->flags & SN_FINST_MASK)) {
5578 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5579 s->flags |= SN_FINST_H;
5580 else
5581 s->flags |= SN_FINST_D;
5582 }
5583 return 0;
5584
5585 aborted_xfer:
5586 txn->req.msg_state = HTTP_MSG_ERROR;
5587 if (txn->status) {
5588 /* Note: we don't send any error if some data were already sent */
5589 stream_int_retnclose(req->prod, NULL);
5590 } else {
5591 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005592 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005593 }
5594 req->analysers = 0;
5595 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5596
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005597 s->fe->fe_counters.srv_aborts++;
5598 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005599 if (objt_server(s->target))
5600 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005601
5602 if (!(s->flags & SN_ERR_MASK))
5603 s->flags |= SN_ERR_SRVCL;
5604 if (!(s->flags & SN_FINST_MASK)) {
5605 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5606 s->flags |= SN_FINST_H;
5607 else
5608 s->flags |= SN_FINST_D;
5609 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005610 return 0;
5611}
5612
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005613/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5614 * processing can continue on next analysers, or zero if it either needs more
5615 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5616 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5617 * when it has nothing left to do, and may remove any analyser when it wants to
5618 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005619 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005620int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005621{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005622 struct http_txn *txn = &s->txn;
5623 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005624 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005625 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005626 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005627 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005628
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005629 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 +02005630 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005631 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005632 rep,
5633 rep->rex, rep->wex,
5634 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005635 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005636 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005637
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005638 /*
5639 * Now parse the partial (or complete) lines.
5640 * We will check the response syntax, and also join multi-line
5641 * headers. An index of all the lines will be elaborated while
5642 * parsing.
5643 *
5644 * For the parsing, we use a 28 states FSM.
5645 *
5646 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005647 * rep->buf->p = beginning of response
5648 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5649 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005650 * msg->eol = end of current header or line (LF or CRLF)
5651 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005652 */
5653
Willy Tarreau628c40c2014-04-24 19:11:26 +02005654 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005655 /* There's a protected area at the end of the buffer for rewriting
5656 * purposes. We don't want to start to parse the request if the
5657 * protected area is affected, because we may have to move processed
5658 * data later, which is much more complicated.
5659 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005660 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005661 if (unlikely(!channel_reserved(rep))) {
5662 /* some data has still not left the buffer, wake us once that's done */
5663 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5664 goto abort_response;
5665 channel_dont_close(rep);
5666 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005667 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005668 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005669 }
5670
Willy Tarreau379357a2013-06-08 12:55:46 +02005671 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5672 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5673 buffer_slow_realign(rep->buf);
5674
Willy Tarreau9b28e032012-10-12 23:49:43 +02005675 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005676 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005677 }
5678
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005679 /* 1: we might have to print this header in debug mode */
5680 if (unlikely((global.mode & MODE_DEBUG) &&
5681 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau8767b132014-10-21 19:36:09 +02005682 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005683 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005684
Willy Tarreau9b28e032012-10-12 23:49:43 +02005685 sol = rep->buf->p;
5686 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005687 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005688
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005689 sol += hdr_idx_first_pos(&txn->hdr_idx);
5690 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005691
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005692 while (cur_idx) {
5693 eol = sol + txn->hdr_idx.v[cur_idx].len;
5694 debug_hdr("srvhdr", s, sol, eol);
5695 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5696 cur_idx = txn->hdr_idx.v[cur_idx].next;
5697 }
5698 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005699
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005700 /*
5701 * Now we quickly check if we have found a full valid response.
5702 * If not so, we check the FD and buffer states before leaving.
5703 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005704 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005705 * responses are checked first.
5706 *
5707 * Depending on whether the client is still there or not, we
5708 * may send an error response back or not. Note that normally
5709 * we should only check for HTTP status there, and check I/O
5710 * errors somewhere else.
5711 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005712
Willy Tarreau655dce92009-11-08 13:10:58 +01005713 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005714 /* Invalid response */
5715 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5716 /* we detected a parsing error. We want to archive this response
5717 * in the dedicated proxy area for later troubleshooting.
5718 */
5719 hdr_response_bad:
5720 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005721 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005722
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005723 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005724 if (objt_server(s->target)) {
5725 objt_server(s->target)->counters.failed_resp++;
5726 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005727 }
Willy Tarreau64648412010-03-05 10:41:54 +01005728 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005729 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005730 rep->analysers = 0;
5731 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005732 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005733 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005734 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005735
5736 if (!(s->flags & SN_ERR_MASK))
5737 s->flags |= SN_ERR_PRXCOND;
5738 if (!(s->flags & SN_FINST_MASK))
5739 s->flags |= SN_FINST_H;
5740
5741 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005742 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005743
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005744 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005745 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005746 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005747 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005748 goto hdr_response_bad;
5749 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005750
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005751 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005752 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005753 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005754 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005755 else if (txn->flags & TX_NOT_FIRST)
5756 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005757
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005758 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005759 if (objt_server(s->target)) {
5760 objt_server(s->target)->counters.failed_resp++;
5761 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005762 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005763
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005764 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005765 rep->analysers = 0;
5766 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005767 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005768 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005769 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005770
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005771 if (!(s->flags & SN_ERR_MASK))
5772 s->flags |= SN_ERR_SRVCL;
5773 if (!(s->flags & SN_FINST_MASK))
5774 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005775 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005776 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005777
Willy Tarreau2a4f5112014-06-23 15:22:31 +02005778 /* read timeout : return a 504 to the client. */
5779 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005780 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005781 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005782 else if (txn->flags & TX_NOT_FIRST)
5783 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005784
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005785 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005786 if (objt_server(s->target)) {
5787 objt_server(s->target)->counters.failed_resp++;
5788 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005789 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005790
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005791 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005792 rep->analysers = 0;
5793 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005794 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005795 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005796 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005797
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005798 if (!(s->flags & SN_ERR_MASK))
5799 s->flags |= SN_ERR_SRVTO;
5800 if (!(s->flags & SN_FINST_MASK))
5801 s->flags |= SN_FINST_H;
5802 return 0;
5803 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005804
Willy Tarreauf003d372012-11-26 13:35:37 +01005805 /* client abort with an abortonclose */
5806 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5807 s->fe->fe_counters.cli_aborts++;
5808 s->be->be_counters.cli_aborts++;
5809 if (objt_server(s->target))
5810 objt_server(s->target)->counters.cli_aborts++;
5811
5812 rep->analysers = 0;
5813 channel_auto_close(rep);
5814
5815 txn->status = 400;
5816 bi_erase(rep);
5817 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5818
5819 if (!(s->flags & SN_ERR_MASK))
5820 s->flags |= SN_ERR_CLICL;
5821 if (!(s->flags & SN_FINST_MASK))
5822 s->flags |= SN_FINST_H;
5823
5824 /* process_session() will take care of the error */
5825 return 0;
5826 }
5827
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005828 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005829 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005830 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005831 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005832 else if (txn->flags & TX_NOT_FIRST)
5833 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005834
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005835 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005836 if (objt_server(s->target)) {
5837 objt_server(s->target)->counters.failed_resp++;
5838 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005839 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005840
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005841 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005842 rep->analysers = 0;
5843 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005844 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005845 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005846 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005847
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005848 if (!(s->flags & SN_ERR_MASK))
5849 s->flags |= SN_ERR_SRVCL;
5850 if (!(s->flags & SN_FINST_MASK))
5851 s->flags |= SN_FINST_H;
5852 return 0;
5853 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005854
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005855 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005856 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005857 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005858 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005859 else if (txn->flags & TX_NOT_FIRST)
5860 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005861
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005862 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005863 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005864 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005865
5866 if (!(s->flags & SN_ERR_MASK))
5867 s->flags |= SN_ERR_CLICL;
5868 if (!(s->flags & SN_FINST_MASK))
5869 s->flags |= SN_FINST_H;
5870
5871 /* process_session() will take care of the error */
5872 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005873 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005874
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005875 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005876 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005877 return 0;
5878 }
5879
5880 /* More interesting part now : we know that we have a complete
5881 * response which at least looks like HTTP. We have an indicator
5882 * of each header's length, so we can parse them quickly.
5883 */
5884
5885 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005886 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005887
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005888 /*
5889 * 1: get the status code
5890 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005891 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005892 if (n < 1 || n > 5)
5893 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005894 /* when the client triggers a 4xx from the server, it's most often due
5895 * to a missing object or permission. These events should be tracked
5896 * because if they happen often, it may indicate a brute force or a
5897 * vulnerability scan.
5898 */
5899 if (n == 4)
5900 session_inc_http_err_ctr(s);
5901
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005902 if (objt_server(s->target))
5903 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005904
Willy Tarreau55645552015-05-01 13:26:00 +02005905 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5906 * exactly one digit "." one digit. This check may be disabled using
5907 * option accept-invalid-http-response.
5908 */
5909 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5910 if (msg->sl.st.v_l != 8) {
5911 msg->err_pos = 0;
5912 goto hdr_response_bad;
5913 }
5914
5915 if (rep->buf->p[4] != '/' ||
5916 !isdigit((unsigned char)rep->buf->p[5]) ||
5917 rep->buf->p[6] != '.' ||
5918 !isdigit((unsigned char)rep->buf->p[7])) {
5919 msg->err_pos = 4;
5920 goto hdr_response_bad;
5921 }
5922 }
5923
Willy Tarreau5b154472009-12-21 20:11:07 +01005924 /* check if the response is HTTP/1.1 or above */
5925 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005926 ((rep->buf->p[5] > '1') ||
5927 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005928 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005929
5930 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005931 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 +01005932
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005933 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005934 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005935
Willy Tarreau9b28e032012-10-12 23:49:43 +02005936 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005937
Willy Tarreau39650402010-03-15 19:44:39 +01005938 /* Adjust server's health based on status code. Note: status codes 501
5939 * and 505 are triggered on demand by client request, so we must not
5940 * count them as server failures.
5941 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005942 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005943 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005944 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005945 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005946 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005947 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005948
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005949 /*
5950 * 2: check for cacheability.
5951 */
5952
5953 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005954 case 100:
5955 /*
5956 * We may be facing a 100-continue response, in which case this
5957 * is not the right response, and we're waiting for the next one.
5958 * Let's allow this response to go to the client and wait for the
5959 * next one.
5960 */
5961 hdr_idx_init(&txn->hdr_idx);
5962 msg->next -= channel_forward(rep, msg->next);
5963 msg->msg_state = HTTP_MSG_RPBEFORE;
5964 txn->status = 0;
5965 s->logs.t_data = -1; /* was not a response yet */
5966 goto next_one;
5967
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005968 case 200:
5969 case 203:
5970 case 206:
5971 case 300:
5972 case 301:
5973 case 410:
5974 /* RFC2616 @13.4:
5975 * "A response received with a status code of
5976 * 200, 203, 206, 300, 301 or 410 MAY be stored
5977 * by a cache (...) unless a cache-control
5978 * directive prohibits caching."
5979 *
5980 * RFC2616 @9.5: POST method :
5981 * "Responses to this method are not cacheable,
5982 * unless the response includes appropriate
5983 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005984 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005985 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005986 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005987 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5988 break;
5989 default:
5990 break;
5991 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005992
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005993 /*
5994 * 3: we may need to capture headers
5995 */
5996 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005997 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005998 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005999 txn->rsp.cap, s->fe->rsp_cap);
6000
Willy Tarreau4db603d2015-05-01 10:05:17 +02006001 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6002 * by RFC7230#3.3.3 :
6003 *
6004 * The length of a message body is determined by one of the following
6005 * (in order of precedence):
6006 *
6007 * 1. Any response to a HEAD request and any response with a 1xx
6008 * (Informational), 204 (No Content), or 304 (Not Modified) status
6009 * code is always terminated by the first empty line after the
6010 * header fields, regardless of the header fields present in the
6011 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006012 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006013 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6014 * the connection will become a tunnel immediately after the empty
6015 * line that concludes the header fields. A client MUST ignore any
6016 * Content-Length or Transfer-Encoding header fields received in
6017 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006018 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006019 * 3. If a Transfer-Encoding header field is present and the chunked
6020 * transfer coding (Section 4.1) is the final encoding, the message
6021 * body length is determined by reading and decoding the chunked
6022 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006023 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006024 * If a Transfer-Encoding header field is present in a response and
6025 * the chunked transfer coding is not the final encoding, the
6026 * message body length is determined by reading the connection until
6027 * it is closed by the server. If a Transfer-Encoding header field
6028 * is present in a request and the chunked transfer coding is not
6029 * the final encoding, the message body length cannot be determined
6030 * reliably; the server MUST respond with the 400 (Bad Request)
6031 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006032 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006033 * If a message is received with both a Transfer-Encoding and a
6034 * Content-Length header field, the Transfer-Encoding overrides the
6035 * Content-Length. Such a message might indicate an attempt to
6036 * perform request smuggling (Section 9.5) or response splitting
6037 * (Section 9.4) and ought to be handled as an error. A sender MUST
6038 * remove the received Content-Length field prior to forwarding such
6039 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006040 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006041 * 4. If a message is received without Transfer-Encoding and with
6042 * either multiple Content-Length header fields having differing
6043 * field-values or a single Content-Length header field having an
6044 * invalid value, then the message framing is invalid and the
6045 * recipient MUST treat it as an unrecoverable error. If this is a
6046 * request message, the server MUST respond with a 400 (Bad Request)
6047 * status code and then close the connection. If this is a response
6048 * message received by a proxy, the proxy MUST close the connection
6049 * to the server, discard the received response, and send a 502 (Bad
6050 * Gateway) response to the client. If this is a response message
6051 * received by a user agent, the user agent MUST close the
6052 * connection to the server and discard the received response.
6053 *
6054 * 5. If a valid Content-Length header field is present without
6055 * Transfer-Encoding, its decimal value defines the expected message
6056 * body length in octets. If the sender closes the connection or
6057 * the recipient times out before the indicated number of octets are
6058 * received, the recipient MUST consider the message to be
6059 * incomplete and close the connection.
6060 *
6061 * 6. If this is a request message and none of the above are true, then
6062 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006063 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006064 * 7. Otherwise, this is a response message without a declared message
6065 * body length, so the message body length is determined by the
6066 * number of octets received prior to the server closing the
6067 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006068 */
6069
6070 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006071 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006072 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006073 * FIXME: should we parse anyway and return an error on chunked encoding ?
6074 */
6075 if (txn->meth == HTTP_METH_HEAD ||
6076 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006077 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006078 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006079 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006080 goto skip_content_length;
6081 }
6082
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006083 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006084 ctx.idx = 0;
Willy Tarreaue77bc172015-05-01 10:06:30 +02006085 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006086 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006087 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6088 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006089 /* bad transfer-encoding (chunked followed by something else) */
6090 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006091 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006092 break;
6093 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006094 }
6095
Willy Tarreaudfa3d922015-04-30 10:57:51 +02006096 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006097 ctx.idx = 0;
Willy Tarreau660418d2015-05-01 10:25:45 +02006098 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreaudfa3d922015-04-30 10:57:51 +02006099 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6100 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6101 }
Willy Tarreau660418d2015-05-01 10:25:45 +02006102 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006103 signed long long cl;
6104
Willy Tarreauad14f752011-09-02 20:33:27 +02006105 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006106 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006107 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006108 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006109
Willy Tarreauad14f752011-09-02 20:33:27 +02006110 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006111 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006112 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006113 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006114
Willy Tarreauad14f752011-09-02 20:33:27 +02006115 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006116 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006117 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006118 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006119
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006120 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006121 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006122 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006123 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006124
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006125 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006126 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006127 }
6128
William Lallemand82fe75c2012-10-23 10:25:10 +02006129 if (s->fe->comp || s->be->comp)
6130 select_compression_response_header(s, rep->buf);
6131
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006132skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006133 /* Now we have to check if we need to modify the Connection header.
6134 * This is more difficult on the response than it is on the request,
6135 * because we can have two different HTTP versions and we don't know
6136 * how the client will interprete a response. For instance, let's say
6137 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6138 * HTTP/1.1 response without any header. Maybe it will bound itself to
6139 * HTTP/1.0 because it only knows about it, and will consider the lack
6140 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6141 * the lack of header as a keep-alive. Thus we will use two flags
6142 * indicating how a request MAY be understood by the client. In case
6143 * of multiple possibilities, we'll fix the header to be explicit. If
6144 * ambiguous cases such as both close and keepalive are seen, then we
6145 * will fall back to explicit close. Note that we won't take risks with
6146 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006147 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006148 */
6149
Willy Tarreaudc008c52010-02-01 16:20:08 +01006150 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6151 txn->status == 101)) {
6152 /* Either we've established an explicit tunnel, or we're
6153 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006154 * to understand the next protocols. We have to switch to tunnel
6155 * mode, so that we transfer the request and responses then let
6156 * this protocol pass unmodified. When we later implement specific
6157 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006158 * header which contains information about that protocol for
6159 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006160 */
6161 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6162 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006163 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6164 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006165 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6166 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006167 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006168
Willy Tarreau70dffda2014-01-30 03:07:23 +01006169 /* this situation happens when combining pretend-keepalive with httpclose. */
6170 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006171 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6172 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006173 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6174
Willy Tarreau60466522010-01-18 19:08:45 +01006175 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006176 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006177 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6178 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006179
Willy Tarreau60466522010-01-18 19:08:45 +01006180 /* now adjust header transformations depending on current state */
6181 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6182 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6183 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006184 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006185 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006186 }
Willy Tarreau60466522010-01-18 19:08:45 +01006187 else { /* SCL / KAL */
6188 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006189 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006190 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006191 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006192
Willy Tarreau60466522010-01-18 19:08:45 +01006193 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006194 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006195
Willy Tarreau60466522010-01-18 19:08:45 +01006196 /* Some keep-alive responses are converted to Server-close if
6197 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006198 */
Willy Tarreau60466522010-01-18 19:08:45 +01006199 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6200 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006201 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006202 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006203 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006204 }
6205
Willy Tarreau7959a552013-09-23 16:44:27 +02006206 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006207 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006208
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006209 /* end of job, return OK */
6210 rep->analysers &= ~an_bit;
6211 rep->analyse_exp = TICK_ETERNITY;
6212 channel_auto_close(rep);
6213 return 1;
6214
6215 abort_keep_alive:
6216 /* A keep-alive request to the server failed on a network error.
6217 * The client is required to retry. We need to close without returning
6218 * any other information so that the client retries.
6219 */
6220 txn->status = 0;
6221 rep->analysers = 0;
6222 s->req->analysers = 0;
6223 channel_auto_close(rep);
6224 s->logs.logwait = 0;
6225 s->logs.level = 0;
6226 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
6227 bi_erase(rep);
6228 stream_int_retnclose(rep->cons, NULL);
6229 return 0;
6230}
6231
6232/* This function performs all the processing enabled for the current response.
6233 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6234 * and updates s->rep->analysers. It might make sense to explode it into several
6235 * other functions. It works like process_request (see indications above).
6236 */
6237int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6238{
6239 struct http_txn *txn = &s->txn;
6240 struct http_msg *msg = &txn->rsp;
6241 struct proxy *cur_proxy;
6242 struct cond_wordlist *wl;
6243 struct http_res_rule *http_res_last_rule = NULL;
6244
6245 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6246 now_ms, __FUNCTION__,
6247 s,
6248 rep,
6249 rep->rex, rep->wex,
6250 rep->flags,
6251 rep->buf->i,
6252 rep->analysers);
6253
6254 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6255 return 0;
6256
6257 rep->analysers &= ~an_bit;
6258 rep->analyse_exp = TICK_ETERNITY;
6259
Willy Tarreau70730dd2014-04-24 18:06:27 +02006260 /* The stats applet needs to adjust the Connection header but we don't
6261 * apply any filter there.
6262 */
6263 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6264 goto skip_filters;
6265
Willy Tarreau58975672014-04-24 21:13:57 +02006266 /*
6267 * We will have to evaluate the filters.
6268 * As opposed to version 1.2, now they will be evaluated in the
6269 * filters order and not in the header order. This means that
6270 * each filter has to be validated among all headers.
6271 *
6272 * Filters are tried with ->be first, then with ->fe if it is
6273 * different from ->be.
6274 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006275
Willy Tarreau58975672014-04-24 21:13:57 +02006276 cur_proxy = s->be;
6277 while (1) {
6278 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006279
Willy Tarreau58975672014-04-24 21:13:57 +02006280 /* evaluate http-response rules */
6281 if (!http_res_last_rule)
6282 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 +02006283
Willy Tarreau58975672014-04-24 21:13:57 +02006284 /* try headers filters */
6285 if (rule_set->rsp_exp != NULL) {
6286 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6287 return_bad_resp:
6288 if (objt_server(s->target)) {
6289 objt_server(s->target)->counters.failed_resp++;
6290 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006291 }
Willy Tarreau58975672014-04-24 21:13:57 +02006292 s->be->be_counters.failed_resp++;
6293 return_srv_prx_502:
6294 rep->analysers = 0;
6295 txn->status = 502;
6296 s->logs.t_data = -1; /* was not a valid response */
6297 rep->prod->flags |= SI_FL_NOLINGER;
6298 bi_erase(rep);
6299 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6300 if (!(s->flags & SN_ERR_MASK))
6301 s->flags |= SN_ERR_PRXCOND;
6302 if (!(s->flags & SN_FINST_MASK))
6303 s->flags |= SN_FINST_H;
6304 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006305 }
Willy Tarreau58975672014-04-24 21:13:57 +02006306 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006307
Willy Tarreau58975672014-04-24 21:13:57 +02006308 /* has the response been denied ? */
6309 if (txn->flags & TX_SVDENY) {
6310 if (objt_server(s->target))
6311 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006312
Willy Tarreau58975672014-04-24 21:13:57 +02006313 s->be->be_counters.denied_resp++;
6314 s->fe->fe_counters.denied_resp++;
6315 if (s->listener->counters)
6316 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006317
Willy Tarreau58975672014-04-24 21:13:57 +02006318 goto return_srv_prx_502;
6319 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006320
Willy Tarreau58975672014-04-24 21:13:57 +02006321 /* add response headers from the rule sets in the same order */
6322 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006323 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006324 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006325 if (wl->cond) {
6326 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6327 ret = acl_pass(ret);
6328 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6329 ret = !ret;
6330 if (!ret)
6331 continue;
6332 }
6333 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6334 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006335 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006336
Willy Tarreau58975672014-04-24 21:13:57 +02006337 /* check whether we're already working on the frontend */
6338 if (cur_proxy == s->fe)
6339 break;
6340 cur_proxy = s->fe;
6341 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006342
Willy Tarreau58975672014-04-24 21:13:57 +02006343 /* OK that's all we can do for 1xx responses */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006344 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006345 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006346
Willy Tarreau58975672014-04-24 21:13:57 +02006347 /*
6348 * Now check for a server cookie.
6349 */
6350 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6351 (s->be->options & PR_O_CHK_CACHE))
6352 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006353
Willy Tarreau58975672014-04-24 21:13:57 +02006354 /*
6355 * Check for cache-control or pragma headers if required.
6356 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006357 if (((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) && txn->status != 101)
Willy Tarreau58975672014-04-24 21:13:57 +02006358 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006359
Willy Tarreau58975672014-04-24 21:13:57 +02006360 /*
6361 * Add server cookie in the response if needed
6362 */
6363 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6364 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6365 (!(s->flags & SN_DIRECT) ||
6366 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6367 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6368 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6369 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6370 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6371 !(s->flags & SN_IGNORE_PRST)) {
6372 /* the server is known, it's not the one the client requested, or the
6373 * cookie's last seen date needs to be refreshed. We have to
6374 * insert a set-cookie here, except if we want to insert only on POST
6375 * requests and this one isn't. Note that servers which don't have cookies
6376 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006377 */
Willy Tarreau58975672014-04-24 21:13:57 +02006378 if (!objt_server(s->target)->cookie) {
6379 chunk_printf(&trash,
6380 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6381 s->be->cookie_name);
6382 }
6383 else {
6384 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006385
Willy Tarreau58975672014-04-24 21:13:57 +02006386 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6387 /* emit last_date, which is mandatory */
6388 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6389 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6390 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006391
Willy Tarreau58975672014-04-24 21:13:57 +02006392 if (s->be->cookie_maxlife) {
6393 /* emit first_date, which is either the original one or
6394 * the current date.
6395 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006396 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006397 s30tob64(txn->cookie_first_date ?
6398 txn->cookie_first_date >> 2 :
6399 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006400 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006401 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006402 }
Willy Tarreau58975672014-04-24 21:13:57 +02006403 chunk_appendf(&trash, "; path=/");
6404 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006405
Willy Tarreau58975672014-04-24 21:13:57 +02006406 if (s->be->cookie_domain)
6407 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006408
Willy Tarreau58975672014-04-24 21:13:57 +02006409 if (s->be->ck_opts & PR_CK_HTTPONLY)
6410 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006411
Willy Tarreau58975672014-04-24 21:13:57 +02006412 if (s->be->ck_opts & PR_CK_SECURE)
6413 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006414
Willy Tarreau58975672014-04-24 21:13:57 +02006415 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6416 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006417
Willy Tarreau58975672014-04-24 21:13:57 +02006418 txn->flags &= ~TX_SCK_MASK;
6419 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6420 /* the server did not change, only the date was updated */
6421 txn->flags |= TX_SCK_UPDATED;
6422 else
6423 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006424
Willy Tarreau58975672014-04-24 21:13:57 +02006425 /* Here, we will tell an eventual cache on the client side that we don't
6426 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6427 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6428 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006429 */
Willy Tarreau58975672014-04-24 21:13:57 +02006430 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006431
Willy Tarreau58975672014-04-24 21:13:57 +02006432 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006433
Willy Tarreau58975672014-04-24 21:13:57 +02006434 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6435 "Cache-control: private", 22) < 0))
6436 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006437 }
Willy Tarreau58975672014-04-24 21:13:57 +02006438 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006439
Willy Tarreau58975672014-04-24 21:13:57 +02006440 /*
6441 * Check if result will be cacheable with a cookie.
6442 * We'll block the response if security checks have caught
6443 * nasty things such as a cacheable cookie.
6444 */
6445 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6446 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6447 (s->be->options & PR_O_CHK_CACHE)) {
6448 /* we're in presence of a cacheable response containing
6449 * a set-cookie header. We'll block it as requested by
6450 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006451 */
Willy Tarreau58975672014-04-24 21:13:57 +02006452 if (objt_server(s->target))
6453 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006454
Willy Tarreau58975672014-04-24 21:13:57 +02006455 s->be->be_counters.denied_resp++;
6456 s->fe->fe_counters.denied_resp++;
6457 if (s->listener->counters)
6458 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006459
Willy Tarreau58975672014-04-24 21:13:57 +02006460 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6461 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6462 send_log(s->be, LOG_ALERT,
6463 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6464 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6465 goto return_srv_prx_502;
6466 }
Willy Tarreau03945942009-12-22 16:50:27 +01006467
Willy Tarreau70730dd2014-04-24 18:06:27 +02006468 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006469 /*
6470 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6471 * If an "Upgrade" token is found, the header is left untouched in order
6472 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006473 * if anything but "Upgrade" is present in the Connection header. We don't
6474 * want to touch any 101 response either since it's switching to another
6475 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006476 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006477 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006478 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6479 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6480 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6481 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006482
Willy Tarreau58975672014-04-24 21:13:57 +02006483 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6484 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6485 /* we want a keep-alive response here. Keep-alive header
6486 * required if either side is not 1.1.
6487 */
6488 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6489 want_flags |= TX_CON_KAL_SET;
6490 }
6491 else {
6492 /* we want a close response here. Close header required if
6493 * the server is 1.1, regardless of the client.
6494 */
6495 if (msg->flags & HTTP_MSGF_VER_11)
6496 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006497 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006498
Willy Tarreau58975672014-04-24 21:13:57 +02006499 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6500 http_change_connection_header(txn, msg, want_flags);
6501 }
6502
6503 skip_header_mangling:
6504 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6505 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6506 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006507
Willy Tarreau58975672014-04-24 21:13:57 +02006508 /* if the user wants to log as soon as possible, without counting
6509 * bytes from the server, then this is the right moment. We have
6510 * to temporarily assign bytes_out to log what we currently have.
6511 */
6512 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6513 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6514 s->logs.bytes_out = txn->rsp.eoh;
6515 s->do_log(s);
6516 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006517 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006518 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006519}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006520
Willy Tarreaud98cf932009-12-27 22:54:55 +01006521/* This function is an analyser which forwards response body (including chunk
6522 * sizes if any). It is called as soon as we must forward, even if we forward
6523 * zero byte. The only situation where it must not be called is when we're in
6524 * tunnel mode and we want to forward till the close. It's used both to forward
6525 * remaining data and to resync after end of body. It expects the msg_state to
6526 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6527 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006528 *
6529 * It is capable of compressing response data both in content-length mode and
6530 * in chunked mode. The state machines follows different flows depending on
6531 * whether content-length and chunked modes are used, since there are no
6532 * trailers in content-length :
6533 *
6534 * chk-mode cl-mode
6535 * ,----- BODY -----.
6536 * / \
6537 * V size > 0 V chk-mode
6538 * .--> SIZE -------------> DATA -------------> CRLF
6539 * | | size == 0 | last byte |
6540 * | v final crlf v inspected |
6541 * | TRAILERS -----------> DONE |
6542 * | |
6543 * `----------------------------------------------'
6544 *
6545 * Compression only happens in the DATA state, and must be flushed in final
6546 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6547 * is performed at once on final states for all bytes parsed, or when leaving
6548 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006549 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006550int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006551{
6552 struct http_txn *txn = &s->txn;
6553 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006554 static struct buffer *tmpbuf = NULL;
6555 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006556 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006557
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006558 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6559 return 0;
6560
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006561 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006562 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006563 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006564 /* Output closed while we were sending data. We must abort and
6565 * wake the other side up.
6566 */
6567 msg->msg_state = HTTP_MSG_ERROR;
6568 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006569 return 1;
6570 }
6571
Willy Tarreau4fe41902010-06-07 22:27:41 +02006572 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006573 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006574
Willy Tarreau5bebcd02014-07-10 19:06:10 +02006575 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006576 /* we have msg->sov which points to the first byte of message
6577 * body, and res->buf.p still points to the beginning of the
6578 * message. We forward the headers now, as we don't need them
6579 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006580 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006581 b_adv(res->buf, msg->sov);
6582 msg->next -= msg->sov;
6583 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006584
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006585 /* The previous analysers guarantee that the state is somewhere
6586 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6587 * msg->next are always correct.
6588 */
6589 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6590 if (msg->flags & HTTP_MSGF_TE_CHNK)
6591 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6592 else
6593 msg->msg_state = HTTP_MSG_DATA;
6594 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006595 }
6596
Willy Tarreauefdf0942014-04-24 20:08:57 +02006597 if (res->to_forward) {
6598 /* We can't process the buffer's contents yet */
6599 res->flags |= CF_WAKE_WRITE;
6600 goto missing_data;
6601 }
6602
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006603 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6604 /* We need a compression buffer in the DATA state to put the
6605 * output of compressed data, and in CRLF state to let the
6606 * TRAILERS state finish the job of removing the trailing CRLF.
6607 */
6608 if (unlikely(tmpbuf == NULL)) {
6609 /* this is the first time we need the compression buffer */
6610 tmpbuf = pool_alloc2(pool2_buffer);
6611 if (tmpbuf == NULL)
6612 goto aborted_xfer; /* no memory */
6613 }
6614
6615 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006616 if (ret < 0) {
6617 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006618 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006619 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006620 compressing = 1;
6621 }
6622
Willy Tarreaud98cf932009-12-27 22:54:55 +01006623 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006624 switch (msg->msg_state - HTTP_MSG_DATA) {
6625 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006626 /* we may have some pending data starting at res->buf->p */
6627 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006628 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006629 if (ret < 0)
6630 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006631
Willy Tarreaud5a67832014-04-21 10:54:27 +02006632 if (msg->chunk_len) {
6633 /* input empty or output full */
6634 if (res->buf->i > msg->next)
6635 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006636 goto missing_data;
6637 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006638 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006639 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006640 if (msg->chunk_len > res->buf->i - msg->next) {
6641 /* output full */
6642 res->flags |= CF_WAKE_WRITE;
6643 goto missing_data;
6644 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006645 msg->next += msg->chunk_len;
6646 msg->chunk_len = 0;
6647 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006648
6649 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006650 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006651 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006652 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006653 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006654 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006655 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006656 /* fall through for HTTP_MSG_CHUNK_CRLF */
6657
6658 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6659 /* we want the CRLF after the data */
6660
6661 ret = http_skip_chunk_crlf(msg);
6662 if (ret == 0)
6663 goto missing_data;
6664 else if (ret < 0) {
6665 if (msg->err_pos >= 0)
6666 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6667 goto return_bad_res;
6668 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006669 /* we're in MSG_CHUNK_SIZE now, fall through */
6670
6671 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006672 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006673 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006674 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006675 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006676
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006677 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006678 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006679 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006680 else if (ret < 0) {
6681 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006682 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006683 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006684 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006685 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006686 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006687
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006688 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006689 if (unlikely(compressing)) {
6690 /* we need to flush output contents before syncing FSMs */
6691 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6692 compressing = 0;
6693 }
6694
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006695 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006696 if (ret == 0)
6697 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006698 else if (ret < 0) {
6699 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006700 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006701 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006702 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006703 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006704
6705 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006706 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006707 if (unlikely(compressing)) {
6708 /* we need to flush output contents before syncing FSMs */
6709 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6710 compressing = 0;
6711 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006712
Willy Tarreauc623c172014-04-18 09:53:50 +02006713 /* we may have some pending data starting at res->buf->p
6714 * such as a last chunk of data or trailers.
6715 */
6716 b_adv(res->buf, msg->next);
6717 msg->next = 0;
6718
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006719 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006720 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006721 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6722 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006723 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006724
Willy Tarreau610ecce2010-01-04 21:15:02 +01006725 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006726 /* some state changes occurred, maybe the analyser
6727 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006728 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006729 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006730 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006731 /* response errors are most likely due to
6732 * the client aborting the transfer.
6733 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006734 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006735 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006736 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006737 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006738 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006739 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006740 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006741 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006742 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006743 }
6744 }
6745
Willy Tarreaud98cf932009-12-27 22:54:55 +01006746 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006747 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006748 if (unlikely(compressing)) {
6749 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006750 compressing = 0;
6751 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006752
Willy Tarreauc623c172014-04-18 09:53:50 +02006753 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6754 b_adv(res->buf, msg->next);
6755 msg->next = 0;
6756 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6757 }
6758
Willy Tarreauf003d372012-11-26 13:35:37 +01006759 if (res->flags & CF_SHUTW)
6760 goto aborted_xfer;
6761
6762 /* stop waiting for data if the input is closed before the end. If the
6763 * client side was already closed, it means that the client has aborted,
6764 * so we don't want to count this as a server abort. Otherwise it's a
6765 * server abort.
6766 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006767 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006768 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006769 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006770 if (!(s->flags & SN_ERR_MASK))
6771 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006772 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006773 if (objt_server(s->target))
6774 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006775 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006776 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006777
Willy Tarreau40dba092010-03-04 18:14:51 +01006778 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006779 if (!s->req->analysers)
6780 goto return_bad_res;
6781
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006782 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006783 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006784 * Similarly, with keep-alive on the client side, we don't want to forward a
6785 * close.
6786 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006787 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006788 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6789 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006790 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006791
Willy Tarreau5c620922011-05-11 19:56:11 +02006792 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006793 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006794 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006795 * modes are already handled by the stream sock layer. We must not do
6796 * this in content-length mode because it could present the MSG_MORE
6797 * flag with the last block of forwarded data, which would cause an
6798 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006799 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006800 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006801 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006802
Willy Tarreaud98cf932009-12-27 22:54:55 +01006803 /* the session handler will take care of timeouts and errors */
6804 return 0;
6805
Willy Tarreau40dba092010-03-04 18:14:51 +01006806 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006807 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006808 if (objt_server(s->target))
6809 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006810
6811 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006812 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006813 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006814 compressing = 0;
6815 }
6816
Willy Tarreauc623c172014-04-18 09:53:50 +02006817 /* we may have some pending data starting at res->buf->p */
6818 if (s->comp_algo == NULL) {
6819 b_adv(res->buf, msg->next);
6820 msg->next = 0;
6821 }
6822
Willy Tarreaud98cf932009-12-27 22:54:55 +01006823 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006824 /* don't send any error message as we're in the body */
6825 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006826 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006827 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006828 if (objt_server(s->target))
6829 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006830
6831 if (!(s->flags & SN_ERR_MASK))
6832 s->flags |= SN_ERR_PRXCOND;
6833 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006834 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006835 return 0;
6836
6837 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006838 if (unlikely(compressing)) {
6839 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6840 compressing = 0;
6841 }
6842
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006843 txn->rsp.msg_state = HTTP_MSG_ERROR;
6844 /* don't send any error message as we're in the body */
6845 stream_int_retnclose(res->cons, NULL);
6846 res->analysers = 0;
6847 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6848
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006849 s->fe->fe_counters.cli_aborts++;
6850 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006851 if (objt_server(s->target))
6852 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006853
6854 if (!(s->flags & SN_ERR_MASK))
6855 s->flags |= SN_ERR_CLICL;
6856 if (!(s->flags & SN_FINST_MASK))
6857 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006858 return 0;
6859}
6860
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006861/* Iterate the same filter through all request headers.
6862 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006863 * Since it can manage the switch to another backend, it updates the per-proxy
6864 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006865 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006866int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006867{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006868 char *cur_ptr, *cur_end, *cur_next;
6869 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006870 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006871 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006872 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006873
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006874 last_hdr = 0;
6875
Willy Tarreau9b28e032012-10-12 23:49:43 +02006876 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006877 old_idx = 0;
6878
6879 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006880 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006881 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006882 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006883 (exp->action == ACT_ALLOW ||
6884 exp->action == ACT_DENY ||
6885 exp->action == ACT_TARPIT))
6886 return 0;
6887
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006888 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006889 if (!cur_idx)
6890 break;
6891
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006892 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006893 cur_ptr = cur_next;
6894 cur_end = cur_ptr + cur_hdr->len;
6895 cur_next = cur_end + cur_hdr->cr + 1;
6896
6897 /* Now we have one header between cur_ptr and cur_end,
6898 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006899 */
6900
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02006901 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006902 switch (exp->action) {
6903 case ACT_SETBE:
6904 /* It is not possible to jump a second time.
6905 * FIXME: should we return an HTTP/500 here so that
6906 * the admin knows there's a problem ?
6907 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006908 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006909 break;
6910
6911 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006912 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006913 last_hdr = 1;
6914 break;
6915
6916 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006917 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006918 last_hdr = 1;
6919 break;
6920
6921 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006922 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006923 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006924 break;
6925
6926 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006927 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006928 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006929 break;
6930
6931 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006932 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6933 if (trash.len < 0)
6934 return -1;
6935
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006936 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006937 /* FIXME: if the user adds a newline in the replacement, the
6938 * index will not be recalculated for now, and the new line
6939 * will not be counted as a new header.
6940 */
6941
6942 cur_end += delta;
6943 cur_next += delta;
6944 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006945 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006946 break;
6947
6948 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006949 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006950 cur_next += delta;
6951
Willy Tarreaufa355d42009-11-29 18:12:29 +01006952 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006953 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6954 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006955 cur_hdr->len = 0;
6956 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006957 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006958 break;
6959
6960 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006961 }
6962
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006963 /* keep the link from this header to next one in case of later
6964 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006965 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006966 old_idx = cur_idx;
6967 }
6968 return 0;
6969}
6970
6971
6972/* Apply the filter to the request line.
6973 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6974 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006975 * Since it can manage the switch to another backend, it updates the per-proxy
6976 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006977 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006978int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006979{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006980 char *cur_ptr, *cur_end;
6981 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006982 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006983 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006984
Willy Tarreau3d300592007-03-18 18:34:41 +01006985 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006986 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006987 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006988 (exp->action == ACT_ALLOW ||
6989 exp->action == ACT_DENY ||
6990 exp->action == ACT_TARPIT))
6991 return 0;
6992 else if (exp->action == ACT_REMOVE)
6993 return 0;
6994
6995 done = 0;
6996
Willy Tarreau9b28e032012-10-12 23:49:43 +02006997 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006998 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006999
7000 /* Now we have the request line between cur_ptr and cur_end */
7001
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007002 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007003 switch (exp->action) {
7004 case ACT_SETBE:
7005 /* It is not possible to jump a second time.
7006 * FIXME: should we return an HTTP/500 here so that
7007 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007008 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007009 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007010 break;
7011
7012 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007013 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007014 done = 1;
7015 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007016
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007017 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007018 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007019 done = 1;
7020 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007021
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007022 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007023 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007024 done = 1;
7025 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007026
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007027 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007028 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007029 done = 1;
7030 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007031
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007032 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007033 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7034 if (trash.len < 0)
7035 return -1;
7036
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007037 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007038 /* FIXME: if the user adds a newline in the replacement, the
7039 * index will not be recalculated for now, and the new line
7040 * will not be counted as a new header.
7041 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007042
Willy Tarreaufa355d42009-11-29 18:12:29 +01007043 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007044 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007045 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007046 HTTP_MSG_RQMETH,
7047 cur_ptr, cur_end + 1,
7048 NULL, NULL);
7049 if (unlikely(!cur_end))
7050 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007051
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007052 /* we have a full request and we know that we have either a CR
7053 * or an LF at <ptr>.
7054 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007055 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7056 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007057 /* there is no point trying this regex on headers */
7058 return 1;
7059 }
7060 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007061 return done;
7062}
Willy Tarreau97de6242006-12-27 17:18:38 +01007063
Willy Tarreau58f10d72006-12-04 02:26:12 +01007064
Willy Tarreau58f10d72006-12-04 02:26:12 +01007065
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007066/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01007067 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007068 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007069 * unparsable request. Since it can manage the switch to another backend, it
7070 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007071 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007072int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007073{
Willy Tarreau6c123b12010-01-28 20:22:06 +01007074 struct http_txn *txn = &s->txn;
7075 struct hdr_exp *exp;
7076
7077 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007078 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007079
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007080 /*
7081 * The interleaving of transformations and verdicts
7082 * makes it difficult to decide to continue or stop
7083 * the evaluation.
7084 */
7085
Willy Tarreau6c123b12010-01-28 20:22:06 +01007086 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7087 break;
7088
Willy Tarreau3d300592007-03-18 18:34:41 +01007089 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007090 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007091 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007092 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007093
7094 /* if this filter had a condition, evaluate it now and skip to
7095 * next filter if the condition does not match.
7096 */
7097 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007098 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007099 ret = acl_pass(ret);
7100 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7101 ret = !ret;
7102
7103 if (!ret)
7104 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007105 }
7106
7107 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007108 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007109 if (unlikely(ret < 0))
7110 return -1;
7111
7112 if (likely(ret == 0)) {
7113 /* The filter did not match the request, it can be
7114 * iterated through all headers.
7115 */
Willy Tarreaua256fae2015-01-30 20:58:58 +01007116 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7117 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007118 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007119 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007120 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007121}
7122
7123
Willy Tarreaua15645d2007-03-18 16:22:39 +01007124
Willy Tarreau58f10d72006-12-04 02:26:12 +01007125/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007126 * Try to retrieve the server associated to the appsession.
7127 * If the server is found, it's assigned to the session.
7128 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007129void manage_client_side_appsession(struct session *s, const char *buf, int len) {
7130 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007131 appsess *asession = NULL;
7132 char *sessid_temp = NULL;
7133
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007134 if (len > s->be->appsession_len) {
7135 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007136 }
7137
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007138 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007139 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007140 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007141 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007142 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007143 }
7144
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007145 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007146 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007147 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007148 return;
7149 }
7150
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007151 memcpy(txn->sessid, buf, len);
7152 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007153 }
7154
7155 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7156 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007157 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007158 return;
7159 }
7160
Cyril Bontéb21570a2009-11-29 20:04:48 +01007161 memcpy(sessid_temp, buf, len);
7162 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007163
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007164 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007165 /* free previously allocated memory */
7166 pool_free2(apools.sessid, sessid_temp);
7167
7168 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007169 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7170 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007171 asession->request_count++;
7172
7173 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007174 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007175
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007176 while (srv) {
7177 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007178 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007179 (s->be->options & PR_O_PERSIST) ||
7180 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007181 /* we found the server and it's usable */
7182 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007183 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007184 s->flags |= SN_DIRECT | SN_ASSIGNED;
7185 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007186
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007187 break;
7188 } else {
7189 txn->flags &= ~TX_CK_MASK;
7190 txn->flags |= TX_CK_DOWN;
7191 }
7192 }
7193 srv = srv->next;
7194 }
7195 }
7196 }
7197}
7198
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007199/* Find the end of a cookie value contained between <s> and <e>. It works the
7200 * same way as with headers above except that the semi-colon also ends a token.
7201 * See RFC2965 for more information. Note that it requires a valid header to
7202 * return a valid result.
7203 */
7204char *find_cookie_value_end(char *s, const char *e)
7205{
7206 int quoted, qdpair;
7207
7208 quoted = qdpair = 0;
7209 for (; s < e; s++) {
7210 if (qdpair) qdpair = 0;
7211 else if (quoted) {
7212 if (*s == '\\') qdpair = 1;
7213 else if (*s == '"') quoted = 0;
7214 }
7215 else if (*s == '"') quoted = 1;
7216 else if (*s == ',' || *s == ';') return s;
7217 }
7218 return s;
7219}
7220
7221/* Delete a value in a header between delimiters <from> and <next> in buffer
7222 * <buf>. The number of characters displaced is returned, and the pointer to
7223 * the first delimiter is updated if required. The function tries as much as
7224 * possible to respect the following principles :
7225 * - replace <from> delimiter by the <next> one unless <from> points to a
7226 * colon, in which case <next> is simply removed
7227 * - set exactly one space character after the new first delimiter, unless
7228 * there are not enough characters in the block being moved to do so.
7229 * - remove unneeded spaces before the previous delimiter and after the new
7230 * one.
7231 *
7232 * It is the caller's responsibility to ensure that :
7233 * - <from> points to a valid delimiter or the colon ;
7234 * - <next> points to a valid delimiter or the final CR/LF ;
7235 * - there are non-space chars before <from> ;
7236 * - there is a CR/LF at or after <next>.
7237 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007238int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007239{
7240 char *prev = *from;
7241
7242 if (*prev == ':') {
7243 /* We're removing the first value, preserve the colon and add a
7244 * space if possible.
7245 */
7246 if (!http_is_crlf[(unsigned char)*next])
7247 next++;
7248 prev++;
7249 if (prev < next)
7250 *prev++ = ' ';
7251
7252 while (http_is_spht[(unsigned char)*next])
7253 next++;
7254 } else {
7255 /* Remove useless spaces before the old delimiter. */
7256 while (http_is_spht[(unsigned char)*(prev-1)])
7257 prev--;
7258 *from = prev;
7259
7260 /* copy the delimiter and if possible a space if we're
7261 * not at the end of the line.
7262 */
7263 if (!http_is_crlf[(unsigned char)*next]) {
7264 *prev++ = *next++;
7265 if (prev + 1 < next)
7266 *prev++ = ' ';
7267 while (http_is_spht[(unsigned char)*next])
7268 next++;
7269 }
7270 }
7271 return buffer_replace2(buf, prev, next, NULL, 0);
7272}
7273
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007274/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007275 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007276 * desirable to call it only when needed. This code is quite complex because
7277 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7278 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007279 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007280void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007281{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007282 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007283 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007284 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007285 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7286 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007287
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007288 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007289 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007290 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007291
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007292 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007293 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007294 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007295
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007296 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007297 hdr_beg = hdr_next;
7298 hdr_end = hdr_beg + cur_hdr->len;
7299 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007300
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007301 /* We have one full header between hdr_beg and hdr_end, and the
7302 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007303 * "Cookie:" headers.
7304 */
7305
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007306 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007307 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007308 old_idx = cur_idx;
7309 continue;
7310 }
7311
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007312 del_from = NULL; /* nothing to be deleted */
7313 preserve_hdr = 0; /* assume we may kill the whole header */
7314
Willy Tarreau58f10d72006-12-04 02:26:12 +01007315 /* Now look for cookies. Conforming to RFC2109, we have to support
7316 * attributes whose name begin with a '$', and associate them with
7317 * the right cookie, if we want to delete this cookie.
7318 * So there are 3 cases for each cookie read :
7319 * 1) it's a special attribute, beginning with a '$' : ignore it.
7320 * 2) it's a server id cookie that we *MAY* want to delete : save
7321 * some pointers on it (last semi-colon, beginning of cookie...)
7322 * 3) it's an application cookie : we *MAY* have to delete a previous
7323 * "special" cookie.
7324 * At the end of loop, if a "special" cookie remains, we may have to
7325 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007326 * *MUST* delete it.
7327 *
7328 * Note: RFC2965 is unclear about the processing of spaces around
7329 * the equal sign in the ATTR=VALUE form. A careful inspection of
7330 * the RFC explicitly allows spaces before it, and not within the
7331 * tokens (attrs or values). An inspection of RFC2109 allows that
7332 * too but section 10.1.3 lets one think that spaces may be allowed
7333 * after the equal sign too, resulting in some (rare) buggy
7334 * implementations trying to do that. So let's do what servers do.
7335 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7336 * allowed quoted strings in values, with any possible character
7337 * after a backslash, including control chars and delimitors, which
7338 * causes parsing to become ambiguous. Browsers also allow spaces
7339 * within values even without quotes.
7340 *
7341 * We have to keep multiple pointers in order to support cookie
7342 * removal at the beginning, middle or end of header without
7343 * corrupting the header. All of these headers are valid :
7344 *
7345 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7346 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7347 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7348 * | | | | | | | | |
7349 * | | | | | | | | hdr_end <--+
7350 * | | | | | | | +--> next
7351 * | | | | | | +----> val_end
7352 * | | | | | +-----------> val_beg
7353 * | | | | +--------------> equal
7354 * | | | +----------------> att_end
7355 * | | +---------------------> att_beg
7356 * | +--------------------------> prev
7357 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007358 */
7359
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007360 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7361 /* Iterate through all cookies on this line */
7362
7363 /* find att_beg */
7364 att_beg = prev + 1;
7365 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7366 att_beg++;
7367
7368 /* find att_end : this is the first character after the last non
7369 * space before the equal. It may be equal to hdr_end.
7370 */
7371 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007372
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007373 while (equal < hdr_end) {
7374 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007375 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007376 if (http_is_spht[(unsigned char)*equal++])
7377 continue;
7378 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007379 }
7380
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007381 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7382 * is between <att_beg> and <equal>, both may be identical.
7383 */
7384
7385 /* look for end of cookie if there is an equal sign */
7386 if (equal < hdr_end && *equal == '=') {
7387 /* look for the beginning of the value */
7388 val_beg = equal + 1;
7389 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7390 val_beg++;
7391
7392 /* find the end of the value, respecting quotes */
7393 next = find_cookie_value_end(val_beg, hdr_end);
7394
7395 /* make val_end point to the first white space or delimitor after the value */
7396 val_end = next;
7397 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7398 val_end--;
7399 } else {
7400 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007401 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007402
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007403 /* We have nothing to do with attributes beginning with '$'. However,
7404 * they will automatically be removed if a header before them is removed,
7405 * since they're supposed to be linked together.
7406 */
7407 if (*att_beg == '$')
7408 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007409
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007410 /* Ignore cookies with no equal sign */
7411 if (equal == next) {
7412 /* This is not our cookie, so we must preserve it. But if we already
7413 * scheduled another cookie for removal, we cannot remove the
7414 * complete header, but we can remove the previous block itself.
7415 */
7416 preserve_hdr = 1;
7417 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007418 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007419 val_end += delta;
7420 next += delta;
7421 hdr_end += delta;
7422 hdr_next += delta;
7423 cur_hdr->len += delta;
7424 http_msg_move_end(&txn->req, delta);
7425 prev = del_from;
7426 del_from = NULL;
7427 }
7428 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007429 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007430
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007431 /* if there are spaces around the equal sign, we need to
7432 * strip them otherwise we'll get trouble for cookie captures,
7433 * or even for rewrites. Since this happens extremely rarely,
7434 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007435 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007436 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7437 int stripped_before = 0;
7438 int stripped_after = 0;
7439
7440 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007441 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007442 equal += stripped_before;
7443 val_beg += stripped_before;
7444 }
7445
7446 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007447 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007448 val_beg += stripped_after;
7449 stripped_before += stripped_after;
7450 }
7451
7452 val_end += stripped_before;
7453 next += stripped_before;
7454 hdr_end += stripped_before;
7455 hdr_next += stripped_before;
7456 cur_hdr->len += stripped_before;
7457 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007458 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007459 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007460
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007461 /* First, let's see if we want to capture this cookie. We check
7462 * that we don't already have a client side cookie, because we
7463 * can only capture one. Also as an optimisation, we ignore
7464 * cookies shorter than the declared name.
7465 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007466 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7467 (val_end - att_beg >= s->fe->capture_namelen) &&
7468 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007469 int log_len = val_end - att_beg;
7470
7471 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7472 Alert("HTTP logging : out of memory.\n");
7473 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007474 if (log_len > s->fe->capture_len)
7475 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007476 memcpy(txn->cli_cookie, att_beg, log_len);
7477 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007478 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007479 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007480
Willy Tarreaubca99692010-10-06 19:25:55 +02007481 /* Persistence cookies in passive, rewrite or insert mode have the
7482 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007483 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007484 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007485 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007486 * For cookies in prefix mode, the form is :
7487 *
7488 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007489 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007490 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7491 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7492 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007493 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007494
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007495 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7496 * have the server ID between val_beg and delim, and the original cookie between
7497 * delim+1 and val_end. Otherwise, delim==val_end :
7498 *
7499 * Cookie: NAME=SRV; # in all but prefix modes
7500 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7501 * | || || | |+-> next
7502 * | || || | +--> val_end
7503 * | || || +---------> delim
7504 * | || |+------------> val_beg
7505 * | || +-------------> att_end = equal
7506 * | |+-----------------> att_beg
7507 * | +------------------> prev
7508 * +-------------------------> hdr_beg
7509 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007510
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007511 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007512 for (delim = val_beg; delim < val_end; delim++)
7513 if (*delim == COOKIE_DELIM)
7514 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007515 } else {
7516 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007517 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007518 /* Now check if the cookie contains a date field, which would
7519 * appear after a vertical bar ('|') just after the server name
7520 * and before the delimiter.
7521 */
7522 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7523 if (vbar1) {
7524 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007525 * right is the last seen date. It is a base64 encoded
7526 * 30-bit value representing the UNIX date since the
7527 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007528 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007529 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007530 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007531 if (val_end - vbar1 >= 5) {
7532 val = b64tos30(vbar1);
7533 if (val > 0)
7534 txn->cookie_last_date = val << 2;
7535 }
7536 /* look for a second vertical bar */
7537 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7538 if (vbar1 && (val_end - vbar1 > 5)) {
7539 val = b64tos30(vbar1 + 1);
7540 if (val > 0)
7541 txn->cookie_first_date = val << 2;
7542 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007543 }
7544 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007545
Willy Tarreauf64d1412010-10-07 20:06:11 +02007546 /* if the cookie has an expiration date and the proxy wants to check
7547 * it, then we do that now. We first check if the cookie is too old,
7548 * then only if it has expired. We detect strict overflow because the
7549 * time resolution here is not great (4 seconds). Cookies with dates
7550 * in the future are ignored if their offset is beyond one day. This
7551 * allows an admin to fix timezone issues without expiring everyone
7552 * and at the same time avoids keeping unwanted side effects for too
7553 * long.
7554 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007555 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7556 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007557 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007558 txn->flags &= ~TX_CK_MASK;
7559 txn->flags |= TX_CK_OLD;
7560 delim = val_beg; // let's pretend we have not found the cookie
7561 txn->cookie_first_date = 0;
7562 txn->cookie_last_date = 0;
7563 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007564 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7565 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007566 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007567 txn->flags &= ~TX_CK_MASK;
7568 txn->flags |= TX_CK_EXPIRED;
7569 delim = val_beg; // let's pretend we have not found the cookie
7570 txn->cookie_first_date = 0;
7571 txn->cookie_last_date = 0;
7572 }
7573
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007574 /* Here, we'll look for the first running server which supports the cookie.
7575 * This allows to share a same cookie between several servers, for example
7576 * to dedicate backup servers to specific servers only.
7577 * However, to prevent clients from sticking to cookie-less backup server
7578 * when they have incidentely learned an empty cookie, we simply ignore
7579 * empty cookies and mark them as invalid.
7580 * The same behaviour is applied when persistence must be ignored.
7581 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007582 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007583 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007584
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007585 while (srv) {
7586 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7587 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007588 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007589 (s->be->options & PR_O_PERSIST) ||
7590 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007591 /* we found the server and we can use it */
7592 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007593 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007594 s->flags |= SN_DIRECT | SN_ASSIGNED;
7595 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007596 break;
7597 } else {
7598 /* we found a server, but it's down,
7599 * mark it as such and go on in case
7600 * another one is available.
7601 */
7602 txn->flags &= ~TX_CK_MASK;
7603 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007604 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007605 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007606 srv = srv->next;
7607 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007608
Willy Tarreauf64d1412010-10-07 20:06:11 +02007609 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007610 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007611 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007612 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007613 txn->flags |= TX_CK_UNUSED;
7614 else
7615 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007616 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007617
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007618 /* depending on the cookie mode, we may have to either :
7619 * - delete the complete cookie if we're in insert+indirect mode, so that
7620 * the server never sees it ;
7621 * - remove the server id from the cookie value, and tag the cookie as an
7622 * application cookie so that it does not get accidentely removed later,
7623 * if we're in cookie prefix mode
7624 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007625 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007626 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007627
Willy Tarreau9b28e032012-10-12 23:49:43 +02007628 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007629 val_end += delta;
7630 next += delta;
7631 hdr_end += delta;
7632 hdr_next += delta;
7633 cur_hdr->len += delta;
7634 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007635
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007636 del_from = NULL;
7637 preserve_hdr = 1; /* we want to keep this cookie */
7638 }
7639 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007640 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007641 del_from = prev;
7642 }
7643 } else {
7644 /* This is not our cookie, so we must preserve it. But if we already
7645 * scheduled another cookie for removal, we cannot remove the
7646 * complete header, but we can remove the previous block itself.
7647 */
7648 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007649
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007650 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007651 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007652 if (att_beg >= del_from)
7653 att_beg += delta;
7654 if (att_end >= del_from)
7655 att_end += delta;
7656 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007657 val_end += delta;
7658 next += delta;
7659 hdr_end += delta;
7660 hdr_next += delta;
7661 cur_hdr->len += delta;
7662 http_msg_move_end(&txn->req, delta);
7663 prev = del_from;
7664 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007665 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007666 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007667
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007668 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007669 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007670 int cmp_len, value_len;
7671 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007672
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007673 if (s->be->options2 & PR_O2_AS_PFX) {
7674 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7675 value_begin = att_beg + s->be->appsession_name_len;
7676 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007677 } else {
7678 cmp_len = att_end - att_beg;
7679 value_begin = val_beg;
7680 value_len = val_end - val_beg;
7681 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007682
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007683 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007684 if (cmp_len == s->be->appsession_name_len &&
7685 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7686 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007687 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007688 }
7689
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007690 /* continue with next cookie on this header line */
7691 att_beg = next;
7692 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007693
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007694 /* There are no more cookies on this line.
7695 * We may still have one (or several) marked for deletion at the
7696 * end of the line. We must do this now in two ways :
7697 * - if some cookies must be preserved, we only delete from the
7698 * mark to the end of line ;
7699 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007700 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007701 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007702 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007703 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007704 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007705 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007706 cur_hdr->len += delta;
7707 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007708 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007709
7710 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007711 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7712 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007713 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007714 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007715 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007716 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007717 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007718 }
7719
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007720 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007721 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007722 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007723}
7724
7725
Willy Tarreaua15645d2007-03-18 16:22:39 +01007726/* Iterate the same filter through all response headers contained in <rtr>.
7727 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7728 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007729int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007730{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007731 char *cur_ptr, *cur_end, *cur_next;
7732 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007733 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007734 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007735 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007736
7737 last_hdr = 0;
7738
Willy Tarreau9b28e032012-10-12 23:49:43 +02007739 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007740 old_idx = 0;
7741
7742 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007743 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007744 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007745 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007746 (exp->action == ACT_ALLOW ||
7747 exp->action == ACT_DENY))
7748 return 0;
7749
7750 cur_idx = txn->hdr_idx.v[old_idx].next;
7751 if (!cur_idx)
7752 break;
7753
7754 cur_hdr = &txn->hdr_idx.v[cur_idx];
7755 cur_ptr = cur_next;
7756 cur_end = cur_ptr + cur_hdr->len;
7757 cur_next = cur_end + cur_hdr->cr + 1;
7758
7759 /* Now we have one header between cur_ptr and cur_end,
7760 * and the next header starts at cur_next.
7761 */
7762
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007763 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007764 switch (exp->action) {
7765 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007766 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007767 last_hdr = 1;
7768 break;
7769
7770 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007771 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007772 last_hdr = 1;
7773 break;
7774
7775 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007776 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7777 if (trash.len < 0)
7778 return -1;
7779
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007780 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007781 /* FIXME: if the user adds a newline in the replacement, the
7782 * index will not be recalculated for now, and the new line
7783 * will not be counted as a new header.
7784 */
7785
7786 cur_end += delta;
7787 cur_next += delta;
7788 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007789 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007790 break;
7791
7792 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007793 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007794 cur_next += delta;
7795
Willy Tarreaufa355d42009-11-29 18:12:29 +01007796 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007797 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7798 txn->hdr_idx.used--;
7799 cur_hdr->len = 0;
7800 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007801 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007802 break;
7803
7804 }
7805 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007806
7807 /* keep the link from this header to next one in case of later
7808 * removal of next header.
7809 */
7810 old_idx = cur_idx;
7811 }
7812 return 0;
7813}
7814
7815
7816/* Apply the filter to the status line in the response buffer <rtr>.
7817 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7818 * or -1 if a replacement resulted in an invalid status line.
7819 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007820int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007821{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007822 char *cur_ptr, *cur_end;
7823 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007824 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007825 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007826
7827
Willy Tarreau3d300592007-03-18 18:34:41 +01007828 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007829 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007830 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007831 (exp->action == ACT_ALLOW ||
7832 exp->action == ACT_DENY))
7833 return 0;
7834 else if (exp->action == ACT_REMOVE)
7835 return 0;
7836
7837 done = 0;
7838
Willy Tarreau9b28e032012-10-12 23:49:43 +02007839 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007840 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007841
7842 /* Now we have the status line between cur_ptr and cur_end */
7843
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007844 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007845 switch (exp->action) {
7846 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007847 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007848 done = 1;
7849 break;
7850
7851 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007852 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007853 done = 1;
7854 break;
7855
7856 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007857 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7858 if (trash.len < 0)
7859 return -1;
7860
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007861 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007862 /* FIXME: if the user adds a newline in the replacement, the
7863 * index will not be recalculated for now, and the new line
7864 * will not be counted as a new header.
7865 */
7866
Willy Tarreaufa355d42009-11-29 18:12:29 +01007867 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007868 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007869 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007870 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007871 cur_ptr, cur_end + 1,
7872 NULL, NULL);
7873 if (unlikely(!cur_end))
7874 return -1;
7875
7876 /* we have a full respnse and we know that we have either a CR
7877 * or an LF at <ptr>.
7878 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007879 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007880 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007881 /* there is no point trying this regex on headers */
7882 return 1;
7883 }
7884 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007885 return done;
7886}
7887
7888
7889
7890/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007891 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007892 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7893 * unparsable response.
7894 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007895int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007896{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007897 struct http_txn *txn = &s->txn;
7898 struct hdr_exp *exp;
7899
7900 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007901 int ret;
7902
7903 /*
7904 * The interleaving of transformations and verdicts
7905 * makes it difficult to decide to continue or stop
7906 * the evaluation.
7907 */
7908
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007909 if (txn->flags & TX_SVDENY)
7910 break;
7911
Willy Tarreau3d300592007-03-18 18:34:41 +01007912 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007913 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7914 exp->action == ACT_PASS)) {
7915 exp = exp->next;
7916 continue;
7917 }
7918
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007919 /* if this filter had a condition, evaluate it now and skip to
7920 * next filter if the condition does not match.
7921 */
7922 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007923 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007924 ret = acl_pass(ret);
7925 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7926 ret = !ret;
7927 if (!ret)
7928 continue;
7929 }
7930
Willy Tarreaua15645d2007-03-18 16:22:39 +01007931 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007932 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007933 if (unlikely(ret < 0))
7934 return -1;
7935
7936 if (likely(ret == 0)) {
7937 /* The filter did not match the response, it can be
7938 * iterated through all headers.
7939 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007940 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7941 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007942 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007943 }
7944 return 0;
7945}
7946
7947
Willy Tarreaua15645d2007-03-18 16:22:39 +01007948/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007949 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007950 * desirable to call it only when needed. This function is also used when we
7951 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007952 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007953void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007954{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007955 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007956 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007957 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007958 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007959 char *hdr_beg, *hdr_end, *hdr_next;
7960 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007961
Willy Tarreaua15645d2007-03-18 16:22:39 +01007962 /* Iterate through the headers.
7963 * we start with the start line.
7964 */
7965 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007966 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007967
7968 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7969 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007970 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007971
7972 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007973 hdr_beg = hdr_next;
7974 hdr_end = hdr_beg + cur_hdr->len;
7975 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007976
Willy Tarreau24581ba2010-08-31 22:39:35 +02007977 /* We have one full header between hdr_beg and hdr_end, and the
7978 * next header starts at hdr_next. We're only interested in
7979 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007980 */
7981
Willy Tarreau24581ba2010-08-31 22:39:35 +02007982 is_cookie2 = 0;
7983 prev = hdr_beg + 10;
7984 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007985 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007986 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7987 if (!val) {
7988 old_idx = cur_idx;
7989 continue;
7990 }
7991 is_cookie2 = 1;
7992 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007993 }
7994
Willy Tarreau24581ba2010-08-31 22:39:35 +02007995 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7996 * <prev> points to the colon.
7997 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007998 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007999
Willy Tarreau24581ba2010-08-31 22:39:35 +02008000 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8001 * check-cache is enabled) and we are not interested in checking
8002 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008003 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008004 if (s->be->cookie_name == NULL &&
8005 s->be->appsession_name == NULL &&
8006 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008007 return;
8008
Willy Tarreau24581ba2010-08-31 22:39:35 +02008009 /* OK so now we know we have to process this response cookie.
8010 * The format of the Set-Cookie header is slightly different
8011 * from the format of the Cookie header in that it does not
8012 * support the comma as a cookie delimiter (thus the header
8013 * cannot be folded) because the Expires attribute described in
8014 * the original Netscape's spec may contain an unquoted date
8015 * with a comma inside. We have to live with this because
8016 * many browsers don't support Max-Age and some browsers don't
8017 * support quoted strings. However the Set-Cookie2 header is
8018 * clean.
8019 *
8020 * We have to keep multiple pointers in order to support cookie
8021 * removal at the beginning, middle or end of header without
8022 * corrupting the header (in case of set-cookie2). A special
8023 * pointer, <scav> points to the beginning of the set-cookie-av
8024 * fields after the first semi-colon. The <next> pointer points
8025 * either to the end of line (set-cookie) or next unquoted comma
8026 * (set-cookie2). All of these headers are valid :
8027 *
8028 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8029 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8030 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8031 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8032 * | | | | | | | | | |
8033 * | | | | | | | | +-> next hdr_end <--+
8034 * | | | | | | | +------------> scav
8035 * | | | | | | +--------------> val_end
8036 * | | | | | +--------------------> val_beg
8037 * | | | | +----------------------> equal
8038 * | | | +------------------------> att_end
8039 * | | +----------------------------> att_beg
8040 * | +------------------------------> prev
8041 * +-----------------------------------------> hdr_beg
8042 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008043
Willy Tarreau24581ba2010-08-31 22:39:35 +02008044 for (; prev < hdr_end; prev = next) {
8045 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008046
Willy Tarreau24581ba2010-08-31 22:39:35 +02008047 /* find att_beg */
8048 att_beg = prev + 1;
8049 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8050 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008051
Willy Tarreau24581ba2010-08-31 22:39:35 +02008052 /* find att_end : this is the first character after the last non
8053 * space before the equal. It may be equal to hdr_end.
8054 */
8055 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008056
Willy Tarreau24581ba2010-08-31 22:39:35 +02008057 while (equal < hdr_end) {
8058 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8059 break;
8060 if (http_is_spht[(unsigned char)*equal++])
8061 continue;
8062 att_end = equal;
8063 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008064
Willy Tarreau24581ba2010-08-31 22:39:35 +02008065 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8066 * is between <att_beg> and <equal>, both may be identical.
8067 */
8068
8069 /* look for end of cookie if there is an equal sign */
8070 if (equal < hdr_end && *equal == '=') {
8071 /* look for the beginning of the value */
8072 val_beg = equal + 1;
8073 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8074 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008075
Willy Tarreau24581ba2010-08-31 22:39:35 +02008076 /* find the end of the value, respecting quotes */
8077 next = find_cookie_value_end(val_beg, hdr_end);
8078
8079 /* make val_end point to the first white space or delimitor after the value */
8080 val_end = next;
8081 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8082 val_end--;
8083 } else {
8084 /* <equal> points to next comma, semi-colon or EOL */
8085 val_beg = val_end = next = equal;
8086 }
8087
8088 if (next < hdr_end) {
8089 /* Set-Cookie2 supports multiple cookies, and <next> points to
8090 * a colon or semi-colon before the end. So skip all attr-value
8091 * pairs and look for the next comma. For Set-Cookie, since
8092 * commas are permitted in values, skip to the end.
8093 */
8094 if (is_cookie2)
8095 next = find_hdr_value_end(next, hdr_end);
8096 else
8097 next = hdr_end;
8098 }
8099
8100 /* Now everything is as on the diagram above */
8101
8102 /* Ignore cookies with no equal sign */
8103 if (equal == val_end)
8104 continue;
8105
8106 /* If there are spaces around the equal sign, we need to
8107 * strip them otherwise we'll get trouble for cookie captures,
8108 * or even for rewrites. Since this happens extremely rarely,
8109 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008110 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008111 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8112 int stripped_before = 0;
8113 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008114
Willy Tarreau24581ba2010-08-31 22:39:35 +02008115 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008116 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008117 equal += stripped_before;
8118 val_beg += stripped_before;
8119 }
8120
8121 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008122 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008123 val_beg += stripped_after;
8124 stripped_before += stripped_after;
8125 }
8126
8127 val_end += stripped_before;
8128 next += stripped_before;
8129 hdr_end += stripped_before;
8130 hdr_next += stripped_before;
8131 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008132 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008133 }
8134
8135 /* First, let's see if we want to capture this cookie. We check
8136 * that we don't already have a server side cookie, because we
8137 * can only capture one. Also as an optimisation, we ignore
8138 * cookies shorter than the declared name.
8139 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008140 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008141 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008142 (val_end - att_beg >= s->fe->capture_namelen) &&
8143 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008144 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008145 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008146 Alert("HTTP logging : out of memory.\n");
8147 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008148 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008149 if (log_len > s->fe->capture_len)
8150 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008151 memcpy(txn->srv_cookie, att_beg, log_len);
8152 txn->srv_cookie[log_len] = 0;
8153 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008154 }
8155
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008156 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008157 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008158 if (!(s->flags & SN_IGNORE_PRST) &&
8159 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8160 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008161 /* assume passive cookie by default */
8162 txn->flags &= ~TX_SCK_MASK;
8163 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008164
8165 /* If the cookie is in insert mode on a known server, we'll delete
8166 * this occurrence because we'll insert another one later.
8167 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008168 * a direct access.
8169 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008170 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008171 /* The "preserve" flag was set, we don't want to touch the
8172 * server's cookie.
8173 */
8174 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008175 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8176 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008177 /* this cookie must be deleted */
8178 if (*prev == ':' && next == hdr_end) {
8179 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008180 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008181 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8182 txn->hdr_idx.used--;
8183 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008184 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008185 hdr_next += delta;
8186 http_msg_move_end(&txn->rsp, delta);
8187 /* note: while both invalid now, <next> and <hdr_end>
8188 * are still equal, so the for() will stop as expected.
8189 */
8190 } else {
8191 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008192 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008193 next = prev;
8194 hdr_end += delta;
8195 hdr_next += delta;
8196 cur_hdr->len += delta;
8197 http_msg_move_end(&txn->rsp, delta);
8198 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008199 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008200 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008201 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008202 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008203 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008204 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008205 * with this server since we know it.
8206 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008207 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008208 next += delta;
8209 hdr_end += delta;
8210 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008211 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008212 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008213
Willy Tarreauf1348312010-10-07 15:54:11 +02008214 txn->flags &= ~TX_SCK_MASK;
8215 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008216 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008217 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008218 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008219 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008220 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008221 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008222 next += delta;
8223 hdr_end += delta;
8224 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008225 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008226 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008227
Willy Tarreau827aee92011-03-10 16:55:02 +01008228 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008229 txn->flags &= ~TX_SCK_MASK;
8230 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008231 }
8232 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008233 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008234 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008235 int cmp_len, value_len;
8236 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008237
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008238 if (s->be->options2 & PR_O2_AS_PFX) {
8239 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8240 value_begin = att_beg + s->be->appsession_name_len;
8241 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008242 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008243 cmp_len = att_end - att_beg;
8244 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008245 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008246 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008247
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008248 if ((cmp_len == s->be->appsession_name_len) &&
8249 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008250 /* free a possibly previously allocated memory */
8251 pool_free2(apools.sessid, txn->sessid);
8252
Cyril Bontéb21570a2009-11-29 20:04:48 +01008253 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008254 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008255 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008256 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008257 return;
8258 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008259 memcpy(txn->sessid, value_begin, value_len);
8260 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008261 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008262 }
8263 /* that's done for this cookie, check the next one on the same
8264 * line when next != hdr_end (only if is_cookie2).
8265 */
8266 }
8267 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008268 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008269 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008270
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008271 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008272 appsess *asession = NULL;
8273 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008274 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008275 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008276 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008277 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8278 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008279 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008280 return;
8281 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008282 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8283
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008284 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8285 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008286 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8287 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008288 return;
8289 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008290 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8291 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008292
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008293 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008294 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008295 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008296 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8297 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008298 return;
8299 }
8300 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008301 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008302
8303 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008304 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008305 }
8306
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008307 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008308 asession->request_count++;
8309 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008310}
8311
8312
Willy Tarreaua15645d2007-03-18 16:22:39 +01008313/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008314 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008315 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008316void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008317{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008318 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008319 char *p1, *p2;
8320
8321 char *cur_ptr, *cur_end, *cur_next;
8322 int cur_idx;
8323
Willy Tarreau5df51872007-11-25 16:20:08 +01008324 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008325 return;
8326
8327 /* Iterate through the headers.
8328 * we start with the start line.
8329 */
8330 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008331 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008332
8333 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8334 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008335 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008336
8337 cur_hdr = &txn->hdr_idx.v[cur_idx];
8338 cur_ptr = cur_next;
8339 cur_end = cur_ptr + cur_hdr->len;
8340 cur_next = cur_end + cur_hdr->cr + 1;
8341
8342 /* We have one full header between cur_ptr and cur_end, and the
8343 * next header starts at cur_next. We're only interested in
8344 * "Cookie:" headers.
8345 */
8346
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008347 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8348 if (val) {
8349 if ((cur_end - (cur_ptr + val) >= 8) &&
8350 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8351 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8352 return;
8353 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008354 }
8355
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008356 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8357 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008358 continue;
8359
8360 /* OK, right now we know we have a cache-control header at cur_ptr */
8361
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008362 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008363
8364 if (p1 >= cur_end) /* no more info */
8365 continue;
8366
8367 /* p1 is at the beginning of the value */
8368 p2 = p1;
8369
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008370 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008371 p2++;
8372
8373 /* we have a complete value between p1 and p2 */
8374 if (p2 < cur_end && *p2 == '=') {
8375 /* we have something of the form no-cache="set-cookie" */
8376 if ((cur_end - p1 >= 21) &&
8377 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8378 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008379 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008380 continue;
8381 }
8382
8383 /* OK, so we know that either p2 points to the end of string or to a comma */
8384 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008385 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008386 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8387 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8388 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008389 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008390 return;
8391 }
8392
8393 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008394 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008395 continue;
8396 }
8397 }
8398}
8399
8400
Willy Tarreau58f10d72006-12-04 02:26:12 +01008401/*
8402 * Try to retrieve a known appsession in the URI, then the associated server.
8403 * If the server is found, it's assigned to the session.
8404 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008405void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008406{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008407 char *end_params, *first_param, *cur_param, *next_param;
8408 char separator;
8409 int value_len;
8410
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008411 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008412
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008413 if (s->be->appsession_name == NULL ||
8414 (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 +01008415 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008416 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008417
Cyril Bontéb21570a2009-11-29 20:04:48 +01008418 first_param = NULL;
8419 switch (mode) {
8420 case PR_O2_AS_M_PP:
8421 first_param = memchr(begin, ';', len);
8422 break;
8423 case PR_O2_AS_M_QS:
8424 first_param = memchr(begin, '?', len);
8425 break;
8426 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008427
Cyril Bontéb21570a2009-11-29 20:04:48 +01008428 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008429 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008430 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008431
Cyril Bontéb21570a2009-11-29 20:04:48 +01008432 switch (mode) {
8433 case PR_O2_AS_M_PP:
8434 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8435 end_params = (char *) begin + len;
8436 }
8437 separator = ';';
8438 break;
8439 case PR_O2_AS_M_QS:
8440 end_params = (char *) begin + len;
8441 separator = '&';
8442 break;
8443 default:
8444 /* unknown mode, shouldn't happen */
8445 return;
8446 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008447
Cyril Bontéb21570a2009-11-29 20:04:48 +01008448 cur_param = next_param = end_params;
8449 while (cur_param > first_param) {
8450 cur_param--;
8451 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8452 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008453 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8454 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8455 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008456 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008457 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8458 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008459 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008460 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008461 }
8462 break;
8463 }
8464 next_param = cur_param;
8465 }
8466 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008467#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008468 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008469 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008470#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008471}
8472
Willy Tarreaub2513902006-12-17 14:52:38 +01008473/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008474 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008475 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008476 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008477 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008478 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008479 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008480 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008481 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008482int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008483{
8484 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008485 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008486 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008487
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008488 if (!uri_auth)
8489 return 0;
8490
Cyril Bonté70be45d2010-10-12 00:14:35 +02008491 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008492 return 0;
8493
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008494 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008495 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008496 return 0;
8497
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008498 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008499 return 0;
8500
Willy Tarreaub2513902006-12-17 14:52:38 +01008501 return 1;
8502}
8503
Willy Tarreau4076a152009-04-02 15:18:36 +02008504/*
8505 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008506 * By default it tries to report the error position as msg->err_pos. However if
8507 * this one is not set, it will then report msg->next, which is the last known
8508 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008509 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008510 */
8511void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008512 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008513 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008514{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008515 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008516 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008517
Willy Tarreau9b28e032012-10-12 23:49:43 +02008518 es->len = MIN(chn->buf->i, sizeof(es->buf));
8519 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008520 len1 = MIN(len1, es->len);
8521 len2 = es->len - len1; /* remaining data if buffer wraps */
8522
Willy Tarreau9b28e032012-10-12 23:49:43 +02008523 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008524 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008525 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008526
Willy Tarreau4076a152009-04-02 15:18:36 +02008527 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008528 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008529 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008530 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008531
Willy Tarreau4076a152009-04-02 15:18:36 +02008532 es->when = date; // user-visible date
8533 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008534 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008535 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008536 if (objt_conn(s->req->prod->end))
8537 es->src = __objt_conn(s->req->prod->end)->addr.from;
8538 else
8539 memset(&es->src, 0, sizeof(es->src));
8540
Willy Tarreau078272e2010-12-12 12:46:33 +01008541 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008542 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008543 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008544 es->s_flags = s->flags;
8545 es->t_flags = s->txn.flags;
8546 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008547 es->b_out = chn->buf->o;
8548 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008549 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008550 es->m_clen = msg->chunk_len;
8551 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008552}
Willy Tarreaub2513902006-12-17 14:52:38 +01008553
Willy Tarreau294c4732011-12-16 21:35:50 +01008554/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8555 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8556 * performed over the whole headers. Otherwise it must contain a valid header
8557 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8558 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8559 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8560 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008561 * -1. The value fetch stops at commas, so this function is suited for use with
8562 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008563 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008564 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008565unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008566 struct hdr_idx *idx, int occ,
8567 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008568{
Willy Tarreau294c4732011-12-16 21:35:50 +01008569 struct hdr_ctx local_ctx;
8570 char *ptr_hist[MAX_HDR_HISTORY];
8571 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008572 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008573 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008574
Willy Tarreau294c4732011-12-16 21:35:50 +01008575 if (!ctx) {
8576 local_ctx.idx = 0;
8577 ctx = &local_ctx;
8578 }
8579
Willy Tarreaubce70882009-09-07 11:51:47 +02008580 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008581 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008582 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008583 occ--;
8584 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008585 *vptr = ctx->line + ctx->val;
8586 *vlen = ctx->vlen;
8587 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008588 }
8589 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008590 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008591 }
8592
8593 /* negative occurrence, we scan all the list then walk back */
8594 if (-occ > MAX_HDR_HISTORY)
8595 return 0;
8596
Willy Tarreau294c4732011-12-16 21:35:50 +01008597 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008598 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008599 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8600 len_hist[hist_ptr] = ctx->vlen;
8601 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008602 hist_ptr = 0;
8603 found++;
8604 }
8605 if (-occ > found)
8606 return 0;
8607 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008608 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8609 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8610 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008611 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008612 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008613 if (hist_ptr >= MAX_HDR_HISTORY)
8614 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008615 *vptr = ptr_hist[hist_ptr];
8616 *vlen = len_hist[hist_ptr];
8617 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008618}
8619
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008620/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8621 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8622 * performed over the whole headers. Otherwise it must contain a valid header
8623 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8624 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8625 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8626 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8627 * -1. This function differs from http_get_hdr() in that it only returns full
8628 * line header values and does not stop at commas.
8629 * The return value is 0 if nothing was found, or non-zero otherwise.
8630 */
8631unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8632 struct hdr_idx *idx, int occ,
8633 struct hdr_ctx *ctx, char **vptr, int *vlen)
8634{
8635 struct hdr_ctx local_ctx;
8636 char *ptr_hist[MAX_HDR_HISTORY];
8637 int len_hist[MAX_HDR_HISTORY];
8638 unsigned int hist_ptr;
8639 int found;
8640
8641 if (!ctx) {
8642 local_ctx.idx = 0;
8643 ctx = &local_ctx;
8644 }
8645
8646 if (occ >= 0) {
8647 /* search from the beginning */
8648 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8649 occ--;
8650 if (occ <= 0) {
8651 *vptr = ctx->line + ctx->val;
8652 *vlen = ctx->vlen;
8653 return 1;
8654 }
8655 }
8656 return 0;
8657 }
8658
8659 /* negative occurrence, we scan all the list then walk back */
8660 if (-occ > MAX_HDR_HISTORY)
8661 return 0;
8662
8663 found = hist_ptr = 0;
8664 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8665 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8666 len_hist[hist_ptr] = ctx->vlen;
8667 if (++hist_ptr >= MAX_HDR_HISTORY)
8668 hist_ptr = 0;
8669 found++;
8670 }
8671 if (-occ > found)
8672 return 0;
8673 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8674 * find occurrence -occ, so we have to check [hist_ptr+occ].
8675 */
8676 hist_ptr += occ;
8677 if (hist_ptr >= MAX_HDR_HISTORY)
8678 hist_ptr -= MAX_HDR_HISTORY;
8679 *vptr = ptr_hist[hist_ptr];
8680 *vlen = len_hist[hist_ptr];
8681 return 1;
8682}
8683
Willy Tarreaubaaee002006-06-26 02:48:02 +02008684/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008685 * Print a debug line with a header. Always stop at the first CR or LF char,
8686 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8687 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008688 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008689void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008690{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008691 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008692 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008693 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008694 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8695 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 +02008696
8697 for (max = 0; start + max < end; max++)
8698 if (start[max] == '\r' || start[max] == '\n')
8699 break;
8700
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008701 UBOUND(max, trash.size - trash.len - 3);
8702 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8703 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008704 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008705}
8706
Willy Tarreau0937bc42009-12-22 15:03:09 +01008707/*
8708 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8709 * the required fields are properly allocated and that we only need to (re)init
8710 * them. This should be used before processing any new request.
8711 */
8712void http_init_txn(struct session *s)
8713{
8714 struct http_txn *txn = &s->txn;
8715 struct proxy *fe = s->fe;
8716
8717 txn->flags = 0;
8718 txn->status = -1;
8719
Willy Tarreauf64d1412010-10-07 20:06:11 +02008720 txn->cookie_first_date = 0;
8721 txn->cookie_last_date = 0;
8722
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008723 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008724 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008725 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008726 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008727 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008728 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008729 txn->req.chunk_len = 0LL;
8730 txn->req.body_len = 0LL;
8731 txn->rsp.chunk_len = 0LL;
8732 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008733 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8734 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008735 txn->req.chn = s->req;
8736 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008737
8738 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008739
8740 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8741 if (fe->options2 & PR_O2_REQBUG_OK)
8742 txn->req.err_pos = -1; /* let buggy requests pass */
8743
Willy Tarreau46023632010-01-07 22:51:47 +01008744 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008745 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8746
Willy Tarreau46023632010-01-07 22:51:47 +01008747 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008748 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8749
8750 if (txn->hdr_idx.v)
8751 hdr_idx_init(&txn->hdr_idx);
8752}
8753
8754/* to be used at the end of a transaction */
8755void http_end_txn(struct session *s)
8756{
8757 struct http_txn *txn = &s->txn;
8758
Willy Tarreau75195602014-03-11 15:48:55 +01008759 /* release any possible compression context */
8760 if (s->flags & SN_COMP_READY)
8761 s->comp_algo->end(&s->comp_ctx);
8762 s->comp_algo = NULL;
8763 s->flags &= ~SN_COMP_READY;
8764
Willy Tarreau0937bc42009-12-22 15:03:09 +01008765 /* these ones will have been dynamically allocated */
8766 pool_free2(pool2_requri, txn->uri);
8767 pool_free2(pool2_capture, txn->cli_cookie);
8768 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008769 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008770 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008771
William Lallemanda73203e2012-03-12 12:48:57 +01008772 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008773 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008774 txn->uri = NULL;
8775 txn->srv_cookie = NULL;
8776 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008777
8778 if (txn->req.cap) {
8779 struct cap_hdr *h;
8780 for (h = s->fe->req_cap; h; h = h->next)
8781 pool_free2(h->pool, txn->req.cap[h->index]);
8782 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8783 }
8784
8785 if (txn->rsp.cap) {
8786 struct cap_hdr *h;
8787 for (h = s->fe->rsp_cap; h; h = h->next)
8788 pool_free2(h->pool, txn->rsp.cap[h->index]);
8789 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8790 }
8791
Willy Tarreau0937bc42009-12-22 15:03:09 +01008792}
8793
8794/* to be used at the end of a transaction to prepare a new one */
8795void http_reset_txn(struct session *s)
8796{
8797 http_end_txn(s);
8798 http_init_txn(s);
8799
8800 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008801 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008802 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008803 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008804 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008805 /* re-init store persistence */
8806 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008807 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008808
Willy Tarreau0937bc42009-12-22 15:03:09 +01008809 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008810
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008811 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008812
Willy Tarreau739cfba2010-01-25 23:11:14 +01008813 /* We must trim any excess data from the response buffer, because we
8814 * may have blocked an invalid response from a server that we don't
8815 * want to accidentely forward once we disable the analysers, nor do
8816 * we want those data to come along with next response. A typical
8817 * example of such data would be from a buggy server responding to
8818 * a HEAD with some data, or sending more than the advertised
8819 * content-length.
8820 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008821 if (unlikely(s->rep->buf->i))
8822 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008823
Willy Tarreau0937bc42009-12-22 15:03:09 +01008824 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008825 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008826
Willy Tarreaud04e8582010-05-31 12:31:35 +02008827 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008828 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008829
8830 s->req->rex = TICK_ETERNITY;
8831 s->req->wex = TICK_ETERNITY;
8832 s->req->analyse_exp = TICK_ETERNITY;
8833 s->rep->rex = TICK_ETERNITY;
8834 s->rep->wex = TICK_ETERNITY;
8835 s->rep->analyse_exp = TICK_ETERNITY;
8836}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008837
Sasha Pachev218f0642014-06-16 12:05:59 -06008838void free_http_res_rules(struct list *r)
8839{
8840 struct http_res_rule *tr, *pr;
8841
8842 list_for_each_entry_safe(pr, tr, r, list) {
8843 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008844 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008845 free(pr);
8846 }
8847}
8848
8849void free_http_req_rules(struct list *r)
8850{
Willy Tarreauff011f22011-01-06 17:51:27 +01008851 struct http_req_rule *tr, *pr;
8852
8853 list_for_each_entry_safe(pr, tr, r, list) {
8854 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008855 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008856 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008857
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008858 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008859 free(pr);
8860 }
8861}
8862
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008863/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008864struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8865{
8866 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008867 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008868 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008869 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008870
8871 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8872 if (!rule) {
8873 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008874 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008875 }
8876
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008877 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008878 rule->action = HTTP_REQ_ACT_ALLOW;
8879 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008880 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008881 rule->action = HTTP_REQ_ACT_DENY;
8882 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008883 } else if (!strcmp(args[0], "tarpit")) {
8884 rule->action = HTTP_REQ_ACT_TARPIT;
8885 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008886 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008887 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008888 cur_arg = 1;
8889
8890 while(*args[cur_arg]) {
8891 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008892 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008893 cur_arg+=2;
8894 continue;
8895 } else
8896 break;
8897 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008898 } else if (!strcmp(args[0], "set-nice")) {
8899 rule->action = HTTP_REQ_ACT_SET_NICE;
8900 cur_arg = 1;
8901
8902 if (!*args[cur_arg] ||
8903 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8904 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8905 file, linenum, args[0]);
8906 goto out_err;
8907 }
8908 rule->arg.nice = atoi(args[cur_arg]);
8909 if (rule->arg.nice < -1024)
8910 rule->arg.nice = -1024;
8911 else if (rule->arg.nice > 1024)
8912 rule->arg.nice = 1024;
8913 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008914 } else if (!strcmp(args[0], "set-tos")) {
8915#ifdef IP_TOS
8916 char *err;
8917 rule->action = HTTP_REQ_ACT_SET_TOS;
8918 cur_arg = 1;
8919
8920 if (!*args[cur_arg] ||
8921 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8922 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8923 file, linenum, args[0]);
8924 goto out_err;
8925 }
8926
8927 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8928 if (err && *err != '\0') {
8929 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8930 file, linenum, err, args[0]);
8931 goto out_err;
8932 }
8933 cur_arg++;
8934#else
8935 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8936 goto out_err;
8937#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008938 } else if (!strcmp(args[0], "set-mark")) {
8939#ifdef SO_MARK
8940 char *err;
8941 rule->action = HTTP_REQ_ACT_SET_MARK;
8942 cur_arg = 1;
8943
8944 if (!*args[cur_arg] ||
8945 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8946 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8947 file, linenum, args[0]);
8948 goto out_err;
8949 }
8950
8951 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8952 if (err && *err != '\0') {
8953 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8954 file, linenum, err, args[0]);
8955 goto out_err;
8956 }
8957 cur_arg++;
8958 global.last_checks |= LSTCHK_NETADM;
8959#else
8960 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8961 goto out_err;
8962#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008963 } else if (!strcmp(args[0], "set-log-level")) {
8964 rule->action = HTTP_REQ_ACT_SET_LOGL;
8965 cur_arg = 1;
8966
8967 if (!*args[cur_arg] ||
8968 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8969 bad_log_level:
8970 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8971 file, linenum, args[0]);
8972 goto out_err;
8973 }
8974 if (strcmp(args[cur_arg], "silent") == 0)
8975 rule->arg.loglevel = -1;
8976 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8977 goto bad_log_level;
8978 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008979 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8980 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8981 cur_arg = 1;
8982
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008983 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8984 (*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 +01008985 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8986 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008987 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008988 }
8989
8990 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8991 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8992 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008993
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008994 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008995 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008996 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8997 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008998 free(proxy->conf.lfs_file);
8999 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9000 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009001 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009002 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9003 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009004 cur_arg = 1;
9005
9006 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann8f249f42014-06-24 11:10:00 +02009007 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06009008 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9009 file, linenum, args[0]);
9010 goto out_err;
9011 }
9012
9013 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9014 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9015 LIST_INIT(&rule->arg.hdr_add.fmt);
9016
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009017 error = NULL;
9018 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9019 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9020 args[cur_arg + 1], error);
9021 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009022 goto out_err;
9023 }
9024
9025 proxy->conf.args.ctx = ARGC_HRQ;
9026 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9027 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9028 file, linenum);
9029
9030 free(proxy->conf.lfs_file);
9031 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9032 proxy->conf.lfs_line = proxy->conf.args.line;
9033 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009034 } else if (strcmp(args[0], "del-header") == 0) {
9035 rule->action = HTTP_REQ_ACT_DEL_HDR;
9036 cur_arg = 1;
9037
9038 if (!*args[cur_arg] ||
9039 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9040 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9041 file, linenum, args[0]);
9042 goto out_err;
9043 }
9044
9045 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9046 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9047
9048 proxy->conf.args.ctx = ARGC_HRQ;
9049 free(proxy->conf.lfs_file);
9050 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9051 proxy->conf.lfs_line = proxy->conf.args.line;
9052 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009053 } else if (strcmp(args[0], "redirect") == 0) {
9054 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009055 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009056
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009057 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009058 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9059 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9060 goto out_err;
9061 }
9062
9063 /* this redirect rule might already contain a parsed condition which
9064 * we'll pass to the http-request rule.
9065 */
9066 rule->action = HTTP_REQ_ACT_REDIR;
9067 rule->arg.redir = redir;
9068 rule->cond = redir->cond;
9069 redir->cond = NULL;
9070 cur_arg = 2;
9071 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009072 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9073 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9074 rule->action = HTTP_REQ_ACT_ADD_ACL;
9075 /*
9076 * '+ 8' for 'add-acl('
9077 * '- 9' for 'add-acl(' + trailing ')'
9078 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009079 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009080
9081 cur_arg = 1;
9082
9083 if (!*args[cur_arg] ||
9084 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9085 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9086 file, linenum, args[0]);
9087 goto out_err;
9088 }
9089
9090 LIST_INIT(&rule->arg.map.key);
9091 proxy->conf.args.ctx = ARGC_HRQ;
9092 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9093 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9094 file, linenum);
9095 free(proxy->conf.lfs_file);
9096 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9097 proxy->conf.lfs_line = proxy->conf.args.line;
9098 cur_arg += 1;
9099 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9100 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9101 rule->action = HTTP_REQ_ACT_DEL_ACL;
9102 /*
9103 * '+ 8' for 'del-acl('
9104 * '- 9' for 'del-acl(' + trailing ')'
9105 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009106 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009107
9108 cur_arg = 1;
9109
9110 if (!*args[cur_arg] ||
9111 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9112 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9113 file, linenum, args[0]);
9114 goto out_err;
9115 }
9116
9117 LIST_INIT(&rule->arg.map.key);
9118 proxy->conf.args.ctx = ARGC_HRQ;
9119 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9120 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9121 file, linenum);
9122 free(proxy->conf.lfs_file);
9123 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9124 proxy->conf.lfs_line = proxy->conf.args.line;
9125 cur_arg += 1;
9126 } else if (strncmp(args[0], "del-map", 7) == 0) {
9127 /* http-request del-map(<reference (map name)>) <key pattern> */
9128 rule->action = HTTP_REQ_ACT_DEL_MAP;
9129 /*
9130 * '+ 8' for 'del-map('
9131 * '- 9' for 'del-map(' + trailing ')'
9132 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009133 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009134
9135 cur_arg = 1;
9136
9137 if (!*args[cur_arg] ||
9138 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9139 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9140 file, linenum, args[0]);
9141 goto out_err;
9142 }
9143
9144 LIST_INIT(&rule->arg.map.key);
9145 proxy->conf.args.ctx = ARGC_HRQ;
9146 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9147 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9148 file, linenum);
9149 free(proxy->conf.lfs_file);
9150 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9151 proxy->conf.lfs_line = proxy->conf.args.line;
9152 cur_arg += 1;
9153 } else if (strncmp(args[0], "set-map", 7) == 0) {
9154 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9155 rule->action = HTTP_REQ_ACT_SET_MAP;
9156 /*
9157 * '+ 8' for 'set-map('
9158 * '- 9' for 'set-map(' + trailing ')'
9159 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009160 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009161
9162 cur_arg = 1;
9163
9164 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9165 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9166 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9167 file, linenum, args[0]);
9168 goto out_err;
9169 }
9170
9171 LIST_INIT(&rule->arg.map.key);
9172 LIST_INIT(&rule->arg.map.value);
9173 proxy->conf.args.ctx = ARGC_HRQ;
9174
9175 /* key pattern */
9176 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9177 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9178 file, linenum);
9179
9180 /* value pattern */
9181 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9182 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9183 file, linenum);
9184 free(proxy->conf.lfs_file);
9185 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9186 proxy->conf.lfs_line = proxy->conf.args.line;
9187
9188 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009189 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9190 char *errmsg = NULL;
9191 cur_arg = 1;
9192 /* try in the module list */
9193 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9194 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9195 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9196 free(errmsg);
9197 goto out_err;
9198 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009199 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009200 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'add-acl', 'del-acl', 'del-map', 'set-map', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01009201 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009202 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009203 }
9204
9205 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9206 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009207 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009208
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009209 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9210 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9211 file, linenum, args[0], errmsg);
9212 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009213 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009214 }
9215 rule->cond = cond;
9216 }
9217 else if (*args[cur_arg]) {
9218 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9219 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9220 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009221 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009222 }
9223
9224 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009225 out_err:
9226 free(rule);
9227 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009228}
9229
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009230/* parse an "http-respose" rule */
9231struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9232{
9233 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009234 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009235 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009236 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009237
9238 rule = calloc(1, sizeof(*rule));
9239 if (!rule) {
9240 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9241 goto out_err;
9242 }
9243
9244 if (!strcmp(args[0], "allow")) {
9245 rule->action = HTTP_RES_ACT_ALLOW;
9246 cur_arg = 1;
9247 } else if (!strcmp(args[0], "deny")) {
9248 rule->action = HTTP_RES_ACT_DENY;
9249 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009250 } else if (!strcmp(args[0], "set-nice")) {
9251 rule->action = HTTP_RES_ACT_SET_NICE;
9252 cur_arg = 1;
9253
9254 if (!*args[cur_arg] ||
9255 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9256 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9257 file, linenum, args[0]);
9258 goto out_err;
9259 }
9260 rule->arg.nice = atoi(args[cur_arg]);
9261 if (rule->arg.nice < -1024)
9262 rule->arg.nice = -1024;
9263 else if (rule->arg.nice > 1024)
9264 rule->arg.nice = 1024;
9265 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009266 } else if (!strcmp(args[0], "set-tos")) {
9267#ifdef IP_TOS
9268 char *err;
9269 rule->action = HTTP_RES_ACT_SET_TOS;
9270 cur_arg = 1;
9271
9272 if (!*args[cur_arg] ||
9273 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9274 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9275 file, linenum, args[0]);
9276 goto out_err;
9277 }
9278
9279 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9280 if (err && *err != '\0') {
9281 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9282 file, linenum, err, args[0]);
9283 goto out_err;
9284 }
9285 cur_arg++;
9286#else
9287 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9288 goto out_err;
9289#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009290 } else if (!strcmp(args[0], "set-mark")) {
9291#ifdef SO_MARK
9292 char *err;
9293 rule->action = HTTP_RES_ACT_SET_MARK;
9294 cur_arg = 1;
9295
9296 if (!*args[cur_arg] ||
9297 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9298 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9299 file, linenum, args[0]);
9300 goto out_err;
9301 }
9302
9303 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9304 if (err && *err != '\0') {
9305 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9306 file, linenum, err, args[0]);
9307 goto out_err;
9308 }
9309 cur_arg++;
9310 global.last_checks |= LSTCHK_NETADM;
9311#else
9312 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9313 goto out_err;
9314#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009315 } else if (!strcmp(args[0], "set-log-level")) {
9316 rule->action = HTTP_RES_ACT_SET_LOGL;
9317 cur_arg = 1;
9318
9319 if (!*args[cur_arg] ||
9320 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9321 bad_log_level:
9322 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9323 file, linenum, args[0]);
9324 goto out_err;
9325 }
9326 if (strcmp(args[cur_arg], "silent") == 0)
9327 rule->arg.loglevel = -1;
9328 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9329 goto bad_log_level;
9330 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009331 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9332 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9333 cur_arg = 1;
9334
9335 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9336 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9337 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9338 file, linenum, args[0]);
9339 goto out_err;
9340 }
9341
9342 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9343 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9344 LIST_INIT(&rule->arg.hdr_add.fmt);
9345
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009346 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009347 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009348 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9349 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009350 free(proxy->conf.lfs_file);
9351 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9352 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009353 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009354 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009355 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009356 cur_arg = 1;
9357
9358 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmanna772b942014-08-08 17:29:06 +02009359 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9360 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009361 file, linenum, args[0]);
9362 goto out_err;
9363 }
9364
9365 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9366 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9367 LIST_INIT(&rule->arg.hdr_add.fmt);
9368
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009369 error = NULL;
9370 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9371 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9372 args[cur_arg + 1], error);
9373 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009374 goto out_err;
9375 }
9376
9377 proxy->conf.args.ctx = ARGC_HRQ;
9378 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9379 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9380 file, linenum);
9381
9382 free(proxy->conf.lfs_file);
9383 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9384 proxy->conf.lfs_line = proxy->conf.args.line;
9385 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009386 } else if (strcmp(args[0], "del-header") == 0) {
9387 rule->action = HTTP_RES_ACT_DEL_HDR;
9388 cur_arg = 1;
9389
9390 if (!*args[cur_arg] ||
9391 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9392 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9393 file, linenum, args[0]);
9394 goto out_err;
9395 }
9396
9397 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9398 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9399
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009400 proxy->conf.args.ctx = ARGC_HRS;
9401 free(proxy->conf.lfs_file);
9402 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9403 proxy->conf.lfs_line = proxy->conf.args.line;
9404 cur_arg += 1;
9405 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9406 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9407 rule->action = HTTP_RES_ACT_ADD_ACL;
9408 /*
9409 * '+ 8' for 'add-acl('
9410 * '- 9' for 'add-acl(' + trailing ')'
9411 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009412 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009413
9414 cur_arg = 1;
9415
9416 if (!*args[cur_arg] ||
9417 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9418 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9419 file, linenum, args[0]);
9420 goto out_err;
9421 }
9422
9423 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009424 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009425 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9426 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9427 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009428 free(proxy->conf.lfs_file);
9429 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9430 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009431
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009432 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009433 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9434 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9435 rule->action = HTTP_RES_ACT_DEL_ACL;
9436 /*
9437 * '+ 8' for 'del-acl('
9438 * '- 9' for 'del-acl(' + trailing ')'
9439 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009440 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009441
9442 cur_arg = 1;
9443
9444 if (!*args[cur_arg] ||
9445 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9446 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9447 file, linenum, args[0]);
9448 goto out_err;
9449 }
9450
9451 LIST_INIT(&rule->arg.map.key);
9452 proxy->conf.args.ctx = ARGC_HRS;
9453 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9454 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9455 file, linenum);
9456 free(proxy->conf.lfs_file);
9457 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9458 proxy->conf.lfs_line = proxy->conf.args.line;
9459 cur_arg += 1;
9460 } else if (strncmp(args[0], "del-map", 7) == 0) {
9461 /* http-response del-map(<reference (map name)>) <key pattern> */
9462 rule->action = HTTP_RES_ACT_DEL_MAP;
9463 /*
9464 * '+ 8' for 'del-map('
9465 * '- 9' for 'del-map(' + trailing ')'
9466 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009467 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009468
9469 cur_arg = 1;
9470
9471 if (!*args[cur_arg] ||
9472 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9473 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9474 file, linenum, args[0]);
9475 goto out_err;
9476 }
9477
9478 LIST_INIT(&rule->arg.map.key);
9479 proxy->conf.args.ctx = ARGC_HRS;
9480 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9481 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9482 file, linenum);
9483 free(proxy->conf.lfs_file);
9484 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9485 proxy->conf.lfs_line = proxy->conf.args.line;
9486 cur_arg += 1;
9487 } else if (strncmp(args[0], "set-map", 7) == 0) {
9488 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9489 rule->action = HTTP_RES_ACT_SET_MAP;
9490 /*
9491 * '+ 8' for 'set-map('
9492 * '- 9' for 'set-map(' + trailing ')'
9493 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009494 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009495
9496 cur_arg = 1;
9497
9498 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9499 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9500 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9501 file, linenum, args[0]);
9502 goto out_err;
9503 }
9504
9505 LIST_INIT(&rule->arg.map.key);
9506 LIST_INIT(&rule->arg.map.value);
9507
9508 proxy->conf.args.ctx = ARGC_HRS;
9509
9510 /* key pattern */
9511 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9512 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9513 file, linenum);
9514
9515 /* value pattern */
9516 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9517 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9518 file, linenum);
9519
9520 free(proxy->conf.lfs_file);
9521 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9522 proxy->conf.lfs_line = proxy->conf.args.line;
9523
9524 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009525 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9526 char *errmsg = NULL;
9527 cur_arg = 1;
9528 /* try in the module list */
9529 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9530 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9531 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9532 free(errmsg);
9533 goto out_err;
9534 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009535 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009536 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'del-header', 'set-header', 'replace-header', 'replace-value', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', 'del-acl', 'add-acl', 'del-map', 'set-map', but got '%s'%s.\n",
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009537 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9538 goto out_err;
9539 }
9540
9541 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9542 struct acl_cond *cond;
9543 char *errmsg = NULL;
9544
9545 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9546 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9547 file, linenum, args[0], errmsg);
9548 free(errmsg);
9549 goto out_err;
9550 }
9551 rule->cond = cond;
9552 }
9553 else if (*args[cur_arg]) {
9554 Alert("parsing [%s:%d]: 'http-response %s' expects"
9555 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9556 file, linenum, args[0], args[cur_arg]);
9557 goto out_err;
9558 }
9559
9560 return rule;
9561 out_err:
9562 free(rule);
9563 return NULL;
9564}
9565
Willy Tarreau4baae242012-12-27 12:00:31 +01009566/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009567 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9568 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009569 */
9570struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009571 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009572{
9573 struct redirect_rule *rule;
9574 int cur_arg;
9575 int type = REDIRECT_TYPE_NONE;
9576 int code = 302;
9577 const char *destination = NULL;
9578 const char *cookie = NULL;
9579 int cookie_set = 0;
9580 unsigned int flags = REDIRECT_FLAG_NONE;
9581 struct acl_cond *cond = NULL;
9582
9583 cur_arg = 0;
9584 while (*(args[cur_arg])) {
9585 if (strcmp(args[cur_arg], "location") == 0) {
9586 if (!*args[cur_arg + 1])
9587 goto missing_arg;
9588
9589 type = REDIRECT_TYPE_LOCATION;
9590 cur_arg++;
9591 destination = args[cur_arg];
9592 }
9593 else if (strcmp(args[cur_arg], "prefix") == 0) {
9594 if (!*args[cur_arg + 1])
9595 goto missing_arg;
9596
9597 type = REDIRECT_TYPE_PREFIX;
9598 cur_arg++;
9599 destination = args[cur_arg];
9600 }
9601 else if (strcmp(args[cur_arg], "scheme") == 0) {
9602 if (!*args[cur_arg + 1])
9603 goto missing_arg;
9604
9605 type = REDIRECT_TYPE_SCHEME;
9606 cur_arg++;
9607 destination = args[cur_arg];
9608 }
9609 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9610 if (!*args[cur_arg + 1])
9611 goto missing_arg;
9612
9613 cur_arg++;
9614 cookie = args[cur_arg];
9615 cookie_set = 1;
9616 }
9617 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9618 if (!*args[cur_arg + 1])
9619 goto missing_arg;
9620
9621 cur_arg++;
9622 cookie = args[cur_arg];
9623 cookie_set = 0;
9624 }
9625 else if (strcmp(args[cur_arg], "code") == 0) {
9626 if (!*args[cur_arg + 1])
9627 goto missing_arg;
9628
9629 cur_arg++;
9630 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009631 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009632 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009633 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009634 args[cur_arg - 1], args[cur_arg]);
9635 return NULL;
9636 }
9637 }
9638 else if (!strcmp(args[cur_arg],"drop-query")) {
9639 flags |= REDIRECT_FLAG_DROP_QS;
9640 }
9641 else if (!strcmp(args[cur_arg],"append-slash")) {
9642 flags |= REDIRECT_FLAG_APPEND_SLASH;
9643 }
9644 else if (strcmp(args[cur_arg], "if") == 0 ||
9645 strcmp(args[cur_arg], "unless") == 0) {
9646 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9647 if (!cond) {
9648 memprintf(errmsg, "error in condition: %s", *errmsg);
9649 return NULL;
9650 }
9651 break;
9652 }
9653 else {
9654 memprintf(errmsg,
9655 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9656 args[cur_arg]);
9657 return NULL;
9658 }
9659 cur_arg++;
9660 }
9661
9662 if (type == REDIRECT_TYPE_NONE) {
9663 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9664 return NULL;
9665 }
9666
9667 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9668 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009669 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009670
9671 if (!use_fmt) {
9672 /* old-style static redirect rule */
9673 rule->rdr_str = strdup(destination);
9674 rule->rdr_len = strlen(destination);
9675 }
9676 else {
9677 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009678
9679 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9680 * if prefix == "/", we don't want to add anything, otherwise it
9681 * makes it hard for the user to configure a self-redirection.
9682 */
Godbach543b9782014-12-18 15:44:58 +08009683 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009684 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009685 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009686 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9687 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009688 free(curproxy->conf.lfs_file);
9689 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9690 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009691 }
9692 }
9693
Willy Tarreau4baae242012-12-27 12:00:31 +01009694 if (cookie) {
9695 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9696 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9697 */
9698 rule->cookie_len = strlen(cookie);
9699 if (cookie_set) {
9700 rule->cookie_str = malloc(rule->cookie_len + 10);
9701 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9702 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9703 rule->cookie_len += 9;
9704 } else {
9705 rule->cookie_str = malloc(rule->cookie_len + 21);
9706 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9707 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9708 rule->cookie_len += 20;
9709 }
9710 }
9711 rule->type = type;
9712 rule->code = code;
9713 rule->flags = flags;
9714 LIST_INIT(&rule->list);
9715 return rule;
9716
9717 missing_arg:
9718 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9719 return NULL;
9720}
9721
Willy Tarreau8797c062007-05-07 00:55:35 +02009722/************************************************************************/
9723/* The code below is dedicated to ACL parsing and matching */
9724/************************************************************************/
9725
9726
Willy Tarreau14174bc2012-04-16 14:34:04 +02009727/* This function ensures that the prerequisites for an L7 fetch are ready,
9728 * which means that a request or response is ready. If some data is missing,
9729 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009730 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9731 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009732 *
9733 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009734 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9735 * decide whether or not an HTTP message is present ;
9736 * 0 if the requested data cannot be fetched or if it is certain that
9737 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009738 * 1 if an HTTP message is ready
9739 */
9740static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009741smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009742 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009743{
9744 struct http_txn *txn = l7;
9745 struct http_msg *msg = &txn->req;
9746
9747 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9748 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9749 */
9750
9751 if (unlikely(!s || !txn))
9752 return 0;
9753
9754 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009755 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009756
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009757 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009758 if (unlikely(!s->req))
9759 return 0;
9760
Willy Tarreauaae75e32013-03-29 12:31:49 +01009761 /* If the buffer does not leave enough free space at the end,
9762 * we must first realign it.
9763 */
9764 if (s->req->buf->p > s->req->buf->data &&
9765 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9766 buffer_slow_realign(s->req->buf);
9767
Willy Tarreau14174bc2012-04-16 14:34:04 +02009768 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009769 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009770 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009771
9772 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009773 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009774 http_msg_analyzer(msg, &txn->hdr_idx);
9775
9776 /* Still no valid request ? */
9777 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009778 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009779 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009780 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009781 }
9782 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009783 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009784 return 0;
9785 }
9786
9787 /* OK we just got a valid HTTP request. We have some minor
9788 * preparation to perform so that further checks can rely
9789 * on HTTP tests.
9790 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009791
9792 /* If the request was parsed but was too large, we must absolutely
9793 * return an error so that it is not processed. At the moment this
9794 * cannot happen, but if the parsers are to change in the future,
9795 * we want this check to be maintained.
9796 */
9797 if (unlikely(s->req->buf->i + s->req->buf->p >
9798 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9799 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009800 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009801 return 1;
9802 }
9803
Willy Tarreau9b28e032012-10-12 23:49:43 +02009804 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009805 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9806 s->flags |= SN_REDIRECTABLE;
9807
Willy Tarreau506d0502013-07-06 13:29:24 +02009808 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9809 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009810 }
9811
Willy Tarreau506d0502013-07-06 13:29:24 +02009812 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009813 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009814 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009815
9816 /* otherwise everything's ready for the request */
9817 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009818 else {
9819 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009820 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9821 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009822 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009823 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009824 }
9825
9826 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009827 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009828 return 1;
9829}
Willy Tarreau8797c062007-05-07 00:55:35 +02009830
Willy Tarreaua4ba9db2014-06-25 16:56:41 +02009831/* Note: these functinos *do* modify the sample. Even in case of success, at
9832 * least the type and uint value are modified.
9833 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009834#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009835 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 +02009836
Willy Tarreau24e32d82012-04-23 23:55:44 +02009837#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009838 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 +02009839
Willy Tarreau8797c062007-05-07 00:55:35 +02009840
9841/* 1. Check on METHOD
9842 * We use the pre-parsed method if it is known, and store its number as an
9843 * integer. If it is unknown, we use the pointer and the length.
9844 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009845static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009846{
9847 int len, meth;
9848
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009849 len = strlen(text);
9850 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009851
9852 pattern->val.i = meth;
9853 if (meth == HTTP_METH_OTHER) {
Willy Tarreau71196f32014-08-29 15:15:50 +02009854 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009855 pattern->len = len;
9856 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009857 else {
9858 pattern->ptr.str = NULL;
9859 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009860 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009861 return 1;
9862}
9863
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009864/* This function fetches the method of current HTTP request and stores
9865 * it in the global pattern struct as a chunk. There are two possibilities :
9866 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9867 * in <len> and <ptr> is NULL ;
9868 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9869 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009870 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009871 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009872static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009873smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009874 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009875{
9876 int meth;
9877 struct http_txn *txn = l7;
9878
Willy Tarreau24e32d82012-04-23 23:55:44 +02009879 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009880
Willy Tarreau8797c062007-05-07 00:55:35 +02009881 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009882 smp->type = SMP_T_METH;
9883 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009884 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009885 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9886 /* ensure the indexes are not affected */
9887 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009888 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009889 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9890 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009891 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009892 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009893 return 1;
9894}
9895
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009896/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009897static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009898{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009899 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009900 struct pattern_list *lst;
9901 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009902
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009903 list_for_each_entry(lst, &expr->patterns, list) {
9904 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009905
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009906 /* well-known method */
9907 if (pattern->val.i != HTTP_METH_OTHER) {
9908 if (smp->data.meth.meth == pattern->val.i)
9909 return pattern;
9910 else
9911 continue;
9912 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009913
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009914 /* Other method, we must compare the strings */
9915 if (pattern->len != smp->data.meth.str.len)
9916 continue;
9917
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009918 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreauc62a0c62014-08-28 20:42:57 +02009919 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
9920 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009921 return pattern;
9922 }
9923 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009924}
9925
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009926static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009927smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009928 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009929{
9930 struct http_txn *txn = l7;
9931 char *ptr;
9932 int len;
9933
Willy Tarreauc0239e02012-04-16 14:42:55 +02009934 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009935
Willy Tarreau8797c062007-05-07 00:55:35 +02009936 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009937 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009938
9939 while ((len-- > 0) && (*ptr++ != '/'));
9940 if (len <= 0)
9941 return 0;
9942
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009943 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009944 smp->data.str.str = ptr;
9945 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009946
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009947 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009948 return 1;
9949}
9950
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009951static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009952smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009953 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009954{
9955 struct http_txn *txn = l7;
9956 char *ptr;
9957 int len;
9958
Willy Tarreauc0239e02012-04-16 14:42:55 +02009959 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009960
Willy Tarreauf26b2522012-12-14 08:33:14 +01009961 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9962 return 0;
9963
Willy Tarreau8797c062007-05-07 00:55:35 +02009964 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009965 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009966
9967 while ((len-- > 0) && (*ptr++ != '/'));
9968 if (len <= 0)
9969 return 0;
9970
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009971 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009972 smp->data.str.str = ptr;
9973 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009974
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009975 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009976 return 1;
9977}
9978
9979/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009980static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009981smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009982 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009983{
9984 struct http_txn *txn = l7;
9985 char *ptr;
9986 int len;
9987
Willy Tarreauc0239e02012-04-16 14:42:55 +02009988 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009989
Willy Tarreauf26b2522012-12-14 08:33:14 +01009990 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9991 return 0;
9992
Willy Tarreau8797c062007-05-07 00:55:35 +02009993 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009994 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009995
Willy Tarreauf853c462012-04-23 18:53:56 +02009996 smp->type = SMP_T_UINT;
9997 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009998 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009999 return 1;
10000}
10001
10002/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010003static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010004smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010005 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +020010006{
10007 struct http_txn *txn = l7;
10008
Willy Tarreauc0239e02012-04-16 14:42:55 +020010009 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010010
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010011 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010012 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010013 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010014 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010015 return 1;
10016}
10017
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010018static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010019smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010020 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010021{
10022 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010023 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010024
Willy Tarreauc0239e02012-04-16 14:42:55 +020010025 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010026
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010027 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 +020010028 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010029 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010030
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010031 smp->type = SMP_T_IPV4;
10032 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010033 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010034 return 1;
10035}
10036
10037static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010038smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010039 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010040{
10041 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010042 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010043
Willy Tarreauc0239e02012-04-16 14:42:55 +020010044 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010045
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010046 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 +020010047 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10048 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010049
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010050 smp->type = SMP_T_UINT;
10051 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010052 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010053 return 1;
10054}
10055
Willy Tarreau185b5c42012-04-26 15:11:51 +020010056/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10057 * Accepts an optional argument of type string containing the header field name,
10058 * and an optional argument of type signed or unsigned integer to request an
10059 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010060 * headers are considered from the first one. It does not stop on commas and
10061 * returns full lines instead (useful for User-Agent or Date for example).
10062 */
10063static int
10064smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010065 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010066{
10067 struct http_txn *txn = l7;
10068 struct hdr_idx *idx = &txn->hdr_idx;
10069 struct hdr_ctx *ctx = smp->ctx.a[0];
10070 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10071 int occ = 0;
10072 const char *name_str = NULL;
10073 int name_len = 0;
10074
10075 if (!ctx) {
10076 /* first call */
10077 ctx = &static_hdr_ctx;
10078 ctx->idx = 0;
10079 smp->ctx.a[0] = ctx;
10080 }
10081
10082 if (args) {
10083 if (args[0].type != ARGT_STR)
10084 return 0;
10085 name_str = args[0].data.str.str;
10086 name_len = args[0].data.str.len;
10087
10088 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10089 occ = args[1].data.uint;
10090 }
10091
10092 CHECK_HTTP_MESSAGE_FIRST();
10093
10094 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10095 /* search for header from the beginning */
10096 ctx->idx = 0;
10097
10098 if (!occ && !(opt & SMP_OPT_ITERATE))
10099 /* no explicit occurrence and single fetch => last header by default */
10100 occ = -1;
10101
10102 if (!occ)
10103 /* prepare to report multiple occurrences for ACL fetches */
10104 smp->flags |= SMP_F_NOT_LAST;
10105
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010106 smp->type = SMP_T_STR;
10107 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010108 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10109 return 1;
10110
10111 smp->flags &= ~SMP_F_NOT_LAST;
10112 return 0;
10113}
10114
10115/* 6. Check on HTTP header count. The number of occurrences is returned.
10116 * Accepts exactly 1 argument of type string. It does not stop on commas and
10117 * returns full lines instead (useful for User-Agent or Date for example).
10118 */
10119static int
10120smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010121 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010122{
10123 struct http_txn *txn = l7;
10124 struct hdr_idx *idx = &txn->hdr_idx;
10125 struct hdr_ctx ctx;
10126 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10127 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010128 const char *name = NULL;
10129 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010130
Willy Tarreau29437342015-04-01 19:16:09 +020010131 if (args && args->type == ARGT_STR) {
10132 name = args->data.str.str;
10133 len = args->data.str.len;
10134 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010135
10136 CHECK_HTTP_MESSAGE_FIRST();
10137
10138 ctx.idx = 0;
10139 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010140 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010141 cnt++;
10142
10143 smp->type = SMP_T_UINT;
10144 smp->data.uint = cnt;
10145 smp->flags = SMP_F_VOL_HDR;
10146 return 1;
10147}
10148
10149/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10150 * Accepts an optional argument of type string containing the header field name,
10151 * and an optional argument of type signed or unsigned integer to request an
10152 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010153 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010154 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010155static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010156smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010157 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010158{
10159 struct http_txn *txn = l7;
10160 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010161 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010162 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010163 int occ = 0;
10164 const char *name_str = NULL;
10165 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010166
Willy Tarreaua890d072013-04-02 12:01:06 +020010167 if (!ctx) {
10168 /* first call */
10169 ctx = &static_hdr_ctx;
10170 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010171 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010172 }
10173
Willy Tarreau185b5c42012-04-26 15:11:51 +020010174 if (args) {
10175 if (args[0].type != ARGT_STR)
10176 return 0;
10177 name_str = args[0].data.str.str;
10178 name_len = args[0].data.str.len;
10179
10180 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10181 occ = args[1].data.uint;
10182 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010183
Willy Tarreaue333ec92012-04-16 16:26:40 +020010184 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010185
Willy Tarreau185b5c42012-04-26 15:11:51 +020010186 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010187 /* search for header from the beginning */
10188 ctx->idx = 0;
10189
Willy Tarreau185b5c42012-04-26 15:11:51 +020010190 if (!occ && !(opt & SMP_OPT_ITERATE))
10191 /* no explicit occurrence and single fetch => last header by default */
10192 occ = -1;
10193
10194 if (!occ)
10195 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010196 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010197
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010198 smp->type = SMP_T_STR;
10199 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010200 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 +020010201 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010202
Willy Tarreau37406352012-04-23 16:16:37 +020010203 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010204 return 0;
10205}
10206
Willy Tarreauc11416f2007-06-17 16:58:38 +020010207/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010208 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010209 */
10210static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010211smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010212 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010213{
10214 struct http_txn *txn = l7;
10215 struct hdr_idx *idx = &txn->hdr_idx;
10216 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010217 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010218 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010219 const char *name = NULL;
10220 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010221
Willy Tarreau29437342015-04-01 19:16:09 +020010222 if (args && args->type == ARGT_STR) {
10223 name = args->data.str.str;
10224 len = args->data.str.len;
10225 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010226
Willy Tarreaue333ec92012-04-16 16:26:40 +020010227 CHECK_HTTP_MESSAGE_FIRST();
10228
Willy Tarreau33a7e692007-06-10 19:45:56 +020010229 ctx.idx = 0;
10230 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010231 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010232 cnt++;
10233
Willy Tarreauf853c462012-04-23 18:53:56 +020010234 smp->type = SMP_T_UINT;
10235 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010236 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010237 return 1;
10238}
10239
Willy Tarreau185b5c42012-04-26 15:11:51 +020010240/* Fetch an HTTP header's integer value. The integer value is returned. It
10241 * takes a mandatory argument of type string and an optional one of type int
10242 * to designate a specific occurrence. It returns an unsigned integer, which
10243 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010244 */
10245static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010246smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010247 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010248{
Willy Tarreauef38c392013-07-22 16:29:32 +020010249 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010250
Willy Tarreauf853c462012-04-23 18:53:56 +020010251 if (ret > 0) {
10252 smp->type = SMP_T_UINT;
10253 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10254 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010255
Willy Tarreaud53e2422012-04-16 17:21:11 +020010256 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010257}
10258
Cyril Bonté69fa9922012-10-25 00:01:06 +020010259/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10260 * and an optional one of type int to designate a specific occurrence.
10261 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010262 */
10263static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010264smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010265 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +020010266{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010267 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010268
Willy Tarreauef38c392013-07-22 16:29:32 +020010269 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010270 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10271 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010272 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010273 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010274 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010275 if (smp->data.str.len < temp->size - 1) {
10276 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10277 temp->str[smp->data.str.len] = '\0';
10278 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10279 smp->type = SMP_T_IPV6;
10280 break;
10281 }
10282 }
10283 }
10284
Willy Tarreaud53e2422012-04-16 17:21:11 +020010285 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010286 if (!(smp->flags & SMP_F_NOT_LAST))
10287 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010288 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010289 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010290}
10291
Willy Tarreau737b0c12007-06-10 21:28:46 +020010292/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10293 * the first '/' after the possible hostname, and ends before the possible '?'.
10294 */
10295static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010296smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010297 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010298{
10299 struct http_txn *txn = l7;
10300 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010301
Willy Tarreauc0239e02012-04-16 14:42:55 +020010302 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010303
Willy Tarreau9b28e032012-10-12 23:49:43 +020010304 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010305 ptr = http_get_path(txn);
10306 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010307 return 0;
10308
10309 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010310 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010311 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010312
10313 while (ptr < end && *ptr != '?')
10314 ptr++;
10315
Willy Tarreauf853c462012-04-23 18:53:56 +020010316 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010317 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010318 return 1;
10319}
10320
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010321/* This produces a concatenation of the first occurrence of the Host header
10322 * followed by the path component if it begins with a slash ('/'). This means
10323 * that '*' will not be added, resulting in exactly the first Host entry.
10324 * If no Host header is found, then the path is returned as-is. The returned
10325 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010326 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010327 */
10328static int
10329smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010330 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010331{
10332 struct http_txn *txn = l7;
10333 char *ptr, *end, *beg;
10334 struct hdr_ctx ctx;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010335 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010336
10337 CHECK_HTTP_MESSAGE_FIRST();
10338
10339 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010340 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +020010341 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010342
10343 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010344 temp = get_trash_chunk();
10345 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010346 smp->type = SMP_T_STR;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010347 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010348 smp->data.str.len = ctx.vlen;
10349
10350 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010351 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010352 beg = http_get_path(txn);
10353 if (!beg)
10354 beg = end;
10355
10356 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10357
10358 if (beg < ptr && *beg == '/') {
10359 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10360 smp->data.str.len += ptr - beg;
10361 }
10362
10363 smp->flags = SMP_F_VOL_1ST;
10364 return 1;
10365}
10366
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010367/* This produces a 32-bit hash of the concatenation of the first occurrence of
10368 * the Host header followed by the path component if it begins with a slash ('/').
10369 * This means that '*' will not be added, resulting in exactly the first Host
10370 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010371 * is hashed using the path hash followed by a full avalanche hash and provides a
10372 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010373 * high-traffic sites without having to store whole paths.
10374 */
10375static int
10376smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010377 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010378{
10379 struct http_txn *txn = l7;
10380 struct hdr_ctx ctx;
10381 unsigned int hash = 0;
10382 char *ptr, *beg, *end;
10383 int len;
10384
10385 CHECK_HTTP_MESSAGE_FIRST();
10386
10387 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010388 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010389 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10390 ptr = ctx.line + ctx.val;
10391 len = ctx.vlen;
10392 while (len--)
10393 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10394 }
10395
10396 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010397 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010398 beg = http_get_path(txn);
10399 if (!beg)
10400 beg = end;
10401
10402 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10403
10404 if (beg < ptr && *beg == '/') {
10405 while (beg < ptr)
10406 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10407 }
10408 hash = full_hash(hash);
10409
10410 smp->type = SMP_T_UINT;
10411 smp->data.uint = hash;
10412 smp->flags = SMP_F_VOL_1ST;
10413 return 1;
10414}
10415
Willy Tarreau4a550602012-12-09 14:53:32 +010010416/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010417 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10418 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10419 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010420 * that in environments where IPv6 is insignificant, truncating the output to
10421 * 8 bytes would still work.
10422 */
10423static int
10424smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010425 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +010010426{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010427 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010428 struct connection *cli_conn = objt_conn(l4->si[0].end);
10429
10430 if (!cli_conn)
10431 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010432
Willy Tarreauef38c392013-07-22 16:29:32 +020010433 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +010010434 return 0;
10435
Willy Tarreau47ca5452012-12-23 20:22:19 +010010436 temp = get_trash_chunk();
Willy Tarreau0dff81c2014-07-15 21:34:06 +020010437 *(unsigned int *)temp->str = htonl(smp->data.uint);
10438 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010439
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010440 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010441 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010442 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010443 temp->len += 4;
10444 break;
10445 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010446 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010447 temp->len += 16;
10448 break;
10449 default:
10450 return 0;
10451 }
10452
10453 smp->data.str = *temp;
10454 smp->type = SMP_T_BIN;
10455 return 1;
10456}
10457
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010458static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010459smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010460 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010461{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010462 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10463 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10464 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010465
Willy Tarreau24e32d82012-04-23 23:55:44 +020010466 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010467
Willy Tarreauf853c462012-04-23 18:53:56 +020010468 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010469 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010470 return 1;
10471}
10472
Willy Tarreau7f18e522010-10-22 20:04:13 +020010473/* return a valid test if the current request is the first one on the connection */
10474static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010475smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010476 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010477{
10478 if (!s)
10479 return 0;
10480
Willy Tarreauf853c462012-04-23 18:53:56 +020010481 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010482 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010483 return 1;
10484}
10485
Willy Tarreau34db1082012-04-19 17:16:54 +020010486/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010487static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010488smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010489 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010490{
10491
Willy Tarreau24e32d82012-04-23 23:55:44 +020010492 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010493 return 0;
10494
Willy Tarreauc0239e02012-04-16 14:42:55 +020010495 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010496
Willy Tarreauc0239e02012-04-16 14:42:55 +020010497 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010498 return 0;
10499
Willy Tarreauf853c462012-04-23 18:53:56 +020010500 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010501 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010502 return 1;
10503}
Willy Tarreau8797c062007-05-07 00:55:35 +020010504
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010505/* Accepts exactly 1 argument of type userlist */
10506static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010507smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010508 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010509{
10510
10511 if (!args || args->type != ARGT_USR)
10512 return 0;
10513
10514 CHECK_HTTP_MESSAGE_FIRST();
10515
10516 if (!get_http_auth(l4))
10517 return 0;
10518
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010519 /* if the user does not belong to the userlist or has a wrong password,
10520 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010521 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010522 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010523 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10524 return 0;
10525
10526 /* pat_match_auth() will need the user list */
10527 smp->ctx.a[0] = args->data.usr;
10528
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010529 smp->type = SMP_T_STR;
10530 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010531 smp->data.str.str = l4->txn.auth.user;
10532 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010533
10534 return 1;
10535}
10536
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010537/* Try to find the next occurrence of a cookie name in a cookie header value.
10538 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10539 * the cookie value is returned into *value and *value_l, and the function
10540 * returns a pointer to the next pointer to search from if the value was found.
10541 * Otherwise if the cookie was not found, NULL is returned and neither value
10542 * nor value_l are touched. The input <hdr> string should first point to the
10543 * header's value, and the <hdr_end> pointer must point to the first character
10544 * not part of the value. <list> must be non-zero if value may represent a list
10545 * of values (cookie headers). This makes it faster to abort parsing when no
10546 * list is expected.
10547 */
10548static char *
10549extract_cookie_value(char *hdr, const char *hdr_end,
10550 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010551 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010552{
10553 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10554 char *next;
10555
10556 /* we search at least a cookie name followed by an equal, and more
10557 * generally something like this :
10558 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10559 */
10560 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10561 /* Iterate through all cookies on this line */
10562
10563 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10564 att_beg++;
10565
10566 /* find att_end : this is the first character after the last non
10567 * space before the equal. It may be equal to hdr_end.
10568 */
10569 equal = att_end = att_beg;
10570
10571 while (equal < hdr_end) {
10572 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10573 break;
10574 if (http_is_spht[(unsigned char)*equal++])
10575 continue;
10576 att_end = equal;
10577 }
10578
10579 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10580 * is between <att_beg> and <equal>, both may be identical.
10581 */
10582
10583 /* look for end of cookie if there is an equal sign */
10584 if (equal < hdr_end && *equal == '=') {
10585 /* look for the beginning of the value */
10586 val_beg = equal + 1;
10587 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10588 val_beg++;
10589
10590 /* find the end of the value, respecting quotes */
10591 next = find_cookie_value_end(val_beg, hdr_end);
10592
10593 /* make val_end point to the first white space or delimitor after the value */
10594 val_end = next;
10595 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10596 val_end--;
10597 } else {
10598 val_beg = val_end = next = equal;
10599 }
10600
10601 /* We have nothing to do with attributes beginning with '$'. However,
10602 * they will automatically be removed if a header before them is removed,
10603 * since they're supposed to be linked together.
10604 */
10605 if (*att_beg == '$')
10606 continue;
10607
10608 /* Ignore cookies with no equal sign */
10609 if (equal == next)
10610 continue;
10611
10612 /* Now we have the cookie name between att_beg and att_end, and
10613 * its value between val_beg and val_end.
10614 */
10615
10616 if (att_end - att_beg == cookie_name_l &&
10617 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10618 /* let's return this value and indicate where to go on from */
10619 *value = val_beg;
10620 *value_l = val_end - val_beg;
10621 return next + 1;
10622 }
10623
10624 /* Set-Cookie headers only have the name in the first attr=value part */
10625 if (!list)
10626 break;
10627 }
10628
10629 return NULL;
10630}
10631
William Lallemanda43ba4e2014-01-28 18:14:25 +010010632/* Fetch a captured HTTP request header. The index is the position of
10633 * the "capture" option in the configuration file
10634 */
10635static int
10636smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10637 const struct arg *args, struct sample *smp, const char *kw)
10638{
10639 struct proxy *fe = l4->fe;
10640 struct http_txn *txn = l7;
10641 int idx;
10642
10643 if (!args || args->type != ARGT_UINT)
10644 return 0;
10645
10646 idx = args->data.uint;
10647
10648 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10649 return 0;
10650
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010651 smp->type = SMP_T_STR;
10652 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010653 smp->data.str.str = txn->req.cap[idx];
10654 smp->data.str.len = strlen(txn->req.cap[idx]);
10655
10656 return 1;
10657}
10658
10659/* Fetch a captured HTTP response header. The index is the position of
10660 * the "capture" option in the configuration file
10661 */
10662static int
10663smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10664 const struct arg *args, struct sample *smp, const char *kw)
10665{
10666 struct proxy *fe = l4->fe;
10667 struct http_txn *txn = l7;
10668 int idx;
10669
10670 if (!args || args->type != ARGT_UINT)
10671 return 0;
10672
10673 idx = args->data.uint;
10674
10675 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10676 return 0;
10677
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010678 smp->type = SMP_T_STR;
10679 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010680 smp->data.str.str = txn->rsp.cap[idx];
10681 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10682
10683 return 1;
10684}
10685
William Lallemand65ad6e12014-01-31 15:08:02 +010010686/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10687static int
10688smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10689 const struct arg *args, struct sample *smp, const char *kw)
10690{
10691 struct chunk *temp;
10692 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010693 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010694
10695 if (!txn->uri)
10696 return 0;
10697
William Lallemand96a77852014-02-05 00:30:02 +010010698 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010699
William Lallemand96a77852014-02-05 00:30:02 +010010700 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10701 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010702
William Lallemand96a77852014-02-05 00:30:02 +010010703 temp = get_trash_chunk();
10704 temp->str = txn->uri;
10705 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010706 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010707 smp->type = SMP_T_STR;
10708 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010709
10710 return 1;
10711
10712}
10713
10714/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10715static int
10716smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10717 const struct arg *args, struct sample *smp, const char *kw)
10718{
10719 struct chunk *temp;
10720 struct http_txn *txn = l7;
10721 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010722
10723 if (!txn->uri)
10724 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010725
William Lallemand65ad6e12014-01-31 15:08:02 +010010726 ptr = txn->uri;
10727
10728 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10729 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010730
William Lallemand65ad6e12014-01-31 15:08:02 +010010731 if (!*ptr)
10732 return 0;
10733
10734 ptr++; /* skip the space */
10735
10736 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010737 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010738 if (!ptr)
10739 return 0;
10740 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10741 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010742
10743 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010744 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010745 smp->type = SMP_T_STR;
10746 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010747
10748 return 1;
10749}
10750
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010751/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10752 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10753 */
10754static int
10755smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10756 const struct arg *args, struct sample *smp, const char *kw)
10757{
10758 struct http_txn *txn = l7;
10759
10760 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10761 return 0;
10762
10763 if (txn->req.flags & HTTP_MSGF_VER_11)
10764 smp->data.str.str = "HTTP/1.1";
10765 else
10766 smp->data.str.str = "HTTP/1.0";
10767
10768 smp->data.str.len = 8;
10769 smp->type = SMP_T_STR;
10770 smp->flags = SMP_F_CONST;
10771 return 1;
10772
10773}
10774
10775/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10776 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10777 */
10778static int
10779smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10780 const struct arg *args, struct sample *smp, const char *kw)
10781{
10782 struct http_txn *txn = l7;
10783
10784 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10785 return 0;
10786
10787 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10788 smp->data.str.str = "HTTP/1.1";
10789 else
10790 smp->data.str.str = "HTTP/1.0";
10791
10792 smp->data.str.len = 8;
10793 smp->type = SMP_T_STR;
10794 smp->flags = SMP_F_CONST;
10795 return 1;
10796
10797}
10798
William Lallemand65ad6e12014-01-31 15:08:02 +010010799
Willy Tarreaue333ec92012-04-16 16:26:40 +020010800/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010801 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010802 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010803 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010804 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010805 * Accepts exactly 1 argument of type string. If the input options indicate
10806 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010807 * The returned sample is of type CSTR. Can be used to parse cookies in other
10808 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010809 */
William Lallemand07c8b242014-05-02 17:11:07 +020010810int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010811 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010812{
10813 struct http_txn *txn = l7;
10814 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010815 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010816 const struct http_msg *msg;
10817 const char *hdr_name;
10818 int hdr_name_len;
10819 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010820 int occ = 0;
10821 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010822
Willy Tarreau24e32d82012-04-23 23:55:44 +020010823 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010824 return 0;
10825
Willy Tarreaua890d072013-04-02 12:01:06 +020010826 if (!ctx) {
10827 /* first call */
10828 ctx = &static_hdr_ctx;
10829 ctx->idx = 0;
10830 smp->ctx.a[2] = ctx;
10831 }
10832
Willy Tarreaue333ec92012-04-16 16:26:40 +020010833 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010834
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010835 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010836 msg = &txn->req;
10837 hdr_name = "Cookie";
10838 hdr_name_len = 6;
10839 } else {
10840 msg = &txn->rsp;
10841 hdr_name = "Set-Cookie";
10842 hdr_name_len = 10;
10843 }
10844
Willy Tarreau28376d62012-04-26 21:26:10 +020010845 if (!occ && !(opt & SMP_OPT_ITERATE))
10846 /* no explicit occurrence and single fetch => last cookie by default */
10847 occ = -1;
10848
10849 /* OK so basically here, either we want only one value and it's the
10850 * last one, or we want to iterate over all of them and we fetch the
10851 * next one.
10852 */
10853
Willy Tarreau9b28e032012-10-12 23:49:43 +020010854 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010855 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010856 /* search for the header from the beginning, we must first initialize
10857 * the search parameters.
10858 */
Willy Tarreau37406352012-04-23 16:16:37 +020010859 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010860 ctx->idx = 0;
10861 }
10862
Willy Tarreau28376d62012-04-26 21:26:10 +020010863 smp->flags |= SMP_F_VOL_HDR;
10864
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010865 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010866 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10867 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010868 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10869 goto out;
10870
Willy Tarreau24e32d82012-04-23 23:55:44 +020010871 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010872 continue;
10873
Willy Tarreau37406352012-04-23 16:16:37 +020010874 smp->ctx.a[0] = ctx->line + ctx->val;
10875 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010876 }
10877
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010878 smp->type = SMP_T_STR;
10879 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010880 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010881 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010882 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010883 &smp->data.str.str,
10884 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010885 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010886 found = 1;
10887 if (occ >= 0) {
10888 /* one value was returned into smp->data.str.{str,len} */
10889 smp->flags |= SMP_F_NOT_LAST;
10890 return 1;
10891 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010892 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010893 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010894 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010895 /* all cookie headers and values were scanned. If we're looking for the
10896 * last occurrence, we may return it now.
10897 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010898 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010899 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010900 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010901}
10902
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010903/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010904 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010905 * multiple cookies may be parsed on the same line. The returned sample is of
10906 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010907 */
10908static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010909smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010910 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010911{
10912 struct http_txn *txn = l7;
10913 struct hdr_idx *idx = &txn->hdr_idx;
10914 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010915 const struct http_msg *msg;
10916 const char *hdr_name;
10917 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010918 int cnt;
10919 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010920 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010921
Willy Tarreau24e32d82012-04-23 23:55:44 +020010922 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010923 return 0;
10924
Willy Tarreaue333ec92012-04-16 16:26:40 +020010925 CHECK_HTTP_MESSAGE_FIRST();
10926
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010927 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010928 msg = &txn->req;
10929 hdr_name = "Cookie";
10930 hdr_name_len = 6;
10931 } else {
10932 msg = &txn->rsp;
10933 hdr_name = "Set-Cookie";
10934 hdr_name_len = 10;
10935 }
10936
Willy Tarreau9b28e032012-10-12 23:49:43 +020010937 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010938 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010939 ctx.idx = 0;
10940 cnt = 0;
10941
10942 while (1) {
10943 /* Note: val_beg == NULL every time we need to fetch a new header */
10944 if (!val_beg) {
10945 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10946 break;
10947
Willy Tarreau24e32d82012-04-23 23:55:44 +020010948 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010949 continue;
10950
10951 val_beg = ctx.line + ctx.val;
10952 val_end = val_beg + ctx.vlen;
10953 }
10954
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010955 smp->type = SMP_T_STR;
10956 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010957 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010958 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010959 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010960 &smp->data.str.str,
10961 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010962 cnt++;
10963 }
10964 }
10965
Willy Tarreaub169eba2013-12-16 15:14:43 +010010966 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010967 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010968 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010969 return 1;
10970}
10971
Willy Tarreau51539362012-05-08 12:46:28 +020010972/* Fetch an cookie's integer value. The integer value is returned. It
10973 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10974 */
10975static int
10976smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010977 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010978{
Willy Tarreauef38c392013-07-22 16:29:32 +020010979 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010980
10981 if (ret > 0) {
10982 smp->type = SMP_T_UINT;
10983 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10984 }
10985
10986 return ret;
10987}
10988
Willy Tarreau8797c062007-05-07 00:55:35 +020010989/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010990/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010991/************************************************************************/
10992
David Cournapeau16023ee2010-12-23 20:55:41 +090010993/*
10994 * Given a path string and its length, find the position of beginning of the
10995 * query string. Returns NULL if no query string is found in the path.
10996 *
10997 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10998 *
10999 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11000 */
bedis4c75cca2012-10-05 08:38:24 +020011001static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011002{
11003 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011004
bedis4c75cca2012-10-05 08:38:24 +020011005 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011006 return p ? p + 1 : NULL;
11007}
11008
bedis4c75cca2012-10-05 08:38:24 +020011009static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011010{
bedis4c75cca2012-10-05 08:38:24 +020011011 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011012}
11013
11014/*
11015 * Given a url parameter, find the starting position of the first occurence,
11016 * or NULL if the parameter is not found.
11017 *
11018 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11019 * the function will return query_string+8.
11020 */
11021static char*
11022find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011023 char* url_param_name, size_t url_param_name_l,
11024 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011025{
11026 char *pos, *last;
11027
11028 pos = query_string;
11029 last = query_string + query_string_l - url_param_name_l - 1;
11030
11031 while (pos <= last) {
11032 if (pos[url_param_name_l] == '=') {
11033 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11034 return pos;
11035 pos += url_param_name_l + 1;
11036 }
bedis4c75cca2012-10-05 08:38:24 +020011037 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011038 pos++;
11039 pos++;
11040 }
11041 return NULL;
11042}
11043
11044/*
11045 * Given a url parameter name, returns its value and size into *value and
11046 * *value_l respectively, and returns non-zero. If the parameter is not found,
11047 * zero is returned and value/value_l are not touched.
11048 */
11049static int
11050find_url_param_value(char* path, size_t path_l,
11051 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011052 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011053{
11054 char *query_string, *qs_end;
11055 char *arg_start;
11056 char *value_start, *value_end;
11057
bedis4c75cca2012-10-05 08:38:24 +020011058 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011059 if (!query_string)
11060 return 0;
11061
11062 qs_end = path + path_l;
11063 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011064 url_param_name, url_param_name_l,
11065 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011066 if (!arg_start)
11067 return 0;
11068
11069 value_start = arg_start + url_param_name_l + 1;
11070 value_end = value_start;
11071
bedis4c75cca2012-10-05 08:38:24 +020011072 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011073 value_end++;
11074
11075 *value = value_start;
11076 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011077 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011078}
11079
11080static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011081smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011082 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090011083{
bedis4c75cca2012-10-05 08:38:24 +020011084 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090011085 struct http_txn *txn = l7;
11086 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011087
bedis4c75cca2012-10-05 08:38:24 +020011088 if (!args || args[0].type != ARGT_STR ||
11089 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011090 return 0;
11091
11092 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011093
bedis4c75cca2012-10-05 08:38:24 +020011094 if (args[1].type)
11095 delim = *args[1].data.str.str;
11096
Willy Tarreau9b28e032012-10-12 23:49:43 +020011097 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011098 args->data.str.str, args->data.str.len,
11099 &smp->data.str.str, &smp->data.str.len,
11100 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011101 return 0;
11102
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011103 smp->type = SMP_T_STR;
11104 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011105 return 1;
11106}
11107
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011108/* Return the signed integer value for the specified url parameter (see url_param
11109 * above).
11110 */
11111static int
11112smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011113 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011114{
Willy Tarreauef38c392013-07-22 16:29:32 +020011115 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011116
11117 if (ret > 0) {
11118 smp->type = SMP_T_UINT;
11119 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11120 }
11121
11122 return ret;
11123}
11124
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011125/* This produces a 32-bit hash of the concatenation of the first occurrence of
11126 * the Host header followed by the path component if it begins with a slash ('/').
11127 * This means that '*' will not be added, resulting in exactly the first Host
11128 * entry. If no Host header is found, then the path is used. The resulting value
11129 * is hashed using the url hash followed by a full avalanche hash and provides a
11130 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11131 * high-traffic sites without having to store whole paths.
11132 * this differs from the base32 functions in that it includes the url parameters
11133 * as well as the path
11134 */
11135static int
11136smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011137 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011138{
11139 struct http_txn *txn = l7;
11140 struct hdr_ctx ctx;
11141 unsigned int hash = 0;
11142 char *ptr, *beg, *end;
11143 int len;
11144
11145 CHECK_HTTP_MESSAGE_FIRST();
11146
11147 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011148 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011149 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11150 ptr = ctx.line + ctx.val;
11151 len = ctx.vlen;
11152 while (len--)
11153 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11154 }
11155
11156 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011157 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 +000011158 beg = http_get_path(txn);
11159 if (!beg)
11160 beg = end;
11161
11162 for (ptr = beg; ptr < end ; ptr++);
11163
11164 if (beg < ptr && *beg == '/') {
11165 while (beg < ptr)
11166 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11167 }
11168 hash = full_hash(hash);
11169
11170 smp->type = SMP_T_UINT;
11171 smp->data.uint = hash;
11172 smp->flags = SMP_F_VOL_1ST;
11173 return 1;
11174}
11175
11176/* This concatenates the source address with the 32-bit hash of the Host and
11177 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11178 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11179 * on the source address length. The URL hash is stored before the address so
11180 * that in environments where IPv6 is insignificant, truncating the output to
11181 * 8 bytes would still work.
11182 */
11183static int
11184smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011185 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011186{
11187 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011188 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011189
Willy Tarreaue155ec22013-11-18 18:33:22 +010011190 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011191 return 0;
11192
11193 temp = get_trash_chunk();
11194 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11195 temp->len += sizeof(smp->data.uint);
11196
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011197 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011198 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011199 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011200 temp->len += 4;
11201 break;
11202 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011203 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011204 temp->len += 16;
11205 break;
11206 default:
11207 return 0;
11208 }
11209
11210 smp->data.str = *temp;
11211 smp->type = SMP_T_BIN;
11212 return 1;
11213}
11214
Willy Tarreau185b5c42012-04-26 15:11:51 +020011215/* This function is used to validate the arguments passed to any "hdr" fetch
11216 * keyword. These keywords support an optional positive or negative occurrence
11217 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11218 * is assumed that the types are already the correct ones. Returns 0 on error,
11219 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11220 * error message in case of error, that the caller is responsible for freeing.
11221 * The initial location must either be freeable or NULL.
11222 */
11223static int val_hdr(struct arg *arg, char **err_msg)
11224{
11225 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011226 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011227 return 0;
11228 }
11229 return 1;
11230}
11231
Willy Tarreau276fae92013-07-25 14:36:01 +020011232/* takes an UINT value on input supposed to represent the time since EPOCH,
11233 * adds an optional offset found in args[0] and emits a string representing
11234 * the date in RFC-1123/5322 format.
11235 */
11236static int sample_conv_http_date(const struct arg *args, struct sample *smp)
11237{
11238 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11239 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11240 struct chunk *temp;
11241 struct tm *tm;
11242 time_t curr_date = smp->data.uint;
11243
11244 /* add offset */
11245 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11246 curr_date += args[0].data.sint;
11247
11248 tm = gmtime(&curr_date);
Thierry FOURNIER95558722015-07-08 00:15:20 +020011249 if (!tm)
11250 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011251
11252 temp = get_trash_chunk();
11253 temp->len = snprintf(temp->str, temp->size - temp->len,
11254 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11255 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11256 tm->tm_hour, tm->tm_min, tm->tm_sec);
11257
11258 smp->data.str = *temp;
11259 smp->type = SMP_T_STR;
11260 return 1;
11261}
11262
Thierry FOURNIERad903512014-04-11 17:51:01 +020011263/* Match language range with language tag. RFC2616 14.4:
11264 *
11265 * A language-range matches a language-tag if it exactly equals
11266 * the tag, or if it exactly equals a prefix of the tag such
11267 * that the first tag character following the prefix is "-".
11268 *
11269 * Return 1 if the strings match, else return 0.
11270 */
11271static inline int language_range_match(const char *range, int range_len,
11272 const char *tag, int tag_len)
11273{
11274 const char *end = range + range_len;
11275 const char *tend = tag + tag_len;
11276 while (range < end) {
11277 if (*range == '-' && tag == tend)
11278 return 1;
11279 if (*range != *tag || tag == tend)
11280 return 0;
11281 range++;
11282 tag++;
11283 }
11284 /* Return true only if the last char of the tag is matched. */
11285 return tag == tend;
11286}
11287
11288/* Arguments: The list of expected value, the number of parts returned and the separator */
11289static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
11290{
11291 const char *al = smp->data.str.str;
11292 const char *end = al + smp->data.str.len;
11293 const char *token;
11294 int toklen;
11295 int qvalue;
11296 const char *str;
11297 const char *w;
11298 int best_q = 0;
11299
11300 /* Set the constant to the sample, because the output of the
11301 * function will be peek in the constant configuration string.
11302 */
11303 smp->flags |= SMP_F_CONST;
11304 smp->data.str.size = 0;
11305 smp->data.str.str = "";
11306 smp->data.str.len = 0;
11307
11308 /* Parse the accept language */
11309 while (1) {
11310
11311 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011312 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011313 al++;
11314 if (al >= end)
11315 break;
11316
11317 /* Start of the fisrt word. */
11318 token = al;
11319
11320 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011321 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011322 al++;
11323 if (al == token)
11324 goto expect_comma;
11325
11326 /* Length of the token. */
11327 toklen = al - token;
11328 qvalue = 1000;
11329
11330 /* Check if the token exists in the list. If the token not exists,
11331 * jump to the next token.
11332 */
11333 str = args[0].data.str.str;
11334 w = str;
11335 while (1) {
11336 if (*str == ';' || *str == '\0') {
11337 if (language_range_match(token, toklen, w, str-w))
11338 goto look_for_q;
11339 if (*str == '\0')
11340 goto expect_comma;
11341 w = str + 1;
11342 }
11343 str++;
11344 }
11345 goto expect_comma;
11346
11347look_for_q:
11348
11349 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011350 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011351 al++;
11352 if (al >= end)
11353 goto process_value;
11354
11355 /* If ',' is found, process the result */
11356 if (*al == ',')
11357 goto process_value;
11358
11359 /* If the character is different from ';', look
11360 * for the end of the header part in best effort.
11361 */
11362 if (*al != ';')
11363 goto expect_comma;
11364
11365 /* Assumes that the char is ';', now expect "q=". */
11366 al++;
11367
11368 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011369 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011370 al++;
11371 if (al >= end)
11372 goto process_value;
11373
11374 /* Expect 'q'. If no 'q', continue in best effort */
11375 if (*al != 'q')
11376 goto process_value;
11377 al++;
11378
11379 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011380 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011381 al++;
11382 if (al >= end)
11383 goto process_value;
11384
11385 /* Expect '='. If no '=', continue in best effort */
11386 if (*al != '=')
11387 goto process_value;
11388 al++;
11389
11390 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011391 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011392 al++;
11393 if (al >= end)
11394 goto process_value;
11395
11396 /* Parse the q value. */
11397 qvalue = parse_qvalue(al, &al);
11398
11399process_value:
11400
11401 /* If the new q value is the best q value, then store the associated
11402 * language in the response. If qvalue is the biggest value (1000),
11403 * break the process.
11404 */
11405 if (qvalue > best_q) {
11406 smp->data.str.str = (char *)w;
11407 smp->data.str.len = str - w;
11408 if (qvalue >= 1000)
11409 break;
11410 best_q = qvalue;
11411 }
11412
11413expect_comma:
11414
11415 /* Expect comma or end. If the end is detected, quit the loop. */
11416 while (al < end && *al != ',')
11417 al++;
11418 if (al >= end)
11419 break;
11420
11421 /* Comma is found, jump it and restart the analyzer. */
11422 al++;
11423 }
11424
11425 /* Set default value if required. */
11426 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11427 smp->data.str.str = args[1].data.str.str;
11428 smp->data.str.len = args[1].data.str.len;
11429 }
11430
11431 /* Return true only if a matching language was found. */
11432 return smp->data.str.len != 0;
11433}
11434
William Lallemand73025dd2014-04-24 14:38:37 +020011435/*
11436 * Return the struct http_req_action_kw associated to a keyword.
11437 */
11438struct http_req_action_kw *action_http_req_custom(const char *kw)
11439{
11440 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11441 struct http_req_action_kw_list *kw_list;
11442 int i;
11443
11444 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11445 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11446 if (!strcmp(kw, kw_list->kw[i].kw))
11447 return &kw_list->kw[i];
11448 }
11449 }
11450 }
11451 return NULL;
11452}
11453
11454/*
11455 * Return the struct http_res_action_kw associated to a keyword.
11456 */
11457struct http_res_action_kw *action_http_res_custom(const char *kw)
11458{
11459 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11460 struct http_res_action_kw_list *kw_list;
11461 int i;
11462
11463 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11464 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11465 if (!strcmp(kw, kw_list->kw[i].kw))
11466 return &kw_list->kw[i];
11467 }
11468 }
11469 }
11470 return NULL;
11471}
11472
Willy Tarreau4a568972010-05-12 08:08:50 +020011473/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011474/* All supported ACL keywords must be declared here. */
11475/************************************************************************/
11476
11477/* Note: must not be declared <const> as its list will be overwritten.
11478 * Please take care of keeping this list alphabetically sorted.
11479 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011480static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011481 { "base", "base", PAT_MATCH_STR },
11482 { "base_beg", "base", PAT_MATCH_BEG },
11483 { "base_dir", "base", PAT_MATCH_DIR },
11484 { "base_dom", "base", PAT_MATCH_DOM },
11485 { "base_end", "base", PAT_MATCH_END },
11486 { "base_len", "base", PAT_MATCH_LEN },
11487 { "base_reg", "base", PAT_MATCH_REG },
11488 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011489
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011490 { "cook", "req.cook", PAT_MATCH_STR },
11491 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11492 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11493 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11494 { "cook_end", "req.cook", PAT_MATCH_END },
11495 { "cook_len", "req.cook", PAT_MATCH_LEN },
11496 { "cook_reg", "req.cook", PAT_MATCH_REG },
11497 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011498
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011499 { "hdr", "req.hdr", PAT_MATCH_STR },
11500 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11501 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11502 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11503 { "hdr_end", "req.hdr", PAT_MATCH_END },
11504 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11505 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11506 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011507
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011508 /* these two declarations uses strings with list storage (in place
11509 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11510 * and delete functions are relative to the list management. The parse
11511 * and match method are related to the corresponding fetch methods. This
11512 * is very particular ACL declaration mode.
11513 */
11514 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11515 { "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 +020011516
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011517 { "path", "path", PAT_MATCH_STR },
11518 { "path_beg", "path", PAT_MATCH_BEG },
11519 { "path_dir", "path", PAT_MATCH_DIR },
11520 { "path_dom", "path", PAT_MATCH_DOM },
11521 { "path_end", "path", PAT_MATCH_END },
11522 { "path_len", "path", PAT_MATCH_LEN },
11523 { "path_reg", "path", PAT_MATCH_REG },
11524 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011525
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011526 { "req_ver", "req.ver", PAT_MATCH_STR },
11527 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011528
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011529 { "scook", "res.cook", PAT_MATCH_STR },
11530 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11531 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11532 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11533 { "scook_end", "res.cook", PAT_MATCH_END },
11534 { "scook_len", "res.cook", PAT_MATCH_LEN },
11535 { "scook_reg", "res.cook", PAT_MATCH_REG },
11536 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011537
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011538 { "shdr", "res.hdr", PAT_MATCH_STR },
11539 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11540 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11541 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11542 { "shdr_end", "res.hdr", PAT_MATCH_END },
11543 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11544 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11545 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011546
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011547 { "url", "url", PAT_MATCH_STR },
11548 { "url_beg", "url", PAT_MATCH_BEG },
11549 { "url_dir", "url", PAT_MATCH_DIR },
11550 { "url_dom", "url", PAT_MATCH_DOM },
11551 { "url_end", "url", PAT_MATCH_END },
11552 { "url_len", "url", PAT_MATCH_LEN },
11553 { "url_reg", "url", PAT_MATCH_REG },
11554 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011555
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011556 { "urlp", "urlp", PAT_MATCH_STR },
11557 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11558 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11559 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11560 { "urlp_end", "urlp", PAT_MATCH_END },
11561 { "urlp_len", "urlp", PAT_MATCH_LEN },
11562 { "urlp_reg", "urlp", PAT_MATCH_REG },
11563 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011564
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011565 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011566}};
11567
11568/************************************************************************/
11569/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011570/************************************************************************/
11571/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011572static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011573 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011574 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11575 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11576
William Lallemanda43ba4e2014-01-28 18:14:25 +010011577 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011578 { "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 +020011579
11580 /* retrieve these captures from the HTTP logs */
11581 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11582 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11583 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11584
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011585 { "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 +020011586 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011587
Willy Tarreau409bcde2013-01-08 00:31:00 +010011588 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11589 * are only here to match the ACL's name, are request-only and are used
11590 * for ACL compatibility only.
11591 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011592 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11593 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011594 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11595 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11596
11597 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11598 * only here to match the ACL's name, are request-only and are used for
11599 * ACL compatibility only.
11600 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011601 { "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 +010011602 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11603 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11604 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11605
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011606 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011607 { "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 +010011608 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011609 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011610 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011611
11612 /* HTTP protocol on the request path */
11613 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011614 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011615
11616 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011617 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11618 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011619
11620 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011621 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11622 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011623
Willy Tarreau18ed2562013-01-14 15:56:36 +010011624 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011625 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011626 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11627 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11628
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011629 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011630 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011631 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011632 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11633 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11634 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11635
11636 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011637 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011638 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11639 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11640
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011641 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011642 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011643 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011644 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11645 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11646 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11647
Willy Tarreau409bcde2013-01-08 00:31:00 +010011648 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011649 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011650 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11651 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011652 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011653
11654 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011655 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011656 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11657 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11658 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11659
11660 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011661 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011662 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11663 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011664 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11665 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011666 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11667 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011668 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11669 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011670}};
11671
Willy Tarreau8797c062007-05-07 00:55:35 +020011672
Willy Tarreau276fae92013-07-25 14:36:01 +020011673/* Note: must not be declared <const> as its list will be overwritten */
11674static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011675 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11676 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011677 { NULL, NULL, 0, 0, 0 },
11678}};
11679
Willy Tarreau8797c062007-05-07 00:55:35 +020011680__attribute__((constructor))
11681static void __http_protocol_init(void)
11682{
11683 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011684 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011685 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020011686}
11687
11688
Willy Tarreau58f10d72006-12-04 02:26:12 +010011689/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011690 * Local variables:
11691 * c-indent-level: 8
11692 * c-basic-offset: 8
11693 * End:
11694 */