blob: 231d49a12875bee8dbc813b881d9d208f64a5b78 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreau522d6c02009-12-06 18:49:18 +010069const char HTTP_100[] =
70 "HTTP/1.1 100 Continue\r\n\r\n";
71
72const struct chunk http_100_chunk = {
73 .str = (char *)&HTTP_100,
74 .len = sizeof(HTTP_100)-1
75};
76
Willy Tarreaua9679ac2010-01-03 17:32:57 +010077/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020081 "Location: "; /* not terminated since it will be concatenated with the URL */
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
89/* same as 302 except that the browser MUST retry with the GET method */
90const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Location: "; /* not terminated since it will be concatenated with the URL */
95
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040096
97/* same as 302 except that the browser MUST retry with the same method */
98const char *HTTP_307 =
99 "HTTP/1.1 307 Temporary Redirect\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Content-length: 0\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
104/* same as 301 except that the browser MUST retry with the same method */
105const char *HTTP_308 =
106 "HTTP/1.1 308 Permanent Redirect\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
111const char *HTTP_401_fmt =
112 "HTTP/1.0 401 Unauthorized\r\n"
113 "Cache-Control: no-cache\r\n"
114 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200115 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
117 "\r\n"
118 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
119
Willy Tarreau844a7e72010-01-31 21:46:18 +0100120const char *HTTP_407_fmt =
121 "HTTP/1.0 407 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
124 "Content-Type: text/html\r\n"
125 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
127 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
128
Willy Tarreau0f772532006-12-23 20:51:41 +0100129
130const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200131 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100132 [HTTP_ERR_400] = 400,
133 [HTTP_ERR_403] = 403,
134 [HTTP_ERR_408] = 408,
135 [HTTP_ERR_500] = 500,
136 [HTTP_ERR_502] = 502,
137 [HTTP_ERR_503] = 503,
138 [HTTP_ERR_504] = 504,
139};
140
Willy Tarreau80587432006-12-24 17:47:20 +0100141static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200142 [HTTP_ERR_200] =
143 "HTTP/1.0 200 OK\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
149
Willy Tarreau0f772532006-12-23 20:51:41 +0100150 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100151 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
157
158 [HTTP_ERR_403] =
159 "HTTP/1.0 403 Forbidden\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
165
166 [HTTP_ERR_408] =
167 "HTTP/1.0 408 Request Time-out\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
173
174 [HTTP_ERR_500] =
175 "HTTP/1.0 500 Server Error\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
181
182 [HTTP_ERR_502] =
183 "HTTP/1.0 502 Bad Gateway\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
189
190 [HTTP_ERR_503] =
191 "HTTP/1.0 503 Service Unavailable\r\n"
192 "Cache-Control: no-cache\r\n"
193 "Connection: close\r\n"
194 "Content-Type: text/html\r\n"
195 "\r\n"
196 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
197
198 [HTTP_ERR_504] =
199 "HTTP/1.0 504 Gateway Time-out\r\n"
200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
204 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
205
206};
207
Cyril Bonté19979e12012-04-04 12:57:21 +0200208/* status codes available for the stats admin page (strictly 4 chars length) */
209const char *stat_status_codes[STAT_STATUS_SIZE] = {
210 [STAT_STATUS_DENY] = "DENY",
211 [STAT_STATUS_DONE] = "DONE",
212 [STAT_STATUS_ERRP] = "ERRP",
213 [STAT_STATUS_EXCD] = "EXCD",
214 [STAT_STATUS_NONE] = "NONE",
215 [STAT_STATUS_PART] = "PART",
216 [STAT_STATUS_UNKN] = "UNKN",
217};
218
219
William Lallemand73025dd2014-04-24 14:38:37 +0200220/* List head of all known action keywords for "http-request" */
221struct http_req_action_kw_list http_req_keywords = {
222 .list = LIST_HEAD_INIT(http_req_keywords.list)
223};
224
225/* List head of all known action keywords for "http-response" */
226struct http_res_action_kw_list http_res_keywords = {
227 .list = LIST_HEAD_INIT(http_res_keywords.list)
228};
229
Willy Tarreau80587432006-12-24 17:47:20 +0100230/* We must put the messages here since GCC cannot initialize consts depending
231 * on strlen().
232 */
233struct chunk http_err_chunks[HTTP_ERR_SIZE];
234
Willy Tarreaua890d072013-04-02 12:01:06 +0200235/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
236static struct hdr_ctx static_hdr_ctx;
237
Willy Tarreau42250582007-04-01 01:30:43 +0200238#define FD_SETS_ARE_BITFIELDS
239#ifdef FD_SETS_ARE_BITFIELDS
240/*
241 * This map is used with all the FD_* macros to check whether a particular bit
242 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
243 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
244 * byte should be encoded. Be careful to always pass bytes from 0 to 255
245 * exclusively to the macros.
246 */
247fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
248fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100249fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200250
251#else
252#error "Check if your OS uses bitfields for fd_sets"
253#endif
254
Willy Tarreau0b748332014-04-29 00:13:29 +0200255static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn);
256
Willy Tarreau80587432006-12-24 17:47:20 +0100257void init_proto_http()
258{
Willy Tarreau42250582007-04-01 01:30:43 +0200259 int i;
260 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100261 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200262
Willy Tarreau80587432006-12-24 17:47:20 +0100263 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
264 if (!http_err_msgs[msg]) {
265 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
266 abort();
267 }
268
269 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
270 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
271 }
Willy Tarreau42250582007-04-01 01:30:43 +0200272
273 /* initialize the log header encoding map : '{|}"#' should be encoded with
274 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
275 * URL encoding only requires '"', '#' to be encoded as well as non-
276 * printable characters above.
277 */
278 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
279 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100280 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200281 for (i = 0; i < 32; i++) {
282 FD_SET(i, hdr_encode_map);
283 FD_SET(i, url_encode_map);
284 }
285 for (i = 127; i < 256; i++) {
286 FD_SET(i, hdr_encode_map);
287 FD_SET(i, url_encode_map);
288 }
289
290 tmp = "\"#{|}";
291 while (*tmp) {
292 FD_SET(*tmp, hdr_encode_map);
293 tmp++;
294 }
295
296 tmp = "\"#";
297 while (*tmp) {
298 FD_SET(*tmp, url_encode_map);
299 tmp++;
300 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200301
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100302 /* initialize the http header encoding map. The draft httpbis define the
303 * header content as:
304 *
305 * HTTP-message = start-line
306 * *( header-field CRLF )
307 * CRLF
308 * [ message-body ]
309 * header-field = field-name ":" OWS field-value OWS
310 * field-value = *( field-content / obs-fold )
311 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
312 * obs-fold = CRLF 1*( SP / HTAB )
313 * field-vchar = VCHAR / obs-text
314 * VCHAR = %x21-7E
315 * obs-text = %x80-FF
316 *
317 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
318 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
319 * "obs-fold" is volontary forgotten because haproxy remove this.
320 */
321 memset(http_encode_map, 0, sizeof(http_encode_map));
322 for (i = 0x00; i <= 0x08; i++)
323 FD_SET(i, http_encode_map);
324 for (i = 0x0a; i <= 0x1f; i++)
325 FD_SET(i, http_encode_map);
326 FD_SET(0x7f, http_encode_map);
327
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200328 /* memory allocations */
329 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100330 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100331}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332
Willy Tarreau53b6c742006-12-17 13:37:46 +0100333/*
334 * We have 26 list of methods (1 per first letter), each of which can have
335 * up to 3 entries (2 valid, 1 null).
336 */
337struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100338 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100339 int len;
340 const char text[8];
341};
342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100343const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100344 ['C' - 'A'] = {
345 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
346 },
347 ['D' - 'A'] = {
348 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
349 },
350 ['G' - 'A'] = {
351 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
352 },
353 ['H' - 'A'] = {
354 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
355 },
356 ['P' - 'A'] = {
357 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
358 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
359 },
360 ['T' - 'A'] = {
361 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
362 },
363 /* rest is empty like this :
364 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
365 */
366};
367
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100368const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
369 [HTTP_METH_NONE] = { "", 0 },
370 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
371 [HTTP_METH_GET] = { "GET", 3 },
372 [HTTP_METH_HEAD] = { "HEAD", 4 },
373 [HTTP_METH_POST] = { "POST", 4 },
374 [HTTP_METH_PUT] = { "PUT", 3 },
375 [HTTP_METH_DELETE] = { "DELETE", 6 },
376 [HTTP_METH_TRACE] = { "TRACE", 5 },
377 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
378};
379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100380/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200381 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100382 * RFC2616 for those chars.
383 */
384
385const char http_is_spht[256] = {
386 [' '] = 1, ['\t'] = 1,
387};
388
389const char http_is_crlf[256] = {
390 ['\r'] = 1, ['\n'] = 1,
391};
392
393const char http_is_lws[256] = {
394 [' '] = 1, ['\t'] = 1,
395 ['\r'] = 1, ['\n'] = 1,
396};
397
398const char http_is_sep[256] = {
399 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
400 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
401 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
402 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
403 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
404};
405
406const char http_is_ctl[256] = {
407 [0 ... 31] = 1,
408 [127] = 1,
409};
410
411/*
412 * A token is any ASCII char that is neither a separator nor a CTL char.
413 * Do not overwrite values in assignment since gcc-2.95 will not handle
414 * them correctly. Instead, define every non-CTL char's status.
415 */
416const char http_is_token[256] = {
417 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
418 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
419 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
420 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
421 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
422 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
423 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
424 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
425 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
426 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
427 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
428 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
429 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
430 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
431 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
432 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
433 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
434 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
435 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
436 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
437 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
438 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
439 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
440 ['|'] = 1, ['}'] = 0, ['~'] = 1,
441};
442
443
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100444/*
445 * An http ver_token is any ASCII which can be found in an HTTP version,
446 * which includes 'H', 'T', 'P', '/', '.' and any digit.
447 */
448const char http_is_ver_token[256] = {
449 ['.'] = 1, ['/'] = 1,
450 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
451 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
452 ['H'] = 1, ['P'] = 1, ['T'] = 1,
453};
454
455
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100456/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100457 * Adds a header and its CRLF at the tail of the message's buffer, just before
458 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100459 * The header is also automatically added to the index <hdr_idx>, and the end
460 * of headers is automatically adjusted. The number of bytes added is returned
461 * on success, otherwise <0 is returned indicating an error.
462 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100463int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100464{
465 int bytes, len;
466
467 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200468 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100469 if (!bytes)
470 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100471 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100472 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
473}
474
475/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100476 * Adds a header and its CRLF at the tail of the message's buffer, just before
477 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100478 * the buffer is only opened and the space reserved, but nothing is copied.
479 * The header is also automatically added to the index <hdr_idx>, and the end
480 * of headers is automatically adjusted. The number of bytes added is returned
481 * on success, otherwise <0 is returned indicating an error.
482 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100483int http_header_add_tail2(struct http_msg *msg,
484 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100485{
486 int bytes;
487
Willy Tarreau9b28e032012-10-12 23:49:43 +0200488 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100489 if (!bytes)
490 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100491 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100492 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
493}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200494
495/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100496 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
497 * If so, returns the position of the first non-space character relative to
498 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
499 * to return a pointer to the place after the first space. Returns 0 if the
500 * header name does not match. Checks are case-insensitive.
501 */
502int http_header_match2(const char *hdr, const char *end,
503 const char *name, int len)
504{
505 const char *val;
506
507 if (hdr + len >= end)
508 return 0;
509 if (hdr[len] != ':')
510 return 0;
511 if (strncasecmp(hdr, name, len) != 0)
512 return 0;
513 val = hdr + len + 1;
514 while (val < end && HTTP_IS_SPHT(*val))
515 val++;
516 if ((val >= end) && (len + 2 <= end - hdr))
517 return len + 2; /* we may replace starting from second space */
518 return val - hdr;
519}
520
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200521/* Find the first or next occurrence of header <name> in message buffer <sol>
522 * using headers index <idx>, and return it in the <ctx> structure. This
523 * structure holds everything necessary to use the header and find next
524 * occurrence. If its <idx> member is 0, the header is searched from the
525 * beginning. Otherwise, the next occurrence is returned. The function returns
526 * 1 when it finds a value, and 0 when there is no more. It is very similar to
527 * http_find_header2() except that it is designed to work with full-line headers
528 * whose comma is not a delimiter but is part of the syntax. As a special case,
529 * if ctx->val is NULL when searching for a new values of a header, the current
530 * header is rescanned. This allows rescanning after a header deletion.
531 */
532int http_find_full_header2(const char *name, int len,
533 char *sol, struct hdr_idx *idx,
534 struct hdr_ctx *ctx)
535{
536 char *eol, *sov;
537 int cur_idx, old_idx;
538
539 cur_idx = ctx->idx;
540 if (cur_idx) {
541 /* We have previously returned a header, let's search another one */
542 sol = ctx->line;
543 eol = sol + idx->v[cur_idx].len;
544 goto next_hdr;
545 }
546
547 /* first request for this header */
548 sol += hdr_idx_first_pos(idx);
549 old_idx = 0;
550 cur_idx = hdr_idx_first_idx(idx);
551 while (cur_idx) {
552 eol = sol + idx->v[cur_idx].len;
553
554 if (len == 0) {
555 /* No argument was passed, we want any header.
556 * To achieve this, we simply build a fake request. */
557 while (sol + len < eol && sol[len] != ':')
558 len++;
559 name = sol;
560 }
561
562 if ((len < eol - sol) &&
563 (sol[len] == ':') &&
564 (strncasecmp(sol, name, len) == 0)) {
565 ctx->del = len;
566 sov = sol + len + 1;
567 while (sov < eol && http_is_lws[(unsigned char)*sov])
568 sov++;
569
570 ctx->line = sol;
571 ctx->prev = old_idx;
572 ctx->idx = cur_idx;
573 ctx->val = sov - sol;
574 ctx->tws = 0;
575 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
576 eol--;
577 ctx->tws++;
578 }
579 ctx->vlen = eol - sov;
580 return 1;
581 }
582 next_hdr:
583 sol = eol + idx->v[cur_idx].cr + 1;
584 old_idx = cur_idx;
585 cur_idx = idx->v[cur_idx].next;
586 }
587 return 0;
588}
589
Willy Tarreau68085d82010-01-18 14:54:04 +0100590/* Find the end of the header value contained between <s> and <e>. See RFC2616,
591 * par 2.2 for more information. Note that it requires a valid header to return
592 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200593 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100594char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200595{
596 int quoted, qdpair;
597
598 quoted = qdpair = 0;
599 for (; s < e; s++) {
600 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200601 else if (quoted) {
602 if (*s == '\\') qdpair = 1;
603 else if (*s == '"') quoted = 0;
604 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200605 else if (*s == '"') quoted = 1;
606 else if (*s == ',') return s;
607 }
608 return s;
609}
610
611/* Find the first or next occurrence of header <name> in message buffer <sol>
612 * using headers index <idx>, and return it in the <ctx> structure. This
613 * structure holds everything necessary to use the header and find next
614 * occurrence. If its <idx> member is 0, the header is searched from the
615 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100616 * 1 when it finds a value, and 0 when there is no more. It is designed to work
617 * with headers defined as comma-separated lists. As a special case, if ctx->val
618 * is NULL when searching for a new values of a header, the current header is
619 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200620 */
621int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100622 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200623 struct hdr_ctx *ctx)
624{
Willy Tarreau68085d82010-01-18 14:54:04 +0100625 char *eol, *sov;
626 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200627
Willy Tarreau68085d82010-01-18 14:54:04 +0100628 cur_idx = ctx->idx;
629 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200630 /* We have previously returned a value, let's search
631 * another one on the same line.
632 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200633 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200634 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100635 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200636 eol = sol + idx->v[cur_idx].len;
637
638 if (sov >= eol)
639 /* no more values in this header */
640 goto next_hdr;
641
Willy Tarreau68085d82010-01-18 14:54:04 +0100642 /* values remaining for this header, skip the comma but save it
643 * for later use (eg: for header deletion).
644 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200645 sov++;
646 while (sov < eol && http_is_lws[(unsigned char)*sov])
647 sov++;
648
649 goto return_hdr;
650 }
651
652 /* first request for this header */
653 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100654 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200655 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200656 while (cur_idx) {
657 eol = sol + idx->v[cur_idx].len;
658
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200659 if (len == 0) {
660 /* No argument was passed, we want any header.
661 * To achieve this, we simply build a fake request. */
662 while (sol + len < eol && sol[len] != ':')
663 len++;
664 name = sol;
665 }
666
Willy Tarreau33a7e692007-06-10 19:45:56 +0200667 if ((len < eol - sol) &&
668 (sol[len] == ':') &&
669 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100670 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200671 sov = sol + len + 1;
672 while (sov < eol && http_is_lws[(unsigned char)*sov])
673 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100674
Willy Tarreau33a7e692007-06-10 19:45:56 +0200675 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100676 ctx->prev = old_idx;
677 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200678 ctx->idx = cur_idx;
679 ctx->val = sov - sol;
680
681 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200682 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200683 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200684 eol--;
685 ctx->tws++;
686 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200687 ctx->vlen = eol - sov;
688 return 1;
689 }
690 next_hdr:
691 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100692 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200693 cur_idx = idx->v[cur_idx].next;
694 }
695 return 0;
696}
697
698int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100699 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200700 struct hdr_ctx *ctx)
701{
702 return http_find_header2(name, strlen(name), sol, idx, ctx);
703}
704
Willy Tarreau68085d82010-01-18 14:54:04 +0100705/* Remove one value of a header. This only works on a <ctx> returned by one of
706 * the http_find_header functions. The value is removed, as well as surrounding
707 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100708 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100709 * message <msg>. The new index is returned. If it is zero, it means there is
710 * no more header, so any processing may stop. The ctx is always left in a form
711 * that can be handled by http_find_header2() to find next occurrence.
712 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100713int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100714{
715 int cur_idx = ctx->idx;
716 char *sol = ctx->line;
717 struct hdr_idx_elem *hdr;
718 int delta, skip_comma;
719
720 if (!cur_idx)
721 return 0;
722
723 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200724 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100725 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200726 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100727 http_msg_move_end(msg, delta);
728 idx->used--;
729 hdr->len = 0; /* unused entry */
730 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100731 if (idx->tail == ctx->idx)
732 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100733 ctx->idx = ctx->prev; /* walk back to the end of previous header */
734 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
735 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200736 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100737 return ctx->idx;
738 }
739
740 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200741 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
742 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100743 */
744
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200745 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200746 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200747 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100748 NULL, 0);
749 hdr->len += delta;
750 http_msg_move_end(msg, delta);
751 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200752 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100753 return ctx->idx;
754}
755
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100756/* This function handles a server error at the stream interface level. The
757 * stream interface is assumed to be already in a closed state. An optional
758 * message is copied into the input buffer, and an HTTP status code stored.
759 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100760 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200761 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200762static void http_server_error(struct session *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100763 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200765 channel_auto_read(si->ob);
766 channel_abort(si->ob);
767 channel_auto_close(si->ob);
768 channel_erase(si->ob);
769 channel_auto_close(si->ib);
770 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100771 if (status > 0 && msg) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200772 s->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200773 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200774 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200775 if (!(s->flags & SN_ERR_MASK))
776 s->flags |= err;
777 if (!(s->flags & SN_FINST_MASK))
778 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200779}
780
Willy Tarreau80587432006-12-24 17:47:20 +0100781/* This function returns the appropriate error location for the given session
782 * and message.
783 */
784
Willy Tarreau783f2582012-09-04 12:19:04 +0200785struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100786{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200787 if (s->be->errmsg[msgnum].str)
788 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100789 else if (s->fe->errmsg[msgnum].str)
790 return &s->fe->errmsg[msgnum];
791 else
792 return &http_err_chunks[msgnum];
793}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200794
Willy Tarreau53b6c742006-12-17 13:37:46 +0100795/*
796 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
797 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
798 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100799enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100800{
801 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100802 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100803
804 m = ((unsigned)*str - 'A');
805
806 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100807 for (h = http_methods[m]; h->len > 0; h++) {
808 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100809 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100810 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100811 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100812 };
813 return HTTP_METH_OTHER;
814 }
815 return HTTP_METH_NONE;
816
817}
818
Willy Tarreau21d2af32008-02-14 20:25:24 +0100819/* Parse the URI from the given transaction (which is assumed to be in request
820 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
821 * It is returned otherwise.
822 */
823static char *
824http_get_path(struct http_txn *txn)
825{
826 char *ptr, *end;
827
Willy Tarreau9b28e032012-10-12 23:49:43 +0200828 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100829 end = ptr + txn->req.sl.rq.u_l;
830
831 if (ptr >= end)
832 return NULL;
833
834 /* RFC2616, par. 5.1.2 :
835 * Request-URI = "*" | absuri | abspath | authority
836 */
837
838 if (*ptr == '*')
839 return NULL;
840
841 if (isalpha((unsigned char)*ptr)) {
842 /* this is a scheme as described by RFC3986, par. 3.1 */
843 ptr++;
844 while (ptr < end &&
845 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
846 ptr++;
847 /* skip '://' */
848 if (ptr == end || *ptr++ != ':')
849 return NULL;
850 if (ptr == end || *ptr++ != '/')
851 return NULL;
852 if (ptr == end || *ptr++ != '/')
853 return NULL;
854 }
855 /* skip [user[:passwd]@]host[:[port]] */
856
857 while (ptr < end && *ptr != '/')
858 ptr++;
859
860 if (ptr == end)
861 return NULL;
862
863 /* OK, we got the '/' ! */
864 return ptr;
865}
866
William Lallemand65ad6e12014-01-31 15:08:02 +0100867/* Parse the URI from the given string and look for the "/" beginning the PATH.
868 * If not found, return NULL. It is returned otherwise.
869 */
870static char *
871http_get_path_from_string(char *str)
872{
873 char *ptr = str;
874
875 /* RFC2616, par. 5.1.2 :
876 * Request-URI = "*" | absuri | abspath | authority
877 */
878
879 if (*ptr == '*')
880 return NULL;
881
882 if (isalpha((unsigned char)*ptr)) {
883 /* this is a scheme as described by RFC3986, par. 3.1 */
884 ptr++;
885 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
886 ptr++;
887 /* skip '://' */
888 if (*ptr == '\0' || *ptr++ != ':')
889 return NULL;
890 if (*ptr == '\0' || *ptr++ != '/')
891 return NULL;
892 if (*ptr == '\0' || *ptr++ != '/')
893 return NULL;
894 }
895 /* skip [user[:passwd]@]host[:[port]] */
896
897 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
898 ptr++;
899
900 if (*ptr == '\0' || *ptr == ' ')
901 return NULL;
902
903 /* OK, we got the '/' ! */
904 return ptr;
905}
906
Willy Tarreau71241ab2012-12-27 11:30:54 +0100907/* Returns a 302 for a redirectable request that reaches a server working in
908 * in redirect mode. This may only be called just after the stream interface
909 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
910 * follow normal proxy processing. NOTE: this function is designed to support
911 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100912 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100913void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100914{
915 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100916 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100917 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200918 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100919
920 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100921 trash.len = strlen(HTTP_302);
922 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100923
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100924 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100925
Willy Tarreauefb453c2008-10-26 20:49:47 +0100926 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100927 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100928 return;
929
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100930 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100931 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100932 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
933 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100934 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100935
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200936 /* 3: add the request URI. Since it was already forwarded, we need
937 * to temporarily rewind the buffer.
938 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100939 txn = &s->txn;
Willy Tarreau211cdec2014-04-17 20:18:08 +0200940 b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200941
Willy Tarreauefb453c2008-10-26 20:49:47 +0100942 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200943 len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200944
Willy Tarreau9b28e032012-10-12 23:49:43 +0200945 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200946
Willy Tarreauefb453c2008-10-26 20:49:47 +0100947 if (!path)
948 return;
949
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100950 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100951 return;
952
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100953 memcpy(trash.str + trash.len, path, len);
954 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100955
956 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100957 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
958 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100959 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100960 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
961 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100962 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100963
964 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200965 si_shutr(si);
966 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100967 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100968 si->state = SI_ST_CLO;
969
970 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200971 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100972
973 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100974 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500975 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100976}
977
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100978/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100979 * that the server side is closed. Note that err_type is actually a
980 * bitmask, where almost only aborts may be cumulated with other
981 * values. We consider that aborted operations are more important
982 * than timeouts or errors due to the fact that nobody else in the
983 * logs might explain incomplete retries. All others should avoid
984 * being cumulated. It should normally not be possible to have multiple
985 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100986 * Note that connection errors appearing on the second request of a keep-alive
987 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100988 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100989void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100991 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100992
993 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100994 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200995 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100996 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100997 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100998 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
999 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001000 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001001 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001002 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001003 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001004 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001005 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001006 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001007 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001008 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1009 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001010 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001011 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001012 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001013 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001014 else if (err_type & SI_ET_CONN_RES)
1015 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001016 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1017 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001018 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001019 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001020 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001021}
1022
Willy Tarreau42250582007-04-01 01:30:43 +02001023extern const char sess_term_cond[8];
1024extern const char sess_fin_state[8];
1025extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001026struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001027struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001028struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001029
Willy Tarreau117f59e2007-03-04 18:17:17 +01001030/*
1031 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001032 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001033 */
1034void capture_headers(char *som, struct hdr_idx *idx,
1035 char **cap, struct cap_hdr *cap_hdr)
1036{
1037 char *eol, *sol, *col, *sov;
1038 int cur_idx;
1039 struct cap_hdr *h;
1040 int len;
1041
1042 sol = som + hdr_idx_first_pos(idx);
1043 cur_idx = hdr_idx_first_idx(idx);
1044
1045 while (cur_idx) {
1046 eol = sol + idx->v[cur_idx].len;
1047
1048 col = sol;
1049 while (col < eol && *col != ':')
1050 col++;
1051
1052 sov = col + 1;
1053 while (sov < eol && http_is_lws[(unsigned char)*sov])
1054 sov++;
1055
1056 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001057 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001058 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1059 if (cap[h->index] == NULL)
1060 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001061 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001062
1063 if (cap[h->index] == NULL) {
1064 Alert("HTTP capture : out of memory.\n");
1065 continue;
1066 }
1067
1068 len = eol - sov;
1069 if (len > h->len)
1070 len = h->len;
1071
1072 memcpy(cap[h->index], sov, len);
1073 cap[h->index][len]=0;
1074 }
1075 }
1076 sol = eol + idx->v[cur_idx].cr + 1;
1077 cur_idx = idx->v[cur_idx].next;
1078 }
1079}
1080
1081
Willy Tarreau42250582007-04-01 01:30:43 +02001082/* either we find an LF at <ptr> or we jump to <bad>.
1083 */
1084#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1085
1086/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1087 * otherwise to <http_msg_ood> with <state> set to <st>.
1088 */
1089#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1090 ptr++; \
1091 if (likely(ptr < end)) \
1092 goto good; \
1093 else { \
1094 state = (st); \
1095 goto http_msg_ood; \
1096 } \
1097 } while (0)
1098
1099
Willy Tarreaubaaee002006-06-26 02:48:02 +02001100/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001101 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001102 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1103 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1104 * will give undefined results.
1105 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1106 * and that msg->sol points to the beginning of the response.
1107 * If a complete line is found (which implies that at least one CR or LF is
1108 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1109 * returned indicating an incomplete line (which does not mean that parts have
1110 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1111 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1112 * upon next call.
1113 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001114 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001115 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1116 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001117 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001118 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001119const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001120 enum ht_state state, const char *ptr, const char *end,
1121 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001122{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001123 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001124
Willy Tarreau8973c702007-01-21 23:58:29 +01001125 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001126 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001127 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001128 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001129 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1130
1131 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001132 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001133 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1134 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001135 state = HTTP_MSG_ERROR;
1136 break;
1137
Willy Tarreau8973c702007-01-21 23:58:29 +01001138 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001139 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001140 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001141 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001142 goto http_msg_rpcode;
1143 }
1144 if (likely(HTTP_IS_SPHT(*ptr)))
1145 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1146 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001147 state = HTTP_MSG_ERROR;
1148 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001149
Willy Tarreau8973c702007-01-21 23:58:29 +01001150 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001151 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001152 if (likely(!HTTP_IS_LWS(*ptr)))
1153 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1154
1155 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001156 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001157 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1158 }
1159
1160 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001161 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001162 http_msg_rsp_reason:
1163 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001164 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001165 msg->sl.st.r_l = 0;
1166 goto http_msg_rpline_eol;
1167
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001169 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001170 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001171 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001172 goto http_msg_rpreason;
1173 }
1174 if (likely(HTTP_IS_SPHT(*ptr)))
1175 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1176 /* so it's a CR/LF, so there is no reason phrase */
1177 goto http_msg_rsp_reason;
1178
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001180 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001181 if (likely(!HTTP_IS_CRLF(*ptr)))
1182 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001183 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001184 http_msg_rpline_eol:
1185 /* We have seen the end of line. Note that we do not
1186 * necessarily have the \n yet, but at least we know that we
1187 * have EITHER \r OR \n, otherwise the response would not be
1188 * complete. We can then record the response length and return
1189 * to the caller which will be able to register it.
1190 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001191 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001192 return ptr;
1193
Willy Tarreau8973c702007-01-21 23:58:29 +01001194 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001195#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001196 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1197 exit(1);
1198#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001199 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001200 }
1201
1202 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001203 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001204 if (ret_state)
1205 *ret_state = state;
1206 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001207 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001208 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001209}
1210
Willy Tarreau8973c702007-01-21 23:58:29 +01001211/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001212 * This function parses a request line between <ptr> and <end>, starting with
1213 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1214 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1215 * will give undefined results.
1216 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1217 * and that msg->sol points to the beginning of the request.
1218 * If a complete line is found (which implies that at least one CR or LF is
1219 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1220 * returned indicating an incomplete line (which does not mean that parts have
1221 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1222 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1223 * upon next call.
1224 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001225 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1227 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001228 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001229 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001230const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001231 enum ht_state state, const char *ptr, const char *end,
1232 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001233{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001234 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001235
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001238 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239 if (likely(HTTP_IS_TOKEN(*ptr)))
1240 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001241
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001242 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001243 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001244 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1245 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001246
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001247 if (likely(HTTP_IS_CRLF(*ptr))) {
1248 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001249 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001250 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001251 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001252 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001253 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001254 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001255 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001256 msg->sl.rq.v_l = 0;
1257 goto http_msg_rqline_eol;
1258 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001259 state = HTTP_MSG_ERROR;
1260 break;
1261
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001262 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001263 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001265 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001266 goto http_msg_rquri;
1267 }
1268 if (likely(HTTP_IS_SPHT(*ptr)))
1269 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1270 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1271 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001272
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001274 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001275 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001277
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001279 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001280 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1281 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001282
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001283 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001284 /* non-ASCII chars are forbidden unless option
1285 * accept-invalid-http-request is enabled in the frontend.
1286 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001287 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001288 if (msg->err_pos < -1)
1289 goto invalid_char;
1290 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001291 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001292 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1293 }
1294
1295 if (likely(HTTP_IS_CRLF(*ptr))) {
1296 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1297 goto http_msg_req09_uri_e;
1298 }
1299
1300 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001301 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001302 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001303 state = HTTP_MSG_ERROR;
1304 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001307 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001309 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 goto http_msg_rqver;
1311 }
1312 if (likely(HTTP_IS_SPHT(*ptr)))
1313 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1314 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1315 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001318 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001319 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001321
1322 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001323 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001324 http_msg_rqline_eol:
1325 /* We have seen the end of line. Note that we do not
1326 * necessarily have the \n yet, but at least we know that we
1327 * have EITHER \r OR \n, otherwise the request would not be
1328 * complete. We can then record the request length and return
1329 * to the caller which will be able to register it.
1330 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001331 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001332 return ptr;
1333 }
1334
1335 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001336 state = HTTP_MSG_ERROR;
1337 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001338
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001339 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001340#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001341 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1342 exit(1);
1343#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001344 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001345 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001348 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001349 if (ret_state)
1350 *ret_state = state;
1351 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001352 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001353 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001355
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001356/*
1357 * Returns the data from Authorization header. Function may be called more
1358 * than once so data is stored in txn->auth_data. When no header is found
1359 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001360 * searching again for something we are unable to find anyway. However, if
1361 * the result if valid, the cache is not reused because we would risk to
1362 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001363 */
1364
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001365/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1366 * set according to global.tune.bufsize.
1367 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001368char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001369
1370int
1371get_http_auth(struct session *s)
1372{
1373
1374 struct http_txn *txn = &s->txn;
1375 struct chunk auth_method;
1376 struct hdr_ctx ctx;
1377 char *h, *p;
1378 int len;
1379
1380#ifdef DEBUG_AUTH
1381 printf("Auth for session %p: %d\n", s, txn->auth.method);
1382#endif
1383
1384 if (txn->auth.method == HTTP_AUTH_WRONG)
1385 return 0;
1386
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001387 txn->auth.method = HTTP_AUTH_WRONG;
1388
1389 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001390
1391 if (txn->flags & TX_USE_PX_CONN) {
1392 h = "Proxy-Authorization";
1393 len = strlen(h);
1394 } else {
1395 h = "Authorization";
1396 len = strlen(h);
1397 }
1398
Willy Tarreau9b28e032012-10-12 23:49:43 +02001399 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001400 return 0;
1401
1402 h = ctx.line + ctx.val;
1403
1404 p = memchr(h, ' ', ctx.vlen);
1405 if (!p || p == h)
1406 return 0;
1407
1408 chunk_initlen(&auth_method, h, 0, p-h);
1409 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1410
1411 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1412
1413 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001414 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001415
1416 if (len < 0)
1417 return 0;
1418
1419
1420 get_http_auth_buff[len] = '\0';
1421
1422 p = strchr(get_http_auth_buff, ':');
1423
1424 if (!p)
1425 return 0;
1426
1427 txn->auth.user = get_http_auth_buff;
1428 *p = '\0';
1429 txn->auth.pass = p+1;
1430
1431 txn->auth.method = HTTP_AUTH_BASIC;
1432 return 1;
1433 }
1434
1435 return 0;
1436}
1437
Willy Tarreau58f10d72006-12-04 02:26:12 +01001438
Willy Tarreau8973c702007-01-21 23:58:29 +01001439/*
1440 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001441 * depending on the initial msg->msg_state. The caller is responsible for
1442 * ensuring that the message does not wrap. The function can be preempted
1443 * everywhere when data are missing and recalled at the exact same location
1444 * with no information loss. The message may even be realigned between two
1445 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001446 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001447 * fields. Note that msg->sol will be initialized after completing the first
1448 * state, so that none of the msg pointers has to be initialized prior to the
1449 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001451void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452{
Willy Tarreau3770f232013-12-07 00:01:53 +01001453 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001455 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001456
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001457 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001458 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001459 ptr = buf->p + msg->next;
1460 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001461
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 if (unlikely(ptr >= end))
1463 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001466 /*
1467 * First, states that are specific to the response only.
1468 * We check them first so that request and headers are
1469 * closer to each other (accessed more often).
1470 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001471 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001472 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001473 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001474 /* we have a start of message, but we have to check
1475 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001476 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001477 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001478 if (unlikely(ptr != buf->p)) {
1479 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001480 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001481 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001482 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001483 }
Willy Tarreau26927362012-05-18 23:22:52 +02001484 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001485 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001486 hdr_idx_init(idx);
1487 state = HTTP_MSG_RPVER;
1488 goto http_msg_rpver;
1489 }
1490
1491 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1492 goto http_msg_invalid;
1493
1494 if (unlikely(*ptr == '\n'))
1495 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1496 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1497 /* stop here */
1498
Willy Tarreau8973c702007-01-21 23:58:29 +01001499 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001500 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001501 EXPECT_LF_HERE(ptr, http_msg_invalid);
1502 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1503 /* stop here */
1504
Willy Tarreau8973c702007-01-21 23:58:29 +01001505 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001506 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001507 case HTTP_MSG_RPVER_SP:
1508 case HTTP_MSG_RPCODE:
1509 case HTTP_MSG_RPCODE_SP:
1510 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001511 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001512 state, ptr, end,
1513 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001514 if (unlikely(!ptr))
1515 return;
1516
1517 /* we have a full response and we know that we have either a CR
1518 * or an LF at <ptr>.
1519 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001520 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1521
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001522 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001523 if (likely(*ptr == '\r'))
1524 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1525 goto http_msg_rpline_end;
1526
Willy Tarreau8973c702007-01-21 23:58:29 +01001527 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001528 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001529 /* msg->sol must point to the first of CR or LF. */
1530 EXPECT_LF_HERE(ptr, http_msg_invalid);
1531 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1532 /* stop here */
1533
1534 /*
1535 * Second, states that are specific to the request only
1536 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001538 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001540 /* we have a start of message, but we have to check
1541 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001542 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001543 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001544 if (likely(ptr != buf->p)) {
1545 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001546 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001547 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001548 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 }
Willy Tarreau26927362012-05-18 23:22:52 +02001550 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001551 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001552 state = HTTP_MSG_RQMETH;
1553 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001555
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1557 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 if (unlikely(*ptr == '\n'))
1560 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1561 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001562 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001565 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 EXPECT_LF_HERE(ptr, http_msg_invalid);
1567 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001568 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001571 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 case HTTP_MSG_RQMETH_SP:
1573 case HTTP_MSG_RQURI:
1574 case HTTP_MSG_RQURI_SP:
1575 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001576 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001577 state, ptr, end,
1578 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 if (unlikely(!ptr))
1580 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001581
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 /* we have a full request and we know that we have either a CR
1583 * or an LF at <ptr>.
1584 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001586
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001587 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 if (likely(*ptr == '\r'))
1589 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001591
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001593 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 /* check for HTTP/0.9 request : no version information available.
1595 * msg->sol must point to the first of CR or LF.
1596 */
1597 if (unlikely(msg->sl.rq.v_l == 0))
1598 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 EXPECT_LF_HERE(ptr, http_msg_invalid);
1601 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001602 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001603
Willy Tarreau8973c702007-01-21 23:58:29 +01001604 /*
1605 * Common states below
1606 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001608 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001609 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 if (likely(!HTTP_IS_CRLF(*ptr))) {
1611 goto http_msg_hdr_name;
1612 }
1613
1614 if (likely(*ptr == '\r'))
1615 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1616 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001617
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001619 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 /* assumes msg->sol points to the first char */
1621 if (likely(HTTP_IS_TOKEN(*ptr)))
1622 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001623
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001624 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001626
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001627 if (likely(msg->err_pos < -1) || *ptr == '\n')
1628 goto http_msg_invalid;
1629
1630 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001631 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001632
1633 /* and we still accept this non-token character */
1634 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001635
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001637 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001638 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 if (likely(HTTP_IS_SPHT(*ptr)))
1640 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001643 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001644
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 if (likely(!HTTP_IS_CRLF(*ptr))) {
1646 goto http_msg_hdr_val;
1647 }
1648
1649 if (likely(*ptr == '\r'))
1650 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1651 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001654 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001655 EXPECT_LF_HERE(ptr, http_msg_invalid);
1656 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001657
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001659 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660 if (likely(HTTP_IS_SPHT(*ptr))) {
1661 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001662 for (; buf->p + msg->sov < ptr; msg->sov++)
1663 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 goto http_msg_hdr_l1_sp;
1665 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001666 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001667 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 goto http_msg_complete_header;
1669
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001670 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001671 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001672 /* assumes msg->sol points to the first char, and msg->sov
1673 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 */
1675 if (likely(!HTTP_IS_CRLF(*ptr)))
1676 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001677
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001678 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 /* Note: we could also copy eol into ->eoh so that we have the
1680 * real header end in case it ends with lots of LWS, but is this
1681 * really needed ?
1682 */
1683 if (likely(*ptr == '\r'))
1684 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1685 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001686
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001687 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001688 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 EXPECT_LF_HERE(ptr, http_msg_invalid);
1690 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001691
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001693 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1695 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001696 for (; buf->p + msg->eol < ptr; msg->eol++)
1697 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 goto http_msg_hdr_val;
1699 }
1700 http_msg_complete_header:
1701 /*
1702 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001703 * Assumes msg->sol points to the first char, msg->sov points
1704 * to the first character of the value and msg->eol to the
1705 * first CR or LF so we know how the line ends. We insert last
1706 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001707 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001708 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 idx, idx->tail) < 0))
1710 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001711
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001712 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001713 if (likely(!HTTP_IS_CRLF(*ptr))) {
1714 goto http_msg_hdr_name;
1715 }
1716
1717 if (likely(*ptr == '\r'))
1718 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1719 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001720
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001722 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001723 /* Assumes msg->sol points to the first of either CR or LF.
1724 * Sets ->sov and ->next to the total header length, ->eoh to
1725 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1726 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001727 EXPECT_LF_HERE(ptr, http_msg_invalid);
1728 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001729 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001730 msg->eoh = msg->sol;
1731 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001732 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001733 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001735
1736 case HTTP_MSG_ERROR:
1737 /* this may only happen if we call http_msg_analyser() twice with an error */
1738 break;
1739
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001741#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1743 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001744#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001745 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 }
1747 http_msg_ood:
1748 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001749 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001750 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001752
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 http_msg_invalid:
1754 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001755 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001756 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 return;
1758}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001759
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001760/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1761 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1762 * nothing is done and 1 is returned.
1763 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001764static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001765{
1766 int delta;
1767 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001768 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001769
1770 if (msg->sl.rq.v_l != 0)
1771 return 1;
1772
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001773 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1774 if (txn->meth != HTTP_METH_GET)
1775 return 0;
1776
Willy Tarreau9b28e032012-10-12 23:49:43 +02001777 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001778 delta = 0;
1779
1780 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001781 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1782 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001783 }
1784 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001785 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001786 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001787 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001788 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001789 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001790 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001791 NULL, NULL);
1792 if (unlikely(!cur_end))
1793 return 0;
1794
1795 /* we have a full HTTP/1.0 request now and we know that
1796 * we have either a CR or an LF at <ptr>.
1797 */
1798 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1799 return 1;
1800}
1801
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001802/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001803 * and "keep-alive" values. If we already know that some headers may safely
1804 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001805 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1806 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001807 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001808 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1809 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1810 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001811 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001812 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001813void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001814{
Willy Tarreau5b154472009-12-21 20:11:07 +01001815 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001816 const char *hdr_val = "Connection";
1817 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001818
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001819 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001820 return;
1821
Willy Tarreau88d349d2010-01-25 12:15:43 +01001822 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1823 hdr_val = "Proxy-Connection";
1824 hdr_len = 16;
1825 }
1826
Willy Tarreau5b154472009-12-21 20:11:07 +01001827 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001828 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001829 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001830 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1831 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001832 if (to_del & 2)
1833 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001834 else
1835 txn->flags |= TX_CON_KAL_SET;
1836 }
1837 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1838 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001839 if (to_del & 1)
1840 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001841 else
1842 txn->flags |= TX_CON_CLO_SET;
1843 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001844 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1845 txn->flags |= TX_HDR_CONN_UPG;
1846 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001847 }
1848
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001849 txn->flags |= TX_HDR_CONN_PRS;
1850 return;
1851}
Willy Tarreau5b154472009-12-21 20:11:07 +01001852
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001853/* Apply desired changes on the Connection: header. Values may be removed and/or
1854 * added depending on the <wanted> flags, which are exclusively composed of
1855 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1856 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1857 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001858void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001859{
1860 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001861 const char *hdr_val = "Connection";
1862 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001863
1864 ctx.idx = 0;
1865
Willy Tarreau88d349d2010-01-25 12:15:43 +01001866
1867 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1868 hdr_val = "Proxy-Connection";
1869 hdr_len = 16;
1870 }
1871
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001872 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001873 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001874 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1875 if (wanted & TX_CON_KAL_SET)
1876 txn->flags |= TX_CON_KAL_SET;
1877 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001878 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001879 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001880 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1881 if (wanted & TX_CON_CLO_SET)
1882 txn->flags |= TX_CON_CLO_SET;
1883 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001884 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001885 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001886 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001887
1888 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1889 return;
1890
1891 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1892 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001893 hdr_val = "Connection: close";
1894 hdr_len = 17;
1895 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1896 hdr_val = "Proxy-Connection: close";
1897 hdr_len = 23;
1898 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001899 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001900 }
1901
1902 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1903 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001904 hdr_val = "Connection: keep-alive";
1905 hdr_len = 22;
1906 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1907 hdr_val = "Proxy-Connection: keep-alive";
1908 hdr_len = 28;
1909 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001910 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001911 }
1912 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001913}
1914
Willy Tarreauc24715e2014-04-17 15:21:20 +02001915/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1916 * the first byte of data after the chunk size, so that we know we can forward
1917 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1918 * the chunk size. That way it is always possible to differentiate between the
1919 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001920 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001922 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001923static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001924{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001925 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001926 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001927 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001928 const char *end = buf->data + buf->size;
1929 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001930 unsigned int chunk = 0;
1931
1932 /* The chunk size is in the following form, though we are only
1933 * interested in the size and CRLF :
1934 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1935 */
1936 while (1) {
1937 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001938 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001939 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001941 if (c < 0) /* not a hex digit anymore */
1942 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001943 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001944 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001945 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001946 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001947 chunk = (chunk << 4) + c;
1948 }
1949
Willy Tarreaud98cf932009-12-27 22:54:55 +01001950 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001951 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001952 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953
1954 while (http_is_spht[(unsigned char)*ptr]) {
1955 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001956 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001957 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001958 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001959 }
1960
Willy Tarreaud98cf932009-12-27 22:54:55 +01001961 /* Up to there, we know that at least one byte is present at *ptr. Check
1962 * for the end of chunk size.
1963 */
1964 while (1) {
1965 if (likely(HTTP_IS_CRLF(*ptr))) {
1966 /* we now have a CR or an LF at ptr */
1967 if (likely(*ptr == '\r')) {
1968 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001969 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001970 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001971 return 0;
1972 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001973
Willy Tarreaud98cf932009-12-27 22:54:55 +01001974 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001975 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001976 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001977 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001978 /* done */
1979 break;
1980 }
1981 else if (*ptr == ';') {
1982 /* chunk extension, ends at next CRLF */
1983 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001984 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001985 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001986 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001987
1988 while (!HTTP_IS_CRLF(*ptr)) {
1989 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001990 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001991 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001992 return 0;
1993 }
1994 /* we have a CRLF now, loop above */
1995 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001996 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001997 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001998 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001999 }
2000
Willy Tarreaud98cf932009-12-27 22:54:55 +01002001 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002002 * which may or may not be present. We save that into ->next,
2003 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002004 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002005 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002006 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002007 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002008 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002009 msg->chunk_len = chunk;
2010 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002011 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002012 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002013 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002014 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002015 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002016}
2017
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002018/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002019 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002020 * the trailers is found, it is automatically scheduled to be forwarded,
2021 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2022 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002023 * except maybe msg->next if it could parse some lines, and returns zero.
2024 * If a parse error is encountered, the function returns < 0 and does not
2025 * change anything except maybe msg->next. Note that the message must
2026 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002027 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002028 * forwarding, and that msg->next exactly matches the length of trailers
2029 * already parsed and not forwarded. It is also important to note that this
2030 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002031 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002032static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002033{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002034 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002035
Willy Tarreaua458b672012-03-05 11:17:50 +01002036 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002037 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002038 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002039 const char *ptr = b_ptr(buf, msg->next);
2040 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002041 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002042
2043 /* scan current line and stop at LF or CRLF */
2044 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002045 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002046 return 0;
2047
2048 if (*ptr == '\n') {
2049 if (!p1)
2050 p1 = ptr;
2051 p2 = ptr;
2052 break;
2053 }
2054
2055 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002056 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002057 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002058 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002059 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002060 p1 = ptr;
2061 }
2062
2063 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002064 if (ptr >= buf->data + buf->size)
2065 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002066 }
2067
2068 /* after LF; point to beginning of next line */
2069 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002070 if (p2 >= buf->data + buf->size)
2071 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002072
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002073 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002074 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002075 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002076
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002077 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002078 /* LF/CRLF at beginning of line => end of trailers at p2.
2079 * Everything was scheduled for forwarding, there's nothing
2080 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002081 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002082 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002083 msg->msg_state = HTTP_MSG_DONE;
2084 return 1;
2085 }
2086 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002087 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002088 }
2089}
2090
Willy Tarreauc24715e2014-04-17 15:21:20 +02002091/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2092 * or a possible LF alone at the end of a chunk. It automatically adjusts
2093 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002094 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2096 * not enough data are available, the function does not change anything and
2097 * returns zero. If a parse error is encountered, the function returns < 0 and
2098 * does not change anything. Note: this function is designed to parse wrapped
2099 * CRLF at the end of the buffer.
2100 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002101static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002102{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002103 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002104 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002105 int bytes;
2106
2107 /* NB: we'll check data availabilty at the end. It's not a
2108 * problem because whatever we match first will be checked
2109 * against the correct length.
2110 */
2111 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002112 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002113 if (*ptr == '\r') {
2114 bytes++;
2115 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002116 if (ptr >= buf->data + buf->size)
2117 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002118 }
2119
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002120 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002121 return 0;
2122
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002123 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002124 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002125 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002126 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002127
2128 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002129 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002130 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002131 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002132 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002133 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2134 return 1;
2135}
Willy Tarreau5b154472009-12-21 20:11:07 +01002136
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002137/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2138 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2139 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2140 * Unparsable qvalues return 1000 as "q=1.000".
2141 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002142int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002143{
2144 int q = 1000;
2145
2146 if (!isdigit(*qvalue))
2147 goto out;
2148 q = (*qvalue++ - '0') * 1000;
2149
2150 if (*qvalue++ != '.')
2151 goto out;
2152
2153 if (!isdigit(*qvalue))
2154 goto out;
2155 q += (*qvalue++ - '0') * 100;
2156
2157 if (!isdigit(*qvalue))
2158 goto out;
2159 q += (*qvalue++ - '0') * 10;
2160
2161 if (!isdigit(*qvalue))
2162 goto out;
2163 q += (*qvalue++ - '0') * 1;
2164 out:
2165 if (q > 1000)
2166 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002167 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002168 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002169 return q;
2170}
William Lallemand82fe75c2012-10-23 10:25:10 +02002171
2172/*
2173 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002174 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002175int select_compression_request_header(struct session *s, struct buffer *req)
2176{
2177 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002178 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002179 struct hdr_ctx ctx;
2180 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002181 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002182
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002183 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2184 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002185 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2186 */
2187 ctx.idx = 0;
2188 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2189 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002190 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2191 (ctx.vlen < 31 ||
2192 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2193 ctx.line[ctx.val + 30] < '6' ||
2194 (ctx.line[ctx.val + 30] == '6' &&
2195 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2196 s->comp_algo = NULL;
2197 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002198 }
2199
William Lallemand82fe75c2012-10-23 10:25:10 +02002200 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002201 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002202 int best_q = 0;
2203
William Lallemand82fe75c2012-10-23 10:25:10 +02002204 ctx.idx = 0;
2205 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002206 const char *qval;
2207 int q;
2208 int toklen;
2209
2210 /* try to isolate the token from the optional q-value */
2211 toklen = 0;
2212 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2213 toklen++;
2214
2215 qval = ctx.line + ctx.val + toklen;
2216 while (1) {
2217 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2218 qval++;
2219
2220 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2221 qval = NULL;
2222 break;
2223 }
2224 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002225
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002226 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2227 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002228
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002229 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2230 qval = NULL;
2231 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002232 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002233 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2234 break;
2235
2236 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2237 qval++;
2238 }
2239
2240 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002241 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002242
2243 if (q <= best_q)
2244 continue;
2245
2246 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2247 if (*(ctx.line + ctx.val) == '*' ||
2248 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2249 s->comp_algo = comp_algo;
2250 best_q = q;
2251 break;
2252 }
2253 }
2254 }
2255 }
2256
2257 /* remove all occurrences of the header when "compression offload" is set */
2258 if (s->comp_algo) {
2259 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2260 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2261 ctx.idx = 0;
2262 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2263 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002264 }
2265 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002266 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002267 }
2268
2269 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002270 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2271 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002272 if (comp_algo->add_data == identity_add_data) {
2273 s->comp_algo = comp_algo;
2274 return 1;
2275 }
2276 }
2277 }
2278
2279 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002280 return 0;
2281}
2282
2283/*
2284 * Selects a comression algorithm depending of the server response.
2285 */
2286int select_compression_response_header(struct session *s, struct buffer *res)
2287{
2288 struct http_txn *txn = &s->txn;
2289 struct http_msg *msg = &txn->rsp;
2290 struct hdr_ctx ctx;
2291 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002292
2293 /* no common compression algorithm was found in request header */
2294 if (s->comp_algo == NULL)
2295 goto fail;
2296
2297 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002298 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002299 goto fail;
2300
William Lallemandd3002612012-11-26 14:34:47 +01002301 /* 200 only */
2302 if (txn->status != 200)
2303 goto fail;
2304
William Lallemand82fe75c2012-10-23 10:25:10 +02002305 /* Content-Length is null */
2306 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2307 goto fail;
2308
2309 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002310 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002311 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2312 goto fail;
2313
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002314 /* no compression when Cache-Control: no-transform is present in the message */
2315 ctx.idx = 0;
2316 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2317 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2318 goto fail;
2319 }
2320
William Lallemand82fe75c2012-10-23 10:25:10 +02002321 comp_type = NULL;
2322
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002323 /* we don't want to compress multipart content-types, nor content-types that are
2324 * not listed in the "compression type" directive if any. If no content-type was
2325 * found but configuration requires one, we don't compress either. Backend has
2326 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002327 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002328 ctx.idx = 0;
2329 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2330 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2331 goto fail;
2332
2333 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2334 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002335 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002336 if (ctx.vlen >= comp_type->name_len &&
2337 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002338 /* this Content-Type should be compressed */
2339 break;
2340 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002341 /* this Content-Type should not be compressed */
2342 if (comp_type == NULL)
2343 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002344 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002345 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002346 else { /* no content-type header */
2347 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2348 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002349 }
2350
William Lallemandd85f9172012-11-09 17:05:39 +01002351 /* limit compression rate */
2352 if (global.comp_rate_lim > 0)
2353 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2354 goto fail;
2355
William Lallemand072a2bf2012-11-20 17:01:01 +01002356 /* limit cpu usage */
2357 if (idle_pct < compress_min_idle)
2358 goto fail;
2359
William Lallemand4c49fae2012-11-07 15:00:23 +01002360 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002361 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002362 goto fail;
2363
William Lallemandec3e3892012-11-12 17:02:18 +01002364 s->flags |= SN_COMP_READY;
2365
William Lallemand82fe75c2012-10-23 10:25:10 +02002366 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002367 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002368 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2369 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2370
2371 /* add Transfer-Encoding header */
2372 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2373 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2374
2375 /*
2376 * Add Content-Encoding header when it's not identity encoding.
2377 * RFC 2616 : Identity encoding: This content-coding is used only in the
2378 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2379 * header.
2380 */
2381 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002382 trash.len = 18;
2383 memcpy(trash.str, "Content-Encoding: ", trash.len);
2384 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2385 trash.len += s->comp_algo->name_len;
2386 trash.str[trash.len] = '\0';
2387 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002388 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002389 return 1;
2390
2391fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002392 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002393 return 0;
2394}
2395
2396
Willy Tarreaud787e662009-07-07 10:14:51 +02002397/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2398 * processing can continue on next analysers, or zero if it either needs more
2399 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2400 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2401 * when it has nothing left to do, and may remove any analyser when it wants to
2402 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002403 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002404int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002405{
Willy Tarreau59234e92008-11-30 23:51:27 +01002406 /*
2407 * We will parse the partial (or complete) lines.
2408 * We will check the request syntax, and also join multi-line
2409 * headers. An index of all the lines will be elaborated while
2410 * parsing.
2411 *
2412 * For the parsing, we use a 28 states FSM.
2413 *
2414 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002415 * req->buf->p = beginning of request
2416 * req->buf->p + msg->eoh = end of processed headers / start of current one
2417 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002418 * msg->eol = end of current header or line (LF or CRLF)
2419 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002420 *
2421 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002422 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002423 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2424 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002425 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002426
Willy Tarreau59234e92008-11-30 23:51:27 +01002427 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002428 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002429 struct http_txn *txn = &s->txn;
2430 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002431 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002432
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002433 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002434 now_ms, __FUNCTION__,
2435 s,
2436 req,
2437 req->rex, req->wex,
2438 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002439 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002440 req->analysers);
2441
Willy Tarreau52a0c602009-08-16 22:45:38 +02002442 /* we're speaking HTTP here, so let's speak HTTP to the client */
2443 s->srv_error = http_return_srv_error;
2444
Willy Tarreau83e3af02009-12-28 17:39:57 +01002445 /* There's a protected area at the end of the buffer for rewriting
2446 * purposes. We don't want to start to parse the request if the
2447 * protected area is affected, because we may have to move processed
2448 * data later, which is much more complicated.
2449 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002450 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002451 if (txn->flags & TX_NOT_FIRST) {
2452 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002453 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002454 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002455 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002456 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002457 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002458 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002459 return 0;
2460 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002461 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2462 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2463 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002464 }
2465
Willy Tarreau065e8332010-01-08 00:30:20 +01002466 /* Note that we have the same problem with the response ; we
2467 * may want to send a redirect, error or anything which requires
2468 * some spare space. So we'll ensure that we have at least
2469 * maxrewrite bytes available in the response buffer before
2470 * processing that one. This will only affect pipelined
2471 * keep-alive requests.
2472 */
2473 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002474 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002475 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2476 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2477 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002478 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002479 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002480 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002481 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002482 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002483 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002484 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002485 return 0;
2486 }
2487 }
2488
Willy Tarreau9b28e032012-10-12 23:49:43 +02002489 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002490 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002491 }
2492
Willy Tarreau59234e92008-11-30 23:51:27 +01002493 /* 1: we might have to print this header in debug mode */
2494 if (unlikely((global.mode & MODE_DEBUG) &&
2495 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002496 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002497 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002498
Willy Tarreau9b28e032012-10-12 23:49:43 +02002499 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002500 /* this is a bit complex : in case of error on the request line,
2501 * we know that rq.l is still zero, so we display only the part
2502 * up to the end of the line (truncated by debug_hdr).
2503 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002504 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002505 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002506
Willy Tarreau59234e92008-11-30 23:51:27 +01002507 sol += hdr_idx_first_pos(&txn->hdr_idx);
2508 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002509
Willy Tarreau59234e92008-11-30 23:51:27 +01002510 while (cur_idx) {
2511 eol = sol + txn->hdr_idx.v[cur_idx].len;
2512 debug_hdr("clihdr", s, sol, eol);
2513 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2514 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002515 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002516 }
2517
Willy Tarreau58f10d72006-12-04 02:26:12 +01002518
Willy Tarreau59234e92008-11-30 23:51:27 +01002519 /*
2520 * Now we quickly check if we have found a full valid request.
2521 * If not so, we check the FD and buffer states before leaving.
2522 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002523 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002524 * requests are checked first. When waiting for a second request
2525 * on a keep-alive session, if we encounter and error, close, t/o,
2526 * we note the error in the session flags but don't set any state.
2527 * Since the error will be noted there, it will not be counted by
2528 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002529 * Last, we may increase some tracked counters' http request errors on
2530 * the cases that are deliberately the client's fault. For instance,
2531 * a timeout or connection reset is not counted as an error. However
2532 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002533 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002534
Willy Tarreau655dce92009-11-08 13:10:58 +01002535 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002536 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002537 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002538 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002539 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002540 session_inc_http_req_ctr(s);
2541 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002542 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002543 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002544 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002545
Willy Tarreau59234e92008-11-30 23:51:27 +01002546 /* 1: Since we are in header mode, if there's no space
2547 * left for headers, we won't be able to free more
2548 * later, so the session will never terminate. We
2549 * must terminate it now.
2550 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002551 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002552 /* FIXME: check if URI is set and return Status
2553 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002554 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002555 session_inc_http_req_ctr(s);
2556 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002557 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002558 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002559 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002560 goto return_bad_req;
2561 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002562
Willy Tarreau59234e92008-11-30 23:51:27 +01002563 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002564 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002565 if (!(s->flags & SN_ERR_MASK))
2566 s->flags |= SN_ERR_CLICL;
2567
Willy Tarreaufcffa692010-01-10 14:21:19 +01002568 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002569 goto failed_keep_alive;
2570
Willy Tarreau59234e92008-11-30 23:51:27 +01002571 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002572 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002573 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002574 session_inc_http_err_ctr(s);
2575 }
2576
Willy Tarreaudc979f22012-12-04 10:39:01 +01002577 txn->status = 400;
2578 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002579 msg->msg_state = HTTP_MSG_ERROR;
2580 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002581
Willy Tarreauda7ff642010-06-23 11:44:09 +02002582 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002583 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002584 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002585 if (s->listener->counters)
2586 s->listener->counters->failed_req++;
2587
Willy Tarreau59234e92008-11-30 23:51:27 +01002588 if (!(s->flags & SN_FINST_MASK))
2589 s->flags |= SN_FINST_R;
2590 return 0;
2591 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002592
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002594 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002595 if (!(s->flags & SN_ERR_MASK))
2596 s->flags |= SN_ERR_CLITO;
2597
Willy Tarreaufcffa692010-01-10 14:21:19 +01002598 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002599 goto failed_keep_alive;
2600
Willy Tarreau59234e92008-11-30 23:51:27 +01002601 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002602 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002603 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002604 session_inc_http_err_ctr(s);
2605 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002606 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002607 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002608 msg->msg_state = HTTP_MSG_ERROR;
2609 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002610
Willy Tarreauda7ff642010-06-23 11:44:09 +02002611 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002612 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002613 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002614 if (s->listener->counters)
2615 s->listener->counters->failed_req++;
2616
Willy Tarreau59234e92008-11-30 23:51:27 +01002617 if (!(s->flags & SN_FINST_MASK))
2618 s->flags |= SN_FINST_R;
2619 return 0;
2620 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002621
Willy Tarreau59234e92008-11-30 23:51:27 +01002622 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002623 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002624 if (!(s->flags & SN_ERR_MASK))
2625 s->flags |= SN_ERR_CLICL;
2626
Willy Tarreaufcffa692010-01-10 14:21:19 +01002627 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002628 goto failed_keep_alive;
2629
Willy Tarreau4076a152009-04-02 15:18:36 +02002630 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002631 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002633 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002634 msg->msg_state = HTTP_MSG_ERROR;
2635 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002636
Willy Tarreauda7ff642010-06-23 11:44:09 +02002637 session_inc_http_err_ctr(s);
2638 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002639 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002640 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002641 if (s->listener->counters)
2642 s->listener->counters->failed_req++;
2643
Willy Tarreau59234e92008-11-30 23:51:27 +01002644 if (!(s->flags & SN_FINST_MASK))
2645 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002646 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002647 }
2648
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002649 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002650 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2651 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002652#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002653 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && conn_ctrl_ready(__objt_conn(s->req->prod->end))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002654 /* We need more data, we have to re-enable quick-ack in case we
2655 * previously disabled it, otherwise we might cause the client
2656 * to delay next data.
2657 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002658 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002659 }
2660#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002661
Willy Tarreaufcffa692010-01-10 14:21:19 +01002662 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2663 /* If the client starts to talk, let's fall back to
2664 * request timeout processing.
2665 */
2666 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002667 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002668 }
2669
Willy Tarreau59234e92008-11-30 23:51:27 +01002670 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002671 if (!tick_isset(req->analyse_exp)) {
2672 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2673 (txn->flags & TX_WAIT_NEXT_RQ) &&
2674 tick_isset(s->be->timeout.httpka))
2675 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2676 else
2677 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2678 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002679
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 /* we're not ready yet */
2681 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002682
2683 failed_keep_alive:
2684 /* Here we process low-level errors for keep-alive requests. In
2685 * short, if the request is not the first one and it experiences
2686 * a timeout, read error or shutdown, we just silently close so
2687 * that the client can try again.
2688 */
2689 txn->status = 0;
2690 msg->msg_state = HTTP_MSG_RQBEFORE;
2691 req->analysers = 0;
2692 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002693 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002694 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002695 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002696 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002697 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002698
Willy Tarreaud787e662009-07-07 10:14:51 +02002699 /* OK now we have a complete HTTP request with indexed headers. Let's
2700 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002701 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002702 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002703 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002704 * byte after the last LF. msg->sov points to the first byte of data.
2705 * msg->eol cannot be trusted because it may have been left uninitialized
2706 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002707 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002708
Willy Tarreauda7ff642010-06-23 11:44:09 +02002709 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002710 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2711
Willy Tarreaub16a5742010-01-10 14:46:16 +01002712 if (txn->flags & TX_WAIT_NEXT_RQ) {
2713 /* kill the pending keep-alive timeout */
2714 txn->flags &= ~TX_WAIT_NEXT_RQ;
2715 req->analyse_exp = TICK_ETERNITY;
2716 }
2717
2718
Willy Tarreaud787e662009-07-07 10:14:51 +02002719 /* Maybe we found in invalid header name while we were configured not
2720 * to block on that, so we have to capture it now.
2721 */
2722 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002723 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002724
Willy Tarreau59234e92008-11-30 23:51:27 +01002725 /*
2726 * 1: identify the method
2727 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002728 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002729
2730 /* we can make use of server redirect on GET and HEAD */
2731 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2732 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002733
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 /*
2735 * 2: check if the URI matches the monitor_uri.
2736 * We have to do this for every request which gets in, because
2737 * the monitor-uri is defined by the frontend.
2738 */
2739 if (unlikely((s->fe->monitor_uri_len != 0) &&
2740 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002741 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002742 s->fe->monitor_uri,
2743 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002744 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002745 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002746 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002747 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002748
Willy Tarreau59234e92008-11-30 23:51:27 +01002749 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002750 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002751
Willy Tarreau59234e92008-11-30 23:51:27 +01002752 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002753 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002754 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002755
Willy Tarreau59234e92008-11-30 23:51:27 +01002756 ret = acl_pass(ret);
2757 if (cond->pol == ACL_COND_UNLESS)
2758 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002759
Willy Tarreau59234e92008-11-30 23:51:27 +01002760 if (ret) {
2761 /* we fail this request, let's return 503 service unavail */
2762 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002763 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002764 if (!(s->flags & SN_ERR_MASK))
2765 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002766 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002767 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002768 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002769
Willy Tarreau59234e92008-11-30 23:51:27 +01002770 /* nothing to fail, let's reply normaly */
2771 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002772 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002773 if (!(s->flags & SN_ERR_MASK))
2774 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002775 goto return_prx_cond;
2776 }
2777
2778 /*
2779 * 3: Maybe we have to copy the original REQURI for the logs ?
2780 * Note: we cannot log anymore if the request has been
2781 * classified as invalid.
2782 */
2783 if (unlikely(s->logs.logwait & LW_REQ)) {
2784 /* we have a complete HTTP request that we must log */
2785 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2786 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002787
Willy Tarreau59234e92008-11-30 23:51:27 +01002788 if (urilen >= REQURI_LEN)
2789 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002790 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002791 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002792
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002793 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002794 s->do_log(s);
2795 } else {
2796 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002797 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002798 }
Willy Tarreau06619262006-12-17 08:37:22 +01002799
Willy Tarreau59234e92008-11-30 23:51:27 +01002800 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002801 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002802 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002803
Willy Tarreau5b154472009-12-21 20:11:07 +01002804 /* ... and check if the request is HTTP/1.1 or above */
2805 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002806 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2807 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2808 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002809 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002810
2811 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002812 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002813
Willy Tarreau88d349d2010-01-25 12:15:43 +01002814 /* if the frontend has "option http-use-proxy-header", we'll check if
2815 * we have what looks like a proxied connection instead of a connection,
2816 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2817 * Note that this is *not* RFC-compliant, however browsers and proxies
2818 * happen to do that despite being non-standard :-(
2819 * We consider that a request not beginning with either '/' or '*' is
2820 * a proxied connection, which covers both "scheme://location" and
2821 * CONNECT ip:port.
2822 */
2823 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002824 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002825 txn->flags |= TX_USE_PX_CONN;
2826
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002827 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002828 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002829
Willy Tarreau59234e92008-11-30 23:51:27 +01002830 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002831 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002832 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002833 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002834
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002835 /* 6: determine the transfer-length.
2836 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2837 * the presence of a message-body in a REQUEST and its transfer length
2838 * must be determined that way (in order of precedence) :
2839 * 1. The presence of a message-body in a request is signaled by the
2840 * inclusion of a Content-Length or Transfer-Encoding header field
2841 * in the request's header fields. When a request message contains
2842 * both a message-body of non-zero length and a method that does
2843 * not define any semantics for that request message-body, then an
2844 * origin server SHOULD either ignore the message-body or respond
2845 * with an appropriate error message (e.g., 413). A proxy or
2846 * gateway, when presented the same request, SHOULD either forward
2847 * the request inbound with the message- body or ignore the
2848 * message-body when determining a response.
2849 *
2850 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2851 * and the "chunked" transfer-coding (Section 6.2) is used, the
2852 * transfer-length is defined by the use of this transfer-coding.
2853 * If a Transfer-Encoding header field is present and the "chunked"
2854 * transfer-coding is not present, the transfer-length is defined
2855 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002856 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002857 * 3. If a Content-Length header field is present, its decimal value in
2858 * OCTETs represents both the entity-length and the transfer-length.
2859 * If a message is received with both a Transfer-Encoding header
2860 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002861 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002862 * 4. By the server closing the connection. (Closing the connection
2863 * cannot be used to indicate the end of a request body, since that
2864 * would leave no possibility for the server to send back a response.)
2865 *
2866 * Whenever a transfer-coding is applied to a message-body, the set of
2867 * transfer-codings MUST include "chunked", unless the message indicates
2868 * it is terminated by closing the connection. When the "chunked"
2869 * transfer-coding is used, it MUST be the last transfer-coding applied
2870 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002871 */
2872
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002873 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002874 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002875 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002876 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002877 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002878 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002879 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2880 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002881 /* bad transfer-encoding (chunked followed by something else) */
2882 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002883 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002884 break;
2885 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002886 }
2887
Willy Tarreau32b47f42009-10-18 20:55:02 +02002888 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002889 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002890 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002891 signed long long cl;
2892
Willy Tarreauad14f752011-09-02 20:33:27 +02002893 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002894 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002895 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002896 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002897
Willy Tarreauad14f752011-09-02 20:33:27 +02002898 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002899 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002900 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002901 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002902
Willy Tarreauad14f752011-09-02 20:33:27 +02002903 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002904 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002905 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002906 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002907
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002908 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002909 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002910 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002911 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002912
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002913 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002914 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002915 }
2916
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002917 /* bodyless requests have a known length */
2918 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002919 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002920
Willy Tarreau179085c2014-04-28 16:48:56 +02002921 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2922 * only change if both the request and the config reference something else.
2923 * Option httpclose by itself sets tunnel mode where headers are mangled.
2924 * However, if another mode is set, it will affect it (eg: server-close/
2925 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2926 * if FE and BE have the same settings (common). The method consists in
2927 * checking if options changed between the two calls (implying that either
2928 * one is non-null, or one of them is non-null and we are there for the first
2929 * time.
2930 */
2931 if (!(txn->flags & TX_HDR_CONN_PRS) ||
2932 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
2933 int tmp = TX_CON_WANT_KAL;
2934
2935 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2936 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
2937 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2938 tmp = TX_CON_WANT_TUN;
2939
2940 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2941 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2942 tmp = TX_CON_WANT_TUN;
2943 }
2944
2945 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
2946 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2947 /* option httpclose + server_close => forceclose */
2948 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2949 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2950 tmp = TX_CON_WANT_CLO;
2951 else
2952 tmp = TX_CON_WANT_SCL;
2953 }
2954
2955 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
2956 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2957 tmp = TX_CON_WANT_CLO;
2958
2959 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2960 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2961
2962 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2963 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2964 /* parse the Connection header and possibly clean it */
2965 int to_del = 0;
2966 if ((msg->flags & HTTP_MSGF_VER_11) ||
2967 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2968 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
2969 to_del |= 2; /* remove "keep-alive" */
2970 if (!(msg->flags & HTTP_MSGF_VER_11))
2971 to_del |= 1; /* remove "close" */
2972 http_parse_connection_header(txn, msg, to_del);
2973 }
2974
2975 /* check if client or config asks for explicit close in KAL/SCL */
2976 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2977 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2978 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2979 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2980 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
2981 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
2982 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2983 }
2984
Willy Tarreaud787e662009-07-07 10:14:51 +02002985 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002986 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002987 req->analyse_exp = TICK_ETERNITY;
2988 return 1;
2989
2990 return_bad_req:
2991 /* We centralize bad requests processing here */
2992 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2993 /* we detected a parsing error. We want to archive this request
2994 * in the dedicated proxy area for later troubleshooting.
2995 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002996 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002997 }
2998
2999 txn->req.msg_state = HTTP_MSG_ERROR;
3000 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003001 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003002
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003003 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003004 if (s->listener->counters)
3005 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003006
3007 return_prx_cond:
3008 if (!(s->flags & SN_ERR_MASK))
3009 s->flags |= SN_ERR_PRXCOND;
3010 if (!(s->flags & SN_FINST_MASK))
3011 s->flags |= SN_FINST_R;
3012
3013 req->analysers = 0;
3014 req->analyse_exp = TICK_ETERNITY;
3015 return 0;
3016}
3017
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003018
Willy Tarreau347a35d2013-11-22 17:51:09 +01003019/* This function prepares an applet to handle the stats. It can deal with the
3020 * "100-continue" expectation, check that admin rules are met for POST requests,
3021 * and program a response message if something was unexpected. It cannot fail
3022 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003023 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003024 * s->target which is supposed to already point to the stats applet. The caller
3025 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003026 */
3027int http_handle_stats(struct session *s, struct channel *req)
3028{
3029 struct stats_admin_rule *stats_admin_rule;
3030 struct stream_interface *si = s->rep->prod;
3031 struct http_txn *txn = &s->txn;
3032 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003033 struct uri_auth *uri_auth = s->be->uri_auth;
3034 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003035 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003036
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003037 appctx = si_appctx(si);
3038 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3039 appctx->st1 = appctx->st2 = 0;
3040 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3041 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003042 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3043 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003044
3045 uri = msg->chn->buf->p + msg->sl.rq.u;
3046 lookup = uri + uri_auth->uri_len;
3047
3048 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3049 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003050 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003051 break;
3052 }
3053 }
3054
3055 if (uri_auth->refresh) {
3056 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3057 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003058 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003059 break;
3060 }
3061 }
3062 }
3063
3064 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3065 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003066 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003067 break;
3068 }
3069 }
3070
3071 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3072 if (memcmp(h, ";st=", 4) == 0) {
3073 int i;
3074 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003075 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003076 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3077 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003078 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003079 break;
3080 }
3081 }
3082 break;
3083 }
3084 }
3085
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003086 appctx->ctx.stats.scope_str = 0;
3087 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003088 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3089 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3090 int itx = 0;
3091 const char *h2;
3092 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3093 const char *err;
3094
3095 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3096 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003097 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003098 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3099 itx++;
3100 h++;
3101 }
3102
3103 if (itx > STAT_SCOPE_TXT_MAXLEN)
3104 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003105 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003106
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003107 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003108 memcpy(scope_txt, h2, itx);
3109 scope_txt[itx] = '\0';
3110 err = invalid_char(scope_txt);
3111 if (err) {
3112 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003113 appctx->ctx.stats.scope_str = 0;
3114 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003115 }
3116 break;
3117 }
3118 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003119
3120 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003121 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003122 int ret = 1;
3123
3124 if (stats_admin_rule->cond) {
3125 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3126 ret = acl_pass(ret);
3127 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3128 ret = !ret;
3129 }
3130
3131 if (ret) {
3132 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003133 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003134 break;
3135 }
3136 }
3137
3138 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003139 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003140 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003141 /* we'll need the request body, possibly after sending 100-continue */
3142 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003143 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003144 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003145 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003146 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3147 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003148 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003149 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003150 else {
3151 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003152 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003153 }
3154
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003155 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003156 return 1;
3157}
3158
Lukas Tribus67db8df2013-06-23 17:37:13 +02003159/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3160 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3161 */
3162static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3163{
3164#ifdef IP_TOS
3165 if (from.ss_family == AF_INET)
3166 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3167#endif
3168#ifdef IPV6_TCLASS
3169 if (from.ss_family == AF_INET6) {
3170 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3171 /* v4-mapped addresses need IP_TOS */
3172 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3173 else
3174 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3175 }
3176#endif
3177}
3178
Sasha Pachev218f0642014-06-16 12:05:59 -06003179/* Returns the number of characters written to destination,
3180 * -1 on internal error and -2 if no replacement took place.
3181 */
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003182static int http_replace_header(struct my_regex *re, char *dst, uint dst_size, char *val, int len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003183 const char *rep_str)
Sasha Pachev218f0642014-06-16 12:05:59 -06003184{
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003185 if (!regex_exec_match2(re, val, len, MAX_MATCH, pmatch))
Sasha Pachev218f0642014-06-16 12:05:59 -06003186 return -2;
3187
3188 return exp_replace(dst, dst_size, val, rep_str, pmatch);
3189}
3190
3191/* Returns the number of characters written to destination,
3192 * -1 on internal error and -2 if no replacement took place.
3193 */
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003194static int http_replace_value(struct my_regex *re, char *dst, uint dst_size, char *val, int len, char delim,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003195 const char *rep_str)
Sasha Pachev218f0642014-06-16 12:05:59 -06003196{
3197 char* p = val;
3198 char* dst_end = dst + dst_size;
3199 char* dst_p = dst;
3200
3201 for (;;) {
3202 char *p_delim;
Sasha Pachev218f0642014-06-16 12:05:59 -06003203
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003204 /* look for delim. */
3205 p_delim = p;
3206 while (p_delim < p + len && *p_delim != delim)
3207 p_delim++;
Sasha Pachev218f0642014-06-16 12:05:59 -06003208
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003209 if (regex_exec_match2(re, p, p_delim-p, MAX_MATCH, pmatch)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06003210 int replace_n = exp_replace(dst_p, dst_end - dst_p, p, rep_str, pmatch);
3211
3212 if (replace_n < 0)
3213 return -1;
3214
3215 dst_p += replace_n;
3216 } else {
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003217 uint len = p_delim - p;
Sasha Pachev218f0642014-06-16 12:05:59 -06003218
3219 if (dst_p + len >= dst_end)
3220 return -1;
3221
3222 memcpy(dst_p, p, len);
3223 dst_p += len;
3224 }
3225
3226 if (dst_p >= dst_end)
3227 return -1;
3228
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003229 /* end of the replacements. */
3230 if (p_delim >= p + len)
Sasha Pachev218f0642014-06-16 12:05:59 -06003231 break;
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003232
3233 /* Next part. */
3234 *dst_p++ = delim;
3235 p = p_delim + 1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003236 }
3237
3238 return dst_p - dst;
3239}
3240
3241static int http_transform_header(struct session* s, struct http_msg *msg, const char* name, uint name_len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003242 char* buf, struct hdr_idx* idx, struct list *fmt, struct my_regex *re,
Sasha Pachev218f0642014-06-16 12:05:59 -06003243 struct hdr_ctx* ctx, int action)
3244{
3245 ctx->idx = 0;
3246
3247 while (http_find_full_header2(name, name_len, buf, idx, ctx)) {
3248 struct hdr_idx_elem *hdr = idx->v + ctx->idx;
3249 int delta;
3250 char* val = (char*)ctx->line + name_len + 2;
3251 char* val_end = (char*)ctx->line + hdr->len;
Sasha Pachev218f0642014-06-16 12:05:59 -06003252 char* reg_dst_buf;
3253 uint reg_dst_buf_size;
3254 int n_replaced;
3255
Sasha Pachev218f0642014-06-16 12:05:59 -06003256 trash.len = build_logline(s, trash.str, trash.size, fmt);
3257
3258 if (trash.len >= trash.size - 1)
3259 return -1;
3260
3261 reg_dst_buf = trash.str + trash.len + 1;
3262 reg_dst_buf_size = trash.size - trash.len - 1;
3263
3264 switch (action) {
3265 case HTTP_REQ_ACT_REPLACE_VAL:
3266 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003267 n_replaced = http_replace_value(re, reg_dst_buf, reg_dst_buf_size, val, val_end-val, ',', trash.str);
Sasha Pachev218f0642014-06-16 12:05:59 -06003268 break;
3269 case HTTP_REQ_ACT_REPLACE_HDR:
3270 case HTTP_RES_ACT_REPLACE_HDR:
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003271 n_replaced = http_replace_header(re, reg_dst_buf, reg_dst_buf_size, val, val_end-val, trash.str);
Sasha Pachev218f0642014-06-16 12:05:59 -06003272 break;
3273 default: /* impossible */
3274 return -1;
3275 }
3276
Sasha Pachev218f0642014-06-16 12:05:59 -06003277 switch (n_replaced) {
3278 case -1: return -1;
3279 case -2: continue;
3280 }
3281
3282 delta = buffer_replace2(msg->chn->buf, val, val_end, reg_dst_buf, n_replaced);
3283
3284 hdr->len += delta;
3285 http_msg_move_end(msg, delta);
3286 }
3287
3288 return 0;
3289}
3290
Willy Tarreau20b0de52012-12-24 15:45:22 +01003291/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003292 * transaction <txn>. Returns the verdict of the first rule that prevents
3293 * further processing of the request (auth, deny, ...), and defaults to
3294 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3295 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3296 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003297 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003298enum rule_result
Willy Tarreau96257ec2012-12-27 10:46:37 +01003299http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003300{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003301 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003302 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003303 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003304 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003305
Willy Tarreauff011f22011-01-06 17:51:27 +01003306 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003307 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003308 continue;
3309
Willy Tarreau96257ec2012-12-27 10:46:37 +01003310 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003311 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003312 int ret;
3313
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003314 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003315 ret = acl_pass(ret);
3316
Willy Tarreauff011f22011-01-06 17:51:27 +01003317 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003318 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003319
3320 if (!ret) /* condition not matched */
3321 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003322 }
3323
Willy Tarreau20b0de52012-12-24 15:45:22 +01003324
Willy Tarreau96257ec2012-12-27 10:46:37 +01003325 switch (rule->action) {
3326 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003327 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003328
3329 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003330 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003331
Willy Tarreauccbcc372012-12-27 12:37:57 +01003332 case HTTP_REQ_ACT_TARPIT:
3333 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003334 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003335
Willy Tarreau96257ec2012-12-27 10:46:37 +01003336 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003337 /* Auth might be performed on regular http-req rules as well as on stats */
3338 auth_realm = rule->arg.auth.realm;
3339 if (!auth_realm) {
3340 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3341 auth_realm = STATS_DEFAULT_REALM;
3342 else
3343 auth_realm = px->id;
3344 }
3345 /* send 401/407 depending on whether we use a proxy or not. We still
3346 * count one error, because normal browsing won't significantly
3347 * increase the counter but brute force attempts will.
3348 */
3349 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3350 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3351 stream_int_retnclose(&s->si[0], &trash);
3352 session_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003353 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003354
Willy Tarreau81499eb2012-12-27 12:19:02 +01003355 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003356 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3357 return HTTP_RULE_RES_BADREQ;
3358 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003359
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003360 case HTTP_REQ_ACT_SET_NICE:
3361 s->task->nice = rule->arg.nice;
3362 break;
3363
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003364 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003365 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003366 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003367 break;
3368
Willy Tarreau51347ed2013-06-11 19:34:13 +02003369 case HTTP_REQ_ACT_SET_MARK:
3370#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003371 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003372 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003373#endif
3374 break;
3375
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003376 case HTTP_REQ_ACT_SET_LOGL:
3377 s->logs.level = rule->arg.loglevel;
3378 break;
3379
Sasha Pachev218f0642014-06-16 12:05:59 -06003380 case HTTP_REQ_ACT_REPLACE_HDR:
3381 case HTTP_REQ_ACT_REPLACE_VAL:
3382 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3383 txn->req.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003384 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003385 return HTTP_RULE_RES_BADREQ;
3386 break;
3387
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003388 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003389 case HTTP_REQ_ACT_SET_HDR:
3390 ctx.idx = 0;
3391 /* remove all occurrences of the header */
3392 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3393 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3394 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003395 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003396 if (rule->action == HTTP_REQ_ACT_DEL_HDR)
3397 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003398 /* now fall through to header addition */
3399
3400 case HTTP_REQ_ACT_ADD_HDR:
3401 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3402 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3403 trash.len = rule->arg.hdr_add.name_len;
3404 trash.str[trash.len++] = ':';
3405 trash.str[trash.len++] = ' ';
3406 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3407 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3408 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003409
3410 case HTTP_REQ_ACT_DEL_ACL:
3411 case HTTP_REQ_ACT_DEL_MAP: {
3412 struct pat_ref *ref;
3413 char *key;
3414 int len;
3415
3416 /* collect reference */
3417 ref = pat_ref_lookup(rule->arg.map.ref);
3418 if (!ref)
3419 continue;
3420
3421 /* collect key */
3422 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3423 key = trash.str;
3424 key[len] = '\0';
3425
3426 /* perform update */
3427 /* returned code: 1=ok, 0=ko */
3428 pat_ref_delete(ref, key);
3429
3430 break;
3431 }
3432
3433 case HTTP_REQ_ACT_ADD_ACL: {
3434 struct pat_ref *ref;
3435 char *key;
3436 struct chunk *trash_key;
3437 int len;
3438
3439 trash_key = get_trash_chunk();
3440
3441 /* collect reference */
3442 ref = pat_ref_lookup(rule->arg.map.ref);
3443 if (!ref)
3444 continue;
3445
3446 /* collect key */
3447 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3448 key = trash_key->str;
3449 key[len] = '\0';
3450
3451 /* perform update */
3452 /* add entry only if it does not already exist */
3453 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003454 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003455
3456 break;
3457 }
3458
3459 case HTTP_REQ_ACT_SET_MAP: {
3460 struct pat_ref *ref;
3461 char *key, *value;
3462 struct chunk *trash_key, *trash_value;
3463 int len;
3464
3465 trash_key = get_trash_chunk();
3466 trash_value = get_trash_chunk();
3467
3468 /* collect reference */
3469 ref = pat_ref_lookup(rule->arg.map.ref);
3470 if (!ref)
3471 continue;
3472
3473 /* collect key */
3474 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3475 key = trash_key->str;
3476 key[len] = '\0';
3477
3478 /* collect value */
3479 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3480 value = trash_value->str;
3481 value[len] = '\0';
3482
3483 /* perform update */
3484 if (pat_ref_find_elt(ref, key) != NULL)
3485 /* update entry if it exists */
3486 pat_ref_set(ref, key, value, NULL);
3487 else
3488 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003489 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003490
3491 break;
3492 }
William Lallemand73025dd2014-04-24 14:38:37 +02003493
3494 case HTTP_REQ_ACT_CUSTOM_CONT:
3495 rule->action_ptr(rule, px, s, txn);
3496 break;
3497
3498 case HTTP_REQ_ACT_CUSTOM_STOP:
3499 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003500 return HTTP_RULE_RES_DONE;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003501 }
3502 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003503
3504 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003505 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003506}
3507
Willy Tarreau71241ab2012-12-27 11:30:54 +01003508
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003509/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3510 * transaction <txn>. Returns the first rule that prevents further processing
3511 * of the response (deny, ...) or NULL if it executed all rules or stopped
3512 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3513 * rule.
3514 */
3515static struct http_res_rule *
3516http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3517{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003518 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003519 struct http_res_rule *rule;
3520 struct hdr_ctx ctx;
3521
3522 list_for_each_entry(rule, rules, list) {
3523 if (rule->action >= HTTP_RES_ACT_MAX)
3524 continue;
3525
3526 /* check optional condition */
3527 if (rule->cond) {
3528 int ret;
3529
3530 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3531 ret = acl_pass(ret);
3532
3533 if (rule->cond->pol == ACL_COND_UNLESS)
3534 ret = !ret;
3535
3536 if (!ret) /* condition not matched */
3537 continue;
3538 }
3539
3540
3541 switch (rule->action) {
3542 case HTTP_RES_ACT_ALLOW:
3543 return NULL; /* "allow" rules are OK */
3544
3545 case HTTP_RES_ACT_DENY:
3546 txn->flags |= TX_SVDENY;
3547 return rule;
3548
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003549 case HTTP_RES_ACT_SET_NICE:
3550 s->task->nice = rule->arg.nice;
3551 break;
3552
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003553 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003554 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003555 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003556 break;
3557
Willy Tarreau51347ed2013-06-11 19:34:13 +02003558 case HTTP_RES_ACT_SET_MARK:
3559#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003560 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003561 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003562#endif
3563 break;
3564
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003565 case HTTP_RES_ACT_SET_LOGL:
3566 s->logs.level = rule->arg.loglevel;
3567 break;
3568
Sasha Pachev218f0642014-06-16 12:05:59 -06003569 case HTTP_RES_ACT_REPLACE_HDR:
3570 case HTTP_RES_ACT_REPLACE_VAL:
3571 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3572 txn->rsp.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003573 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003574 return NULL; /* note: we should report an error here */
3575 break;
3576
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003577 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003578 case HTTP_RES_ACT_SET_HDR:
3579 ctx.idx = 0;
3580 /* remove all occurrences of the header */
3581 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3582 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3583 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3584 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003585 if (rule->action == HTTP_RES_ACT_DEL_HDR)
3586 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003587 /* now fall through to header addition */
3588
3589 case HTTP_RES_ACT_ADD_HDR:
3590 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3591 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3592 trash.len = rule->arg.hdr_add.name_len;
3593 trash.str[trash.len++] = ':';
3594 trash.str[trash.len++] = ' ';
3595 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3596 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3597 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003598
3599 case HTTP_RES_ACT_DEL_ACL:
3600 case HTTP_RES_ACT_DEL_MAP: {
3601 struct pat_ref *ref;
3602 char *key;
3603 int len;
3604
3605 /* collect reference */
3606 ref = pat_ref_lookup(rule->arg.map.ref);
3607 if (!ref)
3608 continue;
3609
3610 /* collect key */
3611 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3612 key = trash.str;
3613 key[len] = '\0';
3614
3615 /* perform update */
3616 /* returned code: 1=ok, 0=ko */
3617 pat_ref_delete(ref, key);
3618
3619 break;
3620 }
3621
3622 case HTTP_RES_ACT_ADD_ACL: {
3623 struct pat_ref *ref;
3624 char *key;
3625 struct chunk *trash_key;
3626 int len;
3627
3628 trash_key = get_trash_chunk();
3629
3630 /* collect reference */
3631 ref = pat_ref_lookup(rule->arg.map.ref);
3632 if (!ref)
3633 continue;
3634
3635 /* collect key */
3636 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3637 key = trash_key->str;
3638 key[len] = '\0';
3639
3640 /* perform update */
3641 /* check if the entry already exists */
3642 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003643 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003644
3645 break;
3646 }
3647
3648 case HTTP_RES_ACT_SET_MAP: {
3649 struct pat_ref *ref;
3650 char *key, *value;
3651 struct chunk *trash_key, *trash_value;
3652 int len;
3653
3654 trash_key = get_trash_chunk();
3655 trash_value = get_trash_chunk();
3656
3657 /* collect reference */
3658 ref = pat_ref_lookup(rule->arg.map.ref);
3659 if (!ref)
3660 continue;
3661
3662 /* collect key */
3663 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3664 key = trash_key->str;
3665 key[len] = '\0';
3666
3667 /* collect value */
3668 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3669 value = trash_value->str;
3670 value[len] = '\0';
3671
3672 /* perform update */
3673 if (pat_ref_find_elt(ref, key) != NULL)
3674 /* update entry if it exists */
3675 pat_ref_set(ref, key, value, NULL);
3676 else
3677 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003678 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003679
3680 break;
3681 }
William Lallemand73025dd2014-04-24 14:38:37 +02003682
3683 case HTTP_RES_ACT_CUSTOM_CONT:
3684 rule->action_ptr(rule, px, s, txn);
3685 break;
3686
3687 case HTTP_RES_ACT_CUSTOM_STOP:
3688 rule->action_ptr(rule, px, s, txn);
3689 return rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003690 }
3691 }
3692
3693 /* we reached the end of the rules, nothing to report */
3694 return NULL;
3695}
3696
3697
Willy Tarreau71241ab2012-12-27 11:30:54 +01003698/* Perform an HTTP redirect based on the information in <rule>. The function
3699 * returns non-zero on success, or zero in case of a, irrecoverable error such
3700 * as too large a request to build a valid response.
3701 */
3702static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3703{
3704 struct http_msg *msg = &txn->req;
3705 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003706 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003707
3708 /* build redirect message */
3709 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003710 case 308:
3711 msg_fmt = HTTP_308;
3712 break;
3713 case 307:
3714 msg_fmt = HTTP_307;
3715 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003716 case 303:
3717 msg_fmt = HTTP_303;
3718 break;
3719 case 301:
3720 msg_fmt = HTTP_301;
3721 break;
3722 case 302:
3723 default:
3724 msg_fmt = HTTP_302;
3725 break;
3726 }
3727
3728 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3729 return 0;
3730
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003731 location = trash.str + trash.len;
3732
Willy Tarreau71241ab2012-12-27 11:30:54 +01003733 switch(rule->type) {
3734 case REDIRECT_TYPE_SCHEME: {
3735 const char *path;
3736 const char *host;
3737 struct hdr_ctx ctx;
3738 int pathlen;
3739 int hostlen;
3740
3741 host = "";
3742 hostlen = 0;
3743 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003744 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003745 host = ctx.line + ctx.val;
3746 hostlen = ctx.vlen;
3747 }
3748
3749 path = http_get_path(txn);
3750 /* build message using path */
3751 if (path) {
3752 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3753 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3754 int qs = 0;
3755 while (qs < pathlen) {
3756 if (path[qs] == '?') {
3757 pathlen = qs;
3758 break;
3759 }
3760 qs++;
3761 }
3762 }
3763 } else {
3764 path = "/";
3765 pathlen = 1;
3766 }
3767
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003768 if (rule->rdr_str) { /* this is an old "redirect" rule */
3769 /* check if we can add scheme + "://" + host + path */
3770 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3771 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003772
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003773 /* add scheme */
3774 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3775 trash.len += rule->rdr_len;
3776 }
3777 else {
3778 /* add scheme with executing log format */
3779 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003780
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003781 /* check if we can add scheme + "://" + host + path */
3782 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3783 return 0;
3784 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003785 /* add "://" */
3786 memcpy(trash.str + trash.len, "://", 3);
3787 trash.len += 3;
3788
3789 /* add host */
3790 memcpy(trash.str + trash.len, host, hostlen);
3791 trash.len += hostlen;
3792
3793 /* add path */
3794 memcpy(trash.str + trash.len, path, pathlen);
3795 trash.len += pathlen;
3796
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003797 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003798 if (trash.len && trash.str[trash.len - 1] != '/' &&
3799 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3800 if (trash.len > trash.size - 5)
3801 return 0;
3802 trash.str[trash.len] = '/';
3803 trash.len++;
3804 }
3805
3806 break;
3807 }
3808 case REDIRECT_TYPE_PREFIX: {
3809 const char *path;
3810 int pathlen;
3811
3812 path = http_get_path(txn);
3813 /* build message using path */
3814 if (path) {
3815 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3816 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3817 int qs = 0;
3818 while (qs < pathlen) {
3819 if (path[qs] == '?') {
3820 pathlen = qs;
3821 break;
3822 }
3823 qs++;
3824 }
3825 }
3826 } else {
3827 path = "/";
3828 pathlen = 1;
3829 }
3830
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003831 if (rule->rdr_str) { /* this is an old "redirect" rule */
3832 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3833 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003834
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003835 /* add prefix. Note that if prefix == "/", we don't want to
3836 * add anything, otherwise it makes it hard for the user to
3837 * configure a self-redirection.
3838 */
3839 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3840 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3841 trash.len += rule->rdr_len;
3842 }
3843 }
3844 else {
3845 /* add prefix with executing log format */
3846 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3847
3848 /* Check length */
3849 if (trash.len + pathlen > trash.size - 4)
3850 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003851 }
3852
3853 /* add path */
3854 memcpy(trash.str + trash.len, path, pathlen);
3855 trash.len += pathlen;
3856
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003857 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003858 if (trash.len && trash.str[trash.len - 1] != '/' &&
3859 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3860 if (trash.len > trash.size - 5)
3861 return 0;
3862 trash.str[trash.len] = '/';
3863 trash.len++;
3864 }
3865
3866 break;
3867 }
3868 case REDIRECT_TYPE_LOCATION:
3869 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003870 if (rule->rdr_str) { /* this is an old "redirect" rule */
3871 if (trash.len + rule->rdr_len > trash.size - 4)
3872 return 0;
3873
3874 /* add location */
3875 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3876 trash.len += rule->rdr_len;
3877 }
3878 else {
3879 /* add location with executing log format */
3880 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003881
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003882 /* Check left length */
3883 if (trash.len > trash.size - 4)
3884 return 0;
3885 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003886 break;
3887 }
3888
3889 if (rule->cookie_len) {
3890 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3891 trash.len += 14;
3892 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3893 trash.len += rule->cookie_len;
3894 memcpy(trash.str + trash.len, "\r\n", 2);
3895 trash.len += 2;
3896 }
3897
3898 /* add end of headers and the keep-alive/close status.
3899 * We may choose to set keep-alive if the Location begins
3900 * with a slash, because the client will come back to the
3901 * same server.
3902 */
3903 txn->status = rule->code;
3904 /* let's log the request time */
3905 s->logs.tv_request = now;
3906
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003907 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003908 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3909 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3910 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3911 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3912 /* keep-alive possible */
3913 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3914 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3915 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3916 trash.len += 30;
3917 } else {
3918 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3919 trash.len += 24;
3920 }
3921 }
3922 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3923 trash.len += 4;
3924 bo_inject(txn->rsp.chn, trash.str, trash.len);
3925 /* "eat" the request */
3926 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02003927 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003928 msg->sov = 0;
3929 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3930 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3931 txn->req.msg_state = HTTP_MSG_CLOSED;
3932 txn->rsp.msg_state = HTTP_MSG_DONE;
3933 } else {
3934 /* keep-alive not possible */
3935 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3936 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3937 trash.len += 29;
3938 } else {
3939 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3940 trash.len += 23;
3941 }
3942 stream_int_retnclose(txn->req.chn->prod, &trash);
3943 txn->req.chn->analysers = 0;
3944 }
3945
3946 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003947 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003948 if (!(s->flags & SN_FINST_MASK))
3949 s->flags |= SN_FINST_R;
3950
3951 return 1;
3952}
3953
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003954/* This stream analyser runs all HTTP request processing which is common to
3955 * frontends and backends, which means blocking ACLs, filters, connection-close,
3956 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003957 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003958 * either needs more data or wants to immediately abort the request (eg: deny,
3959 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003960 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003961int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003962{
Willy Tarreaud787e662009-07-07 10:14:51 +02003963 struct http_txn *txn = &s->txn;
3964 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003965 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003966 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003967 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02003968
Willy Tarreau655dce92009-11-08 13:10:58 +01003969 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003970 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003971 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003972 return 0;
3973 }
3974
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003975 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 +02003976 now_ms, __FUNCTION__,
3977 s,
3978 req,
3979 req->rex, req->wex,
3980 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003981 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003982 req->analysers);
3983
Willy Tarreau65410832014-04-28 21:25:43 +02003984 /* just in case we have some per-backend tracking */
3985 session_inc_be_http_req_ctr(s);
3986
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003987 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003988 if (!LIST_ISEMPTY(&px->http_req_rules)) {
3989 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003990
Willy Tarreau0b748332014-04-29 00:13:29 +02003991 switch (verdict) {
3992 case HTTP_RULE_RES_CONT:
3993 case HTTP_RULE_RES_STOP: /* nothing to do */
3994 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003995
Willy Tarreau0b748332014-04-29 00:13:29 +02003996 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3997 if (txn->flags & TX_CLTARPIT)
3998 goto tarpit;
3999 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004000
Willy Tarreau0b748332014-04-29 00:13:29 +02004001 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4002 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004003
Willy Tarreau0b748332014-04-29 00:13:29 +02004004 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004005 goto done;
4006
Willy Tarreau0b748332014-04-29 00:13:29 +02004007 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4008 goto return_bad_req;
4009 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004010 }
4011
Willy Tarreau52542592014-04-28 18:33:26 +02004012 /* OK at this stage, we know that the request was accepted according to
4013 * the http-request rules, we can check for the stats. Note that the
4014 * URI is detected *before* the req* rules in order not to be affected
4015 * by a possible reqrep, while they are processed *after* so that a
4016 * reqdeny can still block them. This clearly needs to change in 1.6!
4017 */
4018 if (stats_check_uri(s->rep->prod, txn, px)) {
4019 s->target = &http_stats_applet.obj_type;
4020 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
4021 txn->status = 500;
4022 s->logs.tv_request = now;
4023 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004024
Willy Tarreau52542592014-04-28 18:33:26 +02004025 if (!(s->flags & SN_ERR_MASK))
4026 s->flags |= SN_ERR_RESOURCE;
4027 goto return_prx_cond;
4028 }
4029
4030 /* parse the whole stats request and extract the relevant information */
4031 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004032 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4033 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004034
Willy Tarreau0b748332014-04-29 00:13:29 +02004035 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4036 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004037
Willy Tarreau0b748332014-04-29 00:13:29 +02004038 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4039 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004040 }
4041
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004042 /* evaluate the req* rules except reqadd */
4043 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004044 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004045 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004046
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004047 if (txn->flags & TX_CLDENY)
4048 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004049
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004050 if (txn->flags & TX_CLTARPIT)
4051 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004052 }
Willy Tarreau06619262006-12-17 08:37:22 +01004053
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004054 /* add request headers from the rule sets in the same order */
4055 list_for_each_entry(wl, &px->req_add, list) {
4056 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004057 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004058 ret = acl_pass(ret);
4059 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4060 ret = !ret;
4061 if (!ret)
4062 continue;
4063 }
4064
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004065 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004066 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004067 }
4068
Willy Tarreau52542592014-04-28 18:33:26 +02004069
4070 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004071 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004072 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004073 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
4074 s->fe->fe_counters.intercepted_req++;
4075
4076 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
4077 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
4078 if (!(s->flags & SN_FINST_MASK))
4079 s->flags |= SN_FINST_R;
4080
Willy Tarreau70730dd2014-04-24 18:06:27 +02004081 /* we may want to compress the stats page */
4082 if (s->fe->comp || s->be->comp)
4083 select_compression_request_header(s, req->buf);
4084
4085 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02004086 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) |
4087 AN_REQ_HTTP_XFER_BODY | AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_BE | AN_RES_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004088 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004089 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004090
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004091 /* check whether we have some ACLs set to redirect this request */
4092 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004093 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004094 int ret;
4095
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004096 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004097 ret = acl_pass(ret);
4098 if (rule->cond->pol == ACL_COND_UNLESS)
4099 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004100 if (!ret)
4101 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004102 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004103 if (!http_apply_redirect_rule(rule, s, txn))
4104 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004105 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004106 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004107
Willy Tarreau2be39392010-01-03 17:24:51 +01004108 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4109 * If this happens, then the data will not come immediately, so we must
4110 * send all what we have without waiting. Note that due to the small gain
4111 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004112 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004113 * itself once used.
4114 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004115 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004116
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004117 done: /* done with this analyser, continue with next ones that the calling
4118 * points will have set, if any.
4119 */
4120 req->analysers &= ~an_bit;
4121 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004122 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004123
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004124 tarpit:
4125 /* When a connection is tarpitted, we use the tarpit timeout,
4126 * which may be the same as the connect timeout if unspecified.
4127 * If unset, then set it to zero because we really want it to
4128 * eventually expire. We build the tarpit as an analyser.
4129 */
4130 channel_erase(s->req);
4131
4132 /* wipe the request out so that we can drop the connection early
4133 * if the client closes first.
4134 */
4135 channel_dont_connect(req);
4136 req->analysers = 0; /* remove switching rules etc... */
4137 req->analysers |= AN_REQ_HTTP_TARPIT;
4138 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4139 if (!req->analyse_exp)
4140 req->analyse_exp = tick_add(now_ms, 0);
4141 session_inc_http_err_ctr(s);
4142 s->fe->fe_counters.denied_req++;
4143 if (s->fe != s->be)
4144 s->be->be_counters.denied_req++;
4145 if (s->listener->counters)
4146 s->listener->counters->denied_req++;
4147 goto done;
4148
4149 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004150 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004151 txn->status = 403;
4152 s->logs.tv_request = now;
4153 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4154 session_inc_http_err_ctr(s);
4155 s->fe->fe_counters.denied_req++;
4156 if (s->fe != s->be)
4157 s->be->be_counters.denied_req++;
4158 if (s->listener->counters)
4159 s->listener->counters->denied_req++;
4160 goto return_prx_cond;
4161
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004162 return_bad_req:
4163 /* We centralize bad requests processing here */
4164 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4165 /* we detected a parsing error. We want to archive this request
4166 * in the dedicated proxy area for later troubleshooting.
4167 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004168 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004169 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004170
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004171 txn->req.msg_state = HTTP_MSG_ERROR;
4172 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004173 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004174
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004175 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004176 if (s->listener->counters)
4177 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004178
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004179 return_prx_cond:
4180 if (!(s->flags & SN_ERR_MASK))
4181 s->flags |= SN_ERR_PRXCOND;
4182 if (!(s->flags & SN_FINST_MASK))
4183 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004184
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004185 req->analysers = 0;
4186 req->analyse_exp = TICK_ETERNITY;
4187 return 0;
4188}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004189
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004190/* This function performs all the processing enabled for the current request.
4191 * It returns 1 if the processing can continue on next analysers, or zero if it
4192 * needs more data, encounters an error, or wants to immediately abort the
4193 * request. It relies on buffers flags, and updates s->req->analysers.
4194 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004195int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004196{
4197 struct http_txn *txn = &s->txn;
4198 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004199 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004200
Willy Tarreau655dce92009-11-08 13:10:58 +01004201 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004202 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004203 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004204 return 0;
4205 }
4206
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004207 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 +02004208 now_ms, __FUNCTION__,
4209 s,
4210 req,
4211 req->rex, req->wex,
4212 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004213 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004214 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004215
William Lallemand82fe75c2012-10-23 10:25:10 +02004216 if (s->fe->comp || s->be->comp)
4217 select_compression_request_header(s, req->buf);
4218
Willy Tarreau59234e92008-11-30 23:51:27 +01004219 /*
4220 * Right now, we know that we have processed the entire headers
4221 * and that unwanted requests have been filtered out. We can do
4222 * whatever we want with the remaining request. Also, now we
4223 * may have separate values for ->fe, ->be.
4224 */
Willy Tarreau06619262006-12-17 08:37:22 +01004225
Willy Tarreau59234e92008-11-30 23:51:27 +01004226 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004227 * If HTTP PROXY is set we simply get remote server address parsing
4228 * incoming request. Note that this requires that a connection is
4229 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004230 */
4231 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004232 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004233 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004234
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004235 /* Note that for now we don't reuse existing proxy connections */
4236 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004237 txn->req.msg_state = HTTP_MSG_ERROR;
4238 txn->status = 500;
4239 req->analysers = 0;
4240 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4241
4242 if (!(s->flags & SN_ERR_MASK))
4243 s->flags |= SN_ERR_RESOURCE;
4244 if (!(s->flags & SN_FINST_MASK))
4245 s->flags |= SN_FINST_R;
4246
4247 return 0;
4248 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004249
4250 path = http_get_path(txn);
4251 url2sa(req->buf->p + msg->sl.rq.u,
4252 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004253 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004254 /* if the path was found, we have to remove everything between
4255 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4256 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4257 * u_l characters by a single "/".
4258 */
4259 if (path) {
4260 char *cur_ptr = req->buf->p;
4261 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4262 int delta;
4263
4264 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4265 http_msg_move_end(&txn->req, delta);
4266 cur_end += delta;
4267 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4268 goto return_bad_req;
4269 }
4270 else {
4271 char *cur_ptr = req->buf->p;
4272 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4273 int delta;
4274
4275 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4276 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4277 http_msg_move_end(&txn->req, delta);
4278 cur_end += delta;
4279 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4280 goto return_bad_req;
4281 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004282 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004283
Willy Tarreau59234e92008-11-30 23:51:27 +01004284 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004285 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004286 * Note that doing so might move headers in the request, but
4287 * the fields will stay coherent and the URI will not move.
4288 * This should only be performed in the backend.
4289 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004290 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004291 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4292 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004293
Willy Tarreau59234e92008-11-30 23:51:27 +01004294 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004295 * 8: the appsession cookie was looked up very early in 1.2,
4296 * so let's do the same now.
4297 */
4298
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004299 /* It needs to look into the URI unless persistence must be ignored */
4300 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004301 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 +01004302 }
4303
William Lallemanda73203e2012-03-12 12:48:57 +01004304 /* add unique-id if "header-unique-id" is specified */
4305
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004306 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4307 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4308 goto return_bad_req;
4309 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004310 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004311 }
William Lallemanda73203e2012-03-12 12:48:57 +01004312
4313 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004314 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4315 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004316 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004317 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004318 goto return_bad_req;
4319 }
4320
Cyril Bontéb21570a2009-11-29 20:04:48 +01004321 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004322 * 9: add X-Forwarded-For if either the frontend or the backend
4323 * asks for it.
4324 */
4325 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004326 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004327 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004328 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4329 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004330 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004331 /* The header is set to be added only if none is present
4332 * and we found it, so don't do anything.
4333 */
4334 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004335 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004336 /* Add an X-Forwarded-For header unless the source IP is
4337 * in the 'except' network range.
4338 */
4339 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004340 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004341 != s->fe->except_net.s_addr) &&
4342 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004343 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004344 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004345 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004346 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004347 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004348
4349 /* Note: we rely on the backend to get the header name to be used for
4350 * x-forwarded-for, because the header is really meant for the backends.
4351 * However, if the backend did not specify any option, we have to rely
4352 * on the frontend's header name.
4353 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004354 if (s->be->fwdfor_hdr_len) {
4355 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004356 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004357 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004358 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004359 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004360 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004361 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 +01004362
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004363 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004364 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004365 }
4366 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004367 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004368 /* FIXME: for the sake of completeness, we should also support
4369 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004370 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004371 int len;
4372 char pn[INET6_ADDRSTRLEN];
4373 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004374 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004375 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004376
Willy Tarreau59234e92008-11-30 23:51:27 +01004377 /* Note: we rely on the backend to get the header name to be used for
4378 * x-forwarded-for, because the header is really meant for the backends.
4379 * However, if the backend did not specify any option, we have to rely
4380 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004381 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004382 if (s->be->fwdfor_hdr_len) {
4383 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004384 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004385 } else {
4386 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004387 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004388 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004389 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004390
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004391 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004392 goto return_bad_req;
4393 }
4394 }
4395
4396 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004397 * 10: add X-Original-To if either the frontend or the backend
4398 * asks for it.
4399 */
4400 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4401
4402 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004403 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004404 /* Add an X-Original-To header unless the destination IP is
4405 * in the 'except' network range.
4406 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004407 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004408
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004409 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004410 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004411 (((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 +02004412 != s->fe->except_to.s_addr) &&
4413 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004414 (((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 +02004415 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004416 int len;
4417 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004418 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004419
4420 /* Note: we rely on the backend to get the header name to be used for
4421 * x-original-to, because the header is really meant for the backends.
4422 * However, if the backend did not specify any option, we have to rely
4423 * on the frontend's header name.
4424 */
4425 if (s->be->orgto_hdr_len) {
4426 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004427 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004428 } else {
4429 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004430 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004431 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004432 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 +02004433
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004434 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004435 goto return_bad_req;
4436 }
4437 }
4438 }
4439
Willy Tarreau50fc7772012-11-11 22:19:57 +01004440 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4441 * If an "Upgrade" token is found, the header is left untouched in order not to have
4442 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4443 * "Upgrade" is present in the Connection header.
4444 */
4445 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4446 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004447 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4448 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004449 unsigned int want_flags = 0;
4450
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004451 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004452 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004453 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4454 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004455 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004456 want_flags |= TX_CON_CLO_SET;
4457 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004458 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004459 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4460 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004461 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004462 want_flags |= TX_CON_KAL_SET;
4463 }
4464
4465 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004466 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004467 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004468
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004469
Willy Tarreau522d6c02009-12-06 18:49:18 +01004470 /* If we have no server assigned yet and we're balancing on url_param
4471 * with a POST request, we may be interested in checking the body for
4472 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004473 */
4474 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4475 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004476 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004477 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004478 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004479 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004480
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004481 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004482 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004483#ifdef TCP_QUICKACK
4484 /* We expect some data from the client. Unless we know for sure
4485 * we already have a full request, we have to re-enable quick-ack
4486 * in case we previously disabled it, otherwise we might cause
4487 * the client to delay further data.
4488 */
4489 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004490 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004491 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004492 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004493 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004494#endif
4495 }
Willy Tarreau03945942009-12-22 16:50:27 +01004496
Willy Tarreau59234e92008-11-30 23:51:27 +01004497 /*************************************************************
4498 * OK, that's finished for the headers. We have done what we *
4499 * could. Let's switch to the DATA state. *
4500 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004501 req->analyse_exp = TICK_ETERNITY;
4502 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004503
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004504 /* if the server closes the connection, we want to immediately react
4505 * and close the socket to save packets and syscalls.
4506 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004507 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4508 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004509
Willy Tarreau59234e92008-11-30 23:51:27 +01004510 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004511 /* OK let's go on with the BODY now */
4512 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004513
Willy Tarreau59234e92008-11-30 23:51:27 +01004514 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004515 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004516 /* we detected a parsing error. We want to archive this request
4517 * in the dedicated proxy area for later troubleshooting.
4518 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004519 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004520 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004521
Willy Tarreau59234e92008-11-30 23:51:27 +01004522 txn->req.msg_state = HTTP_MSG_ERROR;
4523 txn->status = 400;
4524 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004525 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004526
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004527 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004528 if (s->listener->counters)
4529 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004530
Willy Tarreau59234e92008-11-30 23:51:27 +01004531 if (!(s->flags & SN_ERR_MASK))
4532 s->flags |= SN_ERR_PRXCOND;
4533 if (!(s->flags & SN_FINST_MASK))
4534 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004535 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004536}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004537
Willy Tarreau60b85b02008-11-30 23:28:40 +01004538/* This function is an analyser which processes the HTTP tarpit. It always
4539 * returns zero, at the beginning because it prevents any other processing
4540 * from occurring, and at the end because it terminates the request.
4541 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004542int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004543{
4544 struct http_txn *txn = &s->txn;
4545
4546 /* This connection is being tarpitted. The CLIENT side has
4547 * already set the connect expiration date to the right
4548 * timeout. We just have to check that the client is still
4549 * there and that the timeout has not expired.
4550 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004551 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004552 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004553 !tick_is_expired(req->analyse_exp, now_ms))
4554 return 0;
4555
4556 /* We will set the queue timer to the time spent, just for
4557 * logging purposes. We fake a 500 server error, so that the
4558 * attacker will not suspect his connection has been tarpitted.
4559 * It will not cause trouble to the logs because we can exclude
4560 * the tarpitted connections by filtering on the 'PT' status flags.
4561 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004562 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4563
4564 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004565 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004566 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004567
4568 req->analysers = 0;
4569 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004570
Willy Tarreau60b85b02008-11-30 23:28:40 +01004571 if (!(s->flags & SN_ERR_MASK))
4572 s->flags |= SN_ERR_PRXCOND;
4573 if (!(s->flags & SN_FINST_MASK))
4574 s->flags |= SN_FINST_T;
4575 return 0;
4576}
4577
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004578/* This function is an analyser which waits for the HTTP request body. It waits
4579 * for either the buffer to be full, or the full advertised contents to have
4580 * reached the buffer. It must only be called after the standard HTTP request
4581 * processing has occurred, because it expects the request to be parsed and will
4582 * look for the Expect header. It may send a 100-Continue interim response. It
4583 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4584 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4585 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004586 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004587int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004588{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004589 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004590 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004591
4592 /* We have to parse the HTTP request body to find any required data.
4593 * "balance url_param check_post" should have been the only way to get
4594 * into this. We were brought here after HTTP header analysis, so all
4595 * related structures are ready.
4596 */
4597
Willy Tarreau890988f2014-04-10 11:59:33 +02004598 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4599 /* This is the first call */
4600 if (msg->msg_state < HTTP_MSG_BODY)
4601 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004602
Willy Tarreau890988f2014-04-10 11:59:33 +02004603 if (msg->msg_state < HTTP_MSG_100_SENT) {
4604 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4605 * send an HTTP/1.1 100 Continue intermediate response.
4606 */
4607 if (msg->flags & HTTP_MSGF_VER_11) {
4608 struct hdr_ctx ctx;
4609 ctx.idx = 0;
4610 /* Expect is allowed in 1.1, look for it */
4611 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4612 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4613 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4614 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004615 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004616 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004617 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004618
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004619 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004620 * req->buf->p still points to the beginning of the message. We
4621 * must save the body in msg->next because it survives buffer
4622 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004623 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004624 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004625
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004626 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004627 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4628 else
4629 msg->msg_state = HTTP_MSG_DATA;
4630 }
4631
Willy Tarreau890988f2014-04-10 11:59:33 +02004632 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4633 /* We're in content-length mode, we just have to wait for enough data. */
4634 if (req->buf->i - msg->sov < msg->body_len)
4635 goto missing_data;
4636
4637 /* OK we have everything we need now */
4638 goto http_end;
4639 }
4640
4641 /* OK here we're parsing a chunked-encoded message */
4642
Willy Tarreau522d6c02009-12-06 18:49:18 +01004643 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004644 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004645 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004646 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004647 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004648 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004649
Willy Tarreau115acb92009-12-26 13:56:06 +01004650 if (!ret)
4651 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004652 else if (ret < 0) {
4653 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004654 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004655 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004656 }
4657
Willy Tarreaud98cf932009-12-27 22:54:55 +01004658 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004659 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004660 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004661 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004662 if (msg->msg_state == HTTP_MSG_TRAILERS)
4663 goto http_end;
4664
Willy Tarreau226071e2014-04-10 11:55:45 +02004665 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004666 goto http_end;
4667
4668 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004669 /* we get here if we need to wait for more data. If the buffer is full,
4670 * we have the maximum we can expect.
4671 */
4672 if (buffer_full(req->buf, global.tune.maxrewrite))
4673 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004674
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004675 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004676 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004677 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004678
4679 if (!(s->flags & SN_ERR_MASK))
4680 s->flags |= SN_ERR_CLITO;
4681 if (!(s->flags & SN_FINST_MASK))
4682 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004683 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004684 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004685
4686 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004687 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004688 /* Not enough data. We'll re-use the http-request
4689 * timeout here. Ideally, we should set the timeout
4690 * relative to the accept() date. We just set the
4691 * request timeout once at the beginning of the
4692 * request.
4693 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004694 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004695 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004696 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004697 return 0;
4698 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004699
4700 http_end:
4701 /* The situation will not evolve, so let's give up on the analysis. */
4702 s->logs.tv_request = now; /* update the request timer to reflect full request */
4703 req->analysers &= ~an_bit;
4704 req->analyse_exp = TICK_ETERNITY;
4705 return 1;
4706
4707 return_bad_req: /* let's centralize all bad requests */
4708 txn->req.msg_state = HTTP_MSG_ERROR;
4709 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004710 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004711
Willy Tarreau79ebac62010-06-07 13:47:49 +02004712 if (!(s->flags & SN_ERR_MASK))
4713 s->flags |= SN_ERR_PRXCOND;
4714 if (!(s->flags & SN_FINST_MASK))
4715 s->flags |= SN_FINST_R;
4716
Willy Tarreau522d6c02009-12-06 18:49:18 +01004717 return_err_msg:
4718 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004719 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004720 if (s->listener->counters)
4721 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004722 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004723}
4724
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004725/* send a server's name with an outgoing request over an established connection.
4726 * Note: this function is designed to be called once the request has been scheduled
4727 * for being forwarded. This is the reason why it rewinds the buffer before
4728 * proceeding.
4729 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004730int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004731
4732 struct hdr_ctx ctx;
4733
Mark Lamourinec2247f02012-01-04 13:02:01 -05004734 char *hdr_name = be->server_id_hdr_name;
4735 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004736 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004737 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004738 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004739
William Lallemandd9e90662012-01-30 17:27:17 +01004740 ctx.idx = 0;
4741
Willy Tarreau211cdec2014-04-17 20:18:08 +02004742 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004743 if (old_o) {
4744 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004745 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004746 txn->req.next += old_o;
4747 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004748 }
4749
Willy Tarreau9b28e032012-10-12 23:49:43 +02004750 old_i = chn->buf->i;
4751 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 -05004752 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004753 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004754 }
4755
4756 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004757 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004758 memcpy(hdr_val, hdr_name, hdr_name_len);
4759 hdr_val += hdr_name_len;
4760 *hdr_val++ = ':';
4761 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004762 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4763 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004764
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004765 if (old_o) {
4766 /* If this was a forwarded request, we must readjust the amount of
4767 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004768 * variations. Note that the current state is >= HTTP_MSG_BODY,
4769 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004770 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004771 old_o += chn->buf->i - old_i;
4772 b_adv(chn->buf, old_o);
4773 txn->req.next -= old_o;
4774 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004775 }
4776
Mark Lamourinec2247f02012-01-04 13:02:01 -05004777 return 0;
4778}
4779
Willy Tarreau610ecce2010-01-04 21:15:02 +01004780/* Terminate current transaction and prepare a new one. This is very tricky
4781 * right now but it works.
4782 */
4783void http_end_txn_clean_session(struct session *s)
4784{
Willy Tarreau068621e2013-12-23 15:11:25 +01004785 int prev_status = s->txn.status;
4786
Willy Tarreau610ecce2010-01-04 21:15:02 +01004787 /* FIXME: We need a more portable way of releasing a backend's and a
4788 * server's connections. We need a safer way to reinitialize buffer
4789 * flags. We also need a more accurate method for computing per-request
4790 * data.
4791 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004792
Willy Tarreau4213a112013-12-15 10:25:42 +01004793 /* unless we're doing keep-alive, we want to quickly close the connection
4794 * to the server.
4795 */
4796 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4797 !si_conn_ready(s->req->cons)) {
4798 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4799 si_shutr(s->req->cons);
4800 si_shutw(s->req->cons);
4801 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004802
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004803 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004804 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004805 if (unlikely(s->srv_conn))
4806 sess_change_server(s, NULL);
4807 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004808
4809 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4810 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004811 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004812
4813 if (s->txn.status) {
4814 int n;
4815
4816 n = s->txn.status / 100;
4817 if (n < 1 || n > 5)
4818 n = 0;
4819
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004820 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004821 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004822 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004823 s->fe->fe_counters.p.http.comp_rsp++;
4824 }
Willy Tarreau24657792010-02-26 10:30:28 +01004825 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004826 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004827 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004828 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004829 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004830 s->be->be_counters.p.http.comp_rsp++;
4831 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004832 }
4833
4834 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004835 s->logs.bytes_in -= s->req->buf->i;
4836 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004837
4838 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004839 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004840 !(s->flags & SN_MONITOR) &&
4841 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4842 s->do_log(s);
4843 }
4844
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004845 session_update_time_stats(s);
4846
Willy Tarreau610ecce2010-01-04 21:15:02 +01004847 s->logs.accept_date = date; /* user-visible date for logging */
4848 s->logs.tv_accept = now; /* corrected date for internal use */
4849 tv_zero(&s->logs.tv_request);
4850 s->logs.t_queue = -1;
4851 s->logs.t_connect = -1;
4852 s->logs.t_data = -1;
4853 s->logs.t_close = 0;
4854 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4855 s->logs.srv_queue_size = 0; /* we will get this number soon */
4856
Willy Tarreau9b28e032012-10-12 23:49:43 +02004857 s->logs.bytes_in = s->req->total = s->req->buf->i;
4858 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004859
4860 if (s->pend_pos)
4861 pendconn_free(s->pend_pos);
4862
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004863 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004864 if (s->flags & SN_CURR_SESS) {
4865 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004866 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004867 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004868 if (may_dequeue_tasks(objt_server(s->target), s->be))
4869 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004870 }
4871
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004872 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004873
Willy Tarreau4213a112013-12-15 10:25:42 +01004874 /* only release our endpoint if we don't intend to reuse the
4875 * connection.
4876 */
4877 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4878 !si_conn_ready(s->req->cons)) {
4879 si_release_endpoint(s->req->cons);
4880 }
4881
Willy Tarreau610ecce2010-01-04 21:15:02 +01004882 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004883 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004884 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004885 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004886 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02004887 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);
Willy Tarreau1592d1e2014-06-11 16:49:14 +02004888 s->rep->flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004889 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 +01004890 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004891
Willy Tarreau610ecce2010-01-04 21:15:02 +01004892 s->txn.meth = 0;
4893 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004894 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004895
4896 if (prev_status == 401 || prev_status == 407) {
4897 /* In HTTP keep-alive mode, if we receive a 401, we still have
4898 * a chance of being able to send the visitor again to the same
4899 * server over the same connection. This is required by some
4900 * broken protocols such as NTLM, and anyway whenever there is
4901 * an opportunity for sending the challenge to the proper place,
4902 * it's better to do it (at least it helps with debugging).
4903 */
4904 s->txn.flags |= TX_PREFER_LAST;
4905 }
4906
Willy Tarreauee55dc02010-06-01 10:56:34 +02004907 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004908 s->req->cons->flags |= SI_FL_INDEP_STR;
4909
Willy Tarreau96e31212011-05-30 18:10:30 +02004910 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004911 s->req->flags |= CF_NEVER_WAIT;
4912 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004913 }
4914
Willy Tarreau610ecce2010-01-04 21:15:02 +01004915 /* if the request buffer is not empty, it means we're
4916 * about to process another request, so send pending
4917 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004918 * Just don't do this if the buffer is close to be full,
4919 * because the request will wait for it to flush a little
4920 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004921 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004922 if (s->req->buf->i) {
4923 if (s->rep->buf->o &&
4924 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4925 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004926 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004927 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004928
4929 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004930 channel_auto_read(s->req);
4931 channel_auto_close(s->req);
4932 channel_auto_read(s->rep);
4933 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004934
Willy Tarreau27375622013-12-17 00:00:28 +01004935 /* we're in keep-alive with an idle connection, monitor it */
4936 si_idle_conn(s->req->cons);
4937
Willy Tarreau342b11c2010-11-24 16:22:09 +01004938 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004939 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004940}
4941
4942
4943/* This function updates the request state machine according to the response
4944 * state machine and buffer flags. It returns 1 if it changes anything (flag
4945 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4946 * it is only used to find when a request/response couple is complete. Both
4947 * this function and its equivalent should loop until both return zero. It
4948 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4949 */
4950int http_sync_req_state(struct session *s)
4951{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004952 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004953 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004954 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004955 unsigned int old_state = txn->req.msg_state;
4956
Willy Tarreau610ecce2010-01-04 21:15:02 +01004957 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4958 return 0;
4959
4960 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004961 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004962 * We can shut the read side unless we want to abort_on_close,
4963 * or we have a POST request. The issue with POST requests is
4964 * that some browsers still send a CRLF after the request, and
4965 * this CRLF must be read so that it does not remain in the kernel
4966 * buffers, otherwise a close could cause an RST on some systems
4967 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004968 * Note that if we're using keep-alive on the client side, we'd
4969 * rather poll now and keep the polling enabled for the whole
4970 * session's life than enabling/disabling it between each
4971 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004972 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004973 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4974 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4975 !(s->be->options & PR_O_ABRT_CLOSE) &&
4976 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004977 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004978
Willy Tarreau40f151a2012-12-20 12:10:09 +01004979 /* if the server closes the connection, we want to immediately react
4980 * and close the socket to save packets and syscalls.
4981 */
4982 chn->cons->flags |= SI_FL_NOHALF;
4983
Willy Tarreau610ecce2010-01-04 21:15:02 +01004984 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4985 goto wait_other_side;
4986
4987 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4988 /* The server has not finished to respond, so we
4989 * don't want to move in order not to upset it.
4990 */
4991 goto wait_other_side;
4992 }
4993
4994 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4995 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004996 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004997 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004998 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004999 goto wait_other_side;
5000 }
5001
5002 /* When we get here, it means that both the request and the
5003 * response have finished receiving. Depending on the connection
5004 * mode, we'll have to wait for the last bytes to leave in either
5005 * direction, and sometimes for a close to be effective.
5006 */
5007
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005008 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5009 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005010 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5011 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005012 }
5013 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5014 /* Option forceclose is set, or either side wants to close,
5015 * let's enforce it now that we're not expecting any new
5016 * data to come. The caller knows the session is complete
5017 * once both states are CLOSED.
5018 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005019 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5020 channel_shutr_now(chn);
5021 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005022 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005023 }
5024 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005025 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5026 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005027 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005028 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5029 channel_auto_read(chn);
5030 txn->req.msg_state = HTTP_MSG_TUNNEL;
5031 chn->flags |= CF_NEVER_WAIT;
5032 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005033 }
5034
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005035 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005036 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005037 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005038
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005039 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005040 txn->req.msg_state = HTTP_MSG_CLOSING;
5041 goto http_msg_closing;
5042 }
5043 else {
5044 txn->req.msg_state = HTTP_MSG_CLOSED;
5045 goto http_msg_closed;
5046 }
5047 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005048 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005049 }
5050
5051 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5052 http_msg_closing:
5053 /* nothing else to forward, just waiting for the output buffer
5054 * to be empty and for the shutw_now to take effect.
5055 */
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_CLOSED;
5058 goto http_msg_closed;
5059 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005060 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005061 txn->req.msg_state = HTTP_MSG_ERROR;
5062 goto wait_other_side;
5063 }
5064 }
5065
5066 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5067 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005068 /* see above in MSG_DONE why we only do this in these states */
5069 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5070 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5071 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005072 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005073 goto wait_other_side;
5074 }
5075
5076 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005077 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005078}
5079
5080
5081/* This function updates the response state machine according to the request
5082 * state machine and buffer flags. It returns 1 if it changes anything (flag
5083 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5084 * it is only used to find when a request/response couple is complete. Both
5085 * this function and its equivalent should loop until both return zero. It
5086 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5087 */
5088int http_sync_res_state(struct session *s)
5089{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005090 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005091 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005092 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005093 unsigned int old_state = txn->rsp.msg_state;
5094
Willy Tarreau610ecce2010-01-04 21:15:02 +01005095 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5096 return 0;
5097
5098 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5099 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005100 * still monitor the server connection for a possible close
5101 * while the request is being uploaded, so we don't disable
5102 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005103 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005104 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005105
5106 if (txn->req.msg_state == HTTP_MSG_ERROR)
5107 goto wait_other_side;
5108
5109 if (txn->req.msg_state < HTTP_MSG_DONE) {
5110 /* The client seems to still be sending data, probably
5111 * because we got an error response during an upload.
5112 * We have the choice of either breaking the connection
5113 * or letting it pass through. Let's do the later.
5114 */
5115 goto wait_other_side;
5116 }
5117
5118 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5119 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005120 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005121 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005122 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005123 goto wait_other_side;
5124 }
5125
5126 /* When we get here, it means that both the request and the
5127 * response have finished receiving. Depending on the connection
5128 * mode, we'll have to wait for the last bytes to leave in either
5129 * direction, and sometimes for a close to be effective.
5130 */
5131
5132 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5133 /* Server-close mode : shut read and wait for the request
5134 * side to close its output buffer. The caller will detect
5135 * when we're in DONE and the other is in CLOSED and will
5136 * catch that for the final cleanup.
5137 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005138 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5139 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005140 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005141 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5142 /* Option forceclose is set, or either side wants to close,
5143 * let's enforce it now that we're not expecting any new
5144 * data to come. The caller knows the session is complete
5145 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005146 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005147 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5148 channel_shutr_now(chn);
5149 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005150 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005151 }
5152 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005153 /* The last possible modes are keep-alive and tunnel. Tunnel will
5154 * need to forward remaining data. Keep-alive will need to monitor
5155 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005156 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005157 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005158 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005159 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5160 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005161 }
5162
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005163 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005164 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005165 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005166 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5167 goto http_msg_closing;
5168 }
5169 else {
5170 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5171 goto http_msg_closed;
5172 }
5173 }
5174 goto wait_other_side;
5175 }
5176
5177 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5178 http_msg_closing:
5179 /* nothing else to forward, just waiting for the output buffer
5180 * to be empty and for the shutw_now to take effect.
5181 */
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_CLOSED;
5184 goto http_msg_closed;
5185 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005186 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005187 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005188 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005189 if (objt_server(s->target))
5190 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005191 goto wait_other_side;
5192 }
5193 }
5194
5195 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5196 http_msg_closed:
5197 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005198 bi_erase(chn);
5199 channel_auto_close(chn);
5200 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005201 goto wait_other_side;
5202 }
5203
5204 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005205 /* We force the response to leave immediately if we're waiting for the
5206 * other side, since there is no pending shutdown to push it out.
5207 */
5208 if (!channel_is_empty(chn))
5209 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005210 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005211}
5212
5213
5214/* Resync the request and response state machines. Return 1 if either state
5215 * changes.
5216 */
5217int http_resync_states(struct session *s)
5218{
5219 struct http_txn *txn = &s->txn;
5220 int old_req_state = txn->req.msg_state;
5221 int old_res_state = txn->rsp.msg_state;
5222
Willy Tarreau610ecce2010-01-04 21:15:02 +01005223 http_sync_req_state(s);
5224 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005225 if (!http_sync_res_state(s))
5226 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005227 if (!http_sync_req_state(s))
5228 break;
5229 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005230
Willy Tarreau610ecce2010-01-04 21:15:02 +01005231 /* OK, both state machines agree on a compatible state.
5232 * There are a few cases we're interested in :
5233 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5234 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5235 * directions, so let's simply disable both analysers.
5236 * - HTTP_MSG_CLOSED on the response only means we must abort the
5237 * request.
5238 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5239 * with server-close mode means we've completed one request and we
5240 * must re-initialize the server connection.
5241 */
5242
5243 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5244 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5245 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5246 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5247 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005248 channel_auto_close(s->req);
5249 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005250 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005251 channel_auto_close(s->rep);
5252 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005253 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005254 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5255 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005256 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005257 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005258 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005259 channel_auto_close(s->rep);
5260 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01005261 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005262 channel_abort(s->req);
5263 channel_auto_close(s->req);
5264 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005265 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005266 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005267 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5268 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005269 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005270 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5271 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5272 /* server-close/keep-alive: terminate this transaction,
5273 * possibly killing the server connection and reinitialize
5274 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005275 */
5276 http_end_txn_clean_session(s);
5277 }
5278
Willy Tarreau610ecce2010-01-04 21:15:02 +01005279 return txn->req.msg_state != old_req_state ||
5280 txn->rsp.msg_state != old_res_state;
5281}
5282
Willy Tarreaud98cf932009-12-27 22:54:55 +01005283/* This function is an analyser which forwards request body (including chunk
5284 * sizes if any). It is called as soon as we must forward, even if we forward
5285 * zero byte. The only situation where it must not be called is when we're in
5286 * tunnel mode and we want to forward till the close. It's used both to forward
5287 * remaining data and to resync after end of body. It expects the msg_state to
5288 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5289 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005290 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005291 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005292 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005293int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005294{
5295 struct http_txn *txn = &s->txn;
5296 struct http_msg *msg = &s->txn.req;
5297
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005298 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5299 return 0;
5300
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005301 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005302 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005303 /* Output closed while we were sending data. We must abort and
5304 * wake the other side up.
5305 */
5306 msg->msg_state = HTTP_MSG_ERROR;
5307 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005308 return 1;
5309 }
5310
Willy Tarreaud98cf932009-12-27 22:54:55 +01005311 /* Note that we don't have to send 100-continue back because we don't
5312 * need the data to complete our job, and it's up to the server to
5313 * decide whether to return 100, 417 or anything else in return of
5314 * an "Expect: 100-continue" header.
5315 */
5316
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005317 if (msg->sov) {
5318 /* we have msg->sov which points to the first byte of message
5319 * body, and req->buf.p still points to the beginning of the
5320 * message. We forward the headers now, as we don't need them
5321 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005322 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005323 b_adv(req->buf, msg->sov);
5324 msg->next -= msg->sov;
5325 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005326
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005327 /* The previous analysers guarantee that the state is somewhere
5328 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5329 * msg->next are always correct.
5330 */
5331 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5332 if (msg->flags & HTTP_MSGF_TE_CHNK)
5333 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5334 else
5335 msg->msg_state = HTTP_MSG_DATA;
5336 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005337 }
5338
Willy Tarreau7ba23542014-04-17 21:50:00 +02005339 /* Some post-connect processing might want us to refrain from starting to
5340 * forward data. Currently, the only reason for this is "balance url_param"
5341 * whichs need to parse/process the request after we've enabled forwarding.
5342 */
5343 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5344 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5345 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005346 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005347 goto missing_data;
5348 }
5349 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5350 }
5351
Willy Tarreau80a92c02014-03-12 10:41:13 +01005352 /* in most states, we should abort in case of early close */
5353 channel_auto_close(req);
5354
Willy Tarreauefdf0942014-04-24 20:08:57 +02005355 if (req->to_forward) {
5356 /* We can't process the buffer's contents yet */
5357 req->flags |= CF_WAKE_WRITE;
5358 goto missing_data;
5359 }
5360
Willy Tarreaud98cf932009-12-27 22:54:55 +01005361 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005362 if (msg->msg_state == HTTP_MSG_DATA) {
5363 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005364 /* we may have some pending data starting at req->buf->p */
5365 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005366 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005367 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005368 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005369 msg->next += msg->chunk_len;
5370 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005371
5372 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005373 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005374 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005375 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005376 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005377 }
5378 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005379 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005380 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005381 * TRAILERS state.
5382 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005383 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005384
Willy Tarreau54d23df2012-10-25 19:04:45 +02005385 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005386 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005387 else if (ret < 0) {
5388 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005389 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005390 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005391 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005392 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005393 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005394 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005395 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005396 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005397 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005398
5399 if (ret == 0)
5400 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005401 else if (ret < 0) {
5402 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005403 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005404 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005405 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005406 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005407 /* we're in MSG_CHUNK_SIZE now */
5408 }
5409 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005410 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005411
5412 if (ret == 0)
5413 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005414 else if (ret < 0) {
5415 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005416 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005417 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005418 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005419 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005420 /* we're in HTTP_MSG_DONE now */
5421 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005422 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005423 int old_state = msg->msg_state;
5424
Willy Tarreau610ecce2010-01-04 21:15:02 +01005425 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005426
5427 /* we may have some pending data starting at req->buf->p
5428 * such as last chunk of data or trailers.
5429 */
5430 b_adv(req->buf, msg->next);
5431 msg->next = 0;
5432
Willy Tarreau4fe41902010-06-07 22:27:41 +02005433 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005434 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5435 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005436 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005437 if (http_resync_states(s)) {
5438 /* some state changes occurred, maybe the analyser
5439 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005440 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005441 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005442 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005443 /* request errors are most likely due to
5444 * the server aborting the transfer.
5445 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005446 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005447 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005448 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005449 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005450 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005451 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005452 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005453 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005454
5455 /* If "option abortonclose" is set on the backend, we
5456 * want to monitor the client's connection and forward
5457 * any shutdown notification to the server, which will
5458 * decide whether to close or to go on processing the
5459 * request.
5460 */
5461 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005462 channel_auto_read(req);
5463 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005464 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005465 else if (s->txn.meth == HTTP_METH_POST) {
5466 /* POST requests may require to read extra CRLF
5467 * sent by broken browsers and which could cause
5468 * an RST to be sent upon close on some systems
5469 * (eg: Linux).
5470 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005471 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005472 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005473
Willy Tarreau610ecce2010-01-04 21:15:02 +01005474 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005475 }
5476 }
5477
Willy Tarreaud98cf932009-12-27 22:54:55 +01005478 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005479 /* we may have some pending data starting at req->buf->p */
5480 b_adv(req->buf, msg->next);
5481 msg->next = 0;
5482 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5483
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005484 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005485 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005486 if (!(s->flags & SN_ERR_MASK))
5487 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005488 if (!(s->flags & SN_FINST_MASK)) {
5489 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5490 s->flags |= SN_FINST_H;
5491 else
5492 s->flags |= SN_FINST_D;
5493 }
5494
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005495 s->fe->fe_counters.cli_aborts++;
5496 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005497 if (objt_server(s->target))
5498 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005499
5500 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005501 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005502
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005503 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005504 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005505 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005506
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005507 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005508 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005509 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005510 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005511 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005512
Willy Tarreau5c620922011-05-11 19:56:11 +02005513 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005514 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005515 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005516 * modes are already handled by the stream sock layer. We must not do
5517 * this in content-length mode because it could present the MSG_MORE
5518 * flag with the last block of forwarded data, which would cause an
5519 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005520 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005521 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005522 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005523
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005524 return 0;
5525
Willy Tarreaud98cf932009-12-27 22:54:55 +01005526 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005527 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005528 if (s->listener->counters)
5529 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005530
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005531 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005532 /* we may have some pending data starting at req->buf->p */
5533 b_adv(req->buf, msg->next);
5534 msg->next = 0;
5535
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005536 txn->req.msg_state = HTTP_MSG_ERROR;
5537 if (txn->status) {
5538 /* Note: we don't send any error if some data were already sent */
5539 stream_int_retnclose(req->prod, NULL);
5540 } else {
5541 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005542 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005543 }
5544 req->analysers = 0;
5545 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005546
5547 if (!(s->flags & SN_ERR_MASK))
5548 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005549 if (!(s->flags & SN_FINST_MASK)) {
5550 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5551 s->flags |= SN_FINST_H;
5552 else
5553 s->flags |= SN_FINST_D;
5554 }
5555 return 0;
5556
5557 aborted_xfer:
5558 txn->req.msg_state = HTTP_MSG_ERROR;
5559 if (txn->status) {
5560 /* Note: we don't send any error if some data were already sent */
5561 stream_int_retnclose(req->prod, NULL);
5562 } else {
5563 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005564 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005565 }
5566 req->analysers = 0;
5567 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5568
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005569 s->fe->fe_counters.srv_aborts++;
5570 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005571 if (objt_server(s->target))
5572 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005573
5574 if (!(s->flags & SN_ERR_MASK))
5575 s->flags |= SN_ERR_SRVCL;
5576 if (!(s->flags & SN_FINST_MASK)) {
5577 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5578 s->flags |= SN_FINST_H;
5579 else
5580 s->flags |= SN_FINST_D;
5581 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005582 return 0;
5583}
5584
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005585/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5586 * processing can continue on next analysers, or zero if it either needs more
5587 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5588 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5589 * when it has nothing left to do, and may remove any analyser when it wants to
5590 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005591 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005592int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005593{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005594 struct http_txn *txn = &s->txn;
5595 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005596 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005597 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005598 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005599 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005600
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005601 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 +02005602 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005603 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005604 rep,
5605 rep->rex, rep->wex,
5606 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005607 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005608 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005609
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005610 /*
5611 * Now parse the partial (or complete) lines.
5612 * We will check the response syntax, and also join multi-line
5613 * headers. An index of all the lines will be elaborated while
5614 * parsing.
5615 *
5616 * For the parsing, we use a 28 states FSM.
5617 *
5618 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005619 * rep->buf->p = beginning of response
5620 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5621 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005622 * msg->eol = end of current header or line (LF or CRLF)
5623 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005624 */
5625
Willy Tarreau628c40c2014-04-24 19:11:26 +02005626 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005627 /* There's a protected area at the end of the buffer for rewriting
5628 * purposes. We don't want to start to parse the request if the
5629 * protected area is affected, because we may have to move processed
5630 * data later, which is much more complicated.
5631 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005632 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005633 if (unlikely(!channel_reserved(rep))) {
5634 /* some data has still not left the buffer, wake us once that's done */
5635 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5636 goto abort_response;
5637 channel_dont_close(rep);
5638 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005639 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005640 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005641 }
5642
Willy Tarreau379357a2013-06-08 12:55:46 +02005643 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5644 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5645 buffer_slow_realign(rep->buf);
5646
Willy Tarreau9b28e032012-10-12 23:49:43 +02005647 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005648 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005649 }
5650
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005651 /* 1: we might have to print this header in debug mode */
5652 if (unlikely((global.mode & MODE_DEBUG) &&
5653 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005654 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005655 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005656
Willy Tarreau9b28e032012-10-12 23:49:43 +02005657 sol = rep->buf->p;
5658 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005659 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005660
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005661 sol += hdr_idx_first_pos(&txn->hdr_idx);
5662 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005663
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005664 while (cur_idx) {
5665 eol = sol + txn->hdr_idx.v[cur_idx].len;
5666 debug_hdr("srvhdr", s, sol, eol);
5667 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5668 cur_idx = txn->hdr_idx.v[cur_idx].next;
5669 }
5670 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005671
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005672 /*
5673 * Now we quickly check if we have found a full valid response.
5674 * If not so, we check the FD and buffer states before leaving.
5675 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005676 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005677 * responses are checked first.
5678 *
5679 * Depending on whether the client is still there or not, we
5680 * may send an error response back or not. Note that normally
5681 * we should only check for HTTP status there, and check I/O
5682 * errors somewhere else.
5683 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005684
Willy Tarreau655dce92009-11-08 13:10:58 +01005685 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005686 /* Invalid response */
5687 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5688 /* we detected a parsing error. We want to archive this response
5689 * in the dedicated proxy area for later troubleshooting.
5690 */
5691 hdr_response_bad:
5692 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005693 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005694
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005695 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005696 if (objt_server(s->target)) {
5697 objt_server(s->target)->counters.failed_resp++;
5698 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005699 }
Willy Tarreau64648412010-03-05 10:41:54 +01005700 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005701 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005702 rep->analysers = 0;
5703 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005704 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005705 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005706 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005707
5708 if (!(s->flags & SN_ERR_MASK))
5709 s->flags |= SN_ERR_PRXCOND;
5710 if (!(s->flags & SN_FINST_MASK))
5711 s->flags |= SN_FINST_H;
5712
5713 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005714 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005715
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005716 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005717 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005718 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005719 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005720 goto hdr_response_bad;
5721 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005722
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005723 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005724 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005725 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005726 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005727 else if (txn->flags & TX_NOT_FIRST)
5728 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005729
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005730 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005731 if (objt_server(s->target)) {
5732 objt_server(s->target)->counters.failed_resp++;
5733 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005734 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005735
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005736 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005737 rep->analysers = 0;
5738 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005739 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005740 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005741 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005742
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005743 if (!(s->flags & SN_ERR_MASK))
5744 s->flags |= SN_ERR_SRVCL;
5745 if (!(s->flags & SN_FINST_MASK))
5746 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005747 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005748 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005749
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005750 /* read timeout : return a 504 to the client. */
5751 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005752 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005753 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005754 else if (txn->flags & TX_NOT_FIRST)
5755 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005756
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005757 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005758 if (objt_server(s->target)) {
5759 objt_server(s->target)->counters.failed_resp++;
5760 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005761 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005762
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005763 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005764 rep->analysers = 0;
5765 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005766 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005767 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005768 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005769
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005770 if (!(s->flags & SN_ERR_MASK))
5771 s->flags |= SN_ERR_SRVTO;
5772 if (!(s->flags & SN_FINST_MASK))
5773 s->flags |= SN_FINST_H;
5774 return 0;
5775 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005776
Willy Tarreauf003d372012-11-26 13:35:37 +01005777 /* client abort with an abortonclose */
5778 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5779 s->fe->fe_counters.cli_aborts++;
5780 s->be->be_counters.cli_aborts++;
5781 if (objt_server(s->target))
5782 objt_server(s->target)->counters.cli_aborts++;
5783
5784 rep->analysers = 0;
5785 channel_auto_close(rep);
5786
5787 txn->status = 400;
5788 bi_erase(rep);
5789 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5790
5791 if (!(s->flags & SN_ERR_MASK))
5792 s->flags |= SN_ERR_CLICL;
5793 if (!(s->flags & SN_FINST_MASK))
5794 s->flags |= SN_FINST_H;
5795
5796 /* process_session() will take care of the error */
5797 return 0;
5798 }
5799
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005800 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005801 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005802 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005803 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005804 else if (txn->flags & TX_NOT_FIRST)
5805 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005806
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005807 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005808 if (objt_server(s->target)) {
5809 objt_server(s->target)->counters.failed_resp++;
5810 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005811 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005812
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005813 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005814 rep->analysers = 0;
5815 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005816 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005817 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005818 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005819
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005820 if (!(s->flags & SN_ERR_MASK))
5821 s->flags |= SN_ERR_SRVCL;
5822 if (!(s->flags & SN_FINST_MASK))
5823 s->flags |= SN_FINST_H;
5824 return 0;
5825 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005826
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005827 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005828 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005829 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005830 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005831 else if (txn->flags & TX_NOT_FIRST)
5832 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005833
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005834 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005835 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005836 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005837
5838 if (!(s->flags & SN_ERR_MASK))
5839 s->flags |= SN_ERR_CLICL;
5840 if (!(s->flags & SN_FINST_MASK))
5841 s->flags |= SN_FINST_H;
5842
5843 /* process_session() will take care of the error */
5844 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005845 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005846
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005847 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005848 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005849 return 0;
5850 }
5851
5852 /* More interesting part now : we know that we have a complete
5853 * response which at least looks like HTTP. We have an indicator
5854 * of each header's length, so we can parse them quickly.
5855 */
5856
5857 if (unlikely(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 Tarreaub37c27e2009-10-18 22:53:08 +02005859
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005860 /*
5861 * 1: get the status code
5862 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005863 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005864 if (n < 1 || n > 5)
5865 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005866 /* when the client triggers a 4xx from the server, it's most often due
5867 * to a missing object or permission. These events should be tracked
5868 * because if they happen often, it may indicate a brute force or a
5869 * vulnerability scan.
5870 */
5871 if (n == 4)
5872 session_inc_http_err_ctr(s);
5873
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005874 if (objt_server(s->target))
5875 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005876
Willy Tarreau5b154472009-12-21 20:11:07 +01005877 /* check if the response is HTTP/1.1 or above */
5878 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005879 ((rep->buf->p[5] > '1') ||
5880 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005881 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005882
5883 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005884 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 +01005885
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005886 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005887 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005888
Willy Tarreau9b28e032012-10-12 23:49:43 +02005889 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005890
Willy Tarreau39650402010-03-15 19:44:39 +01005891 /* Adjust server's health based on status code. Note: status codes 501
5892 * and 505 are triggered on demand by client request, so we must not
5893 * count them as server failures.
5894 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005895 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005896 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005897 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005898 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005899 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005900 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005901
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005902 /*
5903 * 2: check for cacheability.
5904 */
5905
5906 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005907 case 100:
5908 /*
5909 * We may be facing a 100-continue response, in which case this
5910 * is not the right response, and we're waiting for the next one.
5911 * Let's allow this response to go to the client and wait for the
5912 * next one.
5913 */
5914 hdr_idx_init(&txn->hdr_idx);
5915 msg->next -= channel_forward(rep, msg->next);
5916 msg->msg_state = HTTP_MSG_RPBEFORE;
5917 txn->status = 0;
5918 s->logs.t_data = -1; /* was not a response yet */
5919 goto next_one;
5920
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005921 case 200:
5922 case 203:
5923 case 206:
5924 case 300:
5925 case 301:
5926 case 410:
5927 /* RFC2616 @13.4:
5928 * "A response received with a status code of
5929 * 200, 203, 206, 300, 301 or 410 MAY be stored
5930 * by a cache (...) unless a cache-control
5931 * directive prohibits caching."
5932 *
5933 * RFC2616 @9.5: POST method :
5934 * "Responses to this method are not cacheable,
5935 * unless the response includes appropriate
5936 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005937 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005938 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005939 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005940 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5941 break;
5942 default:
5943 break;
5944 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005945
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005946 /*
5947 * 3: we may need to capture headers
5948 */
5949 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005950 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005951 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005952 txn->rsp.cap, s->fe->rsp_cap);
5953
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005954 /* 4: determine the transfer-length.
5955 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5956 * the presence of a message-body in a RESPONSE and its transfer length
5957 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005958 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005959 * All responses to the HEAD request method MUST NOT include a
5960 * message-body, even though the presence of entity-header fields
5961 * might lead one to believe they do. All 1xx (informational), 204
5962 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5963 * message-body. All other responses do include a message-body,
5964 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005965 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005966 * 1. Any response which "MUST NOT" include a message-body (such as the
5967 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5968 * always terminated by the first empty line after the header fields,
5969 * regardless of the entity-header fields present in the message.
5970 *
5971 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5972 * the "chunked" transfer-coding (Section 6.2) is used, the
5973 * transfer-length is defined by the use of this transfer-coding.
5974 * If a Transfer-Encoding header field is present and the "chunked"
5975 * transfer-coding is not present, the transfer-length is defined by
5976 * the sender closing the connection.
5977 *
5978 * 3. If a Content-Length header field is present, its decimal value in
5979 * OCTETs represents both the entity-length and the transfer-length.
5980 * If a message is received with both a Transfer-Encoding header
5981 * field and a Content-Length header field, the latter MUST be ignored.
5982 *
5983 * 4. If the message uses the media type "multipart/byteranges", and
5984 * the transfer-length is not otherwise specified, then this self-
5985 * delimiting media type defines the transfer-length. This media
5986 * type MUST NOT be used unless the sender knows that the recipient
5987 * can parse it; the presence in a request of a Range header with
5988 * multiple byte-range specifiers from a 1.1 client implies that the
5989 * client can parse multipart/byteranges responses.
5990 *
5991 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005992 */
5993
5994 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005995 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005996 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005997 * FIXME: should we parse anyway and return an error on chunked encoding ?
5998 */
5999 if (txn->meth == HTTP_METH_HEAD ||
6000 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006001 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006002 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006003 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006004 goto skip_content_length;
6005 }
6006
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006007 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006008 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006009 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006010 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006011 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006012 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6013 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006014 /* bad transfer-encoding (chunked followed by something else) */
6015 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006016 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006017 break;
6018 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006019 }
6020
6021 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
6022 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006023 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006024 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006025 signed long long cl;
6026
Willy Tarreauad14f752011-09-02 20:33:27 +02006027 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006028 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006029 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006030 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006031
Willy Tarreauad14f752011-09-02 20:33:27 +02006032 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006033 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006034 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006035 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006036
Willy Tarreauad14f752011-09-02 20:33:27 +02006037 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006038 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006039 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006040 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006041
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006042 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006043 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006044 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006045 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006046
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006047 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006048 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006049 }
6050
William Lallemand82fe75c2012-10-23 10:25:10 +02006051 if (s->fe->comp || s->be->comp)
6052 select_compression_response_header(s, rep->buf);
6053
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006054skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006055 /* Now we have to check if we need to modify the Connection header.
6056 * This is more difficult on the response than it is on the request,
6057 * because we can have two different HTTP versions and we don't know
6058 * how the client will interprete a response. For instance, let's say
6059 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6060 * HTTP/1.1 response without any header. Maybe it will bound itself to
6061 * HTTP/1.0 because it only knows about it, and will consider the lack
6062 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6063 * the lack of header as a keep-alive. Thus we will use two flags
6064 * indicating how a request MAY be understood by the client. In case
6065 * of multiple possibilities, we'll fix the header to be explicit. If
6066 * ambiguous cases such as both close and keepalive are seen, then we
6067 * will fall back to explicit close. Note that we won't take risks with
6068 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006069 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006070 */
6071
Willy Tarreaudc008c52010-02-01 16:20:08 +01006072 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6073 txn->status == 101)) {
6074 /* Either we've established an explicit tunnel, or we're
6075 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006076 * to understand the next protocols. We have to switch to tunnel
6077 * mode, so that we transfer the request and responses then let
6078 * this protocol pass unmodified. When we later implement specific
6079 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006080 * header which contains information about that protocol for
6081 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006082 */
6083 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6084 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006085 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6086 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006087 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6088 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006089 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006090
Willy Tarreau70dffda2014-01-30 03:07:23 +01006091 /* this situation happens when combining pretend-keepalive with httpclose. */
6092 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006093 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6094 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006095 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6096
Willy Tarreau60466522010-01-18 19:08:45 +01006097 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006098 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006099 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6100 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006101
Willy Tarreau60466522010-01-18 19:08:45 +01006102 /* now adjust header transformations depending on current state */
6103 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6104 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6105 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006106 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006107 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006108 }
Willy Tarreau60466522010-01-18 19:08:45 +01006109 else { /* SCL / KAL */
6110 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006111 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006112 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006113 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006114
Willy Tarreau60466522010-01-18 19:08:45 +01006115 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006116 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006117
Willy Tarreau60466522010-01-18 19:08:45 +01006118 /* Some keep-alive responses are converted to Server-close if
6119 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006120 */
Willy Tarreau60466522010-01-18 19:08:45 +01006121 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6122 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006123 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006124 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006125 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006126 }
6127
Willy Tarreau7959a552013-09-23 16:44:27 +02006128 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006129 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006130
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006131 /* end of job, return OK */
6132 rep->analysers &= ~an_bit;
6133 rep->analyse_exp = TICK_ETERNITY;
6134 channel_auto_close(rep);
6135 return 1;
6136
6137 abort_keep_alive:
6138 /* A keep-alive request to the server failed on a network error.
6139 * The client is required to retry. We need to close without returning
6140 * any other information so that the client retries.
6141 */
6142 txn->status = 0;
6143 rep->analysers = 0;
6144 s->req->analysers = 0;
6145 channel_auto_close(rep);
6146 s->logs.logwait = 0;
6147 s->logs.level = 0;
6148 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
6149 bi_erase(rep);
6150 stream_int_retnclose(rep->cons, NULL);
6151 return 0;
6152}
6153
6154/* This function performs all the processing enabled for the current response.
6155 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6156 * and updates s->rep->analysers. It might make sense to explode it into several
6157 * other functions. It works like process_request (see indications above).
6158 */
6159int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6160{
6161 struct http_txn *txn = &s->txn;
6162 struct http_msg *msg = &txn->rsp;
6163 struct proxy *cur_proxy;
6164 struct cond_wordlist *wl;
6165 struct http_res_rule *http_res_last_rule = NULL;
6166
6167 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6168 now_ms, __FUNCTION__,
6169 s,
6170 rep,
6171 rep->rex, rep->wex,
6172 rep->flags,
6173 rep->buf->i,
6174 rep->analysers);
6175
6176 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6177 return 0;
6178
6179 rep->analysers &= ~an_bit;
6180 rep->analyse_exp = TICK_ETERNITY;
6181
Willy Tarreau70730dd2014-04-24 18:06:27 +02006182 /* The stats applet needs to adjust the Connection header but we don't
6183 * apply any filter there.
6184 */
6185 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6186 goto skip_filters;
6187
Willy Tarreau58975672014-04-24 21:13:57 +02006188 /*
6189 * We will have to evaluate the filters.
6190 * As opposed to version 1.2, now they will be evaluated in the
6191 * filters order and not in the header order. This means that
6192 * each filter has to be validated among all headers.
6193 *
6194 * Filters are tried with ->be first, then with ->fe if it is
6195 * different from ->be.
6196 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006197
Willy Tarreau58975672014-04-24 21:13:57 +02006198 cur_proxy = s->be;
6199 while (1) {
6200 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006201
Willy Tarreau58975672014-04-24 21:13:57 +02006202 /* evaluate http-response rules */
6203 if (!http_res_last_rule)
6204 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 +02006205
Willy Tarreau58975672014-04-24 21:13:57 +02006206 /* try headers filters */
6207 if (rule_set->rsp_exp != NULL) {
6208 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6209 return_bad_resp:
6210 if (objt_server(s->target)) {
6211 objt_server(s->target)->counters.failed_resp++;
6212 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006213 }
Willy Tarreau58975672014-04-24 21:13:57 +02006214 s->be->be_counters.failed_resp++;
6215 return_srv_prx_502:
6216 rep->analysers = 0;
6217 txn->status = 502;
6218 s->logs.t_data = -1; /* was not a valid response */
6219 rep->prod->flags |= SI_FL_NOLINGER;
6220 bi_erase(rep);
6221 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6222 if (!(s->flags & SN_ERR_MASK))
6223 s->flags |= SN_ERR_PRXCOND;
6224 if (!(s->flags & SN_FINST_MASK))
6225 s->flags |= SN_FINST_H;
6226 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006227 }
Willy Tarreau58975672014-04-24 21:13:57 +02006228 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006229
Willy Tarreau58975672014-04-24 21:13:57 +02006230 /* has the response been denied ? */
6231 if (txn->flags & TX_SVDENY) {
6232 if (objt_server(s->target))
6233 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006234
Willy Tarreau58975672014-04-24 21:13:57 +02006235 s->be->be_counters.denied_resp++;
6236 s->fe->fe_counters.denied_resp++;
6237 if (s->listener->counters)
6238 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006239
Willy Tarreau58975672014-04-24 21:13:57 +02006240 goto return_srv_prx_502;
6241 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006242
Willy Tarreau58975672014-04-24 21:13:57 +02006243 /* add response headers from the rule sets in the same order */
6244 list_for_each_entry(wl, &rule_set->rsp_add, list) {
6245 if (txn->status < 200)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006246 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006247 if (wl->cond) {
6248 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6249 ret = acl_pass(ret);
6250 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6251 ret = !ret;
6252 if (!ret)
6253 continue;
6254 }
6255 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6256 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006257 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006258
Willy Tarreau58975672014-04-24 21:13:57 +02006259 /* check whether we're already working on the frontend */
6260 if (cur_proxy == s->fe)
6261 break;
6262 cur_proxy = s->fe;
6263 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006264
Willy Tarreau58975672014-04-24 21:13:57 +02006265 /* OK that's all we can do for 1xx responses */
6266 if (unlikely(txn->status < 200))
6267 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006268
Willy Tarreau58975672014-04-24 21:13:57 +02006269 /*
6270 * Now check for a server cookie.
6271 */
6272 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6273 (s->be->options & PR_O_CHK_CACHE))
6274 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006275
Willy Tarreau58975672014-04-24 21:13:57 +02006276 /*
6277 * Check for cache-control or pragma headers if required.
6278 */
6279 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
6280 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006281
Willy Tarreau58975672014-04-24 21:13:57 +02006282 /*
6283 * Add server cookie in the response if needed
6284 */
6285 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6286 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6287 (!(s->flags & SN_DIRECT) ||
6288 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6289 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6290 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6291 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6292 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6293 !(s->flags & SN_IGNORE_PRST)) {
6294 /* the server is known, it's not the one the client requested, or the
6295 * cookie's last seen date needs to be refreshed. We have to
6296 * insert a set-cookie here, except if we want to insert only on POST
6297 * requests and this one isn't. Note that servers which don't have cookies
6298 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006299 */
Willy Tarreau58975672014-04-24 21:13:57 +02006300 if (!objt_server(s->target)->cookie) {
6301 chunk_printf(&trash,
6302 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6303 s->be->cookie_name);
6304 }
6305 else {
6306 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006307
Willy Tarreau58975672014-04-24 21:13:57 +02006308 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6309 /* emit last_date, which is mandatory */
6310 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6311 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6312 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006313
Willy Tarreau58975672014-04-24 21:13:57 +02006314 if (s->be->cookie_maxlife) {
6315 /* emit first_date, which is either the original one or
6316 * the current date.
6317 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006318 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006319 s30tob64(txn->cookie_first_date ?
6320 txn->cookie_first_date >> 2 :
6321 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006322 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006323 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006324 }
Willy Tarreau58975672014-04-24 21:13:57 +02006325 chunk_appendf(&trash, "; path=/");
6326 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006327
Willy Tarreau58975672014-04-24 21:13:57 +02006328 if (s->be->cookie_domain)
6329 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006330
Willy Tarreau58975672014-04-24 21:13:57 +02006331 if (s->be->ck_opts & PR_CK_HTTPONLY)
6332 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006333
Willy Tarreau58975672014-04-24 21:13:57 +02006334 if (s->be->ck_opts & PR_CK_SECURE)
6335 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006336
Willy Tarreau58975672014-04-24 21:13:57 +02006337 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6338 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006339
Willy Tarreau58975672014-04-24 21:13:57 +02006340 txn->flags &= ~TX_SCK_MASK;
6341 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6342 /* the server did not change, only the date was updated */
6343 txn->flags |= TX_SCK_UPDATED;
6344 else
6345 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006346
Willy Tarreau58975672014-04-24 21:13:57 +02006347 /* Here, we will tell an eventual cache on the client side that we don't
6348 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6349 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6350 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006351 */
Willy Tarreau58975672014-04-24 21:13:57 +02006352 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006353
Willy Tarreau58975672014-04-24 21:13:57 +02006354 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006355
Willy Tarreau58975672014-04-24 21:13:57 +02006356 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6357 "Cache-control: private", 22) < 0))
6358 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006359 }
Willy Tarreau58975672014-04-24 21:13:57 +02006360 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006361
Willy Tarreau58975672014-04-24 21:13:57 +02006362 /*
6363 * Check if result will be cacheable with a cookie.
6364 * We'll block the response if security checks have caught
6365 * nasty things such as a cacheable cookie.
6366 */
6367 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6368 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6369 (s->be->options & PR_O_CHK_CACHE)) {
6370 /* we're in presence of a cacheable response containing
6371 * a set-cookie header. We'll block it as requested by
6372 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006373 */
Willy Tarreau58975672014-04-24 21:13:57 +02006374 if (objt_server(s->target))
6375 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006376
Willy Tarreau58975672014-04-24 21:13:57 +02006377 s->be->be_counters.denied_resp++;
6378 s->fe->fe_counters.denied_resp++;
6379 if (s->listener->counters)
6380 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006381
Willy Tarreau58975672014-04-24 21:13:57 +02006382 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6383 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6384 send_log(s->be, LOG_ALERT,
6385 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6386 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6387 goto return_srv_prx_502;
6388 }
Willy Tarreau03945942009-12-22 16:50:27 +01006389
Willy Tarreau70730dd2014-04-24 18:06:27 +02006390 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006391 /*
6392 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6393 * If an "Upgrade" token is found, the header is left untouched in order
6394 * not to have to deal with some client bugs : some of them fail an upgrade
6395 * if anything but "Upgrade" is present in the Connection header.
6396 */
6397 if (!(txn->flags & TX_HDR_CONN_UPG) &&
6398 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6399 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6400 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6401 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006402
Willy Tarreau58975672014-04-24 21:13:57 +02006403 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6404 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6405 /* we want a keep-alive response here. Keep-alive header
6406 * required if either side is not 1.1.
6407 */
6408 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6409 want_flags |= TX_CON_KAL_SET;
6410 }
6411 else {
6412 /* we want a close response here. Close header required if
6413 * the server is 1.1, regardless of the client.
6414 */
6415 if (msg->flags & HTTP_MSGF_VER_11)
6416 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006417 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006418
Willy Tarreau58975672014-04-24 21:13:57 +02006419 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6420 http_change_connection_header(txn, msg, want_flags);
6421 }
6422
6423 skip_header_mangling:
6424 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6425 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6426 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006427
Willy Tarreau58975672014-04-24 21:13:57 +02006428 /* if the user wants to log as soon as possible, without counting
6429 * bytes from the server, then this is the right moment. We have
6430 * to temporarily assign bytes_out to log what we currently have.
6431 */
6432 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6433 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6434 s->logs.bytes_out = txn->rsp.eoh;
6435 s->do_log(s);
6436 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006437 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006438 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006439}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006440
Willy Tarreaud98cf932009-12-27 22:54:55 +01006441/* This function is an analyser which forwards response body (including chunk
6442 * sizes if any). It is called as soon as we must forward, even if we forward
6443 * zero byte. The only situation where it must not be called is when we're in
6444 * tunnel mode and we want to forward till the close. It's used both to forward
6445 * remaining data and to resync after end of body. It expects the msg_state to
6446 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6447 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006448 *
6449 * It is capable of compressing response data both in content-length mode and
6450 * in chunked mode. The state machines follows different flows depending on
6451 * whether content-length and chunked modes are used, since there are no
6452 * trailers in content-length :
6453 *
6454 * chk-mode cl-mode
6455 * ,----- BODY -----.
6456 * / \
6457 * V size > 0 V chk-mode
6458 * .--> SIZE -------------> DATA -------------> CRLF
6459 * | | size == 0 | last byte |
6460 * | v final crlf v inspected |
6461 * | TRAILERS -----------> DONE |
6462 * | |
6463 * `----------------------------------------------'
6464 *
6465 * Compression only happens in the DATA state, and must be flushed in final
6466 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6467 * is performed at once on final states for all bytes parsed, or when leaving
6468 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006469 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006470int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006471{
6472 struct http_txn *txn = &s->txn;
6473 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006474 static struct buffer *tmpbuf = NULL;
6475 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006476 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006477
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006478 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6479 return 0;
6480
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006481 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006482 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006483 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006484 /* Output closed while we were sending data. We must abort and
6485 * wake the other side up.
6486 */
6487 msg->msg_state = HTTP_MSG_ERROR;
6488 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006489 return 1;
6490 }
6491
Willy Tarreau4fe41902010-06-07 22:27:41 +02006492 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006493 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006494
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006495 if (msg->sov) {
6496 /* we have msg->sov which points to the first byte of message
6497 * body, and res->buf.p still points to the beginning of the
6498 * message. We forward the headers now, as we don't need them
6499 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006500 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006501 b_adv(res->buf, msg->sov);
6502 msg->next -= msg->sov;
6503 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006504
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006505 /* The previous analysers guarantee that the state is somewhere
6506 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6507 * msg->next are always correct.
6508 */
6509 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6510 if (msg->flags & HTTP_MSGF_TE_CHNK)
6511 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6512 else
6513 msg->msg_state = HTTP_MSG_DATA;
6514 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006515 }
6516
Willy Tarreauefdf0942014-04-24 20:08:57 +02006517 if (res->to_forward) {
6518 /* We can't process the buffer's contents yet */
6519 res->flags |= CF_WAKE_WRITE;
6520 goto missing_data;
6521 }
6522
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006523 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6524 /* We need a compression buffer in the DATA state to put the
6525 * output of compressed data, and in CRLF state to let the
6526 * TRAILERS state finish the job of removing the trailing CRLF.
6527 */
6528 if (unlikely(tmpbuf == NULL)) {
6529 /* this is the first time we need the compression buffer */
6530 tmpbuf = pool_alloc2(pool2_buffer);
6531 if (tmpbuf == NULL)
6532 goto aborted_xfer; /* no memory */
6533 }
6534
6535 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006536 if (ret < 0) {
6537 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006538 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006539 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006540 compressing = 1;
6541 }
6542
Willy Tarreaud98cf932009-12-27 22:54:55 +01006543 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006544 switch (msg->msg_state - HTTP_MSG_DATA) {
6545 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006546 /* we may have some pending data starting at res->buf->p */
6547 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006548 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006549 if (ret < 0)
6550 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006551
Willy Tarreaud5a67832014-04-21 10:54:27 +02006552 if (msg->chunk_len) {
6553 /* input empty or output full */
6554 if (res->buf->i > msg->next)
6555 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006556 goto missing_data;
6557 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006558 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006559 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006560 if (msg->chunk_len > res->buf->i - msg->next) {
6561 /* output full */
6562 res->flags |= CF_WAKE_WRITE;
6563 goto missing_data;
6564 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006565 msg->next += msg->chunk_len;
6566 msg->chunk_len = 0;
6567 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006568
6569 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006570 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006571 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006572 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006573 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006574 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006575 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006576 /* fall through for HTTP_MSG_CHUNK_CRLF */
6577
6578 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6579 /* we want the CRLF after the data */
6580
6581 ret = http_skip_chunk_crlf(msg);
6582 if (ret == 0)
6583 goto missing_data;
6584 else if (ret < 0) {
6585 if (msg->err_pos >= 0)
6586 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6587 goto return_bad_res;
6588 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006589 /* we're in MSG_CHUNK_SIZE now, fall through */
6590
6591 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006592 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006593 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006594 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006595 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006596
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006597 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006598 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006599 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006600 else if (ret < 0) {
6601 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006602 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006603 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006604 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006605 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006606 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006607
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006608 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006609 if (unlikely(compressing)) {
6610 /* we need to flush output contents before syncing FSMs */
6611 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6612 compressing = 0;
6613 }
6614
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006615 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006616 if (ret == 0)
6617 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006618 else if (ret < 0) {
6619 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006620 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006621 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006622 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006623 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006624
6625 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006626 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006627 if (unlikely(compressing)) {
6628 /* we need to flush output contents before syncing FSMs */
6629 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6630 compressing = 0;
6631 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006632
Willy Tarreauc623c172014-04-18 09:53:50 +02006633 /* we may have some pending data starting at res->buf->p
6634 * such as a last chunk of data or trailers.
6635 */
6636 b_adv(res->buf, msg->next);
6637 msg->next = 0;
6638
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006639 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006640 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006641 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6642 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006643 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006644
Willy Tarreau610ecce2010-01-04 21:15:02 +01006645 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006646 /* some state changes occurred, maybe the analyser
6647 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006648 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006649 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006650 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006651 /* response errors are most likely due to
6652 * the client aborting the transfer.
6653 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006654 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006655 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006656 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006657 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006658 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006659 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006660 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006661 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006662 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006663 }
6664 }
6665
Willy Tarreaud98cf932009-12-27 22:54:55 +01006666 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006667 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006668 if (unlikely(compressing)) {
6669 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006670 compressing = 0;
6671 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006672
Willy Tarreauc623c172014-04-18 09:53:50 +02006673 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6674 b_adv(res->buf, msg->next);
6675 msg->next = 0;
6676 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6677 }
6678
Willy Tarreauf003d372012-11-26 13:35:37 +01006679 if (res->flags & CF_SHUTW)
6680 goto aborted_xfer;
6681
6682 /* stop waiting for data if the input is closed before the end. If the
6683 * client side was already closed, it means that the client has aborted,
6684 * so we don't want to count this as a server abort. Otherwise it's a
6685 * server abort.
6686 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006687 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006688 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006689 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006690 if (!(s->flags & SN_ERR_MASK))
6691 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006692 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006693 if (objt_server(s->target))
6694 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006695 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006696 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006697
Willy Tarreau40dba092010-03-04 18:14:51 +01006698 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006699 if (!s->req->analysers)
6700 goto return_bad_res;
6701
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006702 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006703 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006704 * Similarly, with keep-alive on the client side, we don't want to forward a
6705 * close.
6706 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006707 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006708 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6709 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006710 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006711
Willy Tarreau5c620922011-05-11 19:56:11 +02006712 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006713 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006714 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006715 * modes are already handled by the stream sock layer. We must not do
6716 * this in content-length mode because it could present the MSG_MORE
6717 * flag with the last block of forwarded data, which would cause an
6718 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006719 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006720 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006721 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006722
Willy Tarreaud98cf932009-12-27 22:54:55 +01006723 /* the session handler will take care of timeouts and errors */
6724 return 0;
6725
Willy Tarreau40dba092010-03-04 18:14:51 +01006726 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006727 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006728 if (objt_server(s->target))
6729 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006730
6731 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006732 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006733 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006734 compressing = 0;
6735 }
6736
Willy Tarreauc623c172014-04-18 09:53:50 +02006737 /* we may have some pending data starting at res->buf->p */
6738 if (s->comp_algo == NULL) {
6739 b_adv(res->buf, msg->next);
6740 msg->next = 0;
6741 }
6742
Willy Tarreaud98cf932009-12-27 22:54:55 +01006743 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006744 /* don't send any error message as we're in the body */
6745 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006746 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006747 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006748 if (objt_server(s->target))
6749 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006750
6751 if (!(s->flags & SN_ERR_MASK))
6752 s->flags |= SN_ERR_PRXCOND;
6753 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006754 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006755 return 0;
6756
6757 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006758 if (unlikely(compressing)) {
6759 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6760 compressing = 0;
6761 }
6762
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006763 txn->rsp.msg_state = HTTP_MSG_ERROR;
6764 /* don't send any error message as we're in the body */
6765 stream_int_retnclose(res->cons, NULL);
6766 res->analysers = 0;
6767 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6768
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006769 s->fe->fe_counters.cli_aborts++;
6770 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006771 if (objt_server(s->target))
6772 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006773
6774 if (!(s->flags & SN_ERR_MASK))
6775 s->flags |= SN_ERR_CLICL;
6776 if (!(s->flags & SN_FINST_MASK))
6777 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006778 return 0;
6779}
6780
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006781/* Iterate the same filter through all request headers.
6782 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006783 * Since it can manage the switch to another backend, it updates the per-proxy
6784 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006785 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006786int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006787{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006788 char *cur_ptr, *cur_end, *cur_next;
6789 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006790 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006791 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006792 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006793
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006794 last_hdr = 0;
6795
Willy Tarreau9b28e032012-10-12 23:49:43 +02006796 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006797 old_idx = 0;
6798
6799 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006800 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006801 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006802 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006803 (exp->action == ACT_ALLOW ||
6804 exp->action == ACT_DENY ||
6805 exp->action == ACT_TARPIT))
6806 return 0;
6807
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006808 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006809 if (!cur_idx)
6810 break;
6811
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006812 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006813 cur_ptr = cur_next;
6814 cur_end = cur_ptr + cur_hdr->len;
6815 cur_next = cur_end + cur_hdr->cr + 1;
6816
6817 /* Now we have one header between cur_ptr and cur_end,
6818 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006819 */
6820
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02006821 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006822 switch (exp->action) {
6823 case ACT_SETBE:
6824 /* It is not possible to jump a second time.
6825 * FIXME: should we return an HTTP/500 here so that
6826 * the admin knows there's a problem ?
6827 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006828 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006829 break;
6830
6831 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006832 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006833 last_hdr = 1;
6834 break;
6835
6836 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006837 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006838 last_hdr = 1;
6839 break;
6840
6841 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006842 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006843 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006844 break;
6845
6846 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006847 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006848 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006849 break;
6850
6851 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006852 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6853 if (trash.len < 0)
6854 return -1;
6855
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006856 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006857 /* FIXME: if the user adds a newline in the replacement, the
6858 * index will not be recalculated for now, and the new line
6859 * will not be counted as a new header.
6860 */
6861
6862 cur_end += delta;
6863 cur_next += delta;
6864 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006865 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006866 break;
6867
6868 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006869 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006870 cur_next += delta;
6871
Willy Tarreaufa355d42009-11-29 18:12:29 +01006872 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006873 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6874 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006875 cur_hdr->len = 0;
6876 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006877 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006878 break;
6879
6880 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006881 }
6882
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006883 /* keep the link from this header to next one in case of later
6884 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006885 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006886 old_idx = cur_idx;
6887 }
6888 return 0;
6889}
6890
6891
6892/* Apply the filter to the request line.
6893 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6894 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006895 * Since it can manage the switch to another backend, it updates the per-proxy
6896 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006897 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006898int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006899{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006900 char *cur_ptr, *cur_end;
6901 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006902 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006903 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006904
Willy Tarreau3d300592007-03-18 18:34:41 +01006905 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006906 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006907 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006908 (exp->action == ACT_ALLOW ||
6909 exp->action == ACT_DENY ||
6910 exp->action == ACT_TARPIT))
6911 return 0;
6912 else if (exp->action == ACT_REMOVE)
6913 return 0;
6914
6915 done = 0;
6916
Willy Tarreau9b28e032012-10-12 23:49:43 +02006917 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006918 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006919
6920 /* Now we have the request line between cur_ptr and cur_end */
6921
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02006922 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006923 switch (exp->action) {
6924 case ACT_SETBE:
6925 /* It is not possible to jump a second time.
6926 * FIXME: should we return an HTTP/500 here so that
6927 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006928 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006929 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006930 break;
6931
6932 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006933 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006934 done = 1;
6935 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006936
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006937 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006938 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006939 done = 1;
6940 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006941
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006942 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006943 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006944 done = 1;
6945 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006946
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006947 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006948 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006949 done = 1;
6950 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006951
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006952 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006953 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6954 if (trash.len < 0)
6955 return -1;
6956
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006957 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006958 /* FIXME: if the user adds a newline in the replacement, the
6959 * index will not be recalculated for now, and the new line
6960 * will not be counted as a new header.
6961 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006962
Willy Tarreaufa355d42009-11-29 18:12:29 +01006963 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006964 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006965 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006966 HTTP_MSG_RQMETH,
6967 cur_ptr, cur_end + 1,
6968 NULL, NULL);
6969 if (unlikely(!cur_end))
6970 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006971
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006972 /* we have a full request and we know that we have either a CR
6973 * or an LF at <ptr>.
6974 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006975 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6976 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006977 /* there is no point trying this regex on headers */
6978 return 1;
6979 }
6980 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006981 return done;
6982}
Willy Tarreau97de6242006-12-27 17:18:38 +01006983
Willy Tarreau58f10d72006-12-04 02:26:12 +01006984
Willy Tarreau58f10d72006-12-04 02:26:12 +01006985
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006986/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006987 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006988 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006989 * unparsable request. Since it can manage the switch to another backend, it
6990 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006991 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006992int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006993{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006994 struct http_txn *txn = &s->txn;
6995 struct hdr_exp *exp;
6996
6997 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006998 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006999
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007000 /*
7001 * The interleaving of transformations and verdicts
7002 * makes it difficult to decide to continue or stop
7003 * the evaluation.
7004 */
7005
Willy Tarreau6c123b12010-01-28 20:22:06 +01007006 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7007 break;
7008
Willy Tarreau3d300592007-03-18 18:34:41 +01007009 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007010 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007011 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007012 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007013
7014 /* if this filter had a condition, evaluate it now and skip to
7015 * next filter if the condition does not match.
7016 */
7017 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007018 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007019 ret = acl_pass(ret);
7020 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7021 ret = !ret;
7022
7023 if (!ret)
7024 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007025 }
7026
7027 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007028 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007029 if (unlikely(ret < 0))
7030 return -1;
7031
7032 if (likely(ret == 0)) {
7033 /* The filter did not match the request, it can be
7034 * iterated through all headers.
7035 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007036 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007037 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007038 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007039 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007040}
7041
7042
Willy Tarreaua15645d2007-03-18 16:22:39 +01007043
Willy Tarreau58f10d72006-12-04 02:26:12 +01007044/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007045 * Try to retrieve the server associated to the appsession.
7046 * If the server is found, it's assigned to the session.
7047 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007048void manage_client_side_appsession(struct session *s, const char *buf, int len) {
7049 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007050 appsess *asession = NULL;
7051 char *sessid_temp = NULL;
7052
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007053 if (len > s->be->appsession_len) {
7054 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007055 }
7056
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007057 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007058 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007059 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007060 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007061 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007062 }
7063
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007064 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007065 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007066 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007067 return;
7068 }
7069
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007070 memcpy(txn->sessid, buf, len);
7071 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007072 }
7073
7074 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7075 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007076 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007077 return;
7078 }
7079
Cyril Bontéb21570a2009-11-29 20:04:48 +01007080 memcpy(sessid_temp, buf, len);
7081 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007082
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007083 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007084 /* free previously allocated memory */
7085 pool_free2(apools.sessid, sessid_temp);
7086
7087 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007088 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7089 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007090 asession->request_count++;
7091
7092 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007093 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007094
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007095 while (srv) {
7096 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007097 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007098 (s->be->options & PR_O_PERSIST) ||
7099 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007100 /* we found the server and it's usable */
7101 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007102 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007103 s->flags |= SN_DIRECT | SN_ASSIGNED;
7104 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007105
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007106 break;
7107 } else {
7108 txn->flags &= ~TX_CK_MASK;
7109 txn->flags |= TX_CK_DOWN;
7110 }
7111 }
7112 srv = srv->next;
7113 }
7114 }
7115 }
7116}
7117
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007118/* Find the end of a cookie value contained between <s> and <e>. It works the
7119 * same way as with headers above except that the semi-colon also ends a token.
7120 * See RFC2965 for more information. Note that it requires a valid header to
7121 * return a valid result.
7122 */
7123char *find_cookie_value_end(char *s, const char *e)
7124{
7125 int quoted, qdpair;
7126
7127 quoted = qdpair = 0;
7128 for (; s < e; s++) {
7129 if (qdpair) qdpair = 0;
7130 else if (quoted) {
7131 if (*s == '\\') qdpair = 1;
7132 else if (*s == '"') quoted = 0;
7133 }
7134 else if (*s == '"') quoted = 1;
7135 else if (*s == ',' || *s == ';') return s;
7136 }
7137 return s;
7138}
7139
7140/* Delete a value in a header between delimiters <from> and <next> in buffer
7141 * <buf>. The number of characters displaced is returned, and the pointer to
7142 * the first delimiter is updated if required. The function tries as much as
7143 * possible to respect the following principles :
7144 * - replace <from> delimiter by the <next> one unless <from> points to a
7145 * colon, in which case <next> is simply removed
7146 * - set exactly one space character after the new first delimiter, unless
7147 * there are not enough characters in the block being moved to do so.
7148 * - remove unneeded spaces before the previous delimiter and after the new
7149 * one.
7150 *
7151 * It is the caller's responsibility to ensure that :
7152 * - <from> points to a valid delimiter or the colon ;
7153 * - <next> points to a valid delimiter or the final CR/LF ;
7154 * - there are non-space chars before <from> ;
7155 * - there is a CR/LF at or after <next>.
7156 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007157int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007158{
7159 char *prev = *from;
7160
7161 if (*prev == ':') {
7162 /* We're removing the first value, preserve the colon and add a
7163 * space if possible.
7164 */
7165 if (!http_is_crlf[(unsigned char)*next])
7166 next++;
7167 prev++;
7168 if (prev < next)
7169 *prev++ = ' ';
7170
7171 while (http_is_spht[(unsigned char)*next])
7172 next++;
7173 } else {
7174 /* Remove useless spaces before the old delimiter. */
7175 while (http_is_spht[(unsigned char)*(prev-1)])
7176 prev--;
7177 *from = prev;
7178
7179 /* copy the delimiter and if possible a space if we're
7180 * not at the end of the line.
7181 */
7182 if (!http_is_crlf[(unsigned char)*next]) {
7183 *prev++ = *next++;
7184 if (prev + 1 < next)
7185 *prev++ = ' ';
7186 while (http_is_spht[(unsigned char)*next])
7187 next++;
7188 }
7189 }
7190 return buffer_replace2(buf, prev, next, NULL, 0);
7191}
7192
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007193/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007194 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007195 * desirable to call it only when needed. This code is quite complex because
7196 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7197 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007198 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007199void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007200{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007201 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007202 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007203 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007204 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7205 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007206
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007207 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007208 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007209 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007210
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007211 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007212 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007213 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007214
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007215 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007216 hdr_beg = hdr_next;
7217 hdr_end = hdr_beg + cur_hdr->len;
7218 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007219
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007220 /* We have one full header between hdr_beg and hdr_end, and the
7221 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007222 * "Cookie:" headers.
7223 */
7224
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007225 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007226 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007227 old_idx = cur_idx;
7228 continue;
7229 }
7230
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007231 del_from = NULL; /* nothing to be deleted */
7232 preserve_hdr = 0; /* assume we may kill the whole header */
7233
Willy Tarreau58f10d72006-12-04 02:26:12 +01007234 /* Now look for cookies. Conforming to RFC2109, we have to support
7235 * attributes whose name begin with a '$', and associate them with
7236 * the right cookie, if we want to delete this cookie.
7237 * So there are 3 cases for each cookie read :
7238 * 1) it's a special attribute, beginning with a '$' : ignore it.
7239 * 2) it's a server id cookie that we *MAY* want to delete : save
7240 * some pointers on it (last semi-colon, beginning of cookie...)
7241 * 3) it's an application cookie : we *MAY* have to delete a previous
7242 * "special" cookie.
7243 * At the end of loop, if a "special" cookie remains, we may have to
7244 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007245 * *MUST* delete it.
7246 *
7247 * Note: RFC2965 is unclear about the processing of spaces around
7248 * the equal sign in the ATTR=VALUE form. A careful inspection of
7249 * the RFC explicitly allows spaces before it, and not within the
7250 * tokens (attrs or values). An inspection of RFC2109 allows that
7251 * too but section 10.1.3 lets one think that spaces may be allowed
7252 * after the equal sign too, resulting in some (rare) buggy
7253 * implementations trying to do that. So let's do what servers do.
7254 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7255 * allowed quoted strings in values, with any possible character
7256 * after a backslash, including control chars and delimitors, which
7257 * causes parsing to become ambiguous. Browsers also allow spaces
7258 * within values even without quotes.
7259 *
7260 * We have to keep multiple pointers in order to support cookie
7261 * removal at the beginning, middle or end of header without
7262 * corrupting the header. All of these headers are valid :
7263 *
7264 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7265 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7266 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7267 * | | | | | | | | |
7268 * | | | | | | | | hdr_end <--+
7269 * | | | | | | | +--> next
7270 * | | | | | | +----> val_end
7271 * | | | | | +-----------> val_beg
7272 * | | | | +--------------> equal
7273 * | | | +----------------> att_end
7274 * | | +---------------------> att_beg
7275 * | +--------------------------> prev
7276 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007277 */
7278
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007279 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7280 /* Iterate through all cookies on this line */
7281
7282 /* find att_beg */
7283 att_beg = prev + 1;
7284 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7285 att_beg++;
7286
7287 /* find att_end : this is the first character after the last non
7288 * space before the equal. It may be equal to hdr_end.
7289 */
7290 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007291
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007292 while (equal < hdr_end) {
7293 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007294 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007295 if (http_is_spht[(unsigned char)*equal++])
7296 continue;
7297 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007298 }
7299
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007300 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7301 * is between <att_beg> and <equal>, both may be identical.
7302 */
7303
7304 /* look for end of cookie if there is an equal sign */
7305 if (equal < hdr_end && *equal == '=') {
7306 /* look for the beginning of the value */
7307 val_beg = equal + 1;
7308 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7309 val_beg++;
7310
7311 /* find the end of the value, respecting quotes */
7312 next = find_cookie_value_end(val_beg, hdr_end);
7313
7314 /* make val_end point to the first white space or delimitor after the value */
7315 val_end = next;
7316 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7317 val_end--;
7318 } else {
7319 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007320 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007321
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007322 /* We have nothing to do with attributes beginning with '$'. However,
7323 * they will automatically be removed if a header before them is removed,
7324 * since they're supposed to be linked together.
7325 */
7326 if (*att_beg == '$')
7327 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007328
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007329 /* Ignore cookies with no equal sign */
7330 if (equal == next) {
7331 /* This is not our cookie, so we must preserve it. But if we already
7332 * scheduled another cookie for removal, we cannot remove the
7333 * complete header, but we can remove the previous block itself.
7334 */
7335 preserve_hdr = 1;
7336 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007337 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007338 val_end += delta;
7339 next += delta;
7340 hdr_end += delta;
7341 hdr_next += delta;
7342 cur_hdr->len += delta;
7343 http_msg_move_end(&txn->req, delta);
7344 prev = del_from;
7345 del_from = NULL;
7346 }
7347 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007348 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007349
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007350 /* if there are spaces around the equal sign, we need to
7351 * strip them otherwise we'll get trouble for cookie captures,
7352 * or even for rewrites. Since this happens extremely rarely,
7353 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007354 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007355 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7356 int stripped_before = 0;
7357 int stripped_after = 0;
7358
7359 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007360 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007361 equal += stripped_before;
7362 val_beg += stripped_before;
7363 }
7364
7365 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007366 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007367 val_beg += stripped_after;
7368 stripped_before += stripped_after;
7369 }
7370
7371 val_end += stripped_before;
7372 next += stripped_before;
7373 hdr_end += stripped_before;
7374 hdr_next += stripped_before;
7375 cur_hdr->len += stripped_before;
7376 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007377 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007378 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007379
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007380 /* First, let's see if we want to capture this cookie. We check
7381 * that we don't already have a client side cookie, because we
7382 * can only capture one. Also as an optimisation, we ignore
7383 * cookies shorter than the declared name.
7384 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007385 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7386 (val_end - att_beg >= s->fe->capture_namelen) &&
7387 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007388 int log_len = val_end - att_beg;
7389
7390 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7391 Alert("HTTP logging : out of memory.\n");
7392 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007393 if (log_len > s->fe->capture_len)
7394 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007395 memcpy(txn->cli_cookie, att_beg, log_len);
7396 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007397 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007398 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007399
Willy Tarreaubca99692010-10-06 19:25:55 +02007400 /* Persistence cookies in passive, rewrite or insert mode have the
7401 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007402 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007403 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007404 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007405 * For cookies in prefix mode, the form is :
7406 *
7407 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007408 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007409 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7410 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7411 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007412 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007413
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007414 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7415 * have the server ID between val_beg and delim, and the original cookie between
7416 * delim+1 and val_end. Otherwise, delim==val_end :
7417 *
7418 * Cookie: NAME=SRV; # in all but prefix modes
7419 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7420 * | || || | |+-> next
7421 * | || || | +--> val_end
7422 * | || || +---------> delim
7423 * | || |+------------> val_beg
7424 * | || +-------------> att_end = equal
7425 * | |+-----------------> att_beg
7426 * | +------------------> prev
7427 * +-------------------------> hdr_beg
7428 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007429
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007430 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007431 for (delim = val_beg; delim < val_end; delim++)
7432 if (*delim == COOKIE_DELIM)
7433 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007434 } else {
7435 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007436 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007437 /* Now check if the cookie contains a date field, which would
7438 * appear after a vertical bar ('|') just after the server name
7439 * and before the delimiter.
7440 */
7441 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7442 if (vbar1) {
7443 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007444 * right is the last seen date. It is a base64 encoded
7445 * 30-bit value representing the UNIX date since the
7446 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007447 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007448 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007449 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007450 if (val_end - vbar1 >= 5) {
7451 val = b64tos30(vbar1);
7452 if (val > 0)
7453 txn->cookie_last_date = val << 2;
7454 }
7455 /* look for a second vertical bar */
7456 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7457 if (vbar1 && (val_end - vbar1 > 5)) {
7458 val = b64tos30(vbar1 + 1);
7459 if (val > 0)
7460 txn->cookie_first_date = val << 2;
7461 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007462 }
7463 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007464
Willy Tarreauf64d1412010-10-07 20:06:11 +02007465 /* if the cookie has an expiration date and the proxy wants to check
7466 * it, then we do that now. We first check if the cookie is too old,
7467 * then only if it has expired. We detect strict overflow because the
7468 * time resolution here is not great (4 seconds). Cookies with dates
7469 * in the future are ignored if their offset is beyond one day. This
7470 * allows an admin to fix timezone issues without expiring everyone
7471 * and at the same time avoids keeping unwanted side effects for too
7472 * long.
7473 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007474 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7475 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007476 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007477 txn->flags &= ~TX_CK_MASK;
7478 txn->flags |= TX_CK_OLD;
7479 delim = val_beg; // let's pretend we have not found the cookie
7480 txn->cookie_first_date = 0;
7481 txn->cookie_last_date = 0;
7482 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007483 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7484 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007485 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007486 txn->flags &= ~TX_CK_MASK;
7487 txn->flags |= TX_CK_EXPIRED;
7488 delim = val_beg; // let's pretend we have not found the cookie
7489 txn->cookie_first_date = 0;
7490 txn->cookie_last_date = 0;
7491 }
7492
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007493 /* Here, we'll look for the first running server which supports the cookie.
7494 * This allows to share a same cookie between several servers, for example
7495 * to dedicate backup servers to specific servers only.
7496 * However, to prevent clients from sticking to cookie-less backup server
7497 * when they have incidentely learned an empty cookie, we simply ignore
7498 * empty cookies and mark them as invalid.
7499 * The same behaviour is applied when persistence must be ignored.
7500 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007501 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007502 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007503
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007504 while (srv) {
7505 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7506 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007507 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007508 (s->be->options & PR_O_PERSIST) ||
7509 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007510 /* we found the server and we can use it */
7511 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007512 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007513 s->flags |= SN_DIRECT | SN_ASSIGNED;
7514 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007515 break;
7516 } else {
7517 /* we found a server, but it's down,
7518 * mark it as such and go on in case
7519 * another one is available.
7520 */
7521 txn->flags &= ~TX_CK_MASK;
7522 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007523 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007524 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007525 srv = srv->next;
7526 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007527
Willy Tarreauf64d1412010-10-07 20:06:11 +02007528 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007529 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007530 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007531 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007532 txn->flags |= TX_CK_UNUSED;
7533 else
7534 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007535 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007536
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007537 /* depending on the cookie mode, we may have to either :
7538 * - delete the complete cookie if we're in insert+indirect mode, so that
7539 * the server never sees it ;
7540 * - remove the server id from the cookie value, and tag the cookie as an
7541 * application cookie so that it does not get accidentely removed later,
7542 * if we're in cookie prefix mode
7543 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007544 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007545 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007546
Willy Tarreau9b28e032012-10-12 23:49:43 +02007547 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007548 val_end += delta;
7549 next += delta;
7550 hdr_end += delta;
7551 hdr_next += delta;
7552 cur_hdr->len += delta;
7553 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007554
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007555 del_from = NULL;
7556 preserve_hdr = 1; /* we want to keep this cookie */
7557 }
7558 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007559 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007560 del_from = prev;
7561 }
7562 } else {
7563 /* This is not our cookie, so we must preserve it. But if we already
7564 * scheduled another cookie for removal, we cannot remove the
7565 * complete header, but we can remove the previous block itself.
7566 */
7567 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007568
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007569 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007570 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007571 if (att_beg >= del_from)
7572 att_beg += delta;
7573 if (att_end >= del_from)
7574 att_end += delta;
7575 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007576 val_end += delta;
7577 next += delta;
7578 hdr_end += delta;
7579 hdr_next += delta;
7580 cur_hdr->len += delta;
7581 http_msg_move_end(&txn->req, delta);
7582 prev = del_from;
7583 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007584 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007585 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007586
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007587 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007588 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007589 int cmp_len, value_len;
7590 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007591
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007592 if (s->be->options2 & PR_O2_AS_PFX) {
7593 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7594 value_begin = att_beg + s->be->appsession_name_len;
7595 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007596 } else {
7597 cmp_len = att_end - att_beg;
7598 value_begin = val_beg;
7599 value_len = val_end - val_beg;
7600 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007601
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007602 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007603 if (cmp_len == s->be->appsession_name_len &&
7604 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7605 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007606 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007607 }
7608
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007609 /* continue with next cookie on this header line */
7610 att_beg = next;
7611 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007612
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007613 /* There are no more cookies on this line.
7614 * We may still have one (or several) marked for deletion at the
7615 * end of the line. We must do this now in two ways :
7616 * - if some cookies must be preserved, we only delete from the
7617 * mark to the end of line ;
7618 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007619 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007620 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007621 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007622 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007623 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007624 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007625 cur_hdr->len += delta;
7626 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007627 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007628
7629 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007630 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7631 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007632 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007633 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007634 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007635 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007636 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007637 }
7638
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007639 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007640 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007641 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007642}
7643
7644
Willy Tarreaua15645d2007-03-18 16:22:39 +01007645/* Iterate the same filter through all response headers contained in <rtr>.
7646 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7647 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007648int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007649{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007650 char *cur_ptr, *cur_end, *cur_next;
7651 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007652 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007653 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007654 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007655
7656 last_hdr = 0;
7657
Willy Tarreau9b28e032012-10-12 23:49:43 +02007658 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007659 old_idx = 0;
7660
7661 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007662 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007663 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007664 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007665 (exp->action == ACT_ALLOW ||
7666 exp->action == ACT_DENY))
7667 return 0;
7668
7669 cur_idx = txn->hdr_idx.v[old_idx].next;
7670 if (!cur_idx)
7671 break;
7672
7673 cur_hdr = &txn->hdr_idx.v[cur_idx];
7674 cur_ptr = cur_next;
7675 cur_end = cur_ptr + cur_hdr->len;
7676 cur_next = cur_end + cur_hdr->cr + 1;
7677
7678 /* Now we have one header between cur_ptr and cur_end,
7679 * and the next header starts at cur_next.
7680 */
7681
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007682 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007683 switch (exp->action) {
7684 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007685 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007686 last_hdr = 1;
7687 break;
7688
7689 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007690 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007691 last_hdr = 1;
7692 break;
7693
7694 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007695 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7696 if (trash.len < 0)
7697 return -1;
7698
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007699 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007700 /* FIXME: if the user adds a newline in the replacement, the
7701 * index will not be recalculated for now, and the new line
7702 * will not be counted as a new header.
7703 */
7704
7705 cur_end += delta;
7706 cur_next += delta;
7707 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007708 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007709 break;
7710
7711 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007712 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007713 cur_next += delta;
7714
Willy Tarreaufa355d42009-11-29 18:12:29 +01007715 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007716 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7717 txn->hdr_idx.used--;
7718 cur_hdr->len = 0;
7719 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007720 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007721 break;
7722
7723 }
7724 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007725
7726 /* keep the link from this header to next one in case of later
7727 * removal of next header.
7728 */
7729 old_idx = cur_idx;
7730 }
7731 return 0;
7732}
7733
7734
7735/* Apply the filter to the status line in the response buffer <rtr>.
7736 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7737 * or -1 if a replacement resulted in an invalid status line.
7738 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007739int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007740{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007741 char *cur_ptr, *cur_end;
7742 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007743 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007744 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007745
7746
Willy Tarreau3d300592007-03-18 18:34:41 +01007747 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007748 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007749 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007750 (exp->action == ACT_ALLOW ||
7751 exp->action == ACT_DENY))
7752 return 0;
7753 else if (exp->action == ACT_REMOVE)
7754 return 0;
7755
7756 done = 0;
7757
Willy Tarreau9b28e032012-10-12 23:49:43 +02007758 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007759 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007760
7761 /* Now we have the status line between cur_ptr and cur_end */
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 done = 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 done = 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
Willy Tarreaufa355d42009-11-29 18:12:29 +01007786 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007787 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007788 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007789 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007790 cur_ptr, cur_end + 1,
7791 NULL, NULL);
7792 if (unlikely(!cur_end))
7793 return -1;
7794
7795 /* we have a full respnse and we know that we have either a CR
7796 * or an LF at <ptr>.
7797 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007798 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007799 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007800 /* there is no point trying this regex on headers */
7801 return 1;
7802 }
7803 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007804 return done;
7805}
7806
7807
7808
7809/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007810 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007811 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7812 * unparsable response.
7813 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007814int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007815{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007816 struct http_txn *txn = &s->txn;
7817 struct hdr_exp *exp;
7818
7819 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007820 int ret;
7821
7822 /*
7823 * The interleaving of transformations and verdicts
7824 * makes it difficult to decide to continue or stop
7825 * the evaluation.
7826 */
7827
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007828 if (txn->flags & TX_SVDENY)
7829 break;
7830
Willy Tarreau3d300592007-03-18 18:34:41 +01007831 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007832 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7833 exp->action == ACT_PASS)) {
7834 exp = exp->next;
7835 continue;
7836 }
7837
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007838 /* if this filter had a condition, evaluate it now and skip to
7839 * next filter if the condition does not match.
7840 */
7841 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007842 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007843 ret = acl_pass(ret);
7844 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7845 ret = !ret;
7846 if (!ret)
7847 continue;
7848 }
7849
Willy Tarreaua15645d2007-03-18 16:22:39 +01007850 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007851 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007852 if (unlikely(ret < 0))
7853 return -1;
7854
7855 if (likely(ret == 0)) {
7856 /* The filter did not match the response, it can be
7857 * iterated through all headers.
7858 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007859 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7860 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007861 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007862 }
7863 return 0;
7864}
7865
7866
Willy Tarreaua15645d2007-03-18 16:22:39 +01007867/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007868 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007869 * desirable to call it only when needed. This function is also used when we
7870 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007871 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007872void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007873{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007874 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007875 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007876 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007877 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007878 char *hdr_beg, *hdr_end, *hdr_next;
7879 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007880
Willy Tarreaua15645d2007-03-18 16:22:39 +01007881 /* Iterate through the headers.
7882 * we start with the start line.
7883 */
7884 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007885 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007886
7887 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7888 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007889 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007890
7891 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007892 hdr_beg = hdr_next;
7893 hdr_end = hdr_beg + cur_hdr->len;
7894 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007895
Willy Tarreau24581ba2010-08-31 22:39:35 +02007896 /* We have one full header between hdr_beg and hdr_end, and the
7897 * next header starts at hdr_next. We're only interested in
7898 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007899 */
7900
Willy Tarreau24581ba2010-08-31 22:39:35 +02007901 is_cookie2 = 0;
7902 prev = hdr_beg + 10;
7903 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007904 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007905 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7906 if (!val) {
7907 old_idx = cur_idx;
7908 continue;
7909 }
7910 is_cookie2 = 1;
7911 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007912 }
7913
Willy Tarreau24581ba2010-08-31 22:39:35 +02007914 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7915 * <prev> points to the colon.
7916 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007917 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007918
Willy Tarreau24581ba2010-08-31 22:39:35 +02007919 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7920 * check-cache is enabled) and we are not interested in checking
7921 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007922 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007923 if (s->be->cookie_name == NULL &&
7924 s->be->appsession_name == NULL &&
7925 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007926 return;
7927
Willy Tarreau24581ba2010-08-31 22:39:35 +02007928 /* OK so now we know we have to process this response cookie.
7929 * The format of the Set-Cookie header is slightly different
7930 * from the format of the Cookie header in that it does not
7931 * support the comma as a cookie delimiter (thus the header
7932 * cannot be folded) because the Expires attribute described in
7933 * the original Netscape's spec may contain an unquoted date
7934 * with a comma inside. We have to live with this because
7935 * many browsers don't support Max-Age and some browsers don't
7936 * support quoted strings. However the Set-Cookie2 header is
7937 * clean.
7938 *
7939 * We have to keep multiple pointers in order to support cookie
7940 * removal at the beginning, middle or end of header without
7941 * corrupting the header (in case of set-cookie2). A special
7942 * pointer, <scav> points to the beginning of the set-cookie-av
7943 * fields after the first semi-colon. The <next> pointer points
7944 * either to the end of line (set-cookie) or next unquoted comma
7945 * (set-cookie2). All of these headers are valid :
7946 *
7947 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7948 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7949 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7950 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7951 * | | | | | | | | | |
7952 * | | | | | | | | +-> next hdr_end <--+
7953 * | | | | | | | +------------> scav
7954 * | | | | | | +--------------> val_end
7955 * | | | | | +--------------------> val_beg
7956 * | | | | +----------------------> equal
7957 * | | | +------------------------> att_end
7958 * | | +----------------------------> att_beg
7959 * | +------------------------------> prev
7960 * +-----------------------------------------> hdr_beg
7961 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007962
Willy Tarreau24581ba2010-08-31 22:39:35 +02007963 for (; prev < hdr_end; prev = next) {
7964 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007965
Willy Tarreau24581ba2010-08-31 22:39:35 +02007966 /* find att_beg */
7967 att_beg = prev + 1;
7968 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7969 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007970
Willy Tarreau24581ba2010-08-31 22:39:35 +02007971 /* find att_end : this is the first character after the last non
7972 * space before the equal. It may be equal to hdr_end.
7973 */
7974 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007975
Willy Tarreau24581ba2010-08-31 22:39:35 +02007976 while (equal < hdr_end) {
7977 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7978 break;
7979 if (http_is_spht[(unsigned char)*equal++])
7980 continue;
7981 att_end = equal;
7982 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007983
Willy Tarreau24581ba2010-08-31 22:39:35 +02007984 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7985 * is between <att_beg> and <equal>, both may be identical.
7986 */
7987
7988 /* look for end of cookie if there is an equal sign */
7989 if (equal < hdr_end && *equal == '=') {
7990 /* look for the beginning of the value */
7991 val_beg = equal + 1;
7992 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7993 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007994
Willy Tarreau24581ba2010-08-31 22:39:35 +02007995 /* find the end of the value, respecting quotes */
7996 next = find_cookie_value_end(val_beg, hdr_end);
7997
7998 /* make val_end point to the first white space or delimitor after the value */
7999 val_end = next;
8000 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8001 val_end--;
8002 } else {
8003 /* <equal> points to next comma, semi-colon or EOL */
8004 val_beg = val_end = next = equal;
8005 }
8006
8007 if (next < hdr_end) {
8008 /* Set-Cookie2 supports multiple cookies, and <next> points to
8009 * a colon or semi-colon before the end. So skip all attr-value
8010 * pairs and look for the next comma. For Set-Cookie, since
8011 * commas are permitted in values, skip to the end.
8012 */
8013 if (is_cookie2)
8014 next = find_hdr_value_end(next, hdr_end);
8015 else
8016 next = hdr_end;
8017 }
8018
8019 /* Now everything is as on the diagram above */
8020
8021 /* Ignore cookies with no equal sign */
8022 if (equal == val_end)
8023 continue;
8024
8025 /* If there are spaces around the equal sign, we need to
8026 * strip them otherwise we'll get trouble for cookie captures,
8027 * or even for rewrites. Since this happens extremely rarely,
8028 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008029 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008030 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8031 int stripped_before = 0;
8032 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008033
Willy Tarreau24581ba2010-08-31 22:39:35 +02008034 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008035 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008036 equal += stripped_before;
8037 val_beg += stripped_before;
8038 }
8039
8040 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008041 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008042 val_beg += stripped_after;
8043 stripped_before += stripped_after;
8044 }
8045
8046 val_end += stripped_before;
8047 next += stripped_before;
8048 hdr_end += stripped_before;
8049 hdr_next += stripped_before;
8050 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008051 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008052 }
8053
8054 /* First, let's see if we want to capture this cookie. We check
8055 * that we don't already have a server side cookie, because we
8056 * can only capture one. Also as an optimisation, we ignore
8057 * cookies shorter than the declared name.
8058 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008059 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008060 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008061 (val_end - att_beg >= s->fe->capture_namelen) &&
8062 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008063 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008064 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008065 Alert("HTTP logging : out of memory.\n");
8066 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008067 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008068 if (log_len > s->fe->capture_len)
8069 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008070 memcpy(txn->srv_cookie, att_beg, log_len);
8071 txn->srv_cookie[log_len] = 0;
8072 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008073 }
8074
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008075 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008076 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008077 if (!(s->flags & SN_IGNORE_PRST) &&
8078 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8079 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008080 /* assume passive cookie by default */
8081 txn->flags &= ~TX_SCK_MASK;
8082 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008083
8084 /* If the cookie is in insert mode on a known server, we'll delete
8085 * this occurrence because we'll insert another one later.
8086 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008087 * a direct access.
8088 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008089 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008090 /* The "preserve" flag was set, we don't want to touch the
8091 * server's cookie.
8092 */
8093 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008094 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8095 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008096 /* this cookie must be deleted */
8097 if (*prev == ':' && next == hdr_end) {
8098 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008099 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008100 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8101 txn->hdr_idx.used--;
8102 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008103 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008104 hdr_next += delta;
8105 http_msg_move_end(&txn->rsp, delta);
8106 /* note: while both invalid now, <next> and <hdr_end>
8107 * are still equal, so the for() will stop as expected.
8108 */
8109 } else {
8110 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008111 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008112 next = prev;
8113 hdr_end += delta;
8114 hdr_next += delta;
8115 cur_hdr->len += delta;
8116 http_msg_move_end(&txn->rsp, delta);
8117 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008118 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008119 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008120 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008121 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008122 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008123 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008124 * with this server since we know it.
8125 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008126 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008127 next += delta;
8128 hdr_end += delta;
8129 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008130 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008131 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008132
Willy Tarreauf1348312010-10-07 15:54:11 +02008133 txn->flags &= ~TX_SCK_MASK;
8134 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008135 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008136 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008137 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008138 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008139 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008140 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008141 next += delta;
8142 hdr_end += delta;
8143 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008144 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008145 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008146
Willy Tarreau827aee92011-03-10 16:55:02 +01008147 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008148 txn->flags &= ~TX_SCK_MASK;
8149 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008150 }
8151 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008152 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008153 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008154 int cmp_len, value_len;
8155 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008156
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008157 if (s->be->options2 & PR_O2_AS_PFX) {
8158 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8159 value_begin = att_beg + s->be->appsession_name_len;
8160 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008161 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008162 cmp_len = att_end - att_beg;
8163 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008164 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008165 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008166
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008167 if ((cmp_len == s->be->appsession_name_len) &&
8168 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008169 /* free a possibly previously allocated memory */
8170 pool_free2(apools.sessid, txn->sessid);
8171
Cyril Bontéb21570a2009-11-29 20:04:48 +01008172 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008173 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008174 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008175 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008176 return;
8177 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008178 memcpy(txn->sessid, value_begin, value_len);
8179 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008180 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008181 }
8182 /* that's done for this cookie, check the next one on the same
8183 * line when next != hdr_end (only if is_cookie2).
8184 */
8185 }
8186 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008187 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008188 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008189
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008190 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008191 appsess *asession = NULL;
8192 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008193 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008194 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008195 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008196 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8197 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008198 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008199 return;
8200 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008201 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8202
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008203 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8204 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008205 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8206 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008207 return;
8208 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008209 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8210 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008211
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008212 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008213 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008214 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008215 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8216 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008217 return;
8218 }
8219 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008220 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008221
8222 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008223 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008224 }
8225
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008226 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008227 asession->request_count++;
8228 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008229}
8230
8231
Willy Tarreaua15645d2007-03-18 16:22:39 +01008232/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008233 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008234 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008235void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008236{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008237 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008238 char *p1, *p2;
8239
8240 char *cur_ptr, *cur_end, *cur_next;
8241 int cur_idx;
8242
Willy Tarreau5df51872007-11-25 16:20:08 +01008243 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008244 return;
8245
8246 /* Iterate through the headers.
8247 * we start with the start line.
8248 */
8249 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008250 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008251
8252 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8253 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008254 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008255
8256 cur_hdr = &txn->hdr_idx.v[cur_idx];
8257 cur_ptr = cur_next;
8258 cur_end = cur_ptr + cur_hdr->len;
8259 cur_next = cur_end + cur_hdr->cr + 1;
8260
8261 /* We have one full header between cur_ptr and cur_end, and the
8262 * next header starts at cur_next. We're only interested in
8263 * "Cookie:" headers.
8264 */
8265
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008266 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8267 if (val) {
8268 if ((cur_end - (cur_ptr + val) >= 8) &&
8269 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8270 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8271 return;
8272 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008273 }
8274
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008275 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8276 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008277 continue;
8278
8279 /* OK, right now we know we have a cache-control header at cur_ptr */
8280
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008281 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008282
8283 if (p1 >= cur_end) /* no more info */
8284 continue;
8285
8286 /* p1 is at the beginning of the value */
8287 p2 = p1;
8288
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008289 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008290 p2++;
8291
8292 /* we have a complete value between p1 and p2 */
8293 if (p2 < cur_end && *p2 == '=') {
8294 /* we have something of the form no-cache="set-cookie" */
8295 if ((cur_end - p1 >= 21) &&
8296 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8297 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008298 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008299 continue;
8300 }
8301
8302 /* OK, so we know that either p2 points to the end of string or to a comma */
8303 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008304 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008305 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8306 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8307 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008308 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008309 return;
8310 }
8311
8312 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008313 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008314 continue;
8315 }
8316 }
8317}
8318
8319
Willy Tarreau58f10d72006-12-04 02:26:12 +01008320/*
8321 * Try to retrieve a known appsession in the URI, then the associated server.
8322 * If the server is found, it's assigned to the session.
8323 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008324void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008325{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008326 char *end_params, *first_param, *cur_param, *next_param;
8327 char separator;
8328 int value_len;
8329
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008330 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008331
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008332 if (s->be->appsession_name == NULL ||
8333 (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 +01008334 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008335 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008336
Cyril Bontéb21570a2009-11-29 20:04:48 +01008337 first_param = NULL;
8338 switch (mode) {
8339 case PR_O2_AS_M_PP:
8340 first_param = memchr(begin, ';', len);
8341 break;
8342 case PR_O2_AS_M_QS:
8343 first_param = memchr(begin, '?', len);
8344 break;
8345 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008346
Cyril Bontéb21570a2009-11-29 20:04:48 +01008347 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008348 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008349 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008350
Cyril Bontéb21570a2009-11-29 20:04:48 +01008351 switch (mode) {
8352 case PR_O2_AS_M_PP:
8353 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8354 end_params = (char *) begin + len;
8355 }
8356 separator = ';';
8357 break;
8358 case PR_O2_AS_M_QS:
8359 end_params = (char *) begin + len;
8360 separator = '&';
8361 break;
8362 default:
8363 /* unknown mode, shouldn't happen */
8364 return;
8365 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008366
Cyril Bontéb21570a2009-11-29 20:04:48 +01008367 cur_param = next_param = end_params;
8368 while (cur_param > first_param) {
8369 cur_param--;
8370 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8371 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008372 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8373 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8374 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008375 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008376 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8377 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008378 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008379 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008380 }
8381 break;
8382 }
8383 next_param = cur_param;
8384 }
8385 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008386#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008387 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008388 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008389#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008390}
8391
Willy Tarreaub2513902006-12-17 14:52:38 +01008392/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008393 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008394 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008395 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008396 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008397 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008398 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008399 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008400 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008401int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008402{
8403 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008404 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008405 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008406
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008407 if (!uri_auth)
8408 return 0;
8409
Cyril Bonté70be45d2010-10-12 00:14:35 +02008410 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008411 return 0;
8412
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008413 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008414 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008415 return 0;
8416
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008417 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008418 return 0;
8419
Willy Tarreaub2513902006-12-17 14:52:38 +01008420 return 1;
8421}
8422
Willy Tarreau4076a152009-04-02 15:18:36 +02008423/*
8424 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008425 * By default it tries to report the error position as msg->err_pos. However if
8426 * this one is not set, it will then report msg->next, which is the last known
8427 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008428 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008429 */
8430void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008431 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008432 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008433{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008434 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008435 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008436
Willy Tarreau9b28e032012-10-12 23:49:43 +02008437 es->len = MIN(chn->buf->i, sizeof(es->buf));
8438 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008439 len1 = MIN(len1, es->len);
8440 len2 = es->len - len1; /* remaining data if buffer wraps */
8441
Willy Tarreau9b28e032012-10-12 23:49:43 +02008442 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008443 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008444 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008445
Willy Tarreau4076a152009-04-02 15:18:36 +02008446 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008447 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008448 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008449 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008450
Willy Tarreau4076a152009-04-02 15:18:36 +02008451 es->when = date; // user-visible date
8452 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008453 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008454 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008455 if (objt_conn(s->req->prod->end))
8456 es->src = __objt_conn(s->req->prod->end)->addr.from;
8457 else
8458 memset(&es->src, 0, sizeof(es->src));
8459
Willy Tarreau078272e2010-12-12 12:46:33 +01008460 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008461 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008462 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008463 es->s_flags = s->flags;
8464 es->t_flags = s->txn.flags;
8465 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008466 es->b_out = chn->buf->o;
8467 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008468 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008469 es->m_clen = msg->chunk_len;
8470 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008471}
Willy Tarreaub2513902006-12-17 14:52:38 +01008472
Willy Tarreau294c4732011-12-16 21:35:50 +01008473/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8474 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8475 * performed over the whole headers. Otherwise it must contain a valid header
8476 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8477 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8478 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8479 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008480 * -1. The value fetch stops at commas, so this function is suited for use with
8481 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008482 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008483 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008484unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008485 struct hdr_idx *idx, int occ,
8486 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008487{
Willy Tarreau294c4732011-12-16 21:35:50 +01008488 struct hdr_ctx local_ctx;
8489 char *ptr_hist[MAX_HDR_HISTORY];
8490 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008491 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008492 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008493
Willy Tarreau294c4732011-12-16 21:35:50 +01008494 if (!ctx) {
8495 local_ctx.idx = 0;
8496 ctx = &local_ctx;
8497 }
8498
Willy Tarreaubce70882009-09-07 11:51:47 +02008499 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008500 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008501 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008502 occ--;
8503 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008504 *vptr = ctx->line + ctx->val;
8505 *vlen = ctx->vlen;
8506 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008507 }
8508 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008509 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008510 }
8511
8512 /* negative occurrence, we scan all the list then walk back */
8513 if (-occ > MAX_HDR_HISTORY)
8514 return 0;
8515
Willy Tarreau294c4732011-12-16 21:35:50 +01008516 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008517 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008518 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8519 len_hist[hist_ptr] = ctx->vlen;
8520 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008521 hist_ptr = 0;
8522 found++;
8523 }
8524 if (-occ > found)
8525 return 0;
8526 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008527 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8528 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8529 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008530 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008531 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008532 if (hist_ptr >= MAX_HDR_HISTORY)
8533 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008534 *vptr = ptr_hist[hist_ptr];
8535 *vlen = len_hist[hist_ptr];
8536 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008537}
8538
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008539/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8540 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8541 * performed over the whole headers. Otherwise it must contain a valid header
8542 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8543 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8544 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8545 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8546 * -1. This function differs from http_get_hdr() in that it only returns full
8547 * line header values and does not stop at commas.
8548 * The return value is 0 if nothing was found, or non-zero otherwise.
8549 */
8550unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8551 struct hdr_idx *idx, int occ,
8552 struct hdr_ctx *ctx, char **vptr, int *vlen)
8553{
8554 struct hdr_ctx local_ctx;
8555 char *ptr_hist[MAX_HDR_HISTORY];
8556 int len_hist[MAX_HDR_HISTORY];
8557 unsigned int hist_ptr;
8558 int found;
8559
8560 if (!ctx) {
8561 local_ctx.idx = 0;
8562 ctx = &local_ctx;
8563 }
8564
8565 if (occ >= 0) {
8566 /* search from the beginning */
8567 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8568 occ--;
8569 if (occ <= 0) {
8570 *vptr = ctx->line + ctx->val;
8571 *vlen = ctx->vlen;
8572 return 1;
8573 }
8574 }
8575 return 0;
8576 }
8577
8578 /* negative occurrence, we scan all the list then walk back */
8579 if (-occ > MAX_HDR_HISTORY)
8580 return 0;
8581
8582 found = hist_ptr = 0;
8583 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8584 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8585 len_hist[hist_ptr] = ctx->vlen;
8586 if (++hist_ptr >= MAX_HDR_HISTORY)
8587 hist_ptr = 0;
8588 found++;
8589 }
8590 if (-occ > found)
8591 return 0;
8592 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8593 * find occurrence -occ, so we have to check [hist_ptr+occ].
8594 */
8595 hist_ptr += occ;
8596 if (hist_ptr >= MAX_HDR_HISTORY)
8597 hist_ptr -= MAX_HDR_HISTORY;
8598 *vptr = ptr_hist[hist_ptr];
8599 *vlen = len_hist[hist_ptr];
8600 return 1;
8601}
8602
Willy Tarreaubaaee002006-06-26 02:48:02 +02008603/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008604 * Print a debug line with a header. Always stop at the first CR or LF char,
8605 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8606 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008607 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008608void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008609{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008610 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008611 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008612 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008613 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8614 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 +02008615
8616 for (max = 0; start + max < end; max++)
8617 if (start[max] == '\r' || start[max] == '\n')
8618 break;
8619
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008620 UBOUND(max, trash.size - trash.len - 3);
8621 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8622 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008623 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008624}
8625
Willy Tarreau0937bc42009-12-22 15:03:09 +01008626/*
8627 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8628 * the required fields are properly allocated and that we only need to (re)init
8629 * them. This should be used before processing any new request.
8630 */
8631void http_init_txn(struct session *s)
8632{
8633 struct http_txn *txn = &s->txn;
8634 struct proxy *fe = s->fe;
8635
8636 txn->flags = 0;
8637 txn->status = -1;
8638
Willy Tarreauf64d1412010-10-07 20:06:11 +02008639 txn->cookie_first_date = 0;
8640 txn->cookie_last_date = 0;
8641
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008642 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008643 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008644 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008645 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008646 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008647 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008648 txn->req.chunk_len = 0LL;
8649 txn->req.body_len = 0LL;
8650 txn->rsp.chunk_len = 0LL;
8651 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008652 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8653 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008654 txn->req.chn = s->req;
8655 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008656
8657 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008658
8659 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8660 if (fe->options2 & PR_O2_REQBUG_OK)
8661 txn->req.err_pos = -1; /* let buggy requests pass */
8662
Willy Tarreau46023632010-01-07 22:51:47 +01008663 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008664 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8665
Willy Tarreau46023632010-01-07 22:51:47 +01008666 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008667 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8668
8669 if (txn->hdr_idx.v)
8670 hdr_idx_init(&txn->hdr_idx);
8671}
8672
8673/* to be used at the end of a transaction */
8674void http_end_txn(struct session *s)
8675{
8676 struct http_txn *txn = &s->txn;
8677
Willy Tarreau75195602014-03-11 15:48:55 +01008678 /* release any possible compression context */
8679 if (s->flags & SN_COMP_READY)
8680 s->comp_algo->end(&s->comp_ctx);
8681 s->comp_algo = NULL;
8682 s->flags &= ~SN_COMP_READY;
8683
Willy Tarreau0937bc42009-12-22 15:03:09 +01008684 /* these ones will have been dynamically allocated */
8685 pool_free2(pool2_requri, txn->uri);
8686 pool_free2(pool2_capture, txn->cli_cookie);
8687 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008688 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008689 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008690
William Lallemanda73203e2012-03-12 12:48:57 +01008691 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008692 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008693 txn->uri = NULL;
8694 txn->srv_cookie = NULL;
8695 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008696
8697 if (txn->req.cap) {
8698 struct cap_hdr *h;
8699 for (h = s->fe->req_cap; h; h = h->next)
8700 pool_free2(h->pool, txn->req.cap[h->index]);
8701 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8702 }
8703
8704 if (txn->rsp.cap) {
8705 struct cap_hdr *h;
8706 for (h = s->fe->rsp_cap; h; h = h->next)
8707 pool_free2(h->pool, txn->rsp.cap[h->index]);
8708 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8709 }
8710
Willy Tarreau0937bc42009-12-22 15:03:09 +01008711}
8712
8713/* to be used at the end of a transaction to prepare a new one */
8714void http_reset_txn(struct session *s)
8715{
8716 http_end_txn(s);
8717 http_init_txn(s);
8718
8719 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008720 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008721 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008722 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008723 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008724 /* re-init store persistence */
8725 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008726 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008727
Willy Tarreau0937bc42009-12-22 15:03:09 +01008728 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008729
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008730 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008731
Willy Tarreau739cfba2010-01-25 23:11:14 +01008732 /* We must trim any excess data from the response buffer, because we
8733 * may have blocked an invalid response from a server that we don't
8734 * want to accidentely forward once we disable the analysers, nor do
8735 * we want those data to come along with next response. A typical
8736 * example of such data would be from a buggy server responding to
8737 * a HEAD with some data, or sending more than the advertised
8738 * content-length.
8739 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008740 if (unlikely(s->rep->buf->i))
8741 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008742
Willy Tarreau0937bc42009-12-22 15:03:09 +01008743 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008744 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008745
Willy Tarreaud04e8582010-05-31 12:31:35 +02008746 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008747 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008748
8749 s->req->rex = TICK_ETERNITY;
8750 s->req->wex = TICK_ETERNITY;
8751 s->req->analyse_exp = TICK_ETERNITY;
8752 s->rep->rex = TICK_ETERNITY;
8753 s->rep->wex = TICK_ETERNITY;
8754 s->rep->analyse_exp = TICK_ETERNITY;
8755}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008756
Sasha Pachev218f0642014-06-16 12:05:59 -06008757void free_http_res_rules(struct list *r)
8758{
8759 struct http_res_rule *tr, *pr;
8760
8761 list_for_each_entry_safe(pr, tr, r, list) {
8762 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008763 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008764 free(pr);
8765 }
8766}
8767
8768void free_http_req_rules(struct list *r)
8769{
Willy Tarreauff011f22011-01-06 17:51:27 +01008770 struct http_req_rule *tr, *pr;
8771
8772 list_for_each_entry_safe(pr, tr, r, list) {
8773 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008774 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008775 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008776
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008777 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008778 free(pr);
8779 }
8780}
8781
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008782/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008783struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8784{
8785 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008786 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008787 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008788 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008789
8790 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8791 if (!rule) {
8792 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008793 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008794 }
8795
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008796 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008797 rule->action = HTTP_REQ_ACT_ALLOW;
8798 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008799 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008800 rule->action = HTTP_REQ_ACT_DENY;
8801 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008802 } else if (!strcmp(args[0], "tarpit")) {
8803 rule->action = HTTP_REQ_ACT_TARPIT;
8804 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008805 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008806 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008807 cur_arg = 1;
8808
8809 while(*args[cur_arg]) {
8810 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008811 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008812 cur_arg+=2;
8813 continue;
8814 } else
8815 break;
8816 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008817 } else if (!strcmp(args[0], "set-nice")) {
8818 rule->action = HTTP_REQ_ACT_SET_NICE;
8819 cur_arg = 1;
8820
8821 if (!*args[cur_arg] ||
8822 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8823 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8824 file, linenum, args[0]);
8825 goto out_err;
8826 }
8827 rule->arg.nice = atoi(args[cur_arg]);
8828 if (rule->arg.nice < -1024)
8829 rule->arg.nice = -1024;
8830 else if (rule->arg.nice > 1024)
8831 rule->arg.nice = 1024;
8832 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008833 } else if (!strcmp(args[0], "set-tos")) {
8834#ifdef IP_TOS
8835 char *err;
8836 rule->action = HTTP_REQ_ACT_SET_TOS;
8837 cur_arg = 1;
8838
8839 if (!*args[cur_arg] ||
8840 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8841 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8842 file, linenum, args[0]);
8843 goto out_err;
8844 }
8845
8846 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8847 if (err && *err != '\0') {
8848 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8849 file, linenum, err, args[0]);
8850 goto out_err;
8851 }
8852 cur_arg++;
8853#else
8854 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8855 goto out_err;
8856#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008857 } else if (!strcmp(args[0], "set-mark")) {
8858#ifdef SO_MARK
8859 char *err;
8860 rule->action = HTTP_REQ_ACT_SET_MARK;
8861 cur_arg = 1;
8862
8863 if (!*args[cur_arg] ||
8864 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8865 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8866 file, linenum, args[0]);
8867 goto out_err;
8868 }
8869
8870 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8871 if (err && *err != '\0') {
8872 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8873 file, linenum, err, args[0]);
8874 goto out_err;
8875 }
8876 cur_arg++;
8877 global.last_checks |= LSTCHK_NETADM;
8878#else
8879 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8880 goto out_err;
8881#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008882 } else if (!strcmp(args[0], "set-log-level")) {
8883 rule->action = HTTP_REQ_ACT_SET_LOGL;
8884 cur_arg = 1;
8885
8886 if (!*args[cur_arg] ||
8887 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8888 bad_log_level:
8889 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8890 file, linenum, args[0]);
8891 goto out_err;
8892 }
8893 if (strcmp(args[cur_arg], "silent") == 0)
8894 rule->arg.loglevel = -1;
8895 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8896 goto bad_log_level;
8897 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008898 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8899 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8900 cur_arg = 1;
8901
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008902 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8903 (*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 +01008904 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8905 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008906 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008907 }
8908
8909 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8910 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8911 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008912
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008913 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008914 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008915 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8916 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008917 free(proxy->conf.lfs_file);
8918 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8919 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008920 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008921 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
8922 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008923 cur_arg = 1;
8924
8925 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008926 (*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 -06008927 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8928 file, linenum, args[0]);
8929 goto out_err;
8930 }
8931
8932 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8933 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8934 LIST_INIT(&rule->arg.hdr_add.fmt);
8935
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008936 error = NULL;
8937 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8938 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8939 args[cur_arg + 1], error);
8940 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008941 goto out_err;
8942 }
8943
8944 proxy->conf.args.ctx = ARGC_HRQ;
8945 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
8946 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8947 file, linenum);
8948
8949 free(proxy->conf.lfs_file);
8950 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8951 proxy->conf.lfs_line = proxy->conf.args.line;
8952 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008953 } else if (strcmp(args[0], "del-header") == 0) {
8954 rule->action = HTTP_REQ_ACT_DEL_HDR;
8955 cur_arg = 1;
8956
8957 if (!*args[cur_arg] ||
8958 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8959 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8960 file, linenum, args[0]);
8961 goto out_err;
8962 }
8963
8964 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8965 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8966
8967 proxy->conf.args.ctx = ARGC_HRQ;
8968 free(proxy->conf.lfs_file);
8969 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8970 proxy->conf.lfs_line = proxy->conf.args.line;
8971 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008972 } else if (strcmp(args[0], "redirect") == 0) {
8973 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008974 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008975
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008976 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008977 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8978 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8979 goto out_err;
8980 }
8981
8982 /* this redirect rule might already contain a parsed condition which
8983 * we'll pass to the http-request rule.
8984 */
8985 rule->action = HTTP_REQ_ACT_REDIR;
8986 rule->arg.redir = redir;
8987 rule->cond = redir->cond;
8988 redir->cond = NULL;
8989 cur_arg = 2;
8990 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008991 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8992 /* http-request add-acl(<reference (acl name)>) <key pattern> */
8993 rule->action = HTTP_REQ_ACT_ADD_ACL;
8994 /*
8995 * '+ 8' for 'add-acl('
8996 * '- 9' for 'add-acl(' + trailing ')'
8997 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008998 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008999
9000 cur_arg = 1;
9001
9002 if (!*args[cur_arg] ||
9003 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9004 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9005 file, linenum, args[0]);
9006 goto out_err;
9007 }
9008
9009 LIST_INIT(&rule->arg.map.key);
9010 proxy->conf.args.ctx = ARGC_HRQ;
9011 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9012 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9013 file, linenum);
9014 free(proxy->conf.lfs_file);
9015 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9016 proxy->conf.lfs_line = proxy->conf.args.line;
9017 cur_arg += 1;
9018 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9019 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9020 rule->action = HTTP_REQ_ACT_DEL_ACL;
9021 /*
9022 * '+ 8' for 'del-acl('
9023 * '- 9' for 'del-acl(' + trailing ')'
9024 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009025 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009026
9027 cur_arg = 1;
9028
9029 if (!*args[cur_arg] ||
9030 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9031 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9032 file, linenum, args[0]);
9033 goto out_err;
9034 }
9035
9036 LIST_INIT(&rule->arg.map.key);
9037 proxy->conf.args.ctx = ARGC_HRQ;
9038 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9039 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9040 file, linenum);
9041 free(proxy->conf.lfs_file);
9042 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9043 proxy->conf.lfs_line = proxy->conf.args.line;
9044 cur_arg += 1;
9045 } else if (strncmp(args[0], "del-map", 7) == 0) {
9046 /* http-request del-map(<reference (map name)>) <key pattern> */
9047 rule->action = HTTP_REQ_ACT_DEL_MAP;
9048 /*
9049 * '+ 8' for 'del-map('
9050 * '- 9' for 'del-map(' + trailing ')'
9051 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009052 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009053
9054 cur_arg = 1;
9055
9056 if (!*args[cur_arg] ||
9057 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9058 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9059 file, linenum, args[0]);
9060 goto out_err;
9061 }
9062
9063 LIST_INIT(&rule->arg.map.key);
9064 proxy->conf.args.ctx = ARGC_HRQ;
9065 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9066 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9067 file, linenum);
9068 free(proxy->conf.lfs_file);
9069 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9070 proxy->conf.lfs_line = proxy->conf.args.line;
9071 cur_arg += 1;
9072 } else if (strncmp(args[0], "set-map", 7) == 0) {
9073 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9074 rule->action = HTTP_REQ_ACT_SET_MAP;
9075 /*
9076 * '+ 8' for 'set-map('
9077 * '- 9' for 'set-map(' + 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] || !*args[cur_arg+1] ||
9084 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9085 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9086 file, linenum, args[0]);
9087 goto out_err;
9088 }
9089
9090 LIST_INIT(&rule->arg.map.key);
9091 LIST_INIT(&rule->arg.map.value);
9092 proxy->conf.args.ctx = ARGC_HRQ;
9093
9094 /* key pattern */
9095 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9096 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9097 file, linenum);
9098
9099 /* value pattern */
9100 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9101 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9102 file, linenum);
9103 free(proxy->conf.lfs_file);
9104 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9105 proxy->conf.lfs_line = proxy->conf.args.line;
9106
9107 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009108 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9109 char *errmsg = NULL;
9110 cur_arg = 1;
9111 /* try in the module list */
9112 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9113 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9114 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9115 free(errmsg);
9116 goto out_err;
9117 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009118 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009119 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 +01009120 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009121 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009122 }
9123
9124 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9125 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009126 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009127
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009128 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9129 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9130 file, linenum, args[0], errmsg);
9131 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009132 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009133 }
9134 rule->cond = cond;
9135 }
9136 else if (*args[cur_arg]) {
9137 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9138 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9139 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009140 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009141 }
9142
9143 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009144 out_err:
9145 free(rule);
9146 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009147}
9148
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009149/* parse an "http-respose" rule */
9150struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9151{
9152 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009153 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009154 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009155 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009156
9157 rule = calloc(1, sizeof(*rule));
9158 if (!rule) {
9159 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9160 goto out_err;
9161 }
9162
9163 if (!strcmp(args[0], "allow")) {
9164 rule->action = HTTP_RES_ACT_ALLOW;
9165 cur_arg = 1;
9166 } else if (!strcmp(args[0], "deny")) {
9167 rule->action = HTTP_RES_ACT_DENY;
9168 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009169 } else if (!strcmp(args[0], "set-nice")) {
9170 rule->action = HTTP_RES_ACT_SET_NICE;
9171 cur_arg = 1;
9172
9173 if (!*args[cur_arg] ||
9174 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9175 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9176 file, linenum, args[0]);
9177 goto out_err;
9178 }
9179 rule->arg.nice = atoi(args[cur_arg]);
9180 if (rule->arg.nice < -1024)
9181 rule->arg.nice = -1024;
9182 else if (rule->arg.nice > 1024)
9183 rule->arg.nice = 1024;
9184 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009185 } else if (!strcmp(args[0], "set-tos")) {
9186#ifdef IP_TOS
9187 char *err;
9188 rule->action = HTTP_RES_ACT_SET_TOS;
9189 cur_arg = 1;
9190
9191 if (!*args[cur_arg] ||
9192 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9193 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9194 file, linenum, args[0]);
9195 goto out_err;
9196 }
9197
9198 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9199 if (err && *err != '\0') {
9200 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9201 file, linenum, err, args[0]);
9202 goto out_err;
9203 }
9204 cur_arg++;
9205#else
9206 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9207 goto out_err;
9208#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009209 } else if (!strcmp(args[0], "set-mark")) {
9210#ifdef SO_MARK
9211 char *err;
9212 rule->action = HTTP_RES_ACT_SET_MARK;
9213 cur_arg = 1;
9214
9215 if (!*args[cur_arg] ||
9216 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9217 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9218 file, linenum, args[0]);
9219 goto out_err;
9220 }
9221
9222 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9223 if (err && *err != '\0') {
9224 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9225 file, linenum, err, args[0]);
9226 goto out_err;
9227 }
9228 cur_arg++;
9229 global.last_checks |= LSTCHK_NETADM;
9230#else
9231 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9232 goto out_err;
9233#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009234 } else if (!strcmp(args[0], "set-log-level")) {
9235 rule->action = HTTP_RES_ACT_SET_LOGL;
9236 cur_arg = 1;
9237
9238 if (!*args[cur_arg] ||
9239 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9240 bad_log_level:
9241 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9242 file, linenum, args[0]);
9243 goto out_err;
9244 }
9245 if (strcmp(args[cur_arg], "silent") == 0)
9246 rule->arg.loglevel = -1;
9247 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9248 goto bad_log_level;
9249 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009250 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9251 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9252 cur_arg = 1;
9253
9254 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9255 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9256 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9257 file, linenum, args[0]);
9258 goto out_err;
9259 }
9260
9261 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9262 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9263 LIST_INIT(&rule->arg.hdr_add.fmt);
9264
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009265 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009266 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009267 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9268 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009269 free(proxy->conf.lfs_file);
9270 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9271 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009272 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009273 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009274 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009275 cur_arg = 1;
9276
9277 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
9278 (*args[cur_arg+3] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9279 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9280 file, linenum, args[0]);
9281 goto out_err;
9282 }
9283
9284 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9285 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9286 LIST_INIT(&rule->arg.hdr_add.fmt);
9287
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009288 error = NULL;
9289 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9290 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9291 args[cur_arg + 1], error);
9292 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009293 goto out_err;
9294 }
9295
9296 proxy->conf.args.ctx = ARGC_HRQ;
9297 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9298 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9299 file, linenum);
9300
9301 free(proxy->conf.lfs_file);
9302 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9303 proxy->conf.lfs_line = proxy->conf.args.line;
9304 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009305 } else if (strcmp(args[0], "del-header") == 0) {
9306 rule->action = HTTP_RES_ACT_DEL_HDR;
9307 cur_arg = 1;
9308
9309 if (!*args[cur_arg] ||
9310 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9311 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9312 file, linenum, args[0]);
9313 goto out_err;
9314 }
9315
9316 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9317 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9318
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009319 proxy->conf.args.ctx = ARGC_HRS;
9320 free(proxy->conf.lfs_file);
9321 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9322 proxy->conf.lfs_line = proxy->conf.args.line;
9323 cur_arg += 1;
9324 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9325 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9326 rule->action = HTTP_RES_ACT_ADD_ACL;
9327 /*
9328 * '+ 8' for 'add-acl('
9329 * '- 9' for 'add-acl(' + trailing ')'
9330 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009331 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009332
9333 cur_arg = 1;
9334
9335 if (!*args[cur_arg] ||
9336 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9337 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9338 file, linenum, args[0]);
9339 goto out_err;
9340 }
9341
9342 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009343 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009344 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9345 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9346 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009347 free(proxy->conf.lfs_file);
9348 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9349 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009350
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009351 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009352 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9353 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9354 rule->action = HTTP_RES_ACT_DEL_ACL;
9355 /*
9356 * '+ 8' for 'del-acl('
9357 * '- 9' for 'del-acl(' + trailing ')'
9358 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009359 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009360
9361 cur_arg = 1;
9362
9363 if (!*args[cur_arg] ||
9364 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9365 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9366 file, linenum, args[0]);
9367 goto out_err;
9368 }
9369
9370 LIST_INIT(&rule->arg.map.key);
9371 proxy->conf.args.ctx = ARGC_HRS;
9372 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9373 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9374 file, linenum);
9375 free(proxy->conf.lfs_file);
9376 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9377 proxy->conf.lfs_line = proxy->conf.args.line;
9378 cur_arg += 1;
9379 } else if (strncmp(args[0], "del-map", 7) == 0) {
9380 /* http-response del-map(<reference (map name)>) <key pattern> */
9381 rule->action = HTTP_RES_ACT_DEL_MAP;
9382 /*
9383 * '+ 8' for 'del-map('
9384 * '- 9' for 'del-map(' + trailing ')'
9385 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009386 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009387
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 LIST_INIT(&rule->arg.map.key);
9398 proxy->conf.args.ctx = ARGC_HRS;
9399 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9400 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9401 file, linenum);
9402 free(proxy->conf.lfs_file);
9403 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9404 proxy->conf.lfs_line = proxy->conf.args.line;
9405 cur_arg += 1;
9406 } else if (strncmp(args[0], "set-map", 7) == 0) {
9407 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9408 rule->action = HTTP_RES_ACT_SET_MAP;
9409 /*
9410 * '+ 8' for 'set-map('
9411 * '- 9' for 'set-map(' + trailing ')'
9412 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009413 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009414
9415 cur_arg = 1;
9416
9417 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9418 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9419 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9420 file, linenum, args[0]);
9421 goto out_err;
9422 }
9423
9424 LIST_INIT(&rule->arg.map.key);
9425 LIST_INIT(&rule->arg.map.value);
9426
9427 proxy->conf.args.ctx = ARGC_HRS;
9428
9429 /* key pattern */
9430 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9431 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9432 file, linenum);
9433
9434 /* value pattern */
9435 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9436 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9437 file, linenum);
9438
9439 free(proxy->conf.lfs_file);
9440 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9441 proxy->conf.lfs_line = proxy->conf.args.line;
9442
9443 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009444 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9445 char *errmsg = NULL;
9446 cur_arg = 1;
9447 /* try in the module list */
9448 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9449 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9450 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9451 free(errmsg);
9452 goto out_err;
9453 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009454 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009455 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 +02009456 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9457 goto out_err;
9458 }
9459
9460 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9461 struct acl_cond *cond;
9462 char *errmsg = NULL;
9463
9464 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9465 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9466 file, linenum, args[0], errmsg);
9467 free(errmsg);
9468 goto out_err;
9469 }
9470 rule->cond = cond;
9471 }
9472 else if (*args[cur_arg]) {
9473 Alert("parsing [%s:%d]: 'http-response %s' expects"
9474 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9475 file, linenum, args[0], args[cur_arg]);
9476 goto out_err;
9477 }
9478
9479 return rule;
9480 out_err:
9481 free(rule);
9482 return NULL;
9483}
9484
Willy Tarreau4baae242012-12-27 12:00:31 +01009485/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009486 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9487 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009488 */
9489struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009490 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009491{
9492 struct redirect_rule *rule;
9493 int cur_arg;
9494 int type = REDIRECT_TYPE_NONE;
9495 int code = 302;
9496 const char *destination = NULL;
9497 const char *cookie = NULL;
9498 int cookie_set = 0;
9499 unsigned int flags = REDIRECT_FLAG_NONE;
9500 struct acl_cond *cond = NULL;
9501
9502 cur_arg = 0;
9503 while (*(args[cur_arg])) {
9504 if (strcmp(args[cur_arg], "location") == 0) {
9505 if (!*args[cur_arg + 1])
9506 goto missing_arg;
9507
9508 type = REDIRECT_TYPE_LOCATION;
9509 cur_arg++;
9510 destination = args[cur_arg];
9511 }
9512 else if (strcmp(args[cur_arg], "prefix") == 0) {
9513 if (!*args[cur_arg + 1])
9514 goto missing_arg;
9515
9516 type = REDIRECT_TYPE_PREFIX;
9517 cur_arg++;
9518 destination = args[cur_arg];
9519 }
9520 else if (strcmp(args[cur_arg], "scheme") == 0) {
9521 if (!*args[cur_arg + 1])
9522 goto missing_arg;
9523
9524 type = REDIRECT_TYPE_SCHEME;
9525 cur_arg++;
9526 destination = args[cur_arg];
9527 }
9528 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9529 if (!*args[cur_arg + 1])
9530 goto missing_arg;
9531
9532 cur_arg++;
9533 cookie = args[cur_arg];
9534 cookie_set = 1;
9535 }
9536 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9537 if (!*args[cur_arg + 1])
9538 goto missing_arg;
9539
9540 cur_arg++;
9541 cookie = args[cur_arg];
9542 cookie_set = 0;
9543 }
9544 else if (strcmp(args[cur_arg], "code") == 0) {
9545 if (!*args[cur_arg + 1])
9546 goto missing_arg;
9547
9548 cur_arg++;
9549 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009550 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009551 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009552 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009553 args[cur_arg - 1], args[cur_arg]);
9554 return NULL;
9555 }
9556 }
9557 else if (!strcmp(args[cur_arg],"drop-query")) {
9558 flags |= REDIRECT_FLAG_DROP_QS;
9559 }
9560 else if (!strcmp(args[cur_arg],"append-slash")) {
9561 flags |= REDIRECT_FLAG_APPEND_SLASH;
9562 }
9563 else if (strcmp(args[cur_arg], "if") == 0 ||
9564 strcmp(args[cur_arg], "unless") == 0) {
9565 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9566 if (!cond) {
9567 memprintf(errmsg, "error in condition: %s", *errmsg);
9568 return NULL;
9569 }
9570 break;
9571 }
9572 else {
9573 memprintf(errmsg,
9574 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9575 args[cur_arg]);
9576 return NULL;
9577 }
9578 cur_arg++;
9579 }
9580
9581 if (type == REDIRECT_TYPE_NONE) {
9582 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9583 return NULL;
9584 }
9585
9586 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9587 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009588 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009589
9590 if (!use_fmt) {
9591 /* old-style static redirect rule */
9592 rule->rdr_str = strdup(destination);
9593 rule->rdr_len = strlen(destination);
9594 }
9595 else {
9596 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009597
9598 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9599 * if prefix == "/", we don't want to add anything, otherwise it
9600 * makes it hard for the user to configure a self-redirection.
9601 */
9602 proxy->conf.args.ctx = ARGC_RDR;
9603 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009604 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009605 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9606 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009607 free(curproxy->conf.lfs_file);
9608 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9609 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009610 }
9611 }
9612
Willy Tarreau4baae242012-12-27 12:00:31 +01009613 if (cookie) {
9614 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9615 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9616 */
9617 rule->cookie_len = strlen(cookie);
9618 if (cookie_set) {
9619 rule->cookie_str = malloc(rule->cookie_len + 10);
9620 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9621 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9622 rule->cookie_len += 9;
9623 } else {
9624 rule->cookie_str = malloc(rule->cookie_len + 21);
9625 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9626 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9627 rule->cookie_len += 20;
9628 }
9629 }
9630 rule->type = type;
9631 rule->code = code;
9632 rule->flags = flags;
9633 LIST_INIT(&rule->list);
9634 return rule;
9635
9636 missing_arg:
9637 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9638 return NULL;
9639}
9640
Willy Tarreau8797c062007-05-07 00:55:35 +02009641/************************************************************************/
9642/* The code below is dedicated to ACL parsing and matching */
9643/************************************************************************/
9644
9645
Willy Tarreau14174bc2012-04-16 14:34:04 +02009646/* This function ensures that the prerequisites for an L7 fetch are ready,
9647 * which means that a request or response is ready. If some data is missing,
9648 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009649 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9650 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009651 *
9652 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009653 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9654 * decide whether or not an HTTP message is present ;
9655 * 0 if the requested data cannot be fetched or if it is certain that
9656 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009657 * 1 if an HTTP message is ready
9658 */
9659static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009660smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009661 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009662{
9663 struct http_txn *txn = l7;
9664 struct http_msg *msg = &txn->req;
9665
9666 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9667 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9668 */
9669
9670 if (unlikely(!s || !txn))
9671 return 0;
9672
9673 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009674 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009675
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009676 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009677 if (unlikely(!s->req))
9678 return 0;
9679
Willy Tarreauaae75e32013-03-29 12:31:49 +01009680 /* If the buffer does not leave enough free space at the end,
9681 * we must first realign it.
9682 */
9683 if (s->req->buf->p > s->req->buf->data &&
9684 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9685 buffer_slow_realign(s->req->buf);
9686
Willy Tarreau14174bc2012-04-16 14:34:04 +02009687 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009688 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009689 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009690
9691 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009692 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009693 http_msg_analyzer(msg, &txn->hdr_idx);
9694
9695 /* Still no valid request ? */
9696 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009697 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009698 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009699 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009700 }
9701 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009702 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009703 return 0;
9704 }
9705
9706 /* OK we just got a valid HTTP request. We have some minor
9707 * preparation to perform so that further checks can rely
9708 * on HTTP tests.
9709 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009710
9711 /* If the request was parsed but was too large, we must absolutely
9712 * return an error so that it is not processed. At the moment this
9713 * cannot happen, but if the parsers are to change in the future,
9714 * we want this check to be maintained.
9715 */
9716 if (unlikely(s->req->buf->i + s->req->buf->p >
9717 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9718 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009719 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009720 return 1;
9721 }
9722
Willy Tarreau9b28e032012-10-12 23:49:43 +02009723 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009724 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9725 s->flags |= SN_REDIRECTABLE;
9726
Willy Tarreau506d0502013-07-06 13:29:24 +02009727 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9728 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009729 }
9730
Willy Tarreau506d0502013-07-06 13:29:24 +02009731 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009732 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009733 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009734
9735 /* otherwise everything's ready for the request */
9736 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009737 else {
9738 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009739 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9740 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009741 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009742 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009743 }
9744
9745 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009746 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009747 return 1;
9748}
Willy Tarreau8797c062007-05-07 00:55:35 +02009749
Willy Tarreauc0239e02012-04-16 14:42:55 +02009750#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009751 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 +02009752
Willy Tarreau24e32d82012-04-23 23:55:44 +02009753#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009754 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 +02009755
Willy Tarreau8797c062007-05-07 00:55:35 +02009756
9757/* 1. Check on METHOD
9758 * We use the pre-parsed method if it is known, and store its number as an
9759 * integer. If it is unknown, we use the pointer and the length.
9760 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009761static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009762{
9763 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009764 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02009765
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009766 len = strlen(text);
9767 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009768
9769 pattern->val.i = meth;
9770 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009771 trash = get_trash_chunk();
9772 if (trash->size < len) {
9773 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
9774 len, trash->size);
9775 return 0;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009776 }
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009777 pattern->ptr.str = trash->str;
Willy Tarreau8797c062007-05-07 00:55:35 +02009778 pattern->len = len;
9779 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009780 else {
9781 pattern->ptr.str = NULL;
9782 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009783 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009784 return 1;
9785}
9786
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009787/* This function fetches the method of current HTTP request and stores
9788 * it in the global pattern struct as a chunk. There are two possibilities :
9789 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9790 * in <len> and <ptr> is NULL ;
9791 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9792 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009793 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009794 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009795static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009796smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009797 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009798{
9799 int meth;
9800 struct http_txn *txn = l7;
9801
Willy Tarreau24e32d82012-04-23 23:55:44 +02009802 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009803
Willy Tarreau8797c062007-05-07 00:55:35 +02009804 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009805 smp->type = SMP_T_METH;
9806 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009807 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009808 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9809 /* ensure the indexes are not affected */
9810 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009811 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009812 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9813 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009814 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009815 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009816 return 1;
9817}
9818
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009819/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009820static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009821{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009822 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009823 struct pattern_list *lst;
9824 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009825
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009826 list_for_each_entry(lst, &expr->patterns, list) {
9827 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009828
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009829 /* well-known method */
9830 if (pattern->val.i != HTTP_METH_OTHER) {
9831 if (smp->data.meth.meth == pattern->val.i)
9832 return pattern;
9833 else
9834 continue;
9835 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009836
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009837 /* Other method, we must compare the strings */
9838 if (pattern->len != smp->data.meth.str.len)
9839 continue;
9840
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009841 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009842 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0) ||
9843 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0))
9844 return pattern;
9845 }
9846 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009847}
9848
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009849static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009850smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009851 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009852{
9853 struct http_txn *txn = l7;
9854 char *ptr;
9855 int len;
9856
Willy Tarreauc0239e02012-04-16 14:42:55 +02009857 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009858
Willy Tarreau8797c062007-05-07 00:55:35 +02009859 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009860 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009861
9862 while ((len-- > 0) && (*ptr++ != '/'));
9863 if (len <= 0)
9864 return 0;
9865
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009866 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009867 smp->data.str.str = ptr;
9868 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009869
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009870 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009871 return 1;
9872}
9873
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009874static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009875smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009876 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009877{
9878 struct http_txn *txn = l7;
9879 char *ptr;
9880 int len;
9881
Willy Tarreauc0239e02012-04-16 14:42:55 +02009882 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009883
Willy Tarreauf26b2522012-12-14 08:33:14 +01009884 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9885 return 0;
9886
Willy Tarreau8797c062007-05-07 00:55:35 +02009887 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009888 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009889
9890 while ((len-- > 0) && (*ptr++ != '/'));
9891 if (len <= 0)
9892 return 0;
9893
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009894 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009895 smp->data.str.str = ptr;
9896 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009897
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009898 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009899 return 1;
9900}
9901
9902/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009903static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009904smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009905 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009906{
9907 struct http_txn *txn = l7;
9908 char *ptr;
9909 int len;
9910
Willy Tarreauc0239e02012-04-16 14:42:55 +02009911 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009912
Willy Tarreauf26b2522012-12-14 08:33:14 +01009913 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9914 return 0;
9915
Willy Tarreau8797c062007-05-07 00:55:35 +02009916 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009917 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009918
Willy Tarreauf853c462012-04-23 18:53:56 +02009919 smp->type = SMP_T_UINT;
9920 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009921 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009922 return 1;
9923}
9924
9925/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009926static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009927smp_fetch_url(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
Willy Tarreauc0239e02012-04-16 14:42:55 +02009932 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009933
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009934 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009935 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009936 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009937 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009938 return 1;
9939}
9940
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009941static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009942smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009943 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009944{
9945 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009946 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009947
Willy Tarreauc0239e02012-04-16 14:42:55 +02009948 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009949
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009950 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 +02009951 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009952 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009953
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009954 smp->type = SMP_T_IPV4;
9955 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009956 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009957 return 1;
9958}
9959
9960static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009961smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009962 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009963{
9964 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009965 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009966
Willy Tarreauc0239e02012-04-16 14:42:55 +02009967 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009968
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009969 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 +02009970 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9971 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009972
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009973 smp->type = SMP_T_UINT;
9974 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009975 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009976 return 1;
9977}
9978
Willy Tarreau185b5c42012-04-26 15:11:51 +02009979/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9980 * Accepts an optional argument of type string containing the header field name,
9981 * and an optional argument of type signed or unsigned integer to request an
9982 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009983 * headers are considered from the first one. It does not stop on commas and
9984 * returns full lines instead (useful for User-Agent or Date for example).
9985 */
9986static int
9987smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009988 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009989{
9990 struct http_txn *txn = l7;
9991 struct hdr_idx *idx = &txn->hdr_idx;
9992 struct hdr_ctx *ctx = smp->ctx.a[0];
9993 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9994 int occ = 0;
9995 const char *name_str = NULL;
9996 int name_len = 0;
9997
9998 if (!ctx) {
9999 /* first call */
10000 ctx = &static_hdr_ctx;
10001 ctx->idx = 0;
10002 smp->ctx.a[0] = ctx;
10003 }
10004
10005 if (args) {
10006 if (args[0].type != ARGT_STR)
10007 return 0;
10008 name_str = args[0].data.str.str;
10009 name_len = args[0].data.str.len;
10010
10011 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10012 occ = args[1].data.uint;
10013 }
10014
10015 CHECK_HTTP_MESSAGE_FIRST();
10016
10017 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10018 /* search for header from the beginning */
10019 ctx->idx = 0;
10020
10021 if (!occ && !(opt & SMP_OPT_ITERATE))
10022 /* no explicit occurrence and single fetch => last header by default */
10023 occ = -1;
10024
10025 if (!occ)
10026 /* prepare to report multiple occurrences for ACL fetches */
10027 smp->flags |= SMP_F_NOT_LAST;
10028
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010029 smp->type = SMP_T_STR;
10030 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010031 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10032 return 1;
10033
10034 smp->flags &= ~SMP_F_NOT_LAST;
10035 return 0;
10036}
10037
10038/* 6. Check on HTTP header count. The number of occurrences is returned.
10039 * Accepts exactly 1 argument of type string. It does not stop on commas and
10040 * returns full lines instead (useful for User-Agent or Date for example).
10041 */
10042static int
10043smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010044 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010045{
10046 struct http_txn *txn = l7;
10047 struct hdr_idx *idx = &txn->hdr_idx;
10048 struct hdr_ctx ctx;
10049 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10050 int cnt;
10051
10052 if (!args || args->type != ARGT_STR)
10053 return 0;
10054
10055 CHECK_HTTP_MESSAGE_FIRST();
10056
10057 ctx.idx = 0;
10058 cnt = 0;
10059 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
10060 cnt++;
10061
10062 smp->type = SMP_T_UINT;
10063 smp->data.uint = cnt;
10064 smp->flags = SMP_F_VOL_HDR;
10065 return 1;
10066}
10067
10068/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10069 * Accepts an optional argument of type string containing the header field name,
10070 * and an optional argument of type signed or unsigned integer to request an
10071 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010072 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010073 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010074static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010075smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010076 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010077{
10078 struct http_txn *txn = l7;
10079 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010080 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010081 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010082 int occ = 0;
10083 const char *name_str = NULL;
10084 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010085
Willy Tarreaua890d072013-04-02 12:01:06 +020010086 if (!ctx) {
10087 /* first call */
10088 ctx = &static_hdr_ctx;
10089 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010090 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010091 }
10092
Willy Tarreau185b5c42012-04-26 15:11:51 +020010093 if (args) {
10094 if (args[0].type != ARGT_STR)
10095 return 0;
10096 name_str = args[0].data.str.str;
10097 name_len = args[0].data.str.len;
10098
10099 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10100 occ = args[1].data.uint;
10101 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010102
Willy Tarreaue333ec92012-04-16 16:26:40 +020010103 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010104
Willy Tarreau185b5c42012-04-26 15:11:51 +020010105 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010106 /* search for header from the beginning */
10107 ctx->idx = 0;
10108
Willy Tarreau185b5c42012-04-26 15:11:51 +020010109 if (!occ && !(opt & SMP_OPT_ITERATE))
10110 /* no explicit occurrence and single fetch => last header by default */
10111 occ = -1;
10112
10113 if (!occ)
10114 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010115 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010116
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010117 smp->type = SMP_T_STR;
10118 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010119 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 +020010120 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010121
Willy Tarreau37406352012-04-23 16:16:37 +020010122 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010123 return 0;
10124}
10125
Willy Tarreauc11416f2007-06-17 16:58:38 +020010126/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010127 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010128 */
10129static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010130smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010131 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010132{
10133 struct http_txn *txn = l7;
10134 struct hdr_idx *idx = &txn->hdr_idx;
10135 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010136 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010137 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +020010138
Willy Tarreau24e32d82012-04-23 23:55:44 +020010139 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010140 return 0;
10141
Willy Tarreaue333ec92012-04-16 16:26:40 +020010142 CHECK_HTTP_MESSAGE_FIRST();
10143
Willy Tarreau33a7e692007-06-10 19:45:56 +020010144 ctx.idx = 0;
10145 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010146 while (http_find_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010147 cnt++;
10148
Willy Tarreauf853c462012-04-23 18:53:56 +020010149 smp->type = SMP_T_UINT;
10150 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010151 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010152 return 1;
10153}
10154
Willy Tarreau185b5c42012-04-26 15:11:51 +020010155/* Fetch an HTTP header's integer value. The integer value is returned. It
10156 * takes a mandatory argument of type string and an optional one of type int
10157 * to designate a specific occurrence. It returns an unsigned integer, which
10158 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010159 */
10160static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010161smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010162 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010163{
Willy Tarreauef38c392013-07-22 16:29:32 +020010164 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010165
Willy Tarreauf853c462012-04-23 18:53:56 +020010166 if (ret > 0) {
10167 smp->type = SMP_T_UINT;
10168 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10169 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010170
Willy Tarreaud53e2422012-04-16 17:21:11 +020010171 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010172}
10173
Cyril Bonté69fa9922012-10-25 00:01:06 +020010174/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10175 * and an optional one of type int to designate a specific occurrence.
10176 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010177 */
10178static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010179smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010180 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +020010181{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010182 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010183
Willy Tarreauef38c392013-07-22 16:29:32 +020010184 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010185 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10186 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010187 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010188 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010189 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010190 if (smp->data.str.len < temp->size - 1) {
10191 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10192 temp->str[smp->data.str.len] = '\0';
10193 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10194 smp->type = SMP_T_IPV6;
10195 break;
10196 }
10197 }
10198 }
10199
Willy Tarreaud53e2422012-04-16 17:21:11 +020010200 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010201 if (!(smp->flags & SMP_F_NOT_LAST))
10202 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010203 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010204 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010205}
10206
Willy Tarreau737b0c12007-06-10 21:28:46 +020010207/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10208 * the first '/' after the possible hostname, and ends before the possible '?'.
10209 */
10210static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010211smp_fetch_path(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 Tarreau737b0c12007-06-10 21:28:46 +020010213{
10214 struct http_txn *txn = l7;
10215 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010216
Willy Tarreauc0239e02012-04-16 14:42:55 +020010217 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010218
Willy Tarreau9b28e032012-10-12 23:49:43 +020010219 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010220 ptr = http_get_path(txn);
10221 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010222 return 0;
10223
10224 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010225 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010226 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010227
10228 while (ptr < end && *ptr != '?')
10229 ptr++;
10230
Willy Tarreauf853c462012-04-23 18:53:56 +020010231 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010232 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010233 return 1;
10234}
10235
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010236/* This produces a concatenation of the first occurrence of the Host header
10237 * followed by the path component if it begins with a slash ('/'). This means
10238 * that '*' will not be added, resulting in exactly the first Host entry.
10239 * If no Host header is found, then the path is returned as-is. The returned
10240 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010241 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010242 */
10243static int
10244smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010245 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010246{
10247 struct http_txn *txn = l7;
10248 char *ptr, *end, *beg;
10249 struct hdr_ctx ctx;
10250
10251 CHECK_HTTP_MESSAGE_FIRST();
10252
10253 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010254 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +020010255 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010256
10257 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +010010258 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010259 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +010010260 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010261 smp->data.str.len = ctx.vlen;
10262
10263 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010264 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010265 beg = http_get_path(txn);
10266 if (!beg)
10267 beg = end;
10268
10269 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10270
10271 if (beg < ptr && *beg == '/') {
10272 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10273 smp->data.str.len += ptr - beg;
10274 }
10275
10276 smp->flags = SMP_F_VOL_1ST;
10277 return 1;
10278}
10279
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010280/* This produces a 32-bit hash of the concatenation of the first occurrence of
10281 * the Host header followed by the path component if it begins with a slash ('/').
10282 * This means that '*' will not be added, resulting in exactly the first Host
10283 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010284 * is hashed using the path hash followed by a full avalanche hash and provides a
10285 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010286 * high-traffic sites without having to store whole paths.
10287 */
10288static int
10289smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010290 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010291{
10292 struct http_txn *txn = l7;
10293 struct hdr_ctx ctx;
10294 unsigned int hash = 0;
10295 char *ptr, *beg, *end;
10296 int len;
10297
10298 CHECK_HTTP_MESSAGE_FIRST();
10299
10300 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010301 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010302 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10303 ptr = ctx.line + ctx.val;
10304 len = ctx.vlen;
10305 while (len--)
10306 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10307 }
10308
10309 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010310 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010311 beg = http_get_path(txn);
10312 if (!beg)
10313 beg = end;
10314
10315 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10316
10317 if (beg < ptr && *beg == '/') {
10318 while (beg < ptr)
10319 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10320 }
10321 hash = full_hash(hash);
10322
10323 smp->type = SMP_T_UINT;
10324 smp->data.uint = hash;
10325 smp->flags = SMP_F_VOL_1ST;
10326 return 1;
10327}
10328
Willy Tarreau4a550602012-12-09 14:53:32 +010010329/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010330 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10331 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10332 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010333 * that in environments where IPv6 is insignificant, truncating the output to
10334 * 8 bytes would still work.
10335 */
10336static int
10337smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010338 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +010010339{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010340 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010341 struct connection *cli_conn = objt_conn(l4->si[0].end);
10342
10343 if (!cli_conn)
10344 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010345
Willy Tarreauef38c392013-07-22 16:29:32 +020010346 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +010010347 return 0;
10348
Willy Tarreau47ca5452012-12-23 20:22:19 +010010349 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +010010350 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10351 temp->len += sizeof(smp->data.uint);
10352
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010353 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010354 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010355 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010356 temp->len += 4;
10357 break;
10358 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010359 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010360 temp->len += 16;
10361 break;
10362 default:
10363 return 0;
10364 }
10365
10366 smp->data.str = *temp;
10367 smp->type = SMP_T_BIN;
10368 return 1;
10369}
10370
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010371static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010372smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010373 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010374{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010375 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10376 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10377 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010378
Willy Tarreau24e32d82012-04-23 23:55:44 +020010379 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010380
Willy Tarreauf853c462012-04-23 18:53:56 +020010381 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010382 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010383 return 1;
10384}
10385
Willy Tarreau7f18e522010-10-22 20:04:13 +020010386/* return a valid test if the current request is the first one on the connection */
10387static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010388smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010389 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010390{
10391 if (!s)
10392 return 0;
10393
Willy Tarreauf853c462012-04-23 18:53:56 +020010394 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010395 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010396 return 1;
10397}
10398
Willy Tarreau34db1082012-04-19 17:16:54 +020010399/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010400static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010401smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010402 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010403{
10404
Willy Tarreau24e32d82012-04-23 23:55:44 +020010405 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010406 return 0;
10407
Willy Tarreauc0239e02012-04-16 14:42:55 +020010408 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010409
Willy Tarreauc0239e02012-04-16 14:42:55 +020010410 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010411 return 0;
10412
Willy Tarreauf853c462012-04-23 18:53:56 +020010413 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010414 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010415 return 1;
10416}
Willy Tarreau8797c062007-05-07 00:55:35 +020010417
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010418/* Accepts exactly 1 argument of type userlist */
10419static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010420smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010421 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010422{
10423
10424 if (!args || args->type != ARGT_USR)
10425 return 0;
10426
10427 CHECK_HTTP_MESSAGE_FIRST();
10428
10429 if (!get_http_auth(l4))
10430 return 0;
10431
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010432 /* if the user does not belong to the userlist or has a wrong password,
10433 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010434 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010435 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010436 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10437 return 0;
10438
10439 /* pat_match_auth() will need the user list */
10440 smp->ctx.a[0] = args->data.usr;
10441
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010442 smp->type = SMP_T_STR;
10443 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010444 smp->data.str.str = l4->txn.auth.user;
10445 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010446
10447 return 1;
10448}
10449
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010450/* Try to find the next occurrence of a cookie name in a cookie header value.
10451 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10452 * the cookie value is returned into *value and *value_l, and the function
10453 * returns a pointer to the next pointer to search from if the value was found.
10454 * Otherwise if the cookie was not found, NULL is returned and neither value
10455 * nor value_l are touched. The input <hdr> string should first point to the
10456 * header's value, and the <hdr_end> pointer must point to the first character
10457 * not part of the value. <list> must be non-zero if value may represent a list
10458 * of values (cookie headers). This makes it faster to abort parsing when no
10459 * list is expected.
10460 */
10461static char *
10462extract_cookie_value(char *hdr, const char *hdr_end,
10463 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010464 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010465{
10466 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10467 char *next;
10468
10469 /* we search at least a cookie name followed by an equal, and more
10470 * generally something like this :
10471 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10472 */
10473 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10474 /* Iterate through all cookies on this line */
10475
10476 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10477 att_beg++;
10478
10479 /* find att_end : this is the first character after the last non
10480 * space before the equal. It may be equal to hdr_end.
10481 */
10482 equal = att_end = att_beg;
10483
10484 while (equal < hdr_end) {
10485 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10486 break;
10487 if (http_is_spht[(unsigned char)*equal++])
10488 continue;
10489 att_end = equal;
10490 }
10491
10492 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10493 * is between <att_beg> and <equal>, both may be identical.
10494 */
10495
10496 /* look for end of cookie if there is an equal sign */
10497 if (equal < hdr_end && *equal == '=') {
10498 /* look for the beginning of the value */
10499 val_beg = equal + 1;
10500 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10501 val_beg++;
10502
10503 /* find the end of the value, respecting quotes */
10504 next = find_cookie_value_end(val_beg, hdr_end);
10505
10506 /* make val_end point to the first white space or delimitor after the value */
10507 val_end = next;
10508 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10509 val_end--;
10510 } else {
10511 val_beg = val_end = next = equal;
10512 }
10513
10514 /* We have nothing to do with attributes beginning with '$'. However,
10515 * they will automatically be removed if a header before them is removed,
10516 * since they're supposed to be linked together.
10517 */
10518 if (*att_beg == '$')
10519 continue;
10520
10521 /* Ignore cookies with no equal sign */
10522 if (equal == next)
10523 continue;
10524
10525 /* Now we have the cookie name between att_beg and att_end, and
10526 * its value between val_beg and val_end.
10527 */
10528
10529 if (att_end - att_beg == cookie_name_l &&
10530 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10531 /* let's return this value and indicate where to go on from */
10532 *value = val_beg;
10533 *value_l = val_end - val_beg;
10534 return next + 1;
10535 }
10536
10537 /* Set-Cookie headers only have the name in the first attr=value part */
10538 if (!list)
10539 break;
10540 }
10541
10542 return NULL;
10543}
10544
William Lallemanda43ba4e2014-01-28 18:14:25 +010010545/* Fetch a captured HTTP request header. The index is the position of
10546 * the "capture" option in the configuration file
10547 */
10548static int
10549smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10550 const struct arg *args, struct sample *smp, const char *kw)
10551{
10552 struct proxy *fe = l4->fe;
10553 struct http_txn *txn = l7;
10554 int idx;
10555
10556 if (!args || args->type != ARGT_UINT)
10557 return 0;
10558
10559 idx = args->data.uint;
10560
10561 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10562 return 0;
10563
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010564 smp->type = SMP_T_STR;
10565 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010566 smp->data.str.str = txn->req.cap[idx];
10567 smp->data.str.len = strlen(txn->req.cap[idx]);
10568
10569 return 1;
10570}
10571
10572/* Fetch a captured HTTP response header. The index is the position of
10573 * the "capture" option in the configuration file
10574 */
10575static int
10576smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10577 const struct arg *args, struct sample *smp, const char *kw)
10578{
10579 struct proxy *fe = l4->fe;
10580 struct http_txn *txn = l7;
10581 int idx;
10582
10583 if (!args || args->type != ARGT_UINT)
10584 return 0;
10585
10586 idx = args->data.uint;
10587
10588 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10589 return 0;
10590
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010591 smp->type = SMP_T_STR;
10592 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010593 smp->data.str.str = txn->rsp.cap[idx];
10594 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10595
10596 return 1;
10597}
10598
William Lallemand65ad6e12014-01-31 15:08:02 +010010599/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10600static int
10601smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10602 const struct arg *args, struct sample *smp, const char *kw)
10603{
10604 struct chunk *temp;
10605 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010606 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010607
10608 if (!txn->uri)
10609 return 0;
10610
William Lallemand96a77852014-02-05 00:30:02 +010010611 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010612
William Lallemand96a77852014-02-05 00:30:02 +010010613 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10614 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010615
William Lallemand96a77852014-02-05 00:30:02 +010010616 temp = get_trash_chunk();
10617 temp->str = txn->uri;
10618 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010619 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010620 smp->type = SMP_T_STR;
10621 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010622
10623 return 1;
10624
10625}
10626
10627/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10628static int
10629smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10630 const struct arg *args, struct sample *smp, const char *kw)
10631{
10632 struct chunk *temp;
10633 struct http_txn *txn = l7;
10634 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010635
10636 if (!txn->uri)
10637 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010638
William Lallemand65ad6e12014-01-31 15:08:02 +010010639 ptr = txn->uri;
10640
10641 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10642 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010643
William Lallemand65ad6e12014-01-31 15:08:02 +010010644 if (!*ptr)
10645 return 0;
10646
10647 ptr++; /* skip the space */
10648
10649 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010650 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010651 if (!ptr)
10652 return 0;
10653 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10654 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010655
10656 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010657 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010658 smp->type = SMP_T_STR;
10659 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010660
10661 return 1;
10662}
10663
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010664/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10665 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10666 */
10667static int
10668smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10669 const struct arg *args, struct sample *smp, const char *kw)
10670{
10671 struct http_txn *txn = l7;
10672
10673 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10674 return 0;
10675
10676 if (txn->req.flags & HTTP_MSGF_VER_11)
10677 smp->data.str.str = "HTTP/1.1";
10678 else
10679 smp->data.str.str = "HTTP/1.0";
10680
10681 smp->data.str.len = 8;
10682 smp->type = SMP_T_STR;
10683 smp->flags = SMP_F_CONST;
10684 return 1;
10685
10686}
10687
10688/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10689 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10690 */
10691static int
10692smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10693 const struct arg *args, struct sample *smp, const char *kw)
10694{
10695 struct http_txn *txn = l7;
10696
10697 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10698 return 0;
10699
10700 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10701 smp->data.str.str = "HTTP/1.1";
10702 else
10703 smp->data.str.str = "HTTP/1.0";
10704
10705 smp->data.str.len = 8;
10706 smp->type = SMP_T_STR;
10707 smp->flags = SMP_F_CONST;
10708 return 1;
10709
10710}
10711
William Lallemand65ad6e12014-01-31 15:08:02 +010010712
Willy Tarreaue333ec92012-04-16 16:26:40 +020010713/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010714 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010715 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010716 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010717 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010718 * Accepts exactly 1 argument of type string. If the input options indicate
10719 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010720 * The returned sample is of type CSTR. Can be used to parse cookies in other
10721 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010722 */
William Lallemand07c8b242014-05-02 17:11:07 +020010723int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010724 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010725{
10726 struct http_txn *txn = l7;
10727 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010728 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010729 const struct http_msg *msg;
10730 const char *hdr_name;
10731 int hdr_name_len;
10732 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010733 int occ = 0;
10734 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010735
Willy Tarreau24e32d82012-04-23 23:55:44 +020010736 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010737 return 0;
10738
Willy Tarreaua890d072013-04-02 12:01:06 +020010739 if (!ctx) {
10740 /* first call */
10741 ctx = &static_hdr_ctx;
10742 ctx->idx = 0;
10743 smp->ctx.a[2] = ctx;
10744 }
10745
Willy Tarreaue333ec92012-04-16 16:26:40 +020010746 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010747
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010748 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010749 msg = &txn->req;
10750 hdr_name = "Cookie";
10751 hdr_name_len = 6;
10752 } else {
10753 msg = &txn->rsp;
10754 hdr_name = "Set-Cookie";
10755 hdr_name_len = 10;
10756 }
10757
Willy Tarreau28376d62012-04-26 21:26:10 +020010758 if (!occ && !(opt & SMP_OPT_ITERATE))
10759 /* no explicit occurrence and single fetch => last cookie by default */
10760 occ = -1;
10761
10762 /* OK so basically here, either we want only one value and it's the
10763 * last one, or we want to iterate over all of them and we fetch the
10764 * next one.
10765 */
10766
Willy Tarreau9b28e032012-10-12 23:49:43 +020010767 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010768 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010769 /* search for the header from the beginning, we must first initialize
10770 * the search parameters.
10771 */
Willy Tarreau37406352012-04-23 16:16:37 +020010772 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010773 ctx->idx = 0;
10774 }
10775
Willy Tarreau28376d62012-04-26 21:26:10 +020010776 smp->flags |= SMP_F_VOL_HDR;
10777
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010778 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010779 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10780 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010781 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10782 goto out;
10783
Willy Tarreau24e32d82012-04-23 23:55:44 +020010784 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010785 continue;
10786
Willy Tarreau37406352012-04-23 16:16:37 +020010787 smp->ctx.a[0] = ctx->line + ctx->val;
10788 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010789 }
10790
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010791 smp->type = SMP_T_STR;
10792 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010793 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010794 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010795 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010796 &smp->data.str.str,
10797 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010798 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010799 found = 1;
10800 if (occ >= 0) {
10801 /* one value was returned into smp->data.str.{str,len} */
10802 smp->flags |= SMP_F_NOT_LAST;
10803 return 1;
10804 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010805 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010806 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010807 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010808 /* all cookie headers and values were scanned. If we're looking for the
10809 * last occurrence, we may return it now.
10810 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010811 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010812 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010813 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010814}
10815
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010816/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010817 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010818 * multiple cookies may be parsed on the same line. The returned sample is of
10819 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010820 */
10821static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010822smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010823 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010824{
10825 struct http_txn *txn = l7;
10826 struct hdr_idx *idx = &txn->hdr_idx;
10827 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010828 const struct http_msg *msg;
10829 const char *hdr_name;
10830 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010831 int cnt;
10832 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010833 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010834
Willy Tarreau24e32d82012-04-23 23:55:44 +020010835 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010836 return 0;
10837
Willy Tarreaue333ec92012-04-16 16:26:40 +020010838 CHECK_HTTP_MESSAGE_FIRST();
10839
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010840 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010841 msg = &txn->req;
10842 hdr_name = "Cookie";
10843 hdr_name_len = 6;
10844 } else {
10845 msg = &txn->rsp;
10846 hdr_name = "Set-Cookie";
10847 hdr_name_len = 10;
10848 }
10849
Willy Tarreau9b28e032012-10-12 23:49:43 +020010850 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010851 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010852 ctx.idx = 0;
10853 cnt = 0;
10854
10855 while (1) {
10856 /* Note: val_beg == NULL every time we need to fetch a new header */
10857 if (!val_beg) {
10858 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10859 break;
10860
Willy Tarreau24e32d82012-04-23 23:55:44 +020010861 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010862 continue;
10863
10864 val_beg = ctx.line + ctx.val;
10865 val_end = val_beg + ctx.vlen;
10866 }
10867
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010868 smp->type = SMP_T_STR;
10869 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010870 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010871 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010872 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010873 &smp->data.str.str,
10874 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010875 cnt++;
10876 }
10877 }
10878
Willy Tarreaub169eba2013-12-16 15:14:43 +010010879 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010880 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010881 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010882 return 1;
10883}
10884
Willy Tarreau51539362012-05-08 12:46:28 +020010885/* Fetch an cookie's integer value. The integer value is returned. It
10886 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10887 */
10888static int
10889smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010890 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010891{
Willy Tarreauef38c392013-07-22 16:29:32 +020010892 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010893
10894 if (ret > 0) {
10895 smp->type = SMP_T_UINT;
10896 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10897 }
10898
10899 return ret;
10900}
10901
Willy Tarreau8797c062007-05-07 00:55:35 +020010902/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010903/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010904/************************************************************************/
10905
David Cournapeau16023ee2010-12-23 20:55:41 +090010906/*
10907 * Given a path string and its length, find the position of beginning of the
10908 * query string. Returns NULL if no query string is found in the path.
10909 *
10910 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10911 *
10912 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10913 */
bedis4c75cca2012-10-05 08:38:24 +020010914static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010915{
10916 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010917
bedis4c75cca2012-10-05 08:38:24 +020010918 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010919 return p ? p + 1 : NULL;
10920}
10921
bedis4c75cca2012-10-05 08:38:24 +020010922static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010923{
bedis4c75cca2012-10-05 08:38:24 +020010924 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010925}
10926
10927/*
10928 * Given a url parameter, find the starting position of the first occurence,
10929 * or NULL if the parameter is not found.
10930 *
10931 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10932 * the function will return query_string+8.
10933 */
10934static char*
10935find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010936 char* url_param_name, size_t url_param_name_l,
10937 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010938{
10939 char *pos, *last;
10940
10941 pos = query_string;
10942 last = query_string + query_string_l - url_param_name_l - 1;
10943
10944 while (pos <= last) {
10945 if (pos[url_param_name_l] == '=') {
10946 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10947 return pos;
10948 pos += url_param_name_l + 1;
10949 }
bedis4c75cca2012-10-05 08:38:24 +020010950 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010951 pos++;
10952 pos++;
10953 }
10954 return NULL;
10955}
10956
10957/*
10958 * Given a url parameter name, returns its value and size into *value and
10959 * *value_l respectively, and returns non-zero. If the parameter is not found,
10960 * zero is returned and value/value_l are not touched.
10961 */
10962static int
10963find_url_param_value(char* path, size_t path_l,
10964 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010965 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010966{
10967 char *query_string, *qs_end;
10968 char *arg_start;
10969 char *value_start, *value_end;
10970
bedis4c75cca2012-10-05 08:38:24 +020010971 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010972 if (!query_string)
10973 return 0;
10974
10975 qs_end = path + path_l;
10976 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010977 url_param_name, url_param_name_l,
10978 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010979 if (!arg_start)
10980 return 0;
10981
10982 value_start = arg_start + url_param_name_l + 1;
10983 value_end = value_start;
10984
bedis4c75cca2012-10-05 08:38:24 +020010985 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010986 value_end++;
10987
10988 *value = value_start;
10989 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010990 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010991}
10992
10993static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010994smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010995 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010996{
bedis4c75cca2012-10-05 08:38:24 +020010997 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010998 struct http_txn *txn = l7;
10999 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011000
bedis4c75cca2012-10-05 08:38:24 +020011001 if (!args || args[0].type != ARGT_STR ||
11002 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011003 return 0;
11004
11005 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011006
bedis4c75cca2012-10-05 08:38:24 +020011007 if (args[1].type)
11008 delim = *args[1].data.str.str;
11009
Willy Tarreau9b28e032012-10-12 23:49:43 +020011010 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011011 args->data.str.str, args->data.str.len,
11012 &smp->data.str.str, &smp->data.str.len,
11013 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011014 return 0;
11015
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011016 smp->type = SMP_T_STR;
11017 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011018 return 1;
11019}
11020
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011021/* Return the signed integer value for the specified url parameter (see url_param
11022 * above).
11023 */
11024static int
11025smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011026 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011027{
Willy Tarreauef38c392013-07-22 16:29:32 +020011028 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011029
11030 if (ret > 0) {
11031 smp->type = SMP_T_UINT;
11032 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11033 }
11034
11035 return ret;
11036}
11037
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011038/* This produces a 32-bit hash of the concatenation of the first occurrence of
11039 * the Host header followed by the path component if it begins with a slash ('/').
11040 * This means that '*' will not be added, resulting in exactly the first Host
11041 * entry. If no Host header is found, then the path is used. The resulting value
11042 * is hashed using the url hash followed by a full avalanche hash and provides a
11043 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11044 * high-traffic sites without having to store whole paths.
11045 * this differs from the base32 functions in that it includes the url parameters
11046 * as well as the path
11047 */
11048static int
11049smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011050 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011051{
11052 struct http_txn *txn = l7;
11053 struct hdr_ctx ctx;
11054 unsigned int hash = 0;
11055 char *ptr, *beg, *end;
11056 int len;
11057
11058 CHECK_HTTP_MESSAGE_FIRST();
11059
11060 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011061 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011062 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11063 ptr = ctx.line + ctx.val;
11064 len = ctx.vlen;
11065 while (len--)
11066 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11067 }
11068
11069 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011070 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 +000011071 beg = http_get_path(txn);
11072 if (!beg)
11073 beg = end;
11074
11075 for (ptr = beg; ptr < end ; ptr++);
11076
11077 if (beg < ptr && *beg == '/') {
11078 while (beg < ptr)
11079 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11080 }
11081 hash = full_hash(hash);
11082
11083 smp->type = SMP_T_UINT;
11084 smp->data.uint = hash;
11085 smp->flags = SMP_F_VOL_1ST;
11086 return 1;
11087}
11088
11089/* This concatenates the source address with the 32-bit hash of the Host and
11090 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11091 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11092 * on the source address length. The URL hash is stored before the address so
11093 * that in environments where IPv6 is insignificant, truncating the output to
11094 * 8 bytes would still work.
11095 */
11096static int
11097smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011098 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011099{
11100 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011101 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011102
Willy Tarreaue155ec22013-11-18 18:33:22 +010011103 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011104 return 0;
11105
11106 temp = get_trash_chunk();
11107 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11108 temp->len += sizeof(smp->data.uint);
11109
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011110 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011111 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011112 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011113 temp->len += 4;
11114 break;
11115 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011116 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011117 temp->len += 16;
11118 break;
11119 default:
11120 return 0;
11121 }
11122
11123 smp->data.str = *temp;
11124 smp->type = SMP_T_BIN;
11125 return 1;
11126}
11127
Willy Tarreau185b5c42012-04-26 15:11:51 +020011128/* This function is used to validate the arguments passed to any "hdr" fetch
11129 * keyword. These keywords support an optional positive or negative occurrence
11130 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11131 * is assumed that the types are already the correct ones. Returns 0 on error,
11132 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11133 * error message in case of error, that the caller is responsible for freeing.
11134 * The initial location must either be freeable or NULL.
11135 */
11136static int val_hdr(struct arg *arg, char **err_msg)
11137{
11138 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011139 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011140 return 0;
11141 }
11142 return 1;
11143}
11144
Willy Tarreau276fae92013-07-25 14:36:01 +020011145/* takes an UINT value on input supposed to represent the time since EPOCH,
11146 * adds an optional offset found in args[0] and emits a string representing
11147 * the date in RFC-1123/5322 format.
11148 */
11149static int sample_conv_http_date(const struct arg *args, struct sample *smp)
11150{
11151 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11152 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11153 struct chunk *temp;
11154 struct tm *tm;
11155 time_t curr_date = smp->data.uint;
11156
11157 /* add offset */
11158 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11159 curr_date += args[0].data.sint;
11160
11161 tm = gmtime(&curr_date);
11162
11163 temp = get_trash_chunk();
11164 temp->len = snprintf(temp->str, temp->size - temp->len,
11165 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11166 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11167 tm->tm_hour, tm->tm_min, tm->tm_sec);
11168
11169 smp->data.str = *temp;
11170 smp->type = SMP_T_STR;
11171 return 1;
11172}
11173
Thierry FOURNIERad903512014-04-11 17:51:01 +020011174/* Match language range with language tag. RFC2616 14.4:
11175 *
11176 * A language-range matches a language-tag if it exactly equals
11177 * the tag, or if it exactly equals a prefix of the tag such
11178 * that the first tag character following the prefix is "-".
11179 *
11180 * Return 1 if the strings match, else return 0.
11181 */
11182static inline int language_range_match(const char *range, int range_len,
11183 const char *tag, int tag_len)
11184{
11185 const char *end = range + range_len;
11186 const char *tend = tag + tag_len;
11187 while (range < end) {
11188 if (*range == '-' && tag == tend)
11189 return 1;
11190 if (*range != *tag || tag == tend)
11191 return 0;
11192 range++;
11193 tag++;
11194 }
11195 /* Return true only if the last char of the tag is matched. */
11196 return tag == tend;
11197}
11198
11199/* Arguments: The list of expected value, the number of parts returned and the separator */
11200static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
11201{
11202 const char *al = smp->data.str.str;
11203 const char *end = al + smp->data.str.len;
11204 const char *token;
11205 int toklen;
11206 int qvalue;
11207 const char *str;
11208 const char *w;
11209 int best_q = 0;
11210
11211 /* Set the constant to the sample, because the output of the
11212 * function will be peek in the constant configuration string.
11213 */
11214 smp->flags |= SMP_F_CONST;
11215 smp->data.str.size = 0;
11216 smp->data.str.str = "";
11217 smp->data.str.len = 0;
11218
11219 /* Parse the accept language */
11220 while (1) {
11221
11222 /* Jump spaces, quit if the end is detected. */
11223 while (al < end && isspace(*al))
11224 al++;
11225 if (al >= end)
11226 break;
11227
11228 /* Start of the fisrt word. */
11229 token = al;
11230
11231 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
11232 while (al < end && *al != ';' && *al != ',' && !isspace(*al))
11233 al++;
11234 if (al == token)
11235 goto expect_comma;
11236
11237 /* Length of the token. */
11238 toklen = al - token;
11239 qvalue = 1000;
11240
11241 /* Check if the token exists in the list. If the token not exists,
11242 * jump to the next token.
11243 */
11244 str = args[0].data.str.str;
11245 w = str;
11246 while (1) {
11247 if (*str == ';' || *str == '\0') {
11248 if (language_range_match(token, toklen, w, str-w))
11249 goto look_for_q;
11250 if (*str == '\0')
11251 goto expect_comma;
11252 w = str + 1;
11253 }
11254 str++;
11255 }
11256 goto expect_comma;
11257
11258look_for_q:
11259
11260 /* Jump spaces, quit if the end is detected. */
11261 while (al < end && isspace(*al))
11262 al++;
11263 if (al >= end)
11264 goto process_value;
11265
11266 /* If ',' is found, process the result */
11267 if (*al == ',')
11268 goto process_value;
11269
11270 /* If the character is different from ';', look
11271 * for the end of the header part in best effort.
11272 */
11273 if (*al != ';')
11274 goto expect_comma;
11275
11276 /* Assumes that the char is ';', now expect "q=". */
11277 al++;
11278
11279 /* Jump spaces, process value if the end is detected. */
11280 while (al < end && isspace(*al))
11281 al++;
11282 if (al >= end)
11283 goto process_value;
11284
11285 /* Expect 'q'. If no 'q', continue in best effort */
11286 if (*al != 'q')
11287 goto process_value;
11288 al++;
11289
11290 /* Jump spaces, process value if the end is detected. */
11291 while (al < end && isspace(*al))
11292 al++;
11293 if (al >= end)
11294 goto process_value;
11295
11296 /* Expect '='. If no '=', continue in best effort */
11297 if (*al != '=')
11298 goto process_value;
11299 al++;
11300
11301 /* Jump spaces, process value if the end is detected. */
11302 while (al < end && isspace(*al))
11303 al++;
11304 if (al >= end)
11305 goto process_value;
11306
11307 /* Parse the q value. */
11308 qvalue = parse_qvalue(al, &al);
11309
11310process_value:
11311
11312 /* If the new q value is the best q value, then store the associated
11313 * language in the response. If qvalue is the biggest value (1000),
11314 * break the process.
11315 */
11316 if (qvalue > best_q) {
11317 smp->data.str.str = (char *)w;
11318 smp->data.str.len = str - w;
11319 if (qvalue >= 1000)
11320 break;
11321 best_q = qvalue;
11322 }
11323
11324expect_comma:
11325
11326 /* Expect comma or end. If the end is detected, quit the loop. */
11327 while (al < end && *al != ',')
11328 al++;
11329 if (al >= end)
11330 break;
11331
11332 /* Comma is found, jump it and restart the analyzer. */
11333 al++;
11334 }
11335
11336 /* Set default value if required. */
11337 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11338 smp->data.str.str = args[1].data.str.str;
11339 smp->data.str.len = args[1].data.str.len;
11340 }
11341
11342 /* Return true only if a matching language was found. */
11343 return smp->data.str.len != 0;
11344}
11345
William Lallemand73025dd2014-04-24 14:38:37 +020011346/*
11347 * Return the struct http_req_action_kw associated to a keyword.
11348 */
11349struct http_req_action_kw *action_http_req_custom(const char *kw)
11350{
11351 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11352 struct http_req_action_kw_list *kw_list;
11353 int i;
11354
11355 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11356 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11357 if (!strcmp(kw, kw_list->kw[i].kw))
11358 return &kw_list->kw[i];
11359 }
11360 }
11361 }
11362 return NULL;
11363}
11364
11365/*
11366 * Return the struct http_res_action_kw associated to a keyword.
11367 */
11368struct http_res_action_kw *action_http_res_custom(const char *kw)
11369{
11370 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11371 struct http_res_action_kw_list *kw_list;
11372 int i;
11373
11374 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11375 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11376 if (!strcmp(kw, kw_list->kw[i].kw))
11377 return &kw_list->kw[i];
11378 }
11379 }
11380 }
11381 return NULL;
11382}
11383
Willy Tarreau4a568972010-05-12 08:08:50 +020011384/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011385/* All supported ACL keywords must be declared here. */
11386/************************************************************************/
11387
11388/* Note: must not be declared <const> as its list will be overwritten.
11389 * Please take care of keeping this list alphabetically sorted.
11390 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011391static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011392 { "base", "base", PAT_MATCH_STR },
11393 { "base_beg", "base", PAT_MATCH_BEG },
11394 { "base_dir", "base", PAT_MATCH_DIR },
11395 { "base_dom", "base", PAT_MATCH_DOM },
11396 { "base_end", "base", PAT_MATCH_END },
11397 { "base_len", "base", PAT_MATCH_LEN },
11398 { "base_reg", "base", PAT_MATCH_REG },
11399 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011400
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011401 { "cook", "req.cook", PAT_MATCH_STR },
11402 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11403 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11404 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11405 { "cook_end", "req.cook", PAT_MATCH_END },
11406 { "cook_len", "req.cook", PAT_MATCH_LEN },
11407 { "cook_reg", "req.cook", PAT_MATCH_REG },
11408 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011409
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011410 { "hdr", "req.hdr", PAT_MATCH_STR },
11411 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11412 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11413 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11414 { "hdr_end", "req.hdr", PAT_MATCH_END },
11415 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11416 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11417 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011418
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011419 /* these two declarations uses strings with list storage (in place
11420 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11421 * and delete functions are relative to the list management. The parse
11422 * and match method are related to the corresponding fetch methods. This
11423 * is very particular ACL declaration mode.
11424 */
11425 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11426 { "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 +020011427
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011428 { "path", "path", PAT_MATCH_STR },
11429 { "path_beg", "path", PAT_MATCH_BEG },
11430 { "path_dir", "path", PAT_MATCH_DIR },
11431 { "path_dom", "path", PAT_MATCH_DOM },
11432 { "path_end", "path", PAT_MATCH_END },
11433 { "path_len", "path", PAT_MATCH_LEN },
11434 { "path_reg", "path", PAT_MATCH_REG },
11435 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011436
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011437 { "req_ver", "req.ver", PAT_MATCH_STR },
11438 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011439
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011440 { "scook", "res.cook", PAT_MATCH_STR },
11441 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11442 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11443 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11444 { "scook_end", "res.cook", PAT_MATCH_END },
11445 { "scook_len", "res.cook", PAT_MATCH_LEN },
11446 { "scook_reg", "res.cook", PAT_MATCH_REG },
11447 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011448
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011449 { "shdr", "res.hdr", PAT_MATCH_STR },
11450 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11451 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11452 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11453 { "shdr_end", "res.hdr", PAT_MATCH_END },
11454 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11455 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11456 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011457
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011458 { "url", "url", PAT_MATCH_STR },
11459 { "url_beg", "url", PAT_MATCH_BEG },
11460 { "url_dir", "url", PAT_MATCH_DIR },
11461 { "url_dom", "url", PAT_MATCH_DOM },
11462 { "url_end", "url", PAT_MATCH_END },
11463 { "url_len", "url", PAT_MATCH_LEN },
11464 { "url_reg", "url", PAT_MATCH_REG },
11465 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011466
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011467 { "urlp", "urlp", PAT_MATCH_STR },
11468 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11469 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11470 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11471 { "urlp_end", "urlp", PAT_MATCH_END },
11472 { "urlp_len", "urlp", PAT_MATCH_LEN },
11473 { "urlp_reg", "urlp", PAT_MATCH_REG },
11474 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011475
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011476 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011477}};
11478
11479/************************************************************************/
11480/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011481/************************************************************************/
11482/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011483static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011484 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011485 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11486 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11487
William Lallemanda43ba4e2014-01-28 18:14:25 +010011488 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011489 { "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 +020011490
11491 /* retrieve these captures from the HTTP logs */
11492 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11493 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11494 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11495
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011496 { "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 +020011497 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011498
Willy Tarreau409bcde2013-01-08 00:31:00 +010011499 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11500 * are only here to match the ACL's name, are request-only and are used
11501 * for ACL compatibility only.
11502 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011503 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11504 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011505 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11506 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11507
11508 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11509 * only here to match the ACL's name, are request-only and are used for
11510 * ACL compatibility only.
11511 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011512 { "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 +010011513 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11514 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11515 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11516
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011517 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011518 { "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 +010011519 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011520 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011521 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011522
11523 /* HTTP protocol on the request path */
11524 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011525 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011526
11527 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011528 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11529 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011530
11531 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011532 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11533 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011534
Willy Tarreau18ed2562013-01-14 15:56:36 +010011535 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011536 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011537 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11538 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11539
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011540 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011541 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011542 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011543 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11544 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11545 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11546
11547 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011548 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011549 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11550 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11551
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011552 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011553 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011554 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011555 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11556 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11557 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11558
Willy Tarreau409bcde2013-01-08 00:31:00 +010011559 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011560 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011561 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11562 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011563 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011564
11565 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011566 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011567 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11568 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11569 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11570
11571 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011572 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011573 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11574 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011575 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11576 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011577 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11578 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011579 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11580 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011581}};
11582
Willy Tarreau8797c062007-05-07 00:55:35 +020011583
Willy Tarreau276fae92013-07-25 14:36:01 +020011584/* Note: must not be declared <const> as its list will be overwritten */
11585static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011586 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11587 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011588 { NULL, NULL, 0, 0, 0 },
11589}};
11590
Willy Tarreau8797c062007-05-07 00:55:35 +020011591__attribute__((constructor))
11592static void __http_protocol_init(void)
11593{
11594 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011595 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011596 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020011597}
11598
11599
Willy Tarreau58f10d72006-12-04 02:26:12 +010011600/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011601 * Local variables:
11602 * c-indent-level: 8
11603 * c-basic-offset: 8
11604 * End:
11605 */