blob: 20e70881cef77e3ac966d21b6f77f936585c7bd7 [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
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002146 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002147 goto out;
2148 q = (*qvalue++ - '0') * 1000;
2149
2150 if (*qvalue++ != '.')
2151 goto out;
2152
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002153 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002154 goto out;
2155 q += (*qvalue++ - '0') * 100;
2156
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002157 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002158 goto out;
2159 q += (*qvalue++ - '0') * 10;
2160
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002161 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002162 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
Willy Tarreau2e47a3a2014-09-30 18:44:22 +02002396void http_adjust_conn_mode(struct session *s, struct http_txn *txn, struct http_msg *msg)
2397{
2398 int tmp = TX_CON_WANT_KAL;
2399
2400 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2401 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
2402 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2403 tmp = TX_CON_WANT_TUN;
2404
2405 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2406 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2407 tmp = TX_CON_WANT_TUN;
2408 }
2409
2410 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
2411 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2412 /* option httpclose + server_close => forceclose */
2413 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2414 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2415 tmp = TX_CON_WANT_CLO;
2416 else
2417 tmp = TX_CON_WANT_SCL;
2418 }
2419
2420 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
2421 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2422 tmp = TX_CON_WANT_CLO;
2423
2424 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2425 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2426
2427 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2428 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2429 /* parse the Connection header and possibly clean it */
2430 int to_del = 0;
2431 if ((msg->flags & HTTP_MSGF_VER_11) ||
2432 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2433 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
2434 to_del |= 2; /* remove "keep-alive" */
2435 if (!(msg->flags & HTTP_MSGF_VER_11))
2436 to_del |= 1; /* remove "close" */
2437 http_parse_connection_header(txn, msg, to_del);
2438 }
2439
2440 /* check if client or config asks for explicit close in KAL/SCL */
2441 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2442 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2443 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2444 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2445 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
2446 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
2447 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2448}
William Lallemand82fe75c2012-10-23 10:25:10 +02002449
Willy Tarreaud787e662009-07-07 10:14:51 +02002450/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2451 * processing can continue on next analysers, or zero if it either needs more
2452 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2453 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2454 * when it has nothing left to do, and may remove any analyser when it wants to
2455 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002456 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002457int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002458{
Willy Tarreau59234e92008-11-30 23:51:27 +01002459 /*
2460 * We will parse the partial (or complete) lines.
2461 * We will check the request syntax, and also join multi-line
2462 * headers. An index of all the lines will be elaborated while
2463 * parsing.
2464 *
2465 * For the parsing, we use a 28 states FSM.
2466 *
2467 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002468 * req->buf->p = beginning of request
2469 * req->buf->p + msg->eoh = end of processed headers / start of current one
2470 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002471 * msg->eol = end of current header or line (LF or CRLF)
2472 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002473 *
2474 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002475 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002476 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2477 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002478 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002479
Willy Tarreau59234e92008-11-30 23:51:27 +01002480 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002481 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002482 struct http_txn *txn = &s->txn;
2483 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002484 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002485
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002486 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 +01002487 now_ms, __FUNCTION__,
2488 s,
2489 req,
2490 req->rex, req->wex,
2491 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002492 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002493 req->analysers);
2494
Willy Tarreau52a0c602009-08-16 22:45:38 +02002495 /* we're speaking HTTP here, so let's speak HTTP to the client */
2496 s->srv_error = http_return_srv_error;
2497
Willy Tarreau83e3af02009-12-28 17:39:57 +01002498 /* There's a protected area at the end of the buffer for rewriting
2499 * purposes. We don't want to start to parse the request if the
2500 * protected area is affected, because we may have to move processed
2501 * data later, which is much more complicated.
2502 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002503 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002504 if (txn->flags & TX_NOT_FIRST) {
2505 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002506 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002507 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002508 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002509 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002510 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002511 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002512 return 0;
2513 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002514 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2515 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2516 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002517 }
2518
Willy Tarreau065e8332010-01-08 00:30:20 +01002519 /* Note that we have the same problem with the response ; we
2520 * may want to send a redirect, error or anything which requires
2521 * some spare space. So we'll ensure that we have at least
2522 * maxrewrite bytes available in the response buffer before
2523 * processing that one. This will only affect pipelined
2524 * keep-alive requests.
2525 */
2526 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002527 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002528 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2529 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2530 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002531 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002532 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002533 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002534 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002535 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002536 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002537 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002538 return 0;
2539 }
2540 }
2541
Willy Tarreau9b28e032012-10-12 23:49:43 +02002542 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002543 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002544 }
2545
Willy Tarreau59234e92008-11-30 23:51:27 +01002546 /* 1: we might have to print this header in debug mode */
2547 if (unlikely((global.mode & MODE_DEBUG) &&
2548 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002549 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002551
Willy Tarreau9b28e032012-10-12 23:49:43 +02002552 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002553 /* this is a bit complex : in case of error on the request line,
2554 * we know that rq.l is still zero, so we display only the part
2555 * up to the end of the line (truncated by debug_hdr).
2556 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002557 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002558 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002559
Willy Tarreau59234e92008-11-30 23:51:27 +01002560 sol += hdr_idx_first_pos(&txn->hdr_idx);
2561 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002562
Willy Tarreau59234e92008-11-30 23:51:27 +01002563 while (cur_idx) {
2564 eol = sol + txn->hdr_idx.v[cur_idx].len;
2565 debug_hdr("clihdr", s, sol, eol);
2566 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2567 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002568 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002569 }
2570
Willy Tarreau58f10d72006-12-04 02:26:12 +01002571
Willy Tarreau59234e92008-11-30 23:51:27 +01002572 /*
2573 * Now we quickly check if we have found a full valid request.
2574 * If not so, we check the FD and buffer states before leaving.
2575 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002576 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002577 * requests are checked first. When waiting for a second request
2578 * on a keep-alive session, if we encounter and error, close, t/o,
2579 * we note the error in the session flags but don't set any state.
2580 * Since the error will be noted there, it will not be counted by
2581 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002582 * Last, we may increase some tracked counters' http request errors on
2583 * the cases that are deliberately the client's fault. For instance,
2584 * a timeout or connection reset is not counted as an error. However
2585 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002587
Willy Tarreau655dce92009-11-08 13:10:58 +01002588 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002589 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002590 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002591 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002592 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002593 session_inc_http_req_ctr(s);
2594 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002595 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002596 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002597 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002598
Willy Tarreau59234e92008-11-30 23:51:27 +01002599 /* 1: Since we are in header mode, if there's no space
2600 * left for headers, we won't be able to free more
2601 * later, so the session will never terminate. We
2602 * must terminate it now.
2603 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002604 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002605 /* FIXME: check if URI is set and return Status
2606 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002607 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002608 session_inc_http_req_ctr(s);
2609 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002610 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002611 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002612 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002613 goto return_bad_req;
2614 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002615
Willy Tarreau59234e92008-11-30 23:51:27 +01002616 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002617 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002618 if (!(s->flags & SN_ERR_MASK))
2619 s->flags |= SN_ERR_CLICL;
2620
Willy Tarreaufcffa692010-01-10 14:21:19 +01002621 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002622 goto failed_keep_alive;
2623
Willy Tarreau59234e92008-11-30 23:51:27 +01002624 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002625 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002626 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002627 session_inc_http_err_ctr(s);
2628 }
2629
Willy Tarreaudc979f22012-12-04 10:39:01 +01002630 txn->status = 400;
2631 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 msg->msg_state = HTTP_MSG_ERROR;
2633 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002634
Willy Tarreauda7ff642010-06-23 11:44:09 +02002635 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002636 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002637 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002638 if (s->listener->counters)
2639 s->listener->counters->failed_req++;
2640
Willy Tarreau59234e92008-11-30 23:51:27 +01002641 if (!(s->flags & SN_FINST_MASK))
2642 s->flags |= SN_FINST_R;
2643 return 0;
2644 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002645
Willy Tarreau59234e92008-11-30 23:51:27 +01002646 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002647 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002648 if (!(s->flags & SN_ERR_MASK))
2649 s->flags |= SN_ERR_CLITO;
2650
Willy Tarreaufcffa692010-01-10 14:21:19 +01002651 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002652 goto failed_keep_alive;
2653
Willy Tarreau59234e92008-11-30 23:51:27 +01002654 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002655 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002656 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002657 session_inc_http_err_ctr(s);
2658 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002659 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002660 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002661 msg->msg_state = HTTP_MSG_ERROR;
2662 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002663
Willy Tarreauda7ff642010-06-23 11:44:09 +02002664 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002665 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002666 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002667 if (s->listener->counters)
2668 s->listener->counters->failed_req++;
2669
Willy Tarreau59234e92008-11-30 23:51:27 +01002670 if (!(s->flags & SN_FINST_MASK))
2671 s->flags |= SN_FINST_R;
2672 return 0;
2673 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002674
Willy Tarreau59234e92008-11-30 23:51:27 +01002675 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002676 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002677 if (!(s->flags & SN_ERR_MASK))
2678 s->flags |= SN_ERR_CLICL;
2679
Willy Tarreaufcffa692010-01-10 14:21:19 +01002680 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002681 goto failed_keep_alive;
2682
Willy Tarreau4076a152009-04-02 15:18:36 +02002683 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002684 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002685 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002686 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002687 msg->msg_state = HTTP_MSG_ERROR;
2688 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002689
Willy Tarreauda7ff642010-06-23 11:44:09 +02002690 session_inc_http_err_ctr(s);
2691 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002692 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002693 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002694 if (s->listener->counters)
2695 s->listener->counters->failed_req++;
2696
Willy Tarreau59234e92008-11-30 23:51:27 +01002697 if (!(s->flags & SN_FINST_MASK))
2698 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002699 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002700 }
2701
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002702 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002703 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2704 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002705#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002706 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 +01002707 /* We need more data, we have to re-enable quick-ack in case we
2708 * previously disabled it, otherwise we might cause the client
2709 * to delay next data.
2710 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002711 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002712 }
2713#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002714
Willy Tarreaufcffa692010-01-10 14:21:19 +01002715 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2716 /* If the client starts to talk, let's fall back to
2717 * request timeout processing.
2718 */
2719 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002720 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002721 }
2722
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002724 if (!tick_isset(req->analyse_exp)) {
2725 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2726 (txn->flags & TX_WAIT_NEXT_RQ) &&
2727 tick_isset(s->be->timeout.httpka))
2728 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2729 else
2730 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2731 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002732
Willy Tarreau59234e92008-11-30 23:51:27 +01002733 /* we're not ready yet */
2734 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002735
2736 failed_keep_alive:
2737 /* Here we process low-level errors for keep-alive requests. In
2738 * short, if the request is not the first one and it experiences
2739 * a timeout, read error or shutdown, we just silently close so
2740 * that the client can try again.
2741 */
2742 txn->status = 0;
2743 msg->msg_state = HTTP_MSG_RQBEFORE;
2744 req->analysers = 0;
2745 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002746 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002747 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002748 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002749 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002750 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002751
Willy Tarreaud787e662009-07-07 10:14:51 +02002752 /* OK now we have a complete HTTP request with indexed headers. Let's
2753 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002754 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002755 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002756 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002757 * byte after the last LF. msg->sov points to the first byte of data.
2758 * msg->eol cannot be trusted because it may have been left uninitialized
2759 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002760 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002761
Willy Tarreauda7ff642010-06-23 11:44:09 +02002762 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002763 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2764
Willy Tarreaub16a5742010-01-10 14:46:16 +01002765 if (txn->flags & TX_WAIT_NEXT_RQ) {
2766 /* kill the pending keep-alive timeout */
2767 txn->flags &= ~TX_WAIT_NEXT_RQ;
2768 req->analyse_exp = TICK_ETERNITY;
2769 }
2770
2771
Willy Tarreaud787e662009-07-07 10:14:51 +02002772 /* Maybe we found in invalid header name while we were configured not
2773 * to block on that, so we have to capture it now.
2774 */
2775 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002776 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002777
Willy Tarreau59234e92008-11-30 23:51:27 +01002778 /*
2779 * 1: identify the method
2780 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002781 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002782
2783 /* we can make use of server redirect on GET and HEAD */
2784 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2785 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002786
Willy Tarreau59234e92008-11-30 23:51:27 +01002787 /*
2788 * 2: check if the URI matches the monitor_uri.
2789 * We have to do this for every request which gets in, because
2790 * the monitor-uri is defined by the frontend.
2791 */
2792 if (unlikely((s->fe->monitor_uri_len != 0) &&
2793 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002794 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002795 s->fe->monitor_uri,
2796 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002797 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002798 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002799 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002800 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002801
Willy Tarreau59234e92008-11-30 23:51:27 +01002802 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002803 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002804
Willy Tarreau59234e92008-11-30 23:51:27 +01002805 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002806 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002807 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002808
Willy Tarreau59234e92008-11-30 23:51:27 +01002809 ret = acl_pass(ret);
2810 if (cond->pol == ACL_COND_UNLESS)
2811 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002812
Willy Tarreau59234e92008-11-30 23:51:27 +01002813 if (ret) {
2814 /* we fail this request, let's return 503 service unavail */
2815 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002816 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002817 if (!(s->flags & SN_ERR_MASK))
2818 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002819 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002820 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002821 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002822
Willy Tarreau59234e92008-11-30 23:51:27 +01002823 /* nothing to fail, let's reply normaly */
2824 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002825 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002826 if (!(s->flags & SN_ERR_MASK))
2827 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002828 goto return_prx_cond;
2829 }
2830
2831 /*
2832 * 3: Maybe we have to copy the original REQURI for the logs ?
2833 * Note: we cannot log anymore if the request has been
2834 * classified as invalid.
2835 */
2836 if (unlikely(s->logs.logwait & LW_REQ)) {
2837 /* we have a complete HTTP request that we must log */
2838 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2839 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002840
Willy Tarreau59234e92008-11-30 23:51:27 +01002841 if (urilen >= REQURI_LEN)
2842 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002843 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002844 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002845
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002846 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002847 s->do_log(s);
2848 } else {
2849 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002850 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002851 }
Willy Tarreau06619262006-12-17 08:37:22 +01002852
Willy Tarreau59234e92008-11-30 23:51:27 +01002853 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002854 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002855 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002856
Willy Tarreau5b154472009-12-21 20:11:07 +01002857 /* ... and check if the request is HTTP/1.1 or above */
2858 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002859 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2860 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2861 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002862 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002863
2864 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002865 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 +01002866
Willy Tarreau88d349d2010-01-25 12:15:43 +01002867 /* if the frontend has "option http-use-proxy-header", we'll check if
2868 * we have what looks like a proxied connection instead of a connection,
2869 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2870 * Note that this is *not* RFC-compliant, however browsers and proxies
2871 * happen to do that despite being non-standard :-(
2872 * We consider that a request not beginning with either '/' or '*' is
2873 * a proxied connection, which covers both "scheme://location" and
2874 * CONNECT ip:port.
2875 */
2876 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002877 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002878 txn->flags |= TX_USE_PX_CONN;
2879
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002880 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002881 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002882
Willy Tarreau59234e92008-11-30 23:51:27 +01002883 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002884 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002885 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002886 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002887
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002888 /* 6: determine the transfer-length.
2889 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2890 * the presence of a message-body in a REQUEST and its transfer length
2891 * must be determined that way (in order of precedence) :
2892 * 1. The presence of a message-body in a request is signaled by the
2893 * inclusion of a Content-Length or Transfer-Encoding header field
2894 * in the request's header fields. When a request message contains
2895 * both a message-body of non-zero length and a method that does
2896 * not define any semantics for that request message-body, then an
2897 * origin server SHOULD either ignore the message-body or respond
2898 * with an appropriate error message (e.g., 413). A proxy or
2899 * gateway, when presented the same request, SHOULD either forward
2900 * the request inbound with the message- body or ignore the
2901 * message-body when determining a response.
2902 *
2903 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2904 * and the "chunked" transfer-coding (Section 6.2) is used, the
2905 * transfer-length is defined by the use of this transfer-coding.
2906 * If a Transfer-Encoding header field is present and the "chunked"
2907 * transfer-coding is not present, the transfer-length is defined
2908 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002909 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002910 * 3. If a Content-Length header field is present, its decimal value in
2911 * OCTETs represents both the entity-length and the transfer-length.
2912 * If a message is received with both a Transfer-Encoding header
2913 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002914 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002915 * 4. By the server closing the connection. (Closing the connection
2916 * cannot be used to indicate the end of a request body, since that
2917 * would leave no possibility for the server to send back a response.)
2918 *
2919 * Whenever a transfer-coding is applied to a message-body, the set of
2920 * transfer-codings MUST include "chunked", unless the message indicates
2921 * it is terminated by closing the connection. When the "chunked"
2922 * transfer-coding is used, it MUST be the last transfer-coding applied
2923 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002924 */
2925
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002926 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002927 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002928 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002929 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002930 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002931 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002932 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2933 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002934 /* bad transfer-encoding (chunked followed by something else) */
2935 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002936 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002937 break;
2938 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002939 }
2940
Willy Tarreau32b47f42009-10-18 20:55:02 +02002941 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002942 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002943 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002944 signed long long cl;
2945
Willy Tarreauad14f752011-09-02 20:33:27 +02002946 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002947 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002948 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002949 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002950
Willy Tarreauad14f752011-09-02 20:33:27 +02002951 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002952 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002953 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002954 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002955
Willy Tarreauad14f752011-09-02 20:33:27 +02002956 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002957 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002958 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002959 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002960
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002961 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002962 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002963 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002964 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002965
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002966 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002967 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002968 }
2969
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002970 /* bodyless requests have a known length */
2971 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002972 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002973
Willy Tarreau179085c2014-04-28 16:48:56 +02002974 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2975 * only change if both the request and the config reference something else.
2976 * Option httpclose by itself sets tunnel mode where headers are mangled.
2977 * However, if another mode is set, it will affect it (eg: server-close/
2978 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2979 * if FE and BE have the same settings (common). The method consists in
2980 * checking if options changed between the two calls (implying that either
2981 * one is non-null, or one of them is non-null and we are there for the first
2982 * time.
2983 */
2984 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau2e47a3a2014-09-30 18:44:22 +02002985 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
2986 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002987
Willy Tarreaud787e662009-07-07 10:14:51 +02002988 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002989 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002990 req->analyse_exp = TICK_ETERNITY;
2991 return 1;
2992
2993 return_bad_req:
2994 /* We centralize bad requests processing here */
2995 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2996 /* we detected a parsing error. We want to archive this request
2997 * in the dedicated proxy area for later troubleshooting.
2998 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002999 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003000 }
3001
3002 txn->req.msg_state = HTTP_MSG_ERROR;
3003 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003004 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003005
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003006 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003007 if (s->listener->counters)
3008 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003009
3010 return_prx_cond:
3011 if (!(s->flags & SN_ERR_MASK))
3012 s->flags |= SN_ERR_PRXCOND;
3013 if (!(s->flags & SN_FINST_MASK))
3014 s->flags |= SN_FINST_R;
3015
3016 req->analysers = 0;
3017 req->analyse_exp = TICK_ETERNITY;
3018 return 0;
3019}
3020
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003021
Willy Tarreau347a35d2013-11-22 17:51:09 +01003022/* This function prepares an applet to handle the stats. It can deal with the
3023 * "100-continue" expectation, check that admin rules are met for POST requests,
3024 * and program a response message if something was unexpected. It cannot fail
3025 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003026 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003027 * s->target which is supposed to already point to the stats applet. The caller
3028 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003029 */
3030int http_handle_stats(struct session *s, struct channel *req)
3031{
3032 struct stats_admin_rule *stats_admin_rule;
3033 struct stream_interface *si = s->rep->prod;
3034 struct http_txn *txn = &s->txn;
3035 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003036 struct uri_auth *uri_auth = s->be->uri_auth;
3037 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003038 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003039
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003040 appctx = si_appctx(si);
3041 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3042 appctx->st1 = appctx->st2 = 0;
3043 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3044 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003045 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3046 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003047
3048 uri = msg->chn->buf->p + msg->sl.rq.u;
3049 lookup = uri + uri_auth->uri_len;
3050
3051 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3052 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003053 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003054 break;
3055 }
3056 }
3057
3058 if (uri_auth->refresh) {
3059 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3060 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003061 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003062 break;
3063 }
3064 }
3065 }
3066
3067 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3068 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003069 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003070 break;
3071 }
3072 }
3073
3074 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3075 if (memcmp(h, ";st=", 4) == 0) {
3076 int i;
3077 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003078 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003079 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3080 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003081 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003082 break;
3083 }
3084 }
3085 break;
3086 }
3087 }
3088
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003089 appctx->ctx.stats.scope_str = 0;
3090 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003091 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3092 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3093 int itx = 0;
3094 const char *h2;
3095 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3096 const char *err;
3097
3098 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3099 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003100 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003101 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3102 itx++;
3103 h++;
3104 }
3105
3106 if (itx > STAT_SCOPE_TXT_MAXLEN)
3107 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003108 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003109
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003110 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003111 memcpy(scope_txt, h2, itx);
3112 scope_txt[itx] = '\0';
3113 err = invalid_char(scope_txt);
3114 if (err) {
3115 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003116 appctx->ctx.stats.scope_str = 0;
3117 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003118 }
3119 break;
3120 }
3121 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003122
3123 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003124 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003125 int ret = 1;
3126
3127 if (stats_admin_rule->cond) {
3128 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3129 ret = acl_pass(ret);
3130 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3131 ret = !ret;
3132 }
3133
3134 if (ret) {
3135 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003136 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003137 break;
3138 }
3139 }
3140
3141 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003142 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003143 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003144 /* we'll need the request body, possibly after sending 100-continue */
3145 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003146 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003147 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003148 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003149 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3150 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003151 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003152 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003153 else {
3154 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003155 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003156 }
3157
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003158 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003159 return 1;
3160}
3161
Lukas Tribus67db8df2013-06-23 17:37:13 +02003162/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3163 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3164 */
3165static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3166{
3167#ifdef IP_TOS
3168 if (from.ss_family == AF_INET)
3169 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3170#endif
3171#ifdef IPV6_TCLASS
3172 if (from.ss_family == AF_INET6) {
3173 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3174 /* v4-mapped addresses need IP_TOS */
3175 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3176 else
3177 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3178 }
3179#endif
3180}
3181
Sasha Pachev218f0642014-06-16 12:05:59 -06003182/* Returns the number of characters written to destination,
3183 * -1 on internal error and -2 if no replacement took place.
3184 */
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003185static int http_replace_header(struct my_regex *re, char *dst, uint dst_size, char *val, int len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003186 const char *rep_str)
Sasha Pachev218f0642014-06-16 12:05:59 -06003187{
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003188 if (!regex_exec_match2(re, val, len, MAX_MATCH, pmatch))
Sasha Pachev218f0642014-06-16 12:05:59 -06003189 return -2;
3190
3191 return exp_replace(dst, dst_size, val, rep_str, pmatch);
3192}
3193
3194/* Returns the number of characters written to destination,
3195 * -1 on internal error and -2 if no replacement took place.
3196 */
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003197static 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 +02003198 const char *rep_str)
Sasha Pachev218f0642014-06-16 12:05:59 -06003199{
3200 char* p = val;
3201 char* dst_end = dst + dst_size;
3202 char* dst_p = dst;
3203
3204 for (;;) {
3205 char *p_delim;
Sasha Pachev218f0642014-06-16 12:05:59 -06003206
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003207 /* look for delim. */
3208 p_delim = p;
3209 while (p_delim < p + len && *p_delim != delim)
3210 p_delim++;
Sasha Pachev218f0642014-06-16 12:05:59 -06003211
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003212 if (regex_exec_match2(re, p, p_delim-p, MAX_MATCH, pmatch)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06003213 int replace_n = exp_replace(dst_p, dst_end - dst_p, p, rep_str, pmatch);
3214
3215 if (replace_n < 0)
3216 return -1;
3217
3218 dst_p += replace_n;
3219 } else {
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003220 uint len = p_delim - p;
Sasha Pachev218f0642014-06-16 12:05:59 -06003221
3222 if (dst_p + len >= dst_end)
3223 return -1;
3224
3225 memcpy(dst_p, p, len);
3226 dst_p += len;
3227 }
3228
3229 if (dst_p >= dst_end)
3230 return -1;
3231
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003232 /* end of the replacements. */
3233 if (p_delim >= p + len)
Sasha Pachev218f0642014-06-16 12:05:59 -06003234 break;
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003235
3236 /* Next part. */
3237 *dst_p++ = delim;
3238 p = p_delim + 1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003239 }
3240
3241 return dst_p - dst;
3242}
3243
3244static int http_transform_header(struct session* s, struct http_msg *msg, const char* name, uint name_len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003245 char* buf, struct hdr_idx* idx, struct list *fmt, struct my_regex *re,
Sasha Pachev218f0642014-06-16 12:05:59 -06003246 struct hdr_ctx* ctx, int action)
3247{
3248 ctx->idx = 0;
3249
3250 while (http_find_full_header2(name, name_len, buf, idx, ctx)) {
3251 struct hdr_idx_elem *hdr = idx->v + ctx->idx;
3252 int delta;
3253 char* val = (char*)ctx->line + name_len + 2;
3254 char* val_end = (char*)ctx->line + hdr->len;
Sasha Pachev218f0642014-06-16 12:05:59 -06003255 char* reg_dst_buf;
3256 uint reg_dst_buf_size;
3257 int n_replaced;
3258
Sasha Pachev218f0642014-06-16 12:05:59 -06003259 trash.len = build_logline(s, trash.str, trash.size, fmt);
3260
3261 if (trash.len >= trash.size - 1)
3262 return -1;
3263
3264 reg_dst_buf = trash.str + trash.len + 1;
3265 reg_dst_buf_size = trash.size - trash.len - 1;
3266
3267 switch (action) {
3268 case HTTP_REQ_ACT_REPLACE_VAL:
3269 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003270 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 -06003271 break;
3272 case HTTP_REQ_ACT_REPLACE_HDR:
3273 case HTTP_RES_ACT_REPLACE_HDR:
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003274 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 -06003275 break;
3276 default: /* impossible */
3277 return -1;
3278 }
3279
Sasha Pachev218f0642014-06-16 12:05:59 -06003280 switch (n_replaced) {
3281 case -1: return -1;
3282 case -2: continue;
3283 }
3284
3285 delta = buffer_replace2(msg->chn->buf, val, val_end, reg_dst_buf, n_replaced);
3286
3287 hdr->len += delta;
3288 http_msg_move_end(msg, delta);
3289 }
3290
3291 return 0;
3292}
3293
Willy Tarreau20b0de52012-12-24 15:45:22 +01003294/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003295 * transaction <txn>. Returns the verdict of the first rule that prevents
3296 * further processing of the request (auth, deny, ...), and defaults to
3297 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3298 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3299 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003300 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003301enum rule_result
Willy Tarreau96257ec2012-12-27 10:46:37 +01003302http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003303{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003304 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003305 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003306 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003307 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003308
Willy Tarreauff011f22011-01-06 17:51:27 +01003309 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003310 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003311 continue;
3312
Willy Tarreau96257ec2012-12-27 10:46:37 +01003313 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003314 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003315 int ret;
3316
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003317 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003318 ret = acl_pass(ret);
3319
Willy Tarreauff011f22011-01-06 17:51:27 +01003320 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003321 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003322
3323 if (!ret) /* condition not matched */
3324 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003325 }
3326
Willy Tarreau20b0de52012-12-24 15:45:22 +01003327
Willy Tarreau96257ec2012-12-27 10:46:37 +01003328 switch (rule->action) {
3329 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003330 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003331
3332 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003333 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003334
Willy Tarreauccbcc372012-12-27 12:37:57 +01003335 case HTTP_REQ_ACT_TARPIT:
3336 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003337 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003338
Willy Tarreau96257ec2012-12-27 10:46:37 +01003339 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003340 /* Auth might be performed on regular http-req rules as well as on stats */
3341 auth_realm = rule->arg.auth.realm;
3342 if (!auth_realm) {
3343 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3344 auth_realm = STATS_DEFAULT_REALM;
3345 else
3346 auth_realm = px->id;
3347 }
3348 /* send 401/407 depending on whether we use a proxy or not. We still
3349 * count one error, because normal browsing won't significantly
3350 * increase the counter but brute force attempts will.
3351 */
3352 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3353 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3354 stream_int_retnclose(&s->si[0], &trash);
3355 session_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003356 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003357
Willy Tarreau81499eb2012-12-27 12:19:02 +01003358 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003359 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3360 return HTTP_RULE_RES_BADREQ;
3361 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003362
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003363 case HTTP_REQ_ACT_SET_NICE:
3364 s->task->nice = rule->arg.nice;
3365 break;
3366
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003367 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003368 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003369 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003370 break;
3371
Willy Tarreau51347ed2013-06-11 19:34:13 +02003372 case HTTP_REQ_ACT_SET_MARK:
3373#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003374 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003375 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003376#endif
3377 break;
3378
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003379 case HTTP_REQ_ACT_SET_LOGL:
3380 s->logs.level = rule->arg.loglevel;
3381 break;
3382
Sasha Pachev218f0642014-06-16 12:05:59 -06003383 case HTTP_REQ_ACT_REPLACE_HDR:
3384 case HTTP_REQ_ACT_REPLACE_VAL:
3385 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3386 txn->req.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003387 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003388 return HTTP_RULE_RES_BADREQ;
3389 break;
3390
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003391 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003392 case HTTP_REQ_ACT_SET_HDR:
3393 ctx.idx = 0;
3394 /* remove all occurrences of the header */
3395 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3396 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3397 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003398 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003399 if (rule->action == HTTP_REQ_ACT_DEL_HDR)
3400 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003401 /* now fall through to header addition */
3402
3403 case HTTP_REQ_ACT_ADD_HDR:
3404 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3405 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3406 trash.len = rule->arg.hdr_add.name_len;
3407 trash.str[trash.len++] = ':';
3408 trash.str[trash.len++] = ' ';
3409 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3410 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3411 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003412
3413 case HTTP_REQ_ACT_DEL_ACL:
3414 case HTTP_REQ_ACT_DEL_MAP: {
3415 struct pat_ref *ref;
3416 char *key;
3417 int len;
3418
3419 /* collect reference */
3420 ref = pat_ref_lookup(rule->arg.map.ref);
3421 if (!ref)
3422 continue;
3423
3424 /* collect key */
3425 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3426 key = trash.str;
3427 key[len] = '\0';
3428
3429 /* perform update */
3430 /* returned code: 1=ok, 0=ko */
3431 pat_ref_delete(ref, key);
3432
3433 break;
3434 }
3435
3436 case HTTP_REQ_ACT_ADD_ACL: {
3437 struct pat_ref *ref;
3438 char *key;
3439 struct chunk *trash_key;
3440 int len;
3441
3442 trash_key = get_trash_chunk();
3443
3444 /* collect reference */
3445 ref = pat_ref_lookup(rule->arg.map.ref);
3446 if (!ref)
3447 continue;
3448
3449 /* collect key */
3450 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3451 key = trash_key->str;
3452 key[len] = '\0';
3453
3454 /* perform update */
3455 /* add entry only if it does not already exist */
3456 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003457 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003458
3459 break;
3460 }
3461
3462 case HTTP_REQ_ACT_SET_MAP: {
3463 struct pat_ref *ref;
3464 char *key, *value;
3465 struct chunk *trash_key, *trash_value;
3466 int len;
3467
3468 trash_key = get_trash_chunk();
3469 trash_value = get_trash_chunk();
3470
3471 /* collect reference */
3472 ref = pat_ref_lookup(rule->arg.map.ref);
3473 if (!ref)
3474 continue;
3475
3476 /* collect key */
3477 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3478 key = trash_key->str;
3479 key[len] = '\0';
3480
3481 /* collect value */
3482 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3483 value = trash_value->str;
3484 value[len] = '\0';
3485
3486 /* perform update */
3487 if (pat_ref_find_elt(ref, key) != NULL)
3488 /* update entry if it exists */
3489 pat_ref_set(ref, key, value, NULL);
3490 else
3491 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003492 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003493
3494 break;
3495 }
William Lallemand73025dd2014-04-24 14:38:37 +02003496
3497 case HTTP_REQ_ACT_CUSTOM_CONT:
3498 rule->action_ptr(rule, px, s, txn);
3499 break;
3500
3501 case HTTP_REQ_ACT_CUSTOM_STOP:
3502 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003503 return HTTP_RULE_RES_DONE;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003504 }
3505 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003506
3507 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003508 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003509}
3510
Willy Tarreau71241ab2012-12-27 11:30:54 +01003511
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003512/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3513 * transaction <txn>. Returns the first rule that prevents further processing
3514 * of the response (deny, ...) or NULL if it executed all rules or stopped
3515 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3516 * rule.
3517 */
3518static struct http_res_rule *
3519http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3520{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003521 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003522 struct http_res_rule *rule;
3523 struct hdr_ctx ctx;
3524
3525 list_for_each_entry(rule, rules, list) {
3526 if (rule->action >= HTTP_RES_ACT_MAX)
3527 continue;
3528
3529 /* check optional condition */
3530 if (rule->cond) {
3531 int ret;
3532
3533 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3534 ret = acl_pass(ret);
3535
3536 if (rule->cond->pol == ACL_COND_UNLESS)
3537 ret = !ret;
3538
3539 if (!ret) /* condition not matched */
3540 continue;
3541 }
3542
3543
3544 switch (rule->action) {
3545 case HTTP_RES_ACT_ALLOW:
3546 return NULL; /* "allow" rules are OK */
3547
3548 case HTTP_RES_ACT_DENY:
3549 txn->flags |= TX_SVDENY;
3550 return rule;
3551
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003552 case HTTP_RES_ACT_SET_NICE:
3553 s->task->nice = rule->arg.nice;
3554 break;
3555
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003556 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003557 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003558 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003559 break;
3560
Willy Tarreau51347ed2013-06-11 19:34:13 +02003561 case HTTP_RES_ACT_SET_MARK:
3562#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003563 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003564 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003565#endif
3566 break;
3567
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003568 case HTTP_RES_ACT_SET_LOGL:
3569 s->logs.level = rule->arg.loglevel;
3570 break;
3571
Sasha Pachev218f0642014-06-16 12:05:59 -06003572 case HTTP_RES_ACT_REPLACE_HDR:
3573 case HTTP_RES_ACT_REPLACE_VAL:
3574 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3575 txn->rsp.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003576 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003577 return NULL; /* note: we should report an error here */
3578 break;
3579
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003580 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003581 case HTTP_RES_ACT_SET_HDR:
3582 ctx.idx = 0;
3583 /* remove all occurrences of the header */
3584 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3585 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3586 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3587 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003588 if (rule->action == HTTP_RES_ACT_DEL_HDR)
3589 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003590 /* now fall through to header addition */
3591
3592 case HTTP_RES_ACT_ADD_HDR:
3593 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3594 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3595 trash.len = rule->arg.hdr_add.name_len;
3596 trash.str[trash.len++] = ':';
3597 trash.str[trash.len++] = ' ';
3598 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3599 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3600 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003601
3602 case HTTP_RES_ACT_DEL_ACL:
3603 case HTTP_RES_ACT_DEL_MAP: {
3604 struct pat_ref *ref;
3605 char *key;
3606 int len;
3607
3608 /* collect reference */
3609 ref = pat_ref_lookup(rule->arg.map.ref);
3610 if (!ref)
3611 continue;
3612
3613 /* collect key */
3614 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3615 key = trash.str;
3616 key[len] = '\0';
3617
3618 /* perform update */
3619 /* returned code: 1=ok, 0=ko */
3620 pat_ref_delete(ref, key);
3621
3622 break;
3623 }
3624
3625 case HTTP_RES_ACT_ADD_ACL: {
3626 struct pat_ref *ref;
3627 char *key;
3628 struct chunk *trash_key;
3629 int len;
3630
3631 trash_key = get_trash_chunk();
3632
3633 /* collect reference */
3634 ref = pat_ref_lookup(rule->arg.map.ref);
3635 if (!ref)
3636 continue;
3637
3638 /* collect key */
3639 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3640 key = trash_key->str;
3641 key[len] = '\0';
3642
3643 /* perform update */
3644 /* check if the entry already exists */
3645 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003646 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003647
3648 break;
3649 }
3650
3651 case HTTP_RES_ACT_SET_MAP: {
3652 struct pat_ref *ref;
3653 char *key, *value;
3654 struct chunk *trash_key, *trash_value;
3655 int len;
3656
3657 trash_key = get_trash_chunk();
3658 trash_value = get_trash_chunk();
3659
3660 /* collect reference */
3661 ref = pat_ref_lookup(rule->arg.map.ref);
3662 if (!ref)
3663 continue;
3664
3665 /* collect key */
3666 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3667 key = trash_key->str;
3668 key[len] = '\0';
3669
3670 /* collect value */
3671 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3672 value = trash_value->str;
3673 value[len] = '\0';
3674
3675 /* perform update */
3676 if (pat_ref_find_elt(ref, key) != NULL)
3677 /* update entry if it exists */
3678 pat_ref_set(ref, key, value, NULL);
3679 else
3680 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003681 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003682
3683 break;
3684 }
William Lallemand73025dd2014-04-24 14:38:37 +02003685
3686 case HTTP_RES_ACT_CUSTOM_CONT:
3687 rule->action_ptr(rule, px, s, txn);
3688 break;
3689
3690 case HTTP_RES_ACT_CUSTOM_STOP:
3691 rule->action_ptr(rule, px, s, txn);
3692 return rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003693 }
3694 }
3695
3696 /* we reached the end of the rules, nothing to report */
3697 return NULL;
3698}
3699
3700
Willy Tarreau71241ab2012-12-27 11:30:54 +01003701/* Perform an HTTP redirect based on the information in <rule>. The function
3702 * returns non-zero on success, or zero in case of a, irrecoverable error such
3703 * as too large a request to build a valid response.
3704 */
3705static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3706{
3707 struct http_msg *msg = &txn->req;
3708 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003709 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003710
3711 /* build redirect message */
3712 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003713 case 308:
3714 msg_fmt = HTTP_308;
3715 break;
3716 case 307:
3717 msg_fmt = HTTP_307;
3718 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003719 case 303:
3720 msg_fmt = HTTP_303;
3721 break;
3722 case 301:
3723 msg_fmt = HTTP_301;
3724 break;
3725 case 302:
3726 default:
3727 msg_fmt = HTTP_302;
3728 break;
3729 }
3730
3731 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3732 return 0;
3733
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003734 location = trash.str + trash.len;
3735
Willy Tarreau71241ab2012-12-27 11:30:54 +01003736 switch(rule->type) {
3737 case REDIRECT_TYPE_SCHEME: {
3738 const char *path;
3739 const char *host;
3740 struct hdr_ctx ctx;
3741 int pathlen;
3742 int hostlen;
3743
3744 host = "";
3745 hostlen = 0;
3746 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003747 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003748 host = ctx.line + ctx.val;
3749 hostlen = ctx.vlen;
3750 }
3751
3752 path = http_get_path(txn);
3753 /* build message using path */
3754 if (path) {
3755 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3756 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3757 int qs = 0;
3758 while (qs < pathlen) {
3759 if (path[qs] == '?') {
3760 pathlen = qs;
3761 break;
3762 }
3763 qs++;
3764 }
3765 }
3766 } else {
3767 path = "/";
3768 pathlen = 1;
3769 }
3770
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003771 if (rule->rdr_str) { /* this is an old "redirect" rule */
3772 /* check if we can add scheme + "://" + host + path */
3773 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3774 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003775
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003776 /* add scheme */
3777 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3778 trash.len += rule->rdr_len;
3779 }
3780 else {
3781 /* add scheme with executing log format */
3782 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003783
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003784 /* check if we can add scheme + "://" + host + path */
3785 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3786 return 0;
3787 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003788 /* add "://" */
3789 memcpy(trash.str + trash.len, "://", 3);
3790 trash.len += 3;
3791
3792 /* add host */
3793 memcpy(trash.str + trash.len, host, hostlen);
3794 trash.len += hostlen;
3795
3796 /* add path */
3797 memcpy(trash.str + trash.len, path, pathlen);
3798 trash.len += pathlen;
3799
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003800 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003801 if (trash.len && trash.str[trash.len - 1] != '/' &&
3802 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3803 if (trash.len > trash.size - 5)
3804 return 0;
3805 trash.str[trash.len] = '/';
3806 trash.len++;
3807 }
3808
3809 break;
3810 }
3811 case REDIRECT_TYPE_PREFIX: {
3812 const char *path;
3813 int pathlen;
3814
3815 path = http_get_path(txn);
3816 /* build message using path */
3817 if (path) {
3818 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3819 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3820 int qs = 0;
3821 while (qs < pathlen) {
3822 if (path[qs] == '?') {
3823 pathlen = qs;
3824 break;
3825 }
3826 qs++;
3827 }
3828 }
3829 } else {
3830 path = "/";
3831 pathlen = 1;
3832 }
3833
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003834 if (rule->rdr_str) { /* this is an old "redirect" rule */
3835 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3836 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003837
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003838 /* add prefix. Note that if prefix == "/", we don't want to
3839 * add anything, otherwise it makes it hard for the user to
3840 * configure a self-redirection.
3841 */
3842 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3843 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3844 trash.len += rule->rdr_len;
3845 }
3846 }
3847 else {
3848 /* add prefix with executing log format */
3849 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3850
3851 /* Check length */
3852 if (trash.len + pathlen > trash.size - 4)
3853 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003854 }
3855
3856 /* add path */
3857 memcpy(trash.str + trash.len, path, pathlen);
3858 trash.len += pathlen;
3859
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003860 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003861 if (trash.len && trash.str[trash.len - 1] != '/' &&
3862 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3863 if (trash.len > trash.size - 5)
3864 return 0;
3865 trash.str[trash.len] = '/';
3866 trash.len++;
3867 }
3868
3869 break;
3870 }
3871 case REDIRECT_TYPE_LOCATION:
3872 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003873 if (rule->rdr_str) { /* this is an old "redirect" rule */
3874 if (trash.len + rule->rdr_len > trash.size - 4)
3875 return 0;
3876
3877 /* add location */
3878 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3879 trash.len += rule->rdr_len;
3880 }
3881 else {
3882 /* add location with executing log format */
3883 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003884
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003885 /* Check left length */
3886 if (trash.len > trash.size - 4)
3887 return 0;
3888 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003889 break;
3890 }
3891
3892 if (rule->cookie_len) {
3893 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3894 trash.len += 14;
3895 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3896 trash.len += rule->cookie_len;
3897 memcpy(trash.str + trash.len, "\r\n", 2);
3898 trash.len += 2;
3899 }
3900
3901 /* add end of headers and the keep-alive/close status.
3902 * We may choose to set keep-alive if the Location begins
3903 * with a slash, because the client will come back to the
3904 * same server.
3905 */
3906 txn->status = rule->code;
3907 /* let's log the request time */
3908 s->logs.tv_request = now;
3909
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003910 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003911 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3912 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3913 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3914 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3915 /* keep-alive possible */
3916 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3917 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3918 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3919 trash.len += 30;
3920 } else {
3921 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3922 trash.len += 24;
3923 }
3924 }
3925 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3926 trash.len += 4;
3927 bo_inject(txn->rsp.chn, trash.str, trash.len);
3928 /* "eat" the request */
3929 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02003930 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003931 msg->sov = 0;
3932 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3933 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3934 txn->req.msg_state = HTTP_MSG_CLOSED;
3935 txn->rsp.msg_state = HTTP_MSG_DONE;
3936 } else {
3937 /* keep-alive not possible */
3938 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3939 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3940 trash.len += 29;
3941 } else {
3942 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3943 trash.len += 23;
3944 }
3945 stream_int_retnclose(txn->req.chn->prod, &trash);
3946 txn->req.chn->analysers = 0;
3947 }
3948
3949 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003950 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003951 if (!(s->flags & SN_FINST_MASK))
3952 s->flags |= SN_FINST_R;
3953
3954 return 1;
3955}
3956
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003957/* This stream analyser runs all HTTP request processing which is common to
3958 * frontends and backends, which means blocking ACLs, filters, connection-close,
3959 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003960 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003961 * either needs more data or wants to immediately abort the request (eg: deny,
3962 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003963 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003964int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003965{
Willy Tarreaud787e662009-07-07 10:14:51 +02003966 struct http_txn *txn = &s->txn;
3967 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003968 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003969 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003970 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02003971
Willy Tarreau655dce92009-11-08 13:10:58 +01003972 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003973 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003974 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003975 return 0;
3976 }
3977
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003978 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 +02003979 now_ms, __FUNCTION__,
3980 s,
3981 req,
3982 req->rex, req->wex,
3983 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003984 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003985 req->analysers);
3986
Willy Tarreau65410832014-04-28 21:25:43 +02003987 /* just in case we have some per-backend tracking */
3988 session_inc_be_http_req_ctr(s);
3989
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003990 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003991 if (!LIST_ISEMPTY(&px->http_req_rules)) {
3992 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003993
Willy Tarreau0b748332014-04-29 00:13:29 +02003994 switch (verdict) {
3995 case HTTP_RULE_RES_CONT:
3996 case HTTP_RULE_RES_STOP: /* nothing to do */
3997 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003998
Willy Tarreau0b748332014-04-29 00:13:29 +02003999 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4000 if (txn->flags & TX_CLTARPIT)
4001 goto tarpit;
4002 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004003
Willy Tarreau0b748332014-04-29 00:13:29 +02004004 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4005 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004006
Willy Tarreau0b748332014-04-29 00:13:29 +02004007 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004008 goto done;
4009
Willy Tarreau0b748332014-04-29 00:13:29 +02004010 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4011 goto return_bad_req;
4012 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004013 }
4014
Willy Tarreau52542592014-04-28 18:33:26 +02004015 /* OK at this stage, we know that the request was accepted according to
4016 * the http-request rules, we can check for the stats. Note that the
4017 * URI is detected *before* the req* rules in order not to be affected
4018 * by a possible reqrep, while they are processed *after* so that a
4019 * reqdeny can still block them. This clearly needs to change in 1.6!
4020 */
4021 if (stats_check_uri(s->rep->prod, txn, px)) {
4022 s->target = &http_stats_applet.obj_type;
4023 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
4024 txn->status = 500;
4025 s->logs.tv_request = now;
4026 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004027
Willy Tarreau52542592014-04-28 18:33:26 +02004028 if (!(s->flags & SN_ERR_MASK))
4029 s->flags |= SN_ERR_RESOURCE;
4030 goto return_prx_cond;
4031 }
4032
4033 /* parse the whole stats request and extract the relevant information */
4034 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004035 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4036 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004037
Willy Tarreau0b748332014-04-29 00:13:29 +02004038 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4039 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004040
Willy Tarreau0b748332014-04-29 00:13:29 +02004041 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4042 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004043 }
4044
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004045 /* evaluate the req* rules except reqadd */
4046 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004047 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004048 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004049
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004050 if (txn->flags & TX_CLDENY)
4051 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004052
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004053 if (txn->flags & TX_CLTARPIT)
4054 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004055 }
Willy Tarreau06619262006-12-17 08:37:22 +01004056
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004057 /* add request headers from the rule sets in the same order */
4058 list_for_each_entry(wl, &px->req_add, list) {
4059 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004060 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004061 ret = acl_pass(ret);
4062 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4063 ret = !ret;
4064 if (!ret)
4065 continue;
4066 }
4067
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004068 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004069 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004070 }
4071
Willy Tarreau52542592014-04-28 18:33:26 +02004072
4073 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004074 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004075 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004076 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
4077 s->fe->fe_counters.intercepted_req++;
4078
4079 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
4080 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
4081 if (!(s->flags & SN_FINST_MASK))
4082 s->flags |= SN_FINST_R;
4083
Willy Tarreau70730dd2014-04-24 18:06:27 +02004084 /* we may want to compress the stats page */
4085 if (s->fe->comp || s->be->comp)
4086 select_compression_request_header(s, req->buf);
4087
4088 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02004089 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) |
4090 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 +02004091 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004092 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004093
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004094 /* check whether we have some ACLs set to redirect this request */
4095 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004096 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004097 int ret;
4098
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004099 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004100 ret = acl_pass(ret);
4101 if (rule->cond->pol == ACL_COND_UNLESS)
4102 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004103 if (!ret)
4104 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004105 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004106 if (!http_apply_redirect_rule(rule, s, txn))
4107 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004108 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004109 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004110
Willy Tarreau2be39392010-01-03 17:24:51 +01004111 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4112 * If this happens, then the data will not come immediately, so we must
4113 * send all what we have without waiting. Note that due to the small gain
4114 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004115 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004116 * itself once used.
4117 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004118 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004119
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004120 done: /* done with this analyser, continue with next ones that the calling
4121 * points will have set, if any.
4122 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004123 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004124 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4125 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004126 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004127
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004128 tarpit:
4129 /* When a connection is tarpitted, we use the tarpit timeout,
4130 * which may be the same as the connect timeout if unspecified.
4131 * If unset, then set it to zero because we really want it to
4132 * eventually expire. We build the tarpit as an analyser.
4133 */
4134 channel_erase(s->req);
4135
4136 /* wipe the request out so that we can drop the connection early
4137 * if the client closes first.
4138 */
4139 channel_dont_connect(req);
4140 req->analysers = 0; /* remove switching rules etc... */
4141 req->analysers |= AN_REQ_HTTP_TARPIT;
4142 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4143 if (!req->analyse_exp)
4144 req->analyse_exp = tick_add(now_ms, 0);
4145 session_inc_http_err_ctr(s);
4146 s->fe->fe_counters.denied_req++;
4147 if (s->fe != s->be)
4148 s->be->be_counters.denied_req++;
4149 if (s->listener->counters)
4150 s->listener->counters->denied_req++;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004151 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004152
4153 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004154 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004155 txn->status = 403;
4156 s->logs.tv_request = now;
4157 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4158 session_inc_http_err_ctr(s);
4159 s->fe->fe_counters.denied_req++;
4160 if (s->fe != s->be)
4161 s->be->be_counters.denied_req++;
4162 if (s->listener->counters)
4163 s->listener->counters->denied_req++;
4164 goto return_prx_cond;
4165
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004166 return_bad_req:
4167 /* We centralize bad requests processing here */
4168 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4169 /* we detected a parsing error. We want to archive this request
4170 * in the dedicated proxy area for later troubleshooting.
4171 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004172 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004173 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004174
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004175 txn->req.msg_state = HTTP_MSG_ERROR;
4176 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004177 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004178
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004179 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004180 if (s->listener->counters)
4181 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004182
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004183 return_prx_cond:
4184 if (!(s->flags & SN_ERR_MASK))
4185 s->flags |= SN_ERR_PRXCOND;
4186 if (!(s->flags & SN_FINST_MASK))
4187 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004188
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004189 req->analysers = 0;
4190 req->analyse_exp = TICK_ETERNITY;
4191 return 0;
4192}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004193
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004194/* This function performs all the processing enabled for the current request.
4195 * It returns 1 if the processing can continue on next analysers, or zero if it
4196 * needs more data, encounters an error, or wants to immediately abort the
4197 * request. It relies on buffers flags, and updates s->req->analysers.
4198 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004199int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004200{
4201 struct http_txn *txn = &s->txn;
4202 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004203 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004204
Willy Tarreau655dce92009-11-08 13:10:58 +01004205 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004206 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004207 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004208 return 0;
4209 }
4210
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004211 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 +02004212 now_ms, __FUNCTION__,
4213 s,
4214 req,
4215 req->rex, req->wex,
4216 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004217 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004218 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004219
William Lallemand82fe75c2012-10-23 10:25:10 +02004220 if (s->fe->comp || s->be->comp)
4221 select_compression_request_header(s, req->buf);
4222
Willy Tarreau59234e92008-11-30 23:51:27 +01004223 /*
4224 * Right now, we know that we have processed the entire headers
4225 * and that unwanted requests have been filtered out. We can do
4226 * whatever we want with the remaining request. Also, now we
4227 * may have separate values for ->fe, ->be.
4228 */
Willy Tarreau06619262006-12-17 08:37:22 +01004229
Willy Tarreau59234e92008-11-30 23:51:27 +01004230 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004231 * If HTTP PROXY is set we simply get remote server address parsing
4232 * incoming request. Note that this requires that a connection is
4233 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004234 */
4235 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004236 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004237 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004238
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004239 /* Note that for now we don't reuse existing proxy connections */
4240 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004241 txn->req.msg_state = HTTP_MSG_ERROR;
4242 txn->status = 500;
4243 req->analysers = 0;
4244 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4245
4246 if (!(s->flags & SN_ERR_MASK))
4247 s->flags |= SN_ERR_RESOURCE;
4248 if (!(s->flags & SN_FINST_MASK))
4249 s->flags |= SN_FINST_R;
4250
4251 return 0;
4252 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004253
4254 path = http_get_path(txn);
4255 url2sa(req->buf->p + msg->sl.rq.u,
4256 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004257 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004258 /* if the path was found, we have to remove everything between
4259 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4260 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4261 * u_l characters by a single "/".
4262 */
4263 if (path) {
4264 char *cur_ptr = req->buf->p;
4265 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4266 int delta;
4267
4268 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4269 http_msg_move_end(&txn->req, delta);
4270 cur_end += delta;
4271 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4272 goto return_bad_req;
4273 }
4274 else {
4275 char *cur_ptr = req->buf->p;
4276 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4277 int delta;
4278
4279 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4280 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4281 http_msg_move_end(&txn->req, delta);
4282 cur_end += delta;
4283 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4284 goto return_bad_req;
4285 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004286 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004287
Willy Tarreau59234e92008-11-30 23:51:27 +01004288 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004289 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004290 * Note that doing so might move headers in the request, but
4291 * the fields will stay coherent and the URI will not move.
4292 * This should only be performed in the backend.
4293 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004294 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004295 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4296 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004297
Willy Tarreau59234e92008-11-30 23:51:27 +01004298 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004299 * 8: the appsession cookie was looked up very early in 1.2,
4300 * so let's do the same now.
4301 */
4302
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004303 /* It needs to look into the URI unless persistence must be ignored */
4304 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004305 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 +01004306 }
4307
William Lallemanda73203e2012-03-12 12:48:57 +01004308 /* add unique-id if "header-unique-id" is specified */
4309
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004310 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4311 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4312 goto return_bad_req;
4313 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004314 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004315 }
William Lallemanda73203e2012-03-12 12:48:57 +01004316
4317 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004318 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4319 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004320 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004321 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004322 goto return_bad_req;
4323 }
4324
Cyril Bontéb21570a2009-11-29 20:04:48 +01004325 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004326 * 9: add X-Forwarded-For if either the frontend or the backend
4327 * asks for it.
4328 */
4329 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004330 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004331 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004332 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4333 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004334 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004335 /* The header is set to be added only if none is present
4336 * and we found it, so don't do anything.
4337 */
4338 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004339 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004340 /* Add an X-Forwarded-For header unless the source IP is
4341 * in the 'except' network range.
4342 */
4343 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004344 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004345 != s->fe->except_net.s_addr) &&
4346 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004347 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004348 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004349 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004350 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004351 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004352
4353 /* Note: we rely on the backend to get the header name to be used for
4354 * x-forwarded-for, because the header is really meant for the backends.
4355 * However, if the backend did not specify any option, we have to rely
4356 * on the frontend's header name.
4357 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004358 if (s->be->fwdfor_hdr_len) {
4359 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004360 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004361 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004362 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004363 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004364 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004365 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 +01004366
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004367 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004368 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004369 }
4370 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004371 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004372 /* FIXME: for the sake of completeness, we should also support
4373 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004374 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004375 int len;
4376 char pn[INET6_ADDRSTRLEN];
4377 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004378 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004379 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004380
Willy Tarreau59234e92008-11-30 23:51:27 +01004381 /* Note: we rely on the backend to get the header name to be used for
4382 * x-forwarded-for, because the header is really meant for the backends.
4383 * However, if the backend did not specify any option, we have to rely
4384 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004385 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004386 if (s->be->fwdfor_hdr_len) {
4387 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004388 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004389 } else {
4390 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004391 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004392 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004393 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004394
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004395 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004396 goto return_bad_req;
4397 }
4398 }
4399
4400 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004401 * 10: add X-Original-To if either the frontend or the backend
4402 * asks for it.
4403 */
4404 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4405
4406 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004407 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004408 /* Add an X-Original-To header unless the destination IP is
4409 * in the 'except' network range.
4410 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004411 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004412
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004413 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004414 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004415 (((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 +02004416 != s->fe->except_to.s_addr) &&
4417 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004418 (((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 +02004419 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004420 int len;
4421 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004422 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004423
4424 /* Note: we rely on the backend to get the header name to be used for
4425 * x-original-to, because the header is really meant for the backends.
4426 * However, if the backend did not specify any option, we have to rely
4427 * on the frontend's header name.
4428 */
4429 if (s->be->orgto_hdr_len) {
4430 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004431 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004432 } else {
4433 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004434 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004435 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004436 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 +02004437
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004438 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004439 goto return_bad_req;
4440 }
4441 }
4442 }
4443
Willy Tarreau50fc7772012-11-11 22:19:57 +01004444 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4445 * If an "Upgrade" token is found, the header is left untouched in order not to have
4446 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4447 * "Upgrade" is present in the Connection header.
4448 */
4449 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4450 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004451 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4452 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004453 unsigned int want_flags = 0;
4454
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004455 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004456 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004457 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4458 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004459 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004460 want_flags |= TX_CON_CLO_SET;
4461 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004462 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004463 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4464 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004465 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004466 want_flags |= TX_CON_KAL_SET;
4467 }
4468
4469 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004470 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004471 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004472
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004473
Willy Tarreau522d6c02009-12-06 18:49:18 +01004474 /* If we have no server assigned yet and we're balancing on url_param
4475 * with a POST request, we may be interested in checking the body for
4476 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004477 */
4478 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4479 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004480 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004481 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004482 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004483 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004484
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004485 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004486 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004487#ifdef TCP_QUICKACK
4488 /* We expect some data from the client. Unless we know for sure
4489 * we already have a full request, we have to re-enable quick-ack
4490 * in case we previously disabled it, otherwise we might cause
4491 * the client to delay further data.
4492 */
4493 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004494 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004495 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004496 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004497 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004498#endif
4499 }
Willy Tarreau03945942009-12-22 16:50:27 +01004500
Willy Tarreau59234e92008-11-30 23:51:27 +01004501 /*************************************************************
4502 * OK, that's finished for the headers. We have done what we *
4503 * could. Let's switch to the DATA state. *
4504 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004505 req->analyse_exp = TICK_ETERNITY;
4506 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004507
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004508 /* if the server closes the connection, we want to immediately react
4509 * and close the socket to save packets and syscalls.
4510 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004511 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4512 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004513
Willy Tarreau59234e92008-11-30 23:51:27 +01004514 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004515 /* OK let's go on with the BODY now */
4516 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004517
Willy Tarreau59234e92008-11-30 23:51:27 +01004518 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004519 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004520 /* we detected a parsing error. We want to archive this request
4521 * in the dedicated proxy area for later troubleshooting.
4522 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004523 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004524 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004525
Willy Tarreau59234e92008-11-30 23:51:27 +01004526 txn->req.msg_state = HTTP_MSG_ERROR;
4527 txn->status = 400;
4528 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004529 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004530
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004531 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004532 if (s->listener->counters)
4533 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004534
Willy Tarreau59234e92008-11-30 23:51:27 +01004535 if (!(s->flags & SN_ERR_MASK))
4536 s->flags |= SN_ERR_PRXCOND;
4537 if (!(s->flags & SN_FINST_MASK))
4538 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004539 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004540}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004541
Willy Tarreau60b85b02008-11-30 23:28:40 +01004542/* This function is an analyser which processes the HTTP tarpit. It always
4543 * returns zero, at the beginning because it prevents any other processing
4544 * from occurring, and at the end because it terminates the request.
4545 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004546int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004547{
4548 struct http_txn *txn = &s->txn;
4549
4550 /* This connection is being tarpitted. The CLIENT side has
4551 * already set the connect expiration date to the right
4552 * timeout. We just have to check that the client is still
4553 * there and that the timeout has not expired.
4554 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004555 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004556 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004557 !tick_is_expired(req->analyse_exp, now_ms))
4558 return 0;
4559
4560 /* We will set the queue timer to the time spent, just for
4561 * logging purposes. We fake a 500 server error, so that the
4562 * attacker will not suspect his connection has been tarpitted.
4563 * It will not cause trouble to the logs because we can exclude
4564 * the tarpitted connections by filtering on the 'PT' status flags.
4565 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004566 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4567
4568 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004569 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004570 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004571
4572 req->analysers = 0;
4573 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004574
Willy Tarreau60b85b02008-11-30 23:28:40 +01004575 if (!(s->flags & SN_ERR_MASK))
4576 s->flags |= SN_ERR_PRXCOND;
4577 if (!(s->flags & SN_FINST_MASK))
4578 s->flags |= SN_FINST_T;
4579 return 0;
4580}
4581
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004582/* This function is an analyser which waits for the HTTP request body. It waits
4583 * for either the buffer to be full, or the full advertised contents to have
4584 * reached the buffer. It must only be called after the standard HTTP request
4585 * processing has occurred, because it expects the request to be parsed and will
4586 * look for the Expect header. It may send a 100-Continue interim response. It
4587 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4588 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4589 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004590 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004591int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004592{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004593 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004594 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004595
4596 /* We have to parse the HTTP request body to find any required data.
4597 * "balance url_param check_post" should have been the only way to get
4598 * into this. We were brought here after HTTP header analysis, so all
4599 * related structures are ready.
4600 */
4601
Willy Tarreau890988f2014-04-10 11:59:33 +02004602 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4603 /* This is the first call */
4604 if (msg->msg_state < HTTP_MSG_BODY)
4605 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004606
Willy Tarreau890988f2014-04-10 11:59:33 +02004607 if (msg->msg_state < HTTP_MSG_100_SENT) {
4608 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4609 * send an HTTP/1.1 100 Continue intermediate response.
4610 */
4611 if (msg->flags & HTTP_MSGF_VER_11) {
4612 struct hdr_ctx ctx;
4613 ctx.idx = 0;
4614 /* Expect is allowed in 1.1, look for it */
4615 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4616 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4617 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4618 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004619 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004620 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004621 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004622
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004623 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004624 * req->buf->p still points to the beginning of the message. We
4625 * must save the body in msg->next because it survives buffer
4626 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004627 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004628 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004629
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004630 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004631 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4632 else
4633 msg->msg_state = HTTP_MSG_DATA;
4634 }
4635
Willy Tarreau890988f2014-04-10 11:59:33 +02004636 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4637 /* We're in content-length mode, we just have to wait for enough data. */
4638 if (req->buf->i - msg->sov < msg->body_len)
4639 goto missing_data;
4640
4641 /* OK we have everything we need now */
4642 goto http_end;
4643 }
4644
4645 /* OK here we're parsing a chunked-encoded message */
4646
Willy Tarreau522d6c02009-12-06 18:49:18 +01004647 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004648 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004649 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004650 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004651 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004652 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004653
Willy Tarreau115acb92009-12-26 13:56:06 +01004654 if (!ret)
4655 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004656 else if (ret < 0) {
4657 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004658 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004659 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004660 }
4661
Willy Tarreaud98cf932009-12-27 22:54:55 +01004662 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004663 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004664 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004665 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004666 if (msg->msg_state == HTTP_MSG_TRAILERS)
4667 goto http_end;
4668
Willy Tarreau226071e2014-04-10 11:55:45 +02004669 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004670 goto http_end;
4671
4672 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004673 /* we get here if we need to wait for more data. If the buffer is full,
4674 * we have the maximum we can expect.
4675 */
4676 if (buffer_full(req->buf, global.tune.maxrewrite))
4677 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004678
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004679 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004680 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004681 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004682
4683 if (!(s->flags & SN_ERR_MASK))
4684 s->flags |= SN_ERR_CLITO;
4685 if (!(s->flags & SN_FINST_MASK))
4686 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004687 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004688 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004689
4690 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004691 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004692 /* Not enough data. We'll re-use the http-request
4693 * timeout here. Ideally, we should set the timeout
4694 * relative to the accept() date. We just set the
4695 * request timeout once at the beginning of the
4696 * request.
4697 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004698 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004699 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004700 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004701 return 0;
4702 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004703
4704 http_end:
4705 /* The situation will not evolve, so let's give up on the analysis. */
4706 s->logs.tv_request = now; /* update the request timer to reflect full request */
4707 req->analysers &= ~an_bit;
4708 req->analyse_exp = TICK_ETERNITY;
4709 return 1;
4710
4711 return_bad_req: /* let's centralize all bad requests */
4712 txn->req.msg_state = HTTP_MSG_ERROR;
4713 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004714 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004715
Willy Tarreau79ebac62010-06-07 13:47:49 +02004716 if (!(s->flags & SN_ERR_MASK))
4717 s->flags |= SN_ERR_PRXCOND;
4718 if (!(s->flags & SN_FINST_MASK))
4719 s->flags |= SN_FINST_R;
4720
Willy Tarreau522d6c02009-12-06 18:49:18 +01004721 return_err_msg:
4722 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004723 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004724 if (s->listener->counters)
4725 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004726 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004727}
4728
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004729/* send a server's name with an outgoing request over an established connection.
4730 * Note: this function is designed to be called once the request has been scheduled
4731 * for being forwarded. This is the reason why it rewinds the buffer before
4732 * proceeding.
4733 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004734int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004735
4736 struct hdr_ctx ctx;
4737
Mark Lamourinec2247f02012-01-04 13:02:01 -05004738 char *hdr_name = be->server_id_hdr_name;
4739 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004740 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004741 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004742 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004743
William Lallemandd9e90662012-01-30 17:27:17 +01004744 ctx.idx = 0;
4745
Willy Tarreau211cdec2014-04-17 20:18:08 +02004746 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004747 if (old_o) {
4748 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004749 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004750 txn->req.next += old_o;
4751 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004752 }
4753
Willy Tarreau9b28e032012-10-12 23:49:43 +02004754 old_i = chn->buf->i;
4755 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 -05004756 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004757 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004758 }
4759
4760 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004761 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004762 memcpy(hdr_val, hdr_name, hdr_name_len);
4763 hdr_val += hdr_name_len;
4764 *hdr_val++ = ':';
4765 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004766 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4767 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004768
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004769 if (old_o) {
4770 /* If this was a forwarded request, we must readjust the amount of
4771 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004772 * variations. Note that the current state is >= HTTP_MSG_BODY,
4773 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004774 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004775 old_o += chn->buf->i - old_i;
4776 b_adv(chn->buf, old_o);
4777 txn->req.next -= old_o;
4778 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004779 }
4780
Mark Lamourinec2247f02012-01-04 13:02:01 -05004781 return 0;
4782}
4783
Willy Tarreau610ecce2010-01-04 21:15:02 +01004784/* Terminate current transaction and prepare a new one. This is very tricky
4785 * right now but it works.
4786 */
4787void http_end_txn_clean_session(struct session *s)
4788{
Willy Tarreau068621e2013-12-23 15:11:25 +01004789 int prev_status = s->txn.status;
4790
Willy Tarreau610ecce2010-01-04 21:15:02 +01004791 /* FIXME: We need a more portable way of releasing a backend's and a
4792 * server's connections. We need a safer way to reinitialize buffer
4793 * flags. We also need a more accurate method for computing per-request
4794 * data.
4795 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004796
Willy Tarreau4213a112013-12-15 10:25:42 +01004797 /* unless we're doing keep-alive, we want to quickly close the connection
4798 * to the server.
4799 */
4800 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4801 !si_conn_ready(s->req->cons)) {
4802 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4803 si_shutr(s->req->cons);
4804 si_shutw(s->req->cons);
4805 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004806
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004807 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004808 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004809 if (unlikely(s->srv_conn))
4810 sess_change_server(s, NULL);
4811 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004812
4813 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4814 session_process_counters(s);
4815
4816 if (s->txn.status) {
4817 int n;
4818
4819 n = s->txn.status / 100;
4820 if (n < 1 || n > 5)
4821 n = 0;
4822
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004823 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004824 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004825 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004826 s->fe->fe_counters.p.http.comp_rsp++;
4827 }
Willy Tarreau24657792010-02-26 10:30:28 +01004828 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004829 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004830 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004831 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004832 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004833 s->be->be_counters.p.http.comp_rsp++;
4834 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004835 }
4836
4837 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004838 s->logs.bytes_in -= s->req->buf->i;
4839 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004840
4841 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004842 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004843 !(s->flags & SN_MONITOR) &&
4844 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4845 s->do_log(s);
4846 }
4847
Willy Tarreauc177ea72014-06-25 15:36:04 +02004848 /* stop tracking content-based counters */
4849 session_stop_content_counters(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004850 session_update_time_stats(s);
4851
Willy Tarreau610ecce2010-01-04 21:15:02 +01004852 s->logs.accept_date = date; /* user-visible date for logging */
4853 s->logs.tv_accept = now; /* corrected date for internal use */
4854 tv_zero(&s->logs.tv_request);
4855 s->logs.t_queue = -1;
4856 s->logs.t_connect = -1;
4857 s->logs.t_data = -1;
4858 s->logs.t_close = 0;
4859 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4860 s->logs.srv_queue_size = 0; /* we will get this number soon */
4861
Willy Tarreau9b28e032012-10-12 23:49:43 +02004862 s->logs.bytes_in = s->req->total = s->req->buf->i;
4863 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004864
4865 if (s->pend_pos)
4866 pendconn_free(s->pend_pos);
4867
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004868 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004869 if (s->flags & SN_CURR_SESS) {
4870 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004871 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004872 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004873 if (may_dequeue_tasks(objt_server(s->target), s->be))
4874 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004875 }
4876
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004877 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004878
Willy Tarreau4213a112013-12-15 10:25:42 +01004879 /* only release our endpoint if we don't intend to reuse the
4880 * connection.
4881 */
4882 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4883 !si_conn_ready(s->req->cons)) {
4884 si_release_endpoint(s->req->cons);
4885 }
4886
Willy Tarreau610ecce2010-01-04 21:15:02 +01004887 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004888 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004889 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004890 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004891 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreaub4d05092014-09-01 20:35:55 +02004892 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT|CF_WAKE_CONNECT|CF_WROTE_DATA);
4893 s->rep->flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT|CF_WROTE_DATA);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004894 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 +01004895 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004896
Willy Tarreau610ecce2010-01-04 21:15:02 +01004897 s->txn.meth = 0;
4898 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004899 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004900
4901 if (prev_status == 401 || prev_status == 407) {
4902 /* In HTTP keep-alive mode, if we receive a 401, we still have
4903 * a chance of being able to send the visitor again to the same
4904 * server over the same connection. This is required by some
4905 * broken protocols such as NTLM, and anyway whenever there is
4906 * an opportunity for sending the challenge to the proper place,
4907 * it's better to do it (at least it helps with debugging).
4908 */
4909 s->txn.flags |= TX_PREFER_LAST;
4910 }
4911
Willy Tarreauee55dc02010-06-01 10:56:34 +02004912 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004913 s->req->cons->flags |= SI_FL_INDEP_STR;
4914
Willy Tarreau96e31212011-05-30 18:10:30 +02004915 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004916 s->req->flags |= CF_NEVER_WAIT;
4917 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004918 }
4919
Willy Tarreau610ecce2010-01-04 21:15:02 +01004920 /* if the request buffer is not empty, it means we're
4921 * about to process another request, so send pending
4922 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004923 * Just don't do this if the buffer is close to be full,
4924 * because the request will wait for it to flush a little
4925 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004926 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004927 if (s->req->buf->i) {
4928 if (s->rep->buf->o &&
4929 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4930 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004931 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004932 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004933
4934 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004935 channel_auto_read(s->req);
4936 channel_auto_close(s->req);
4937 channel_auto_read(s->rep);
4938 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004939
Willy Tarreau27375622013-12-17 00:00:28 +01004940 /* we're in keep-alive with an idle connection, monitor it */
4941 si_idle_conn(s->req->cons);
4942
Willy Tarreau342b11c2010-11-24 16:22:09 +01004943 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004944 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004945}
4946
4947
4948/* This function updates the request state machine according to the response
4949 * state machine and buffer flags. It returns 1 if it changes anything (flag
4950 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4951 * it is only used to find when a request/response couple is complete. Both
4952 * this function and its equivalent should loop until both return zero. It
4953 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4954 */
4955int http_sync_req_state(struct session *s)
4956{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004957 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004958 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004959 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004960 unsigned int old_state = txn->req.msg_state;
4961
Willy Tarreau610ecce2010-01-04 21:15:02 +01004962 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4963 return 0;
4964
4965 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004966 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004967 * We can shut the read side unless we want to abort_on_close,
4968 * or we have a POST request. The issue with POST requests is
4969 * that some browsers still send a CRLF after the request, and
4970 * this CRLF must be read so that it does not remain in the kernel
4971 * buffers, otherwise a close could cause an RST on some systems
4972 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004973 * Note that if we're using keep-alive on the client side, we'd
4974 * rather poll now and keep the polling enabled for the whole
4975 * session's life than enabling/disabling it between each
4976 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004977 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004978 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4979 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4980 !(s->be->options & PR_O_ABRT_CLOSE) &&
4981 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004982 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004983
Willy Tarreau40f151a2012-12-20 12:10:09 +01004984 /* if the server closes the connection, we want to immediately react
4985 * and close the socket to save packets and syscalls.
4986 */
4987 chn->cons->flags |= SI_FL_NOHALF;
4988
Willy Tarreau610ecce2010-01-04 21:15:02 +01004989 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4990 goto wait_other_side;
4991
4992 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4993 /* The server has not finished to respond, so we
4994 * don't want to move in order not to upset it.
4995 */
4996 goto wait_other_side;
4997 }
4998
4999 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5000 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005001 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005002 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005003 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005004 goto wait_other_side;
5005 }
5006
5007 /* When we get here, it means that both the request and the
5008 * response have finished receiving. Depending on the connection
5009 * mode, we'll have to wait for the last bytes to leave in either
5010 * direction, and sometimes for a close to be effective.
5011 */
5012
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005013 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5014 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005015 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5016 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005017 }
5018 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5019 /* Option forceclose is set, or either side wants to close,
5020 * let's enforce it now that we're not expecting any new
5021 * data to come. The caller knows the session is complete
5022 * once both states are CLOSED.
5023 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005024 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5025 channel_shutr_now(chn);
5026 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005027 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005028 }
5029 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005030 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5031 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005032 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005033 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5034 channel_auto_read(chn);
5035 txn->req.msg_state = HTTP_MSG_TUNNEL;
5036 chn->flags |= CF_NEVER_WAIT;
5037 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005038 }
5039
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005040 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005041 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005042 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005043
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005044 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005045 txn->req.msg_state = HTTP_MSG_CLOSING;
5046 goto http_msg_closing;
5047 }
5048 else {
5049 txn->req.msg_state = HTTP_MSG_CLOSED;
5050 goto http_msg_closed;
5051 }
5052 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005053 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005054 }
5055
5056 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5057 http_msg_closing:
5058 /* nothing else to forward, just waiting for the output buffer
5059 * to be empty and for the shutw_now to take effect.
5060 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005061 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005062 txn->req.msg_state = HTTP_MSG_CLOSED;
5063 goto http_msg_closed;
5064 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005065 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005066 txn->req.msg_state = HTTP_MSG_ERROR;
5067 goto wait_other_side;
5068 }
5069 }
5070
5071 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5072 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005073 /* see above in MSG_DONE why we only do this in these states */
5074 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5075 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5076 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005077 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005078 goto wait_other_side;
5079 }
5080
5081 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005082 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005083}
5084
5085
5086/* This function updates the response state machine according to the request
5087 * state machine and buffer flags. It returns 1 if it changes anything (flag
5088 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5089 * it is only used to find when a request/response couple is complete. Both
5090 * this function and its equivalent should loop until both return zero. It
5091 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5092 */
5093int http_sync_res_state(struct session *s)
5094{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005095 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005096 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005097 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005098 unsigned int old_state = txn->rsp.msg_state;
5099
Willy Tarreau610ecce2010-01-04 21:15:02 +01005100 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5101 return 0;
5102
5103 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5104 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005105 * still monitor the server connection for a possible close
5106 * while the request is being uploaded, so we don't disable
5107 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005108 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005109 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005110
5111 if (txn->req.msg_state == HTTP_MSG_ERROR)
5112 goto wait_other_side;
5113
5114 if (txn->req.msg_state < HTTP_MSG_DONE) {
5115 /* The client seems to still be sending data, probably
5116 * because we got an error response during an upload.
5117 * We have the choice of either breaking the connection
5118 * or letting it pass through. Let's do the later.
5119 */
5120 goto wait_other_side;
5121 }
5122
5123 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5124 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005125 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005126 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005127 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005128 goto wait_other_side;
5129 }
5130
5131 /* When we get here, it means that both the request and the
5132 * response have finished receiving. Depending on the connection
5133 * mode, we'll have to wait for the last bytes to leave in either
5134 * direction, and sometimes for a close to be effective.
5135 */
5136
5137 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5138 /* Server-close mode : shut read and wait for the request
5139 * side to close its output buffer. The caller will detect
5140 * when we're in DONE and the other is in CLOSED and will
5141 * catch that for the final cleanup.
5142 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005143 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5144 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005145 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005146 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5147 /* Option forceclose is set, or either side wants to close,
5148 * let's enforce it now that we're not expecting any new
5149 * data to come. The caller knows the session is complete
5150 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005151 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005152 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5153 channel_shutr_now(chn);
5154 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005155 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005156 }
5157 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005158 /* The last possible modes are keep-alive and tunnel. Tunnel will
5159 * need to forward remaining data. Keep-alive will need to monitor
5160 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005161 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005162 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005163 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005164 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5165 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005166 }
5167
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005168 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005169 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005170 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005171 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5172 goto http_msg_closing;
5173 }
5174 else {
5175 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5176 goto http_msg_closed;
5177 }
5178 }
5179 goto wait_other_side;
5180 }
5181
5182 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5183 http_msg_closing:
5184 /* nothing else to forward, just waiting for the output buffer
5185 * to be empty and for the shutw_now to take effect.
5186 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005187 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005188 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5189 goto http_msg_closed;
5190 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005191 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005192 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005193 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005194 if (objt_server(s->target))
5195 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005196 goto wait_other_side;
5197 }
5198 }
5199
5200 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5201 http_msg_closed:
5202 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005203 bi_erase(chn);
5204 channel_auto_close(chn);
5205 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005206 goto wait_other_side;
5207 }
5208
5209 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005210 /* We force the response to leave immediately if we're waiting for the
5211 * other side, since there is no pending shutdown to push it out.
5212 */
5213 if (!channel_is_empty(chn))
5214 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005215 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005216}
5217
5218
5219/* Resync the request and response state machines. Return 1 if either state
5220 * changes.
5221 */
5222int http_resync_states(struct session *s)
5223{
5224 struct http_txn *txn = &s->txn;
5225 int old_req_state = txn->req.msg_state;
5226 int old_res_state = txn->rsp.msg_state;
5227
Willy Tarreau610ecce2010-01-04 21:15:02 +01005228 http_sync_req_state(s);
5229 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005230 if (!http_sync_res_state(s))
5231 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005232 if (!http_sync_req_state(s))
5233 break;
5234 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005235
Willy Tarreau610ecce2010-01-04 21:15:02 +01005236 /* OK, both state machines agree on a compatible state.
5237 * There are a few cases we're interested in :
5238 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5239 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5240 * directions, so let's simply disable both analysers.
5241 * - HTTP_MSG_CLOSED on the response only means we must abort the
5242 * request.
5243 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5244 * with server-close mode means we've completed one request and we
5245 * must re-initialize the server connection.
5246 */
5247
5248 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5249 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5250 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5251 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5252 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005253 channel_auto_close(s->req);
5254 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005255 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005256 channel_auto_close(s->rep);
5257 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005258 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005259 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5260 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005261 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005262 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005263 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005264 channel_auto_close(s->rep);
5265 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01005266 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005267 channel_abort(s->req);
5268 channel_auto_close(s->req);
5269 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005270 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005271 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005272 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5273 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005274 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005275 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5276 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5277 /* server-close/keep-alive: terminate this transaction,
5278 * possibly killing the server connection and reinitialize
5279 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005280 */
5281 http_end_txn_clean_session(s);
5282 }
5283
Willy Tarreau610ecce2010-01-04 21:15:02 +01005284 return txn->req.msg_state != old_req_state ||
5285 txn->rsp.msg_state != old_res_state;
5286}
5287
Willy Tarreaud98cf932009-12-27 22:54:55 +01005288/* This function is an analyser which forwards request body (including chunk
5289 * sizes if any). It is called as soon as we must forward, even if we forward
5290 * zero byte. The only situation where it must not be called is when we're in
5291 * tunnel mode and we want to forward till the close. It's used both to forward
5292 * remaining data and to resync after end of body. It expects the msg_state to
5293 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5294 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005295 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005296 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005297 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005298int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005299{
5300 struct http_txn *txn = &s->txn;
5301 struct http_msg *msg = &s->txn.req;
5302
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005303 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5304 return 0;
5305
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005306 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005307 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005308 /* Output closed while we were sending data. We must abort and
5309 * wake the other side up.
5310 */
5311 msg->msg_state = HTTP_MSG_ERROR;
5312 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005313 return 1;
5314 }
5315
Willy Tarreaud98cf932009-12-27 22:54:55 +01005316 /* Note that we don't have to send 100-continue back because we don't
5317 * need the data to complete our job, and it's up to the server to
5318 * decide whether to return 100, 417 or anything else in return of
5319 * an "Expect: 100-continue" header.
5320 */
5321
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005322 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005323 /* we have msg->sov which points to the first byte of message
5324 * body, and req->buf.p still points to the beginning of the
5325 * message. We forward the headers now, as we don't need them
5326 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005327 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005328 b_adv(req->buf, msg->sov);
5329 msg->next -= msg->sov;
5330 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005331
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005332 /* The previous analysers guarantee that the state is somewhere
5333 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5334 * msg->next are always correct.
5335 */
5336 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5337 if (msg->flags & HTTP_MSGF_TE_CHNK)
5338 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5339 else
5340 msg->msg_state = HTTP_MSG_DATA;
5341 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005342 }
5343
Willy Tarreau7ba23542014-04-17 21:50:00 +02005344 /* Some post-connect processing might want us to refrain from starting to
5345 * forward data. Currently, the only reason for this is "balance url_param"
5346 * whichs need to parse/process the request after we've enabled forwarding.
5347 */
5348 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5349 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5350 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005351 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005352 goto missing_data;
5353 }
5354 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5355 }
5356
Willy Tarreau80a92c02014-03-12 10:41:13 +01005357 /* in most states, we should abort in case of early close */
5358 channel_auto_close(req);
5359
Willy Tarreauefdf0942014-04-24 20:08:57 +02005360 if (req->to_forward) {
5361 /* We can't process the buffer's contents yet */
5362 req->flags |= CF_WAKE_WRITE;
5363 goto missing_data;
5364 }
5365
Willy Tarreaud98cf932009-12-27 22:54:55 +01005366 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005367 if (msg->msg_state == HTTP_MSG_DATA) {
5368 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005369 /* we may have some pending data starting at req->buf->p */
5370 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005371 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005372 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005373 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005374 msg->next += msg->chunk_len;
5375 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005376
5377 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005378 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005379 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005380 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005381 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005382 }
5383 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005384 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005385 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005386 * TRAILERS state.
5387 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005388 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005389
Willy Tarreau54d23df2012-10-25 19:04:45 +02005390 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005391 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005392 else if (ret < 0) {
5393 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005394 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005395 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005396 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005397 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005398 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005399 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005400 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005401 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005402 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005403
5404 if (ret == 0)
5405 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005406 else if (ret < 0) {
5407 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005408 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005409 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005410 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005411 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005412 /* we're in MSG_CHUNK_SIZE now */
5413 }
5414 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005415 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005416
5417 if (ret == 0)
5418 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005419 else if (ret < 0) {
5420 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005421 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005422 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005423 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005424 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005425 /* we're in HTTP_MSG_DONE now */
5426 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005427 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005428 int old_state = msg->msg_state;
5429
Willy Tarreau610ecce2010-01-04 21:15:02 +01005430 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005431
5432 /* we may have some pending data starting at req->buf->p
5433 * such as last chunk of data or trailers.
5434 */
5435 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005436 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005437 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005438 msg->next = 0;
5439
Willy Tarreau4fe41902010-06-07 22:27:41 +02005440 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005441 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5442 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005443 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005444 if (http_resync_states(s)) {
5445 /* some state changes occurred, maybe the analyser
5446 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005447 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005448 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005449 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005450 /* request errors are most likely due to
5451 * the server aborting the transfer.
5452 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005453 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005454 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005455 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005456 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005457 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005458 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005459 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005460 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005461
5462 /* If "option abortonclose" is set on the backend, we
5463 * want to monitor the client's connection and forward
5464 * any shutdown notification to the server, which will
5465 * decide whether to close or to go on processing the
5466 * request.
5467 */
5468 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005469 channel_auto_read(req);
5470 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005471 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005472 else if (s->txn.meth == HTTP_METH_POST) {
5473 /* POST requests may require to read extra CRLF
5474 * sent by broken browsers and which could cause
5475 * an RST to be sent upon close on some systems
5476 * (eg: Linux).
5477 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005478 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005479 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005480
Willy Tarreau610ecce2010-01-04 21:15:02 +01005481 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005482 }
5483 }
5484
Willy Tarreaud98cf932009-12-27 22:54:55 +01005485 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005486 /* we may have some pending data starting at req->buf->p */
5487 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005488 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005489 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5490
Willy Tarreaubed410e2014-04-22 08:19:34 +02005491 msg->next = 0;
5492 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5493
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005494 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005495 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005496 if (!(s->flags & SN_ERR_MASK))
5497 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005498 if (!(s->flags & SN_FINST_MASK)) {
5499 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5500 s->flags |= SN_FINST_H;
5501 else
5502 s->flags |= SN_FINST_D;
5503 }
5504
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005505 s->fe->fe_counters.cli_aborts++;
5506 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005507 if (objt_server(s->target))
5508 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005509
5510 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005511 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005512
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005513 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005514 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005515 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005516
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005517 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005518 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005519 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005520 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005521 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005522
Willy Tarreau5c620922011-05-11 19:56:11 +02005523 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005524 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005525 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005526 * modes are already handled by the stream sock layer. We must not do
5527 * this in content-length mode because it could present the MSG_MORE
5528 * flag with the last block of forwarded data, which would cause an
5529 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005530 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005531 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005532 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005533
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005534 return 0;
5535
Willy Tarreaud98cf932009-12-27 22:54:55 +01005536 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005537 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005538 if (s->listener->counters)
5539 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005540
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005541 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005542 /* we may have some pending data starting at req->buf->p */
5543 b_adv(req->buf, msg->next);
5544 msg->next = 0;
5545
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005546 txn->req.msg_state = HTTP_MSG_ERROR;
5547 if (txn->status) {
5548 /* Note: we don't send any error if some data were already sent */
5549 stream_int_retnclose(req->prod, NULL);
5550 } else {
5551 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005552 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005553 }
5554 req->analysers = 0;
5555 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005556
5557 if (!(s->flags & SN_ERR_MASK))
5558 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005559 if (!(s->flags & SN_FINST_MASK)) {
5560 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5561 s->flags |= SN_FINST_H;
5562 else
5563 s->flags |= SN_FINST_D;
5564 }
5565 return 0;
5566
5567 aborted_xfer:
5568 txn->req.msg_state = HTTP_MSG_ERROR;
5569 if (txn->status) {
5570 /* Note: we don't send any error if some data were already sent */
5571 stream_int_retnclose(req->prod, NULL);
5572 } else {
5573 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005574 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005575 }
5576 req->analysers = 0;
5577 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5578
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005579 s->fe->fe_counters.srv_aborts++;
5580 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005581 if (objt_server(s->target))
5582 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005583
5584 if (!(s->flags & SN_ERR_MASK))
5585 s->flags |= SN_ERR_SRVCL;
5586 if (!(s->flags & SN_FINST_MASK)) {
5587 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5588 s->flags |= SN_FINST_H;
5589 else
5590 s->flags |= SN_FINST_D;
5591 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005592 return 0;
5593}
5594
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005595/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5596 * processing can continue on next analysers, or zero if it either needs more
5597 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5598 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5599 * when it has nothing left to do, and may remove any analyser when it wants to
5600 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005601 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005602int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005603{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005604 struct http_txn *txn = &s->txn;
5605 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005606 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005607 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005608 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005609 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005610
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005611 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 +02005612 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005613 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005614 rep,
5615 rep->rex, rep->wex,
5616 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005617 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005618 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005619
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005620 /*
5621 * Now parse the partial (or complete) lines.
5622 * We will check the response syntax, and also join multi-line
5623 * headers. An index of all the lines will be elaborated while
5624 * parsing.
5625 *
5626 * For the parsing, we use a 28 states FSM.
5627 *
5628 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005629 * rep->buf->p = beginning of response
5630 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5631 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005632 * msg->eol = end of current header or line (LF or CRLF)
5633 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005634 */
5635
Willy Tarreau628c40c2014-04-24 19:11:26 +02005636 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005637 /* There's a protected area at the end of the buffer for rewriting
5638 * purposes. We don't want to start to parse the request if the
5639 * protected area is affected, because we may have to move processed
5640 * data later, which is much more complicated.
5641 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005642 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005643 if (unlikely(!channel_reserved(rep))) {
5644 /* some data has still not left the buffer, wake us once that's done */
5645 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5646 goto abort_response;
5647 channel_dont_close(rep);
5648 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005649 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005650 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005651 }
5652
Willy Tarreau379357a2013-06-08 12:55:46 +02005653 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5654 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5655 buffer_slow_realign(rep->buf);
5656
Willy Tarreau9b28e032012-10-12 23:49:43 +02005657 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005658 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005659 }
5660
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005661 /* 1: we might have to print this header in debug mode */
5662 if (unlikely((global.mode & MODE_DEBUG) &&
5663 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005664 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005665 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005666
Willy Tarreau9b28e032012-10-12 23:49:43 +02005667 sol = rep->buf->p;
5668 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005669 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005670
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005671 sol += hdr_idx_first_pos(&txn->hdr_idx);
5672 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005673
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005674 while (cur_idx) {
5675 eol = sol + txn->hdr_idx.v[cur_idx].len;
5676 debug_hdr("srvhdr", s, sol, eol);
5677 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5678 cur_idx = txn->hdr_idx.v[cur_idx].next;
5679 }
5680 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005681
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005682 /*
5683 * Now we quickly check if we have found a full valid response.
5684 * If not so, we check the FD and buffer states before leaving.
5685 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005686 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005687 * responses are checked first.
5688 *
5689 * Depending on whether the client is still there or not, we
5690 * may send an error response back or not. Note that normally
5691 * we should only check for HTTP status there, and check I/O
5692 * errors somewhere else.
5693 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005694
Willy Tarreau655dce92009-11-08 13:10:58 +01005695 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005696 /* Invalid response */
5697 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5698 /* we detected a parsing error. We want to archive this response
5699 * in the dedicated proxy area for later troubleshooting.
5700 */
5701 hdr_response_bad:
5702 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005703 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005704
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005705 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005706 if (objt_server(s->target)) {
5707 objt_server(s->target)->counters.failed_resp++;
5708 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005709 }
Willy Tarreau64648412010-03-05 10:41:54 +01005710 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005711 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005712 rep->analysers = 0;
5713 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005714 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005715 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005716 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005717
5718 if (!(s->flags & SN_ERR_MASK))
5719 s->flags |= SN_ERR_PRXCOND;
5720 if (!(s->flags & SN_FINST_MASK))
5721 s->flags |= SN_FINST_H;
5722
5723 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005724 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005725
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005726 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005727 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005728 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005729 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005730 goto hdr_response_bad;
5731 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005732
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005733 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005734 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005735 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005736 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005737 else if (txn->flags & TX_NOT_FIRST)
5738 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005739
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005740 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005741 if (objt_server(s->target)) {
5742 objt_server(s->target)->counters.failed_resp++;
5743 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005744 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005745
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005746 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005747 rep->analysers = 0;
5748 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005749 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005750 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005751 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005752
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005753 if (!(s->flags & SN_ERR_MASK))
5754 s->flags |= SN_ERR_SRVCL;
5755 if (!(s->flags & SN_FINST_MASK))
5756 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005757 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005758 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005759
Willy Tarreau2a4f5112014-06-23 15:22:31 +02005760 /* read timeout : return a 504 to the client. */
5761 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005762 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005763 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005764 else if (txn->flags & TX_NOT_FIRST)
5765 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005766
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005767 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005768 if (objt_server(s->target)) {
5769 objt_server(s->target)->counters.failed_resp++;
5770 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005771 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005772
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005773 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005774 rep->analysers = 0;
5775 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005776 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005777 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005778 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005779
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005780 if (!(s->flags & SN_ERR_MASK))
5781 s->flags |= SN_ERR_SRVTO;
5782 if (!(s->flags & SN_FINST_MASK))
5783 s->flags |= SN_FINST_H;
5784 return 0;
5785 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005786
Willy Tarreauf003d372012-11-26 13:35:37 +01005787 /* client abort with an abortonclose */
5788 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5789 s->fe->fe_counters.cli_aborts++;
5790 s->be->be_counters.cli_aborts++;
5791 if (objt_server(s->target))
5792 objt_server(s->target)->counters.cli_aborts++;
5793
5794 rep->analysers = 0;
5795 channel_auto_close(rep);
5796
5797 txn->status = 400;
5798 bi_erase(rep);
5799 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5800
5801 if (!(s->flags & SN_ERR_MASK))
5802 s->flags |= SN_ERR_CLICL;
5803 if (!(s->flags & SN_FINST_MASK))
5804 s->flags |= SN_FINST_H;
5805
5806 /* process_session() will take care of the error */
5807 return 0;
5808 }
5809
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005810 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005811 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005812 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005813 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005814 else if (txn->flags & TX_NOT_FIRST)
5815 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005816
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005817 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005818 if (objt_server(s->target)) {
5819 objt_server(s->target)->counters.failed_resp++;
5820 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005821 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005822
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005823 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005824 rep->analysers = 0;
5825 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005826 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005827 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005828 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005829
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005830 if (!(s->flags & SN_ERR_MASK))
5831 s->flags |= SN_ERR_SRVCL;
5832 if (!(s->flags & SN_FINST_MASK))
5833 s->flags |= SN_FINST_H;
5834 return 0;
5835 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005836
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005837 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005838 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005839 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005840 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005841 else if (txn->flags & TX_NOT_FIRST)
5842 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005843
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005844 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005845 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005846 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005847
5848 if (!(s->flags & SN_ERR_MASK))
5849 s->flags |= SN_ERR_CLICL;
5850 if (!(s->flags & SN_FINST_MASK))
5851 s->flags |= SN_FINST_H;
5852
5853 /* process_session() will take care of the error */
5854 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005855 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005856
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005857 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005858 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005859 return 0;
5860 }
5861
5862 /* More interesting part now : we know that we have a complete
5863 * response which at least looks like HTTP. We have an indicator
5864 * of each header's length, so we can parse them quickly.
5865 */
5866
5867 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005868 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005869
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005870 /*
5871 * 1: get the status code
5872 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005873 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005874 if (n < 1 || n > 5)
5875 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005876 /* when the client triggers a 4xx from the server, it's most often due
5877 * to a missing object or permission. These events should be tracked
5878 * because if they happen often, it may indicate a brute force or a
5879 * vulnerability scan.
5880 */
5881 if (n == 4)
5882 session_inc_http_err_ctr(s);
5883
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005884 if (objt_server(s->target))
5885 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005886
Willy Tarreau5b154472009-12-21 20:11:07 +01005887 /* check if the response is HTTP/1.1 or above */
5888 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005889 ((rep->buf->p[5] > '1') ||
5890 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005891 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005892
5893 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005894 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 +01005895
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005896 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005897 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005898
Willy Tarreau9b28e032012-10-12 23:49:43 +02005899 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005900
Willy Tarreau39650402010-03-15 19:44:39 +01005901 /* Adjust server's health based on status code. Note: status codes 501
5902 * and 505 are triggered on demand by client request, so we must not
5903 * count them as server failures.
5904 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005905 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005906 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005907 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005908 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005909 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005910 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005911
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005912 /*
5913 * 2: check for cacheability.
5914 */
5915
5916 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005917 case 100:
5918 /*
5919 * We may be facing a 100-continue response, in which case this
5920 * is not the right response, and we're waiting for the next one.
5921 * Let's allow this response to go to the client and wait for the
5922 * next one.
5923 */
5924 hdr_idx_init(&txn->hdr_idx);
5925 msg->next -= channel_forward(rep, msg->next);
5926 msg->msg_state = HTTP_MSG_RPBEFORE;
5927 txn->status = 0;
5928 s->logs.t_data = -1; /* was not a response yet */
5929 goto next_one;
5930
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005931 case 200:
5932 case 203:
5933 case 206:
5934 case 300:
5935 case 301:
5936 case 410:
5937 /* RFC2616 @13.4:
5938 * "A response received with a status code of
5939 * 200, 203, 206, 300, 301 or 410 MAY be stored
5940 * by a cache (...) unless a cache-control
5941 * directive prohibits caching."
5942 *
5943 * RFC2616 @9.5: POST method :
5944 * "Responses to this method are not cacheable,
5945 * unless the response includes appropriate
5946 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005947 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005948 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005949 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005950 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5951 break;
5952 default:
5953 break;
5954 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005955
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005956 /*
5957 * 3: we may need to capture headers
5958 */
5959 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005960 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005961 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005962 txn->rsp.cap, s->fe->rsp_cap);
5963
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005964 /* 4: determine the transfer-length.
5965 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5966 * the presence of a message-body in a RESPONSE and its transfer length
5967 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005968 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005969 * All responses to the HEAD request method MUST NOT include a
5970 * message-body, even though the presence of entity-header fields
5971 * might lead one to believe they do. All 1xx (informational), 204
5972 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5973 * message-body. All other responses do include a message-body,
5974 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005975 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005976 * 1. Any response which "MUST NOT" include a message-body (such as the
5977 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5978 * always terminated by the first empty line after the header fields,
5979 * regardless of the entity-header fields present in the message.
5980 *
5981 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5982 * the "chunked" transfer-coding (Section 6.2) is used, the
5983 * transfer-length is defined by the use of this transfer-coding.
5984 * If a Transfer-Encoding header field is present and the "chunked"
5985 * transfer-coding is not present, the transfer-length is defined by
5986 * the sender closing the connection.
5987 *
5988 * 3. If a Content-Length header field is present, its decimal value in
5989 * OCTETs represents both the entity-length and the transfer-length.
5990 * If a message is received with both a Transfer-Encoding header
5991 * field and a Content-Length header field, the latter MUST be ignored.
5992 *
5993 * 4. If the message uses the media type "multipart/byteranges", and
5994 * the transfer-length is not otherwise specified, then this self-
5995 * delimiting media type defines the transfer-length. This media
5996 * type MUST NOT be used unless the sender knows that the recipient
5997 * can parse it; the presence in a request of a Range header with
5998 * multiple byte-range specifiers from a 1.1 client implies that the
5999 * client can parse multipart/byteranges responses.
6000 *
6001 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006002 */
6003
6004 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006005 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006006 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006007 * FIXME: should we parse anyway and return an error on chunked encoding ?
6008 */
6009 if (txn->meth == HTTP_METH_HEAD ||
6010 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006011 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006012 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006013 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006014 goto skip_content_length;
6015 }
6016
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006017 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006018 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006019 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006020 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006021 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006022 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6023 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006024 /* bad transfer-encoding (chunked followed by something else) */
6025 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006026 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006027 break;
6028 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006029 }
6030
6031 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
6032 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006033 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006034 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006035 signed long long cl;
6036
Willy Tarreauad14f752011-09-02 20:33:27 +02006037 if (!ctx.vlen) {
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 Tarreauad14f752011-09-02 20:33:27 +02006042 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &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; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006045 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006046
Willy Tarreauad14f752011-09-02 20:33:27 +02006047 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006048 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006049 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006050 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006051
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006052 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006053 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006054 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006055 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006056
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006057 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006058 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006059 }
6060
William Lallemand82fe75c2012-10-23 10:25:10 +02006061 if (s->fe->comp || s->be->comp)
6062 select_compression_response_header(s, rep->buf);
6063
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006064skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006065 /* Now we have to check if we need to modify the Connection header.
6066 * This is more difficult on the response than it is on the request,
6067 * because we can have two different HTTP versions and we don't know
6068 * how the client will interprete a response. For instance, let's say
6069 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6070 * HTTP/1.1 response without any header. Maybe it will bound itself to
6071 * HTTP/1.0 because it only knows about it, and will consider the lack
6072 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6073 * the lack of header as a keep-alive. Thus we will use two flags
6074 * indicating how a request MAY be understood by the client. In case
6075 * of multiple possibilities, we'll fix the header to be explicit. If
6076 * ambiguous cases such as both close and keepalive are seen, then we
6077 * will fall back to explicit close. Note that we won't take risks with
6078 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006079 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006080 */
6081
Willy Tarreaudc008c52010-02-01 16:20:08 +01006082 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6083 txn->status == 101)) {
6084 /* Either we've established an explicit tunnel, or we're
6085 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006086 * to understand the next protocols. We have to switch to tunnel
6087 * mode, so that we transfer the request and responses then let
6088 * this protocol pass unmodified. When we later implement specific
6089 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006090 * header which contains information about that protocol for
6091 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006092 */
6093 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6094 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006095 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6096 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006097 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6098 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006099 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006100
Willy Tarreau70dffda2014-01-30 03:07:23 +01006101 /* this situation happens when combining pretend-keepalive with httpclose. */
6102 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006103 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6104 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006105 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6106
Willy Tarreau60466522010-01-18 19:08:45 +01006107 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006108 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006109 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6110 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006111
Willy Tarreau60466522010-01-18 19:08:45 +01006112 /* now adjust header transformations depending on current state */
6113 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6114 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6115 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006116 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006117 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006118 }
Willy Tarreau60466522010-01-18 19:08:45 +01006119 else { /* SCL / KAL */
6120 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006121 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006122 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006123 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006124
Willy Tarreau60466522010-01-18 19:08:45 +01006125 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006126 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006127
Willy Tarreau60466522010-01-18 19:08:45 +01006128 /* Some keep-alive responses are converted to Server-close if
6129 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006130 */
Willy Tarreau60466522010-01-18 19:08:45 +01006131 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6132 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006133 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006134 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006135 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006136 }
6137
Willy Tarreau7959a552013-09-23 16:44:27 +02006138 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006139 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006140
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006141 /* end of job, return OK */
6142 rep->analysers &= ~an_bit;
6143 rep->analyse_exp = TICK_ETERNITY;
6144 channel_auto_close(rep);
6145 return 1;
6146
6147 abort_keep_alive:
6148 /* A keep-alive request to the server failed on a network error.
6149 * The client is required to retry. We need to close without returning
6150 * any other information so that the client retries.
6151 */
6152 txn->status = 0;
6153 rep->analysers = 0;
6154 s->req->analysers = 0;
6155 channel_auto_close(rep);
6156 s->logs.logwait = 0;
6157 s->logs.level = 0;
6158 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
6159 bi_erase(rep);
6160 stream_int_retnclose(rep->cons, NULL);
6161 return 0;
6162}
6163
6164/* This function performs all the processing enabled for the current response.
6165 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6166 * and updates s->rep->analysers. It might make sense to explode it into several
6167 * other functions. It works like process_request (see indications above).
6168 */
6169int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6170{
6171 struct http_txn *txn = &s->txn;
6172 struct http_msg *msg = &txn->rsp;
6173 struct proxy *cur_proxy;
6174 struct cond_wordlist *wl;
6175 struct http_res_rule *http_res_last_rule = NULL;
6176
6177 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6178 now_ms, __FUNCTION__,
6179 s,
6180 rep,
6181 rep->rex, rep->wex,
6182 rep->flags,
6183 rep->buf->i,
6184 rep->analysers);
6185
6186 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6187 return 0;
6188
6189 rep->analysers &= ~an_bit;
6190 rep->analyse_exp = TICK_ETERNITY;
6191
Willy Tarreau70730dd2014-04-24 18:06:27 +02006192 /* The stats applet needs to adjust the Connection header but we don't
6193 * apply any filter there.
6194 */
6195 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6196 goto skip_filters;
6197
Willy Tarreau58975672014-04-24 21:13:57 +02006198 /*
6199 * We will have to evaluate the filters.
6200 * As opposed to version 1.2, now they will be evaluated in the
6201 * filters order and not in the header order. This means that
6202 * each filter has to be validated among all headers.
6203 *
6204 * Filters are tried with ->be first, then with ->fe if it is
6205 * different from ->be.
6206 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006207
Willy Tarreau58975672014-04-24 21:13:57 +02006208 cur_proxy = s->be;
6209 while (1) {
6210 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006211
Willy Tarreau58975672014-04-24 21:13:57 +02006212 /* evaluate http-response rules */
6213 if (!http_res_last_rule)
6214 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 +02006215
Willy Tarreau58975672014-04-24 21:13:57 +02006216 /* try headers filters */
6217 if (rule_set->rsp_exp != NULL) {
6218 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6219 return_bad_resp:
6220 if (objt_server(s->target)) {
6221 objt_server(s->target)->counters.failed_resp++;
6222 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006223 }
Willy Tarreau58975672014-04-24 21:13:57 +02006224 s->be->be_counters.failed_resp++;
6225 return_srv_prx_502:
6226 rep->analysers = 0;
6227 txn->status = 502;
6228 s->logs.t_data = -1; /* was not a valid response */
6229 rep->prod->flags |= SI_FL_NOLINGER;
6230 bi_erase(rep);
6231 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6232 if (!(s->flags & SN_ERR_MASK))
6233 s->flags |= SN_ERR_PRXCOND;
6234 if (!(s->flags & SN_FINST_MASK))
6235 s->flags |= SN_FINST_H;
6236 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006237 }
Willy Tarreau58975672014-04-24 21:13:57 +02006238 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006239
Willy Tarreau58975672014-04-24 21:13:57 +02006240 /* has the response been denied ? */
6241 if (txn->flags & TX_SVDENY) {
6242 if (objt_server(s->target))
6243 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006244
Willy Tarreau58975672014-04-24 21:13:57 +02006245 s->be->be_counters.denied_resp++;
6246 s->fe->fe_counters.denied_resp++;
6247 if (s->listener->counters)
6248 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006249
Willy Tarreau58975672014-04-24 21:13:57 +02006250 goto return_srv_prx_502;
6251 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006252
Willy Tarreau58975672014-04-24 21:13:57 +02006253 /* add response headers from the rule sets in the same order */
6254 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006255 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006256 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006257 if (wl->cond) {
6258 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6259 ret = acl_pass(ret);
6260 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6261 ret = !ret;
6262 if (!ret)
6263 continue;
6264 }
6265 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6266 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006267 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006268
Willy Tarreau58975672014-04-24 21:13:57 +02006269 /* check whether we're already working on the frontend */
6270 if (cur_proxy == s->fe)
6271 break;
6272 cur_proxy = s->fe;
6273 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006274
Willy Tarreau58975672014-04-24 21:13:57 +02006275 /* OK that's all we can do for 1xx responses */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006276 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006277 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006278
Willy Tarreau58975672014-04-24 21:13:57 +02006279 /*
6280 * Now check for a server cookie.
6281 */
6282 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6283 (s->be->options & PR_O_CHK_CACHE))
6284 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006285
Willy Tarreau58975672014-04-24 21:13:57 +02006286 /*
6287 * Check for cache-control or pragma headers if required.
6288 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006289 if (((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)) && txn->status != 101)
Willy Tarreau58975672014-04-24 21:13:57 +02006290 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006291
Willy Tarreau58975672014-04-24 21:13:57 +02006292 /*
6293 * Add server cookie in the response if needed
6294 */
6295 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6296 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6297 (!(s->flags & SN_DIRECT) ||
6298 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6299 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6300 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6301 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6302 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6303 !(s->flags & SN_IGNORE_PRST)) {
6304 /* the server is known, it's not the one the client requested, or the
6305 * cookie's last seen date needs to be refreshed. We have to
6306 * insert a set-cookie here, except if we want to insert only on POST
6307 * requests and this one isn't. Note that servers which don't have cookies
6308 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006309 */
Willy Tarreau58975672014-04-24 21:13:57 +02006310 if (!objt_server(s->target)->cookie) {
6311 chunk_printf(&trash,
6312 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6313 s->be->cookie_name);
6314 }
6315 else {
6316 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006317
Willy Tarreau58975672014-04-24 21:13:57 +02006318 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6319 /* emit last_date, which is mandatory */
6320 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6321 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6322 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006323
Willy Tarreau58975672014-04-24 21:13:57 +02006324 if (s->be->cookie_maxlife) {
6325 /* emit first_date, which is either the original one or
6326 * the current date.
6327 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006328 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006329 s30tob64(txn->cookie_first_date ?
6330 txn->cookie_first_date >> 2 :
6331 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006332 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006333 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006334 }
Willy Tarreau58975672014-04-24 21:13:57 +02006335 chunk_appendf(&trash, "; path=/");
6336 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006337
Willy Tarreau58975672014-04-24 21:13:57 +02006338 if (s->be->cookie_domain)
6339 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006340
Willy Tarreau58975672014-04-24 21:13:57 +02006341 if (s->be->ck_opts & PR_CK_HTTPONLY)
6342 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006343
Willy Tarreau58975672014-04-24 21:13:57 +02006344 if (s->be->ck_opts & PR_CK_SECURE)
6345 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006346
Willy Tarreau58975672014-04-24 21:13:57 +02006347 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6348 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006349
Willy Tarreau58975672014-04-24 21:13:57 +02006350 txn->flags &= ~TX_SCK_MASK;
6351 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6352 /* the server did not change, only the date was updated */
6353 txn->flags |= TX_SCK_UPDATED;
6354 else
6355 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006356
Willy Tarreau58975672014-04-24 21:13:57 +02006357 /* Here, we will tell an eventual cache on the client side that we don't
6358 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6359 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6360 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006361 */
Willy Tarreau58975672014-04-24 21:13:57 +02006362 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006363
Willy Tarreau58975672014-04-24 21:13:57 +02006364 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006365
Willy Tarreau58975672014-04-24 21:13:57 +02006366 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6367 "Cache-control: private", 22) < 0))
6368 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006369 }
Willy Tarreau58975672014-04-24 21:13:57 +02006370 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006371
Willy Tarreau58975672014-04-24 21:13:57 +02006372 /*
6373 * Check if result will be cacheable with a cookie.
6374 * We'll block the response if security checks have caught
6375 * nasty things such as a cacheable cookie.
6376 */
6377 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6378 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6379 (s->be->options & PR_O_CHK_CACHE)) {
6380 /* we're in presence of a cacheable response containing
6381 * a set-cookie header. We'll block it as requested by
6382 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006383 */
Willy Tarreau58975672014-04-24 21:13:57 +02006384 if (objt_server(s->target))
6385 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006386
Willy Tarreau58975672014-04-24 21:13:57 +02006387 s->be->be_counters.denied_resp++;
6388 s->fe->fe_counters.denied_resp++;
6389 if (s->listener->counters)
6390 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006391
Willy Tarreau58975672014-04-24 21:13:57 +02006392 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6393 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6394 send_log(s->be, LOG_ALERT,
6395 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6396 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6397 goto return_srv_prx_502;
6398 }
Willy Tarreau03945942009-12-22 16:50:27 +01006399
Willy Tarreau70730dd2014-04-24 18:06:27 +02006400 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006401 /*
6402 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6403 * If an "Upgrade" token is found, the header is left untouched in order
6404 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006405 * if anything but "Upgrade" is present in the Connection header. We don't
6406 * want to touch any 101 response either since it's switching to another
6407 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006408 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006409 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006410 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6411 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6412 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6413 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006414
Willy Tarreau58975672014-04-24 21:13:57 +02006415 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6416 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6417 /* we want a keep-alive response here. Keep-alive header
6418 * required if either side is not 1.1.
6419 */
6420 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6421 want_flags |= TX_CON_KAL_SET;
6422 }
6423 else {
6424 /* we want a close response here. Close header required if
6425 * the server is 1.1, regardless of the client.
6426 */
6427 if (msg->flags & HTTP_MSGF_VER_11)
6428 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006429 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006430
Willy Tarreau58975672014-04-24 21:13:57 +02006431 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6432 http_change_connection_header(txn, msg, want_flags);
6433 }
6434
6435 skip_header_mangling:
6436 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6437 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6438 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006439
Willy Tarreau58975672014-04-24 21:13:57 +02006440 /* if the user wants to log as soon as possible, without counting
6441 * bytes from the server, then this is the right moment. We have
6442 * to temporarily assign bytes_out to log what we currently have.
6443 */
6444 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6445 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6446 s->logs.bytes_out = txn->rsp.eoh;
6447 s->do_log(s);
6448 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006449 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006450 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006451}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006452
Willy Tarreaud98cf932009-12-27 22:54:55 +01006453/* This function is an analyser which forwards response body (including chunk
6454 * sizes if any). It is called as soon as we must forward, even if we forward
6455 * zero byte. The only situation where it must not be called is when we're in
6456 * tunnel mode and we want to forward till the close. It's used both to forward
6457 * remaining data and to resync after end of body. It expects the msg_state to
6458 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6459 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006460 *
6461 * It is capable of compressing response data both in content-length mode and
6462 * in chunked mode. The state machines follows different flows depending on
6463 * whether content-length and chunked modes are used, since there are no
6464 * trailers in content-length :
6465 *
6466 * chk-mode cl-mode
6467 * ,----- BODY -----.
6468 * / \
6469 * V size > 0 V chk-mode
6470 * .--> SIZE -------------> DATA -------------> CRLF
6471 * | | size == 0 | last byte |
6472 * | v final crlf v inspected |
6473 * | TRAILERS -----------> DONE |
6474 * | |
6475 * `----------------------------------------------'
6476 *
6477 * Compression only happens in the DATA state, and must be flushed in final
6478 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6479 * is performed at once on final states for all bytes parsed, or when leaving
6480 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006481 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006482int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006483{
6484 struct http_txn *txn = &s->txn;
6485 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006486 static struct buffer *tmpbuf = NULL;
6487 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006488 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006489
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006490 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6491 return 0;
6492
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006493 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006494 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006495 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006496 /* Output closed while we were sending data. We must abort and
6497 * wake the other side up.
6498 */
6499 msg->msg_state = HTTP_MSG_ERROR;
6500 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006501 return 1;
6502 }
6503
Willy Tarreau4fe41902010-06-07 22:27:41 +02006504 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006505 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006506
Willy Tarreau5bebcd02014-07-10 19:06:10 +02006507 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006508 /* we have msg->sov which points to the first byte of message
6509 * body, and res->buf.p still points to the beginning of the
6510 * message. We forward the headers now, as we don't need them
6511 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006512 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006513 b_adv(res->buf, msg->sov);
6514 msg->next -= msg->sov;
6515 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006516
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006517 /* The previous analysers guarantee that the state is somewhere
6518 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6519 * msg->next are always correct.
6520 */
6521 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6522 if (msg->flags & HTTP_MSGF_TE_CHNK)
6523 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6524 else
6525 msg->msg_state = HTTP_MSG_DATA;
6526 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006527 }
6528
Willy Tarreauefdf0942014-04-24 20:08:57 +02006529 if (res->to_forward) {
6530 /* We can't process the buffer's contents yet */
6531 res->flags |= CF_WAKE_WRITE;
6532 goto missing_data;
6533 }
6534
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006535 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6536 /* We need a compression buffer in the DATA state to put the
6537 * output of compressed data, and in CRLF state to let the
6538 * TRAILERS state finish the job of removing the trailing CRLF.
6539 */
6540 if (unlikely(tmpbuf == NULL)) {
6541 /* this is the first time we need the compression buffer */
6542 tmpbuf = pool_alloc2(pool2_buffer);
6543 if (tmpbuf == NULL)
6544 goto aborted_xfer; /* no memory */
6545 }
6546
6547 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006548 if (ret < 0) {
6549 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006550 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006551 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006552 compressing = 1;
6553 }
6554
Willy Tarreaud98cf932009-12-27 22:54:55 +01006555 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006556 switch (msg->msg_state - HTTP_MSG_DATA) {
6557 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006558 /* we may have some pending data starting at res->buf->p */
6559 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006560 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006561 if (ret < 0)
6562 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006563
Willy Tarreaud5a67832014-04-21 10:54:27 +02006564 if (msg->chunk_len) {
6565 /* input empty or output full */
6566 if (res->buf->i > msg->next)
6567 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006568 goto missing_data;
6569 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006570 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006571 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006572 if (msg->chunk_len > res->buf->i - msg->next) {
6573 /* output full */
6574 res->flags |= CF_WAKE_WRITE;
6575 goto missing_data;
6576 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006577 msg->next += msg->chunk_len;
6578 msg->chunk_len = 0;
6579 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006580
6581 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006582 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006583 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006584 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006585 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006586 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006587 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006588 /* fall through for HTTP_MSG_CHUNK_CRLF */
6589
6590 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6591 /* we want the CRLF after the data */
6592
6593 ret = http_skip_chunk_crlf(msg);
6594 if (ret == 0)
6595 goto missing_data;
6596 else if (ret < 0) {
6597 if (msg->err_pos >= 0)
6598 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6599 goto return_bad_res;
6600 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006601 /* we're in MSG_CHUNK_SIZE now, fall through */
6602
6603 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006604 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006605 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006606 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006607 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006608
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006609 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006610 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006611 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006612 else if (ret < 0) {
6613 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006614 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006615 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006616 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006617 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006618 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006619
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006620 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006621 if (unlikely(compressing)) {
6622 /* we need to flush output contents before syncing FSMs */
6623 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6624 compressing = 0;
6625 }
6626
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006627 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006628 if (ret == 0)
6629 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006630 else if (ret < 0) {
6631 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006632 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006633 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006634 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006635 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006636
6637 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006638 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006639 if (unlikely(compressing)) {
6640 /* we need to flush output contents before syncing FSMs */
6641 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6642 compressing = 0;
6643 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006644
Willy Tarreauc623c172014-04-18 09:53:50 +02006645 /* we may have some pending data starting at res->buf->p
6646 * such as a last chunk of data or trailers.
6647 */
6648 b_adv(res->buf, msg->next);
6649 msg->next = 0;
6650
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006651 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006652 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006653 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6654 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006655 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006656
Willy Tarreau610ecce2010-01-04 21:15:02 +01006657 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006658 /* some state changes occurred, maybe the analyser
6659 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006660 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006661 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006662 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006663 /* response errors are most likely due to
6664 * the client aborting the transfer.
6665 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006666 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006667 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006668 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006669 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006670 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006671 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006672 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006673 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006674 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006675 }
6676 }
6677
Willy Tarreaud98cf932009-12-27 22:54:55 +01006678 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006679 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006680 if (unlikely(compressing)) {
6681 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006682 compressing = 0;
6683 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006684
Willy Tarreauc623c172014-04-18 09:53:50 +02006685 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6686 b_adv(res->buf, msg->next);
6687 msg->next = 0;
6688 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6689 }
6690
Willy Tarreauf003d372012-11-26 13:35:37 +01006691 if (res->flags & CF_SHUTW)
6692 goto aborted_xfer;
6693
6694 /* stop waiting for data if the input is closed before the end. If the
6695 * client side was already closed, it means that the client has aborted,
6696 * so we don't want to count this as a server abort. Otherwise it's a
6697 * server abort.
6698 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006699 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006700 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006701 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006702 if (!(s->flags & SN_ERR_MASK))
6703 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006704 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006705 if (objt_server(s->target))
6706 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006707 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006708 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006709
Willy Tarreau40dba092010-03-04 18:14:51 +01006710 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006711 if (!s->req->analysers)
6712 goto return_bad_res;
6713
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006714 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006715 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006716 * Similarly, with keep-alive on the client side, we don't want to forward a
6717 * close.
6718 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006719 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006720 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6721 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006722 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006723
Willy Tarreau5c620922011-05-11 19:56:11 +02006724 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006725 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006726 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006727 * modes are already handled by the stream sock layer. We must not do
6728 * this in content-length mode because it could present the MSG_MORE
6729 * flag with the last block of forwarded data, which would cause an
6730 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006731 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006732 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006733 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006734
Willy Tarreaud98cf932009-12-27 22:54:55 +01006735 /* the session handler will take care of timeouts and errors */
6736 return 0;
6737
Willy Tarreau40dba092010-03-04 18:14:51 +01006738 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006739 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006740 if (objt_server(s->target))
6741 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006742
6743 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006744 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006745 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006746 compressing = 0;
6747 }
6748
Willy Tarreauc623c172014-04-18 09:53:50 +02006749 /* we may have some pending data starting at res->buf->p */
6750 if (s->comp_algo == NULL) {
6751 b_adv(res->buf, msg->next);
6752 msg->next = 0;
6753 }
6754
Willy Tarreaud98cf932009-12-27 22:54:55 +01006755 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006756 /* don't send any error message as we're in the body */
6757 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006758 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006759 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006760 if (objt_server(s->target))
6761 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006762
6763 if (!(s->flags & SN_ERR_MASK))
6764 s->flags |= SN_ERR_PRXCOND;
6765 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006766 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006767 return 0;
6768
6769 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006770 if (unlikely(compressing)) {
6771 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6772 compressing = 0;
6773 }
6774
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006775 txn->rsp.msg_state = HTTP_MSG_ERROR;
6776 /* don't send any error message as we're in the body */
6777 stream_int_retnclose(res->cons, NULL);
6778 res->analysers = 0;
6779 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6780
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006781 s->fe->fe_counters.cli_aborts++;
6782 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006783 if (objt_server(s->target))
6784 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006785
6786 if (!(s->flags & SN_ERR_MASK))
6787 s->flags |= SN_ERR_CLICL;
6788 if (!(s->flags & SN_FINST_MASK))
6789 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006790 return 0;
6791}
6792
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006793/* Iterate the same filter through all request headers.
6794 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006795 * Since it can manage the switch to another backend, it updates the per-proxy
6796 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006797 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006798int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006799{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006800 char *cur_ptr, *cur_end, *cur_next;
6801 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006802 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006803 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006804 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006805
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006806 last_hdr = 0;
6807
Willy Tarreau9b28e032012-10-12 23:49:43 +02006808 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006809 old_idx = 0;
6810
6811 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006812 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006813 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006814 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006815 (exp->action == ACT_ALLOW ||
6816 exp->action == ACT_DENY ||
6817 exp->action == ACT_TARPIT))
6818 return 0;
6819
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006820 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006821 if (!cur_idx)
6822 break;
6823
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006824 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006825 cur_ptr = cur_next;
6826 cur_end = cur_ptr + cur_hdr->len;
6827 cur_next = cur_end + cur_hdr->cr + 1;
6828
6829 /* Now we have one header between cur_ptr and cur_end,
6830 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006831 */
6832
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02006833 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006834 switch (exp->action) {
6835 case ACT_SETBE:
6836 /* It is not possible to jump a second time.
6837 * FIXME: should we return an HTTP/500 here so that
6838 * the admin knows there's a problem ?
6839 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006840 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006841 break;
6842
6843 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006844 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006845 last_hdr = 1;
6846 break;
6847
6848 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006849 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006850 last_hdr = 1;
6851 break;
6852
6853 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006854 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006855 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006856 break;
6857
6858 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006859 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006860 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006861 break;
6862
6863 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006864 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6865 if (trash.len < 0)
6866 return -1;
6867
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006868 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006869 /* FIXME: if the user adds a newline in the replacement, the
6870 * index will not be recalculated for now, and the new line
6871 * will not be counted as a new header.
6872 */
6873
6874 cur_end += delta;
6875 cur_next += delta;
6876 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006877 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006878 break;
6879
6880 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006881 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006882 cur_next += delta;
6883
Willy Tarreaufa355d42009-11-29 18:12:29 +01006884 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006885 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6886 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006887 cur_hdr->len = 0;
6888 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006889 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006890 break;
6891
6892 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006893 }
6894
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006895 /* keep the link from this header to next one in case of later
6896 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006897 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006898 old_idx = cur_idx;
6899 }
6900 return 0;
6901}
6902
6903
6904/* Apply the filter to the request line.
6905 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6906 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006907 * Since it can manage the switch to another backend, it updates the per-proxy
6908 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006909 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006910int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006911{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006912 char *cur_ptr, *cur_end;
6913 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006914 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006915 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006916
Willy Tarreau3d300592007-03-18 18:34:41 +01006917 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006918 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006919 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006920 (exp->action == ACT_ALLOW ||
6921 exp->action == ACT_DENY ||
6922 exp->action == ACT_TARPIT))
6923 return 0;
6924 else if (exp->action == ACT_REMOVE)
6925 return 0;
6926
6927 done = 0;
6928
Willy Tarreau9b28e032012-10-12 23:49:43 +02006929 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006930 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006931
6932 /* Now we have the request line between cur_ptr and cur_end */
6933
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02006934 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006935 switch (exp->action) {
6936 case ACT_SETBE:
6937 /* It is not possible to jump a second time.
6938 * FIXME: should we return an HTTP/500 here so that
6939 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006940 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006941 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006942 break;
6943
6944 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006945 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006946 done = 1;
6947 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006948
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006949 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006950 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006951 done = 1;
6952 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006953
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006954 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006955 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006956 done = 1;
6957 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006958
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006959 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006960 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006961 done = 1;
6962 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006963
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006964 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006965 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6966 if (trash.len < 0)
6967 return -1;
6968
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006969 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006970 /* FIXME: if the user adds a newline in the replacement, the
6971 * index will not be recalculated for now, and the new line
6972 * will not be counted as a new header.
6973 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006974
Willy Tarreaufa355d42009-11-29 18:12:29 +01006975 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006976 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006977 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006978 HTTP_MSG_RQMETH,
6979 cur_ptr, cur_end + 1,
6980 NULL, NULL);
6981 if (unlikely(!cur_end))
6982 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006983
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006984 /* we have a full request and we know that we have either a CR
6985 * or an LF at <ptr>.
6986 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006987 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6988 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006989 /* there is no point trying this regex on headers */
6990 return 1;
6991 }
6992 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006993 return done;
6994}
Willy Tarreau97de6242006-12-27 17:18:38 +01006995
Willy Tarreau58f10d72006-12-04 02:26:12 +01006996
Willy Tarreau58f10d72006-12-04 02:26:12 +01006997
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006998/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006999 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007000 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007001 * unparsable request. Since it can manage the switch to another backend, it
7002 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007003 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007004int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007005{
Willy Tarreau6c123b12010-01-28 20:22:06 +01007006 struct http_txn *txn = &s->txn;
7007 struct hdr_exp *exp;
7008
7009 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007010 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007011
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007012 /*
7013 * The interleaving of transformations and verdicts
7014 * makes it difficult to decide to continue or stop
7015 * the evaluation.
7016 */
7017
Willy Tarreau6c123b12010-01-28 20:22:06 +01007018 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7019 break;
7020
Willy Tarreau3d300592007-03-18 18:34:41 +01007021 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007022 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007023 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007024 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007025
7026 /* if this filter had a condition, evaluate it now and skip to
7027 * next filter if the condition does not match.
7028 */
7029 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007030 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007031 ret = acl_pass(ret);
7032 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7033 ret = !ret;
7034
7035 if (!ret)
7036 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007037 }
7038
7039 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007040 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007041 if (unlikely(ret < 0))
7042 return -1;
7043
7044 if (likely(ret == 0)) {
7045 /* The filter did not match the request, it can be
7046 * iterated through all headers.
7047 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007048 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007049 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007050 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007051 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007052}
7053
7054
Willy Tarreaua15645d2007-03-18 16:22:39 +01007055
Willy Tarreau58f10d72006-12-04 02:26:12 +01007056/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007057 * Try to retrieve the server associated to the appsession.
7058 * If the server is found, it's assigned to the session.
7059 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007060void manage_client_side_appsession(struct session *s, const char *buf, int len) {
7061 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007062 appsess *asession = NULL;
7063 char *sessid_temp = NULL;
7064
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007065 if (len > s->be->appsession_len) {
7066 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007067 }
7068
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007069 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007070 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007071 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007072 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007073 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007074 }
7075
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007076 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007077 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007078 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007079 return;
7080 }
7081
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007082 memcpy(txn->sessid, buf, len);
7083 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007084 }
7085
7086 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7087 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007088 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007089 return;
7090 }
7091
Cyril Bontéb21570a2009-11-29 20:04:48 +01007092 memcpy(sessid_temp, buf, len);
7093 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007094
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007095 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007096 /* free previously allocated memory */
7097 pool_free2(apools.sessid, sessid_temp);
7098
7099 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007100 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7101 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007102 asession->request_count++;
7103
7104 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007105 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007106
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007107 while (srv) {
7108 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007109 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007110 (s->be->options & PR_O_PERSIST) ||
7111 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007112 /* we found the server and it's usable */
7113 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007114 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007115 s->flags |= SN_DIRECT | SN_ASSIGNED;
7116 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007117
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007118 break;
7119 } else {
7120 txn->flags &= ~TX_CK_MASK;
7121 txn->flags |= TX_CK_DOWN;
7122 }
7123 }
7124 srv = srv->next;
7125 }
7126 }
7127 }
7128}
7129
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007130/* Find the end of a cookie value contained between <s> and <e>. It works the
7131 * same way as with headers above except that the semi-colon also ends a token.
7132 * See RFC2965 for more information. Note that it requires a valid header to
7133 * return a valid result.
7134 */
7135char *find_cookie_value_end(char *s, const char *e)
7136{
7137 int quoted, qdpair;
7138
7139 quoted = qdpair = 0;
7140 for (; s < e; s++) {
7141 if (qdpair) qdpair = 0;
7142 else if (quoted) {
7143 if (*s == '\\') qdpair = 1;
7144 else if (*s == '"') quoted = 0;
7145 }
7146 else if (*s == '"') quoted = 1;
7147 else if (*s == ',' || *s == ';') return s;
7148 }
7149 return s;
7150}
7151
7152/* Delete a value in a header between delimiters <from> and <next> in buffer
7153 * <buf>. The number of characters displaced is returned, and the pointer to
7154 * the first delimiter is updated if required. The function tries as much as
7155 * possible to respect the following principles :
7156 * - replace <from> delimiter by the <next> one unless <from> points to a
7157 * colon, in which case <next> is simply removed
7158 * - set exactly one space character after the new first delimiter, unless
7159 * there are not enough characters in the block being moved to do so.
7160 * - remove unneeded spaces before the previous delimiter and after the new
7161 * one.
7162 *
7163 * It is the caller's responsibility to ensure that :
7164 * - <from> points to a valid delimiter or the colon ;
7165 * - <next> points to a valid delimiter or the final CR/LF ;
7166 * - there are non-space chars before <from> ;
7167 * - there is a CR/LF at or after <next>.
7168 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007169int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007170{
7171 char *prev = *from;
7172
7173 if (*prev == ':') {
7174 /* We're removing the first value, preserve the colon and add a
7175 * space if possible.
7176 */
7177 if (!http_is_crlf[(unsigned char)*next])
7178 next++;
7179 prev++;
7180 if (prev < next)
7181 *prev++ = ' ';
7182
7183 while (http_is_spht[(unsigned char)*next])
7184 next++;
7185 } else {
7186 /* Remove useless spaces before the old delimiter. */
7187 while (http_is_spht[(unsigned char)*(prev-1)])
7188 prev--;
7189 *from = prev;
7190
7191 /* copy the delimiter and if possible a space if we're
7192 * not at the end of the line.
7193 */
7194 if (!http_is_crlf[(unsigned char)*next]) {
7195 *prev++ = *next++;
7196 if (prev + 1 < next)
7197 *prev++ = ' ';
7198 while (http_is_spht[(unsigned char)*next])
7199 next++;
7200 }
7201 }
7202 return buffer_replace2(buf, prev, next, NULL, 0);
7203}
7204
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007205/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007206 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007207 * desirable to call it only when needed. This code is quite complex because
7208 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7209 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007210 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007211void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007212{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007213 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007214 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007215 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007216 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7217 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007218
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007219 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007220 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007221 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007222
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007223 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007224 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007225 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007226
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007227 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007228 hdr_beg = hdr_next;
7229 hdr_end = hdr_beg + cur_hdr->len;
7230 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007231
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007232 /* We have one full header between hdr_beg and hdr_end, and the
7233 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007234 * "Cookie:" headers.
7235 */
7236
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007237 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007238 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007239 old_idx = cur_idx;
7240 continue;
7241 }
7242
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007243 del_from = NULL; /* nothing to be deleted */
7244 preserve_hdr = 0; /* assume we may kill the whole header */
7245
Willy Tarreau58f10d72006-12-04 02:26:12 +01007246 /* Now look for cookies. Conforming to RFC2109, we have to support
7247 * attributes whose name begin with a '$', and associate them with
7248 * the right cookie, if we want to delete this cookie.
7249 * So there are 3 cases for each cookie read :
7250 * 1) it's a special attribute, beginning with a '$' : ignore it.
7251 * 2) it's a server id cookie that we *MAY* want to delete : save
7252 * some pointers on it (last semi-colon, beginning of cookie...)
7253 * 3) it's an application cookie : we *MAY* have to delete a previous
7254 * "special" cookie.
7255 * At the end of loop, if a "special" cookie remains, we may have to
7256 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007257 * *MUST* delete it.
7258 *
7259 * Note: RFC2965 is unclear about the processing of spaces around
7260 * the equal sign in the ATTR=VALUE form. A careful inspection of
7261 * the RFC explicitly allows spaces before it, and not within the
7262 * tokens (attrs or values). An inspection of RFC2109 allows that
7263 * too but section 10.1.3 lets one think that spaces may be allowed
7264 * after the equal sign too, resulting in some (rare) buggy
7265 * implementations trying to do that. So let's do what servers do.
7266 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7267 * allowed quoted strings in values, with any possible character
7268 * after a backslash, including control chars and delimitors, which
7269 * causes parsing to become ambiguous. Browsers also allow spaces
7270 * within values even without quotes.
7271 *
7272 * We have to keep multiple pointers in order to support cookie
7273 * removal at the beginning, middle or end of header without
7274 * corrupting the header. All of these headers are valid :
7275 *
7276 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7277 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7278 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7279 * | | | | | | | | |
7280 * | | | | | | | | hdr_end <--+
7281 * | | | | | | | +--> next
7282 * | | | | | | +----> val_end
7283 * | | | | | +-----------> val_beg
7284 * | | | | +--------------> equal
7285 * | | | +----------------> att_end
7286 * | | +---------------------> att_beg
7287 * | +--------------------------> prev
7288 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007289 */
7290
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007291 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7292 /* Iterate through all cookies on this line */
7293
7294 /* find att_beg */
7295 att_beg = prev + 1;
7296 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7297 att_beg++;
7298
7299 /* find att_end : this is the first character after the last non
7300 * space before the equal. It may be equal to hdr_end.
7301 */
7302 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007303
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007304 while (equal < hdr_end) {
7305 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007306 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007307 if (http_is_spht[(unsigned char)*equal++])
7308 continue;
7309 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007310 }
7311
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007312 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7313 * is between <att_beg> and <equal>, both may be identical.
7314 */
7315
7316 /* look for end of cookie if there is an equal sign */
7317 if (equal < hdr_end && *equal == '=') {
7318 /* look for the beginning of the value */
7319 val_beg = equal + 1;
7320 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7321 val_beg++;
7322
7323 /* find the end of the value, respecting quotes */
7324 next = find_cookie_value_end(val_beg, hdr_end);
7325
7326 /* make val_end point to the first white space or delimitor after the value */
7327 val_end = next;
7328 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7329 val_end--;
7330 } else {
7331 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007332 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007333
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007334 /* We have nothing to do with attributes beginning with '$'. However,
7335 * they will automatically be removed if a header before them is removed,
7336 * since they're supposed to be linked together.
7337 */
7338 if (*att_beg == '$')
7339 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007340
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007341 /* Ignore cookies with no equal sign */
7342 if (equal == next) {
7343 /* This is not our cookie, so we must preserve it. But if we already
7344 * scheduled another cookie for removal, we cannot remove the
7345 * complete header, but we can remove the previous block itself.
7346 */
7347 preserve_hdr = 1;
7348 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007349 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007350 val_end += delta;
7351 next += delta;
7352 hdr_end += delta;
7353 hdr_next += delta;
7354 cur_hdr->len += delta;
7355 http_msg_move_end(&txn->req, delta);
7356 prev = del_from;
7357 del_from = NULL;
7358 }
7359 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007360 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007361
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007362 /* if there are spaces around the equal sign, we need to
7363 * strip them otherwise we'll get trouble for cookie captures,
7364 * or even for rewrites. Since this happens extremely rarely,
7365 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007366 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007367 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7368 int stripped_before = 0;
7369 int stripped_after = 0;
7370
7371 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007372 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007373 equal += stripped_before;
7374 val_beg += stripped_before;
7375 }
7376
7377 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007378 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007379 val_beg += stripped_after;
7380 stripped_before += stripped_after;
7381 }
7382
7383 val_end += stripped_before;
7384 next += stripped_before;
7385 hdr_end += stripped_before;
7386 hdr_next += stripped_before;
7387 cur_hdr->len += stripped_before;
7388 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007389 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007390 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007391
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007392 /* First, let's see if we want to capture this cookie. We check
7393 * that we don't already have a client side cookie, because we
7394 * can only capture one. Also as an optimisation, we ignore
7395 * cookies shorter than the declared name.
7396 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007397 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7398 (val_end - att_beg >= s->fe->capture_namelen) &&
7399 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007400 int log_len = val_end - att_beg;
7401
7402 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7403 Alert("HTTP logging : out of memory.\n");
7404 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007405 if (log_len > s->fe->capture_len)
7406 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007407 memcpy(txn->cli_cookie, att_beg, log_len);
7408 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007409 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007410 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007411
Willy Tarreaubca99692010-10-06 19:25:55 +02007412 /* Persistence cookies in passive, rewrite or insert mode have the
7413 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007414 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007415 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007416 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007417 * For cookies in prefix mode, the form is :
7418 *
7419 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007420 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007421 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7422 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7423 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007424 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007425
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007426 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7427 * have the server ID between val_beg and delim, and the original cookie between
7428 * delim+1 and val_end. Otherwise, delim==val_end :
7429 *
7430 * Cookie: NAME=SRV; # in all but prefix modes
7431 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7432 * | || || | |+-> next
7433 * | || || | +--> val_end
7434 * | || || +---------> delim
7435 * | || |+------------> val_beg
7436 * | || +-------------> att_end = equal
7437 * | |+-----------------> att_beg
7438 * | +------------------> prev
7439 * +-------------------------> hdr_beg
7440 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007441
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007442 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007443 for (delim = val_beg; delim < val_end; delim++)
7444 if (*delim == COOKIE_DELIM)
7445 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007446 } else {
7447 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007448 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007449 /* Now check if the cookie contains a date field, which would
7450 * appear after a vertical bar ('|') just after the server name
7451 * and before the delimiter.
7452 */
7453 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7454 if (vbar1) {
7455 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007456 * right is the last seen date. It is a base64 encoded
7457 * 30-bit value representing the UNIX date since the
7458 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007459 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007460 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007461 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007462 if (val_end - vbar1 >= 5) {
7463 val = b64tos30(vbar1);
7464 if (val > 0)
7465 txn->cookie_last_date = val << 2;
7466 }
7467 /* look for a second vertical bar */
7468 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7469 if (vbar1 && (val_end - vbar1 > 5)) {
7470 val = b64tos30(vbar1 + 1);
7471 if (val > 0)
7472 txn->cookie_first_date = val << 2;
7473 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007474 }
7475 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007476
Willy Tarreauf64d1412010-10-07 20:06:11 +02007477 /* if the cookie has an expiration date and the proxy wants to check
7478 * it, then we do that now. We first check if the cookie is too old,
7479 * then only if it has expired. We detect strict overflow because the
7480 * time resolution here is not great (4 seconds). Cookies with dates
7481 * in the future are ignored if their offset is beyond one day. This
7482 * allows an admin to fix timezone issues without expiring everyone
7483 * and at the same time avoids keeping unwanted side effects for too
7484 * long.
7485 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007486 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7487 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007488 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007489 txn->flags &= ~TX_CK_MASK;
7490 txn->flags |= TX_CK_OLD;
7491 delim = val_beg; // let's pretend we have not found the cookie
7492 txn->cookie_first_date = 0;
7493 txn->cookie_last_date = 0;
7494 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007495 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7496 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007497 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007498 txn->flags &= ~TX_CK_MASK;
7499 txn->flags |= TX_CK_EXPIRED;
7500 delim = val_beg; // let's pretend we have not found the cookie
7501 txn->cookie_first_date = 0;
7502 txn->cookie_last_date = 0;
7503 }
7504
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007505 /* Here, we'll look for the first running server which supports the cookie.
7506 * This allows to share a same cookie between several servers, for example
7507 * to dedicate backup servers to specific servers only.
7508 * However, to prevent clients from sticking to cookie-less backup server
7509 * when they have incidentely learned an empty cookie, we simply ignore
7510 * empty cookies and mark them as invalid.
7511 * The same behaviour is applied when persistence must be ignored.
7512 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007513 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007514 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007515
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007516 while (srv) {
7517 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7518 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007519 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007520 (s->be->options & PR_O_PERSIST) ||
7521 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007522 /* we found the server and we can use it */
7523 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007524 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007525 s->flags |= SN_DIRECT | SN_ASSIGNED;
7526 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007527 break;
7528 } else {
7529 /* we found a server, but it's down,
7530 * mark it as such and go on in case
7531 * another one is available.
7532 */
7533 txn->flags &= ~TX_CK_MASK;
7534 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007535 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007536 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007537 srv = srv->next;
7538 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007539
Willy Tarreauf64d1412010-10-07 20:06:11 +02007540 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007541 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007542 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007543 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007544 txn->flags |= TX_CK_UNUSED;
7545 else
7546 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007547 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007548
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007549 /* depending on the cookie mode, we may have to either :
7550 * - delete the complete cookie if we're in insert+indirect mode, so that
7551 * the server never sees it ;
7552 * - remove the server id from the cookie value, and tag the cookie as an
7553 * application cookie so that it does not get accidentely removed later,
7554 * if we're in cookie prefix mode
7555 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007556 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007557 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007558
Willy Tarreau9b28e032012-10-12 23:49:43 +02007559 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007560 val_end += delta;
7561 next += delta;
7562 hdr_end += delta;
7563 hdr_next += delta;
7564 cur_hdr->len += delta;
7565 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007566
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007567 del_from = NULL;
7568 preserve_hdr = 1; /* we want to keep this cookie */
7569 }
7570 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007571 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007572 del_from = prev;
7573 }
7574 } else {
7575 /* This is not our cookie, so we must preserve it. But if we already
7576 * scheduled another cookie for removal, we cannot remove the
7577 * complete header, but we can remove the previous block itself.
7578 */
7579 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007580
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007581 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007582 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007583 if (att_beg >= del_from)
7584 att_beg += delta;
7585 if (att_end >= del_from)
7586 att_end += delta;
7587 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007588 val_end += delta;
7589 next += delta;
7590 hdr_end += delta;
7591 hdr_next += delta;
7592 cur_hdr->len += delta;
7593 http_msg_move_end(&txn->req, delta);
7594 prev = del_from;
7595 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007596 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007597 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007598
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007599 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007600 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007601 int cmp_len, value_len;
7602 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007603
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007604 if (s->be->options2 & PR_O2_AS_PFX) {
7605 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7606 value_begin = att_beg + s->be->appsession_name_len;
7607 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007608 } else {
7609 cmp_len = att_end - att_beg;
7610 value_begin = val_beg;
7611 value_len = val_end - val_beg;
7612 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007613
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007614 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007615 if (cmp_len == s->be->appsession_name_len &&
7616 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7617 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007618 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007619 }
7620
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007621 /* continue with next cookie on this header line */
7622 att_beg = next;
7623 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007624
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007625 /* There are no more cookies on this line.
7626 * We may still have one (or several) marked for deletion at the
7627 * end of the line. We must do this now in two ways :
7628 * - if some cookies must be preserved, we only delete from the
7629 * mark to the end of line ;
7630 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007631 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007632 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007633 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007634 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007635 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007636 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007637 cur_hdr->len += delta;
7638 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007639 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007640
7641 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007642 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7643 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007644 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007645 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007646 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007647 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007648 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007649 }
7650
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007651 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007652 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007653 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007654}
7655
7656
Willy Tarreaua15645d2007-03-18 16:22:39 +01007657/* Iterate the same filter through all response headers contained in <rtr>.
7658 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7659 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007660int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007661{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007662 char *cur_ptr, *cur_end, *cur_next;
7663 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007664 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007665 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007666 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007667
7668 last_hdr = 0;
7669
Willy Tarreau9b28e032012-10-12 23:49:43 +02007670 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007671 old_idx = 0;
7672
7673 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007674 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007675 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007676 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007677 (exp->action == ACT_ALLOW ||
7678 exp->action == ACT_DENY))
7679 return 0;
7680
7681 cur_idx = txn->hdr_idx.v[old_idx].next;
7682 if (!cur_idx)
7683 break;
7684
7685 cur_hdr = &txn->hdr_idx.v[cur_idx];
7686 cur_ptr = cur_next;
7687 cur_end = cur_ptr + cur_hdr->len;
7688 cur_next = cur_end + cur_hdr->cr + 1;
7689
7690 /* Now we have one header between cur_ptr and cur_end,
7691 * and the next header starts at cur_next.
7692 */
7693
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007694 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007695 switch (exp->action) {
7696 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007697 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007698 last_hdr = 1;
7699 break;
7700
7701 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007702 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007703 last_hdr = 1;
7704 break;
7705
7706 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007707 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7708 if (trash.len < 0)
7709 return -1;
7710
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007711 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007712 /* FIXME: if the user adds a newline in the replacement, the
7713 * index will not be recalculated for now, and the new line
7714 * will not be counted as a new header.
7715 */
7716
7717 cur_end += delta;
7718 cur_next += delta;
7719 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007720 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007721 break;
7722
7723 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007724 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007725 cur_next += delta;
7726
Willy Tarreaufa355d42009-11-29 18:12:29 +01007727 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007728 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7729 txn->hdr_idx.used--;
7730 cur_hdr->len = 0;
7731 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007732 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007733 break;
7734
7735 }
7736 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007737
7738 /* keep the link from this header to next one in case of later
7739 * removal of next header.
7740 */
7741 old_idx = cur_idx;
7742 }
7743 return 0;
7744}
7745
7746
7747/* Apply the filter to the status line in the response buffer <rtr>.
7748 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7749 * or -1 if a replacement resulted in an invalid status line.
7750 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007751int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007752{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007753 char *cur_ptr, *cur_end;
7754 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007755 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007756 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007757
7758
Willy Tarreau3d300592007-03-18 18:34:41 +01007759 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007760 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007761 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007762 (exp->action == ACT_ALLOW ||
7763 exp->action == ACT_DENY))
7764 return 0;
7765 else if (exp->action == ACT_REMOVE)
7766 return 0;
7767
7768 done = 0;
7769
Willy Tarreau9b28e032012-10-12 23:49:43 +02007770 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007771 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007772
7773 /* Now we have the status line between cur_ptr and cur_end */
7774
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007775 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007776 switch (exp->action) {
7777 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007778 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007779 done = 1;
7780 break;
7781
7782 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007783 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007784 done = 1;
7785 break;
7786
7787 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007788 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7789 if (trash.len < 0)
7790 return -1;
7791
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007792 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007793 /* FIXME: if the user adds a newline in the replacement, the
7794 * index will not be recalculated for now, and the new line
7795 * will not be counted as a new header.
7796 */
7797
Willy Tarreaufa355d42009-11-29 18:12:29 +01007798 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007799 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007800 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007801 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007802 cur_ptr, cur_end + 1,
7803 NULL, NULL);
7804 if (unlikely(!cur_end))
7805 return -1;
7806
7807 /* we have a full respnse and we know that we have either a CR
7808 * or an LF at <ptr>.
7809 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007810 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007811 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007812 /* there is no point trying this regex on headers */
7813 return 1;
7814 }
7815 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007816 return done;
7817}
7818
7819
7820
7821/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007822 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007823 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7824 * unparsable response.
7825 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007826int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007827{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007828 struct http_txn *txn = &s->txn;
7829 struct hdr_exp *exp;
7830
7831 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007832 int ret;
7833
7834 /*
7835 * The interleaving of transformations and verdicts
7836 * makes it difficult to decide to continue or stop
7837 * the evaluation.
7838 */
7839
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007840 if (txn->flags & TX_SVDENY)
7841 break;
7842
Willy Tarreau3d300592007-03-18 18:34:41 +01007843 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007844 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7845 exp->action == ACT_PASS)) {
7846 exp = exp->next;
7847 continue;
7848 }
7849
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007850 /* if this filter had a condition, evaluate it now and skip to
7851 * next filter if the condition does not match.
7852 */
7853 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007854 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007855 ret = acl_pass(ret);
7856 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7857 ret = !ret;
7858 if (!ret)
7859 continue;
7860 }
7861
Willy Tarreaua15645d2007-03-18 16:22:39 +01007862 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007863 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007864 if (unlikely(ret < 0))
7865 return -1;
7866
7867 if (likely(ret == 0)) {
7868 /* The filter did not match the response, it can be
7869 * iterated through all headers.
7870 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007871 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7872 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007873 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007874 }
7875 return 0;
7876}
7877
7878
Willy Tarreaua15645d2007-03-18 16:22:39 +01007879/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007880 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007881 * desirable to call it only when needed. This function is also used when we
7882 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007883 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007884void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007885{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007886 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007887 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007888 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007889 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007890 char *hdr_beg, *hdr_end, *hdr_next;
7891 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007892
Willy Tarreaua15645d2007-03-18 16:22:39 +01007893 /* Iterate through the headers.
7894 * we start with the start line.
7895 */
7896 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007897 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007898
7899 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7900 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007901 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007902
7903 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007904 hdr_beg = hdr_next;
7905 hdr_end = hdr_beg + cur_hdr->len;
7906 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007907
Willy Tarreau24581ba2010-08-31 22:39:35 +02007908 /* We have one full header between hdr_beg and hdr_end, and the
7909 * next header starts at hdr_next. We're only interested in
7910 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007911 */
7912
Willy Tarreau24581ba2010-08-31 22:39:35 +02007913 is_cookie2 = 0;
7914 prev = hdr_beg + 10;
7915 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007916 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007917 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7918 if (!val) {
7919 old_idx = cur_idx;
7920 continue;
7921 }
7922 is_cookie2 = 1;
7923 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007924 }
7925
Willy Tarreau24581ba2010-08-31 22:39:35 +02007926 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7927 * <prev> points to the colon.
7928 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007929 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007930
Willy Tarreau24581ba2010-08-31 22:39:35 +02007931 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7932 * check-cache is enabled) and we are not interested in checking
7933 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007934 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007935 if (s->be->cookie_name == NULL &&
7936 s->be->appsession_name == NULL &&
7937 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007938 return;
7939
Willy Tarreau24581ba2010-08-31 22:39:35 +02007940 /* OK so now we know we have to process this response cookie.
7941 * The format of the Set-Cookie header is slightly different
7942 * from the format of the Cookie header in that it does not
7943 * support the comma as a cookie delimiter (thus the header
7944 * cannot be folded) because the Expires attribute described in
7945 * the original Netscape's spec may contain an unquoted date
7946 * with a comma inside. We have to live with this because
7947 * many browsers don't support Max-Age and some browsers don't
7948 * support quoted strings. However the Set-Cookie2 header is
7949 * clean.
7950 *
7951 * We have to keep multiple pointers in order to support cookie
7952 * removal at the beginning, middle or end of header without
7953 * corrupting the header (in case of set-cookie2). A special
7954 * pointer, <scav> points to the beginning of the set-cookie-av
7955 * fields after the first semi-colon. The <next> pointer points
7956 * either to the end of line (set-cookie) or next unquoted comma
7957 * (set-cookie2). All of these headers are valid :
7958 *
7959 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7960 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7961 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7962 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7963 * | | | | | | | | | |
7964 * | | | | | | | | +-> next hdr_end <--+
7965 * | | | | | | | +------------> scav
7966 * | | | | | | +--------------> val_end
7967 * | | | | | +--------------------> val_beg
7968 * | | | | +----------------------> equal
7969 * | | | +------------------------> att_end
7970 * | | +----------------------------> att_beg
7971 * | +------------------------------> prev
7972 * +-----------------------------------------> hdr_beg
7973 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007974
Willy Tarreau24581ba2010-08-31 22:39:35 +02007975 for (; prev < hdr_end; prev = next) {
7976 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007977
Willy Tarreau24581ba2010-08-31 22:39:35 +02007978 /* find att_beg */
7979 att_beg = prev + 1;
7980 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7981 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007982
Willy Tarreau24581ba2010-08-31 22:39:35 +02007983 /* find att_end : this is the first character after the last non
7984 * space before the equal. It may be equal to hdr_end.
7985 */
7986 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007987
Willy Tarreau24581ba2010-08-31 22:39:35 +02007988 while (equal < hdr_end) {
7989 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7990 break;
7991 if (http_is_spht[(unsigned char)*equal++])
7992 continue;
7993 att_end = equal;
7994 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007995
Willy Tarreau24581ba2010-08-31 22:39:35 +02007996 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7997 * is between <att_beg> and <equal>, both may be identical.
7998 */
7999
8000 /* look for end of cookie if there is an equal sign */
8001 if (equal < hdr_end && *equal == '=') {
8002 /* look for the beginning of the value */
8003 val_beg = equal + 1;
8004 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8005 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008006
Willy Tarreau24581ba2010-08-31 22:39:35 +02008007 /* find the end of the value, respecting quotes */
8008 next = find_cookie_value_end(val_beg, hdr_end);
8009
8010 /* make val_end point to the first white space or delimitor after the value */
8011 val_end = next;
8012 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8013 val_end--;
8014 } else {
8015 /* <equal> points to next comma, semi-colon or EOL */
8016 val_beg = val_end = next = equal;
8017 }
8018
8019 if (next < hdr_end) {
8020 /* Set-Cookie2 supports multiple cookies, and <next> points to
8021 * a colon or semi-colon before the end. So skip all attr-value
8022 * pairs and look for the next comma. For Set-Cookie, since
8023 * commas are permitted in values, skip to the end.
8024 */
8025 if (is_cookie2)
8026 next = find_hdr_value_end(next, hdr_end);
8027 else
8028 next = hdr_end;
8029 }
8030
8031 /* Now everything is as on the diagram above */
8032
8033 /* Ignore cookies with no equal sign */
8034 if (equal == val_end)
8035 continue;
8036
8037 /* If there are spaces around the equal sign, we need to
8038 * strip them otherwise we'll get trouble for cookie captures,
8039 * or even for rewrites. Since this happens extremely rarely,
8040 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008041 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008042 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8043 int stripped_before = 0;
8044 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008045
Willy Tarreau24581ba2010-08-31 22:39:35 +02008046 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008047 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008048 equal += stripped_before;
8049 val_beg += stripped_before;
8050 }
8051
8052 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008053 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008054 val_beg += stripped_after;
8055 stripped_before += stripped_after;
8056 }
8057
8058 val_end += stripped_before;
8059 next += stripped_before;
8060 hdr_end += stripped_before;
8061 hdr_next += stripped_before;
8062 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008063 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008064 }
8065
8066 /* First, let's see if we want to capture this cookie. We check
8067 * that we don't already have a server side cookie, because we
8068 * can only capture one. Also as an optimisation, we ignore
8069 * cookies shorter than the declared name.
8070 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008071 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008072 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008073 (val_end - att_beg >= s->fe->capture_namelen) &&
8074 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008075 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008076 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008077 Alert("HTTP logging : out of memory.\n");
8078 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008079 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008080 if (log_len > s->fe->capture_len)
8081 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008082 memcpy(txn->srv_cookie, att_beg, log_len);
8083 txn->srv_cookie[log_len] = 0;
8084 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008085 }
8086
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008087 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008088 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008089 if (!(s->flags & SN_IGNORE_PRST) &&
8090 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8091 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008092 /* assume passive cookie by default */
8093 txn->flags &= ~TX_SCK_MASK;
8094 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008095
8096 /* If the cookie is in insert mode on a known server, we'll delete
8097 * this occurrence because we'll insert another one later.
8098 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008099 * a direct access.
8100 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008101 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008102 /* The "preserve" flag was set, we don't want to touch the
8103 * server's cookie.
8104 */
8105 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008106 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8107 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008108 /* this cookie must be deleted */
8109 if (*prev == ':' && next == hdr_end) {
8110 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008111 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008112 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8113 txn->hdr_idx.used--;
8114 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008115 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008116 hdr_next += delta;
8117 http_msg_move_end(&txn->rsp, delta);
8118 /* note: while both invalid now, <next> and <hdr_end>
8119 * are still equal, so the for() will stop as expected.
8120 */
8121 } else {
8122 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008123 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008124 next = prev;
8125 hdr_end += delta;
8126 hdr_next += delta;
8127 cur_hdr->len += delta;
8128 http_msg_move_end(&txn->rsp, delta);
8129 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008130 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008131 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008132 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008133 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008134 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008135 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008136 * with this server since we know it.
8137 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008138 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008139 next += delta;
8140 hdr_end += delta;
8141 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008142 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008143 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008144
Willy Tarreauf1348312010-10-07 15:54:11 +02008145 txn->flags &= ~TX_SCK_MASK;
8146 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008147 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008148 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008149 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008150 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008151 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008152 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008153 next += delta;
8154 hdr_end += delta;
8155 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008156 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008157 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008158
Willy Tarreau827aee92011-03-10 16:55:02 +01008159 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008160 txn->flags &= ~TX_SCK_MASK;
8161 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008162 }
8163 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008164 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008165 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008166 int cmp_len, value_len;
8167 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008168
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008169 if (s->be->options2 & PR_O2_AS_PFX) {
8170 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8171 value_begin = att_beg + s->be->appsession_name_len;
8172 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008173 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008174 cmp_len = att_end - att_beg;
8175 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008176 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008177 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008178
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008179 if ((cmp_len == s->be->appsession_name_len) &&
8180 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008181 /* free a possibly previously allocated memory */
8182 pool_free2(apools.sessid, txn->sessid);
8183
Cyril Bontéb21570a2009-11-29 20:04:48 +01008184 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008185 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008186 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008187 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008188 return;
8189 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008190 memcpy(txn->sessid, value_begin, value_len);
8191 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008192 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008193 }
8194 /* that's done for this cookie, check the next one on the same
8195 * line when next != hdr_end (only if is_cookie2).
8196 */
8197 }
8198 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008199 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008200 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008201
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008202 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008203 appsess *asession = NULL;
8204 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008205 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008206 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008207 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008208 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8209 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008210 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008211 return;
8212 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008213 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8214
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008215 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8216 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008217 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8218 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008219 return;
8220 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008221 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8222 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008223
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008224 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008225 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008226 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008227 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8228 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008229 return;
8230 }
8231 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008232 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008233
8234 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008235 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008236 }
8237
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008238 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008239 asession->request_count++;
8240 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008241}
8242
8243
Willy Tarreaua15645d2007-03-18 16:22:39 +01008244/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008245 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008246 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008247void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008248{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008249 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008250 char *p1, *p2;
8251
8252 char *cur_ptr, *cur_end, *cur_next;
8253 int cur_idx;
8254
Willy Tarreau5df51872007-11-25 16:20:08 +01008255 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008256 return;
8257
8258 /* Iterate through the headers.
8259 * we start with the start line.
8260 */
8261 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008262 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008263
8264 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8265 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008266 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008267
8268 cur_hdr = &txn->hdr_idx.v[cur_idx];
8269 cur_ptr = cur_next;
8270 cur_end = cur_ptr + cur_hdr->len;
8271 cur_next = cur_end + cur_hdr->cr + 1;
8272
8273 /* We have one full header between cur_ptr and cur_end, and the
8274 * next header starts at cur_next. We're only interested in
8275 * "Cookie:" headers.
8276 */
8277
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008278 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8279 if (val) {
8280 if ((cur_end - (cur_ptr + val) >= 8) &&
8281 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8282 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8283 return;
8284 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008285 }
8286
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008287 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8288 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008289 continue;
8290
8291 /* OK, right now we know we have a cache-control header at cur_ptr */
8292
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008293 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008294
8295 if (p1 >= cur_end) /* no more info */
8296 continue;
8297
8298 /* p1 is at the beginning of the value */
8299 p2 = p1;
8300
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008301 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008302 p2++;
8303
8304 /* we have a complete value between p1 and p2 */
8305 if (p2 < cur_end && *p2 == '=') {
8306 /* we have something of the form no-cache="set-cookie" */
8307 if ((cur_end - p1 >= 21) &&
8308 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8309 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008310 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008311 continue;
8312 }
8313
8314 /* OK, so we know that either p2 points to the end of string or to a comma */
8315 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008316 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008317 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8318 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8319 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008320 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008321 return;
8322 }
8323
8324 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008325 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008326 continue;
8327 }
8328 }
8329}
8330
8331
Willy Tarreau58f10d72006-12-04 02:26:12 +01008332/*
8333 * Try to retrieve a known appsession in the URI, then the associated server.
8334 * If the server is found, it's assigned to the session.
8335 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008336void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008337{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008338 char *end_params, *first_param, *cur_param, *next_param;
8339 char separator;
8340 int value_len;
8341
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008342 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008343
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008344 if (s->be->appsession_name == NULL ||
8345 (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 +01008346 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008347 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008348
Cyril Bontéb21570a2009-11-29 20:04:48 +01008349 first_param = NULL;
8350 switch (mode) {
8351 case PR_O2_AS_M_PP:
8352 first_param = memchr(begin, ';', len);
8353 break;
8354 case PR_O2_AS_M_QS:
8355 first_param = memchr(begin, '?', len);
8356 break;
8357 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008358
Cyril Bontéb21570a2009-11-29 20:04:48 +01008359 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008360 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008361 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008362
Cyril Bontéb21570a2009-11-29 20:04:48 +01008363 switch (mode) {
8364 case PR_O2_AS_M_PP:
8365 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8366 end_params = (char *) begin + len;
8367 }
8368 separator = ';';
8369 break;
8370 case PR_O2_AS_M_QS:
8371 end_params = (char *) begin + len;
8372 separator = '&';
8373 break;
8374 default:
8375 /* unknown mode, shouldn't happen */
8376 return;
8377 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008378
Cyril Bontéb21570a2009-11-29 20:04:48 +01008379 cur_param = next_param = end_params;
8380 while (cur_param > first_param) {
8381 cur_param--;
8382 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8383 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008384 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8385 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8386 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008387 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008388 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8389 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008390 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008391 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008392 }
8393 break;
8394 }
8395 next_param = cur_param;
8396 }
8397 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008398#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008399 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008400 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008401#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008402}
8403
Willy Tarreaub2513902006-12-17 14:52:38 +01008404/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008405 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008406 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008407 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008408 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008409 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008410 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008411 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008412 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008413int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008414{
8415 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008416 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008417 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008418
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008419 if (!uri_auth)
8420 return 0;
8421
Cyril Bonté70be45d2010-10-12 00:14:35 +02008422 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008423 return 0;
8424
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008425 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008426 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008427 return 0;
8428
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008429 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008430 return 0;
8431
Willy Tarreaub2513902006-12-17 14:52:38 +01008432 return 1;
8433}
8434
Willy Tarreau4076a152009-04-02 15:18:36 +02008435/*
8436 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008437 * By default it tries to report the error position as msg->err_pos. However if
8438 * this one is not set, it will then report msg->next, which is the last known
8439 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008440 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008441 */
8442void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008443 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008444 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008445{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008446 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008447 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008448
Willy Tarreau9b28e032012-10-12 23:49:43 +02008449 es->len = MIN(chn->buf->i, sizeof(es->buf));
8450 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008451 len1 = MIN(len1, es->len);
8452 len2 = es->len - len1; /* remaining data if buffer wraps */
8453
Willy Tarreau9b28e032012-10-12 23:49:43 +02008454 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008455 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008456 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008457
Willy Tarreau4076a152009-04-02 15:18:36 +02008458 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008459 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008460 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008461 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008462
Willy Tarreau4076a152009-04-02 15:18:36 +02008463 es->when = date; // user-visible date
8464 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008465 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008466 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008467 if (objt_conn(s->req->prod->end))
8468 es->src = __objt_conn(s->req->prod->end)->addr.from;
8469 else
8470 memset(&es->src, 0, sizeof(es->src));
8471
Willy Tarreau078272e2010-12-12 12:46:33 +01008472 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008473 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008474 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008475 es->s_flags = s->flags;
8476 es->t_flags = s->txn.flags;
8477 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008478 es->b_out = chn->buf->o;
8479 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008480 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008481 es->m_clen = msg->chunk_len;
8482 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008483}
Willy Tarreaub2513902006-12-17 14:52:38 +01008484
Willy Tarreau294c4732011-12-16 21:35:50 +01008485/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8486 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8487 * performed over the whole headers. Otherwise it must contain a valid header
8488 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8489 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8490 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8491 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008492 * -1. The value fetch stops at commas, so this function is suited for use with
8493 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008494 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008495 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008496unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008497 struct hdr_idx *idx, int occ,
8498 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008499{
Willy Tarreau294c4732011-12-16 21:35:50 +01008500 struct hdr_ctx local_ctx;
8501 char *ptr_hist[MAX_HDR_HISTORY];
8502 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008503 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008504 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008505
Willy Tarreau294c4732011-12-16 21:35:50 +01008506 if (!ctx) {
8507 local_ctx.idx = 0;
8508 ctx = &local_ctx;
8509 }
8510
Willy Tarreaubce70882009-09-07 11:51:47 +02008511 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008512 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008513 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008514 occ--;
8515 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008516 *vptr = ctx->line + ctx->val;
8517 *vlen = ctx->vlen;
8518 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008519 }
8520 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008521 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008522 }
8523
8524 /* negative occurrence, we scan all the list then walk back */
8525 if (-occ > MAX_HDR_HISTORY)
8526 return 0;
8527
Willy Tarreau294c4732011-12-16 21:35:50 +01008528 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008529 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008530 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8531 len_hist[hist_ptr] = ctx->vlen;
8532 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008533 hist_ptr = 0;
8534 found++;
8535 }
8536 if (-occ > found)
8537 return 0;
8538 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008539 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8540 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8541 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008542 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008543 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008544 if (hist_ptr >= MAX_HDR_HISTORY)
8545 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008546 *vptr = ptr_hist[hist_ptr];
8547 *vlen = len_hist[hist_ptr];
8548 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008549}
8550
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008551/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8552 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8553 * performed over the whole headers. Otherwise it must contain a valid header
8554 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8555 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8556 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8557 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8558 * -1. This function differs from http_get_hdr() in that it only returns full
8559 * line header values and does not stop at commas.
8560 * The return value is 0 if nothing was found, or non-zero otherwise.
8561 */
8562unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8563 struct hdr_idx *idx, int occ,
8564 struct hdr_ctx *ctx, char **vptr, int *vlen)
8565{
8566 struct hdr_ctx local_ctx;
8567 char *ptr_hist[MAX_HDR_HISTORY];
8568 int len_hist[MAX_HDR_HISTORY];
8569 unsigned int hist_ptr;
8570 int found;
8571
8572 if (!ctx) {
8573 local_ctx.idx = 0;
8574 ctx = &local_ctx;
8575 }
8576
8577 if (occ >= 0) {
8578 /* search from the beginning */
8579 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8580 occ--;
8581 if (occ <= 0) {
8582 *vptr = ctx->line + ctx->val;
8583 *vlen = ctx->vlen;
8584 return 1;
8585 }
8586 }
8587 return 0;
8588 }
8589
8590 /* negative occurrence, we scan all the list then walk back */
8591 if (-occ > MAX_HDR_HISTORY)
8592 return 0;
8593
8594 found = hist_ptr = 0;
8595 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8596 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8597 len_hist[hist_ptr] = ctx->vlen;
8598 if (++hist_ptr >= MAX_HDR_HISTORY)
8599 hist_ptr = 0;
8600 found++;
8601 }
8602 if (-occ > found)
8603 return 0;
8604 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8605 * find occurrence -occ, so we have to check [hist_ptr+occ].
8606 */
8607 hist_ptr += occ;
8608 if (hist_ptr >= MAX_HDR_HISTORY)
8609 hist_ptr -= MAX_HDR_HISTORY;
8610 *vptr = ptr_hist[hist_ptr];
8611 *vlen = len_hist[hist_ptr];
8612 return 1;
8613}
8614
Willy Tarreaubaaee002006-06-26 02:48:02 +02008615/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008616 * Print a debug line with a header. Always stop at the first CR or LF char,
8617 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8618 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008619 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008620void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008621{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008622 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008623 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008624 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008625 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8626 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 +02008627
8628 for (max = 0; start + max < end; max++)
8629 if (start[max] == '\r' || start[max] == '\n')
8630 break;
8631
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008632 UBOUND(max, trash.size - trash.len - 3);
8633 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8634 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008635 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008636}
8637
Willy Tarreau0937bc42009-12-22 15:03:09 +01008638/*
8639 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8640 * the required fields are properly allocated and that we only need to (re)init
8641 * them. This should be used before processing any new request.
8642 */
8643void http_init_txn(struct session *s)
8644{
8645 struct http_txn *txn = &s->txn;
8646 struct proxy *fe = s->fe;
8647
8648 txn->flags = 0;
8649 txn->status = -1;
8650
Willy Tarreauf64d1412010-10-07 20:06:11 +02008651 txn->cookie_first_date = 0;
8652 txn->cookie_last_date = 0;
8653
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008654 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008655 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008656 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008657 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008658 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008659 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008660 txn->req.chunk_len = 0LL;
8661 txn->req.body_len = 0LL;
8662 txn->rsp.chunk_len = 0LL;
8663 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008664 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8665 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008666 txn->req.chn = s->req;
8667 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008668
8669 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008670
8671 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8672 if (fe->options2 & PR_O2_REQBUG_OK)
8673 txn->req.err_pos = -1; /* let buggy requests pass */
8674
Willy Tarreau46023632010-01-07 22:51:47 +01008675 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008676 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8677
Willy Tarreau46023632010-01-07 22:51:47 +01008678 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008679 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8680
8681 if (txn->hdr_idx.v)
8682 hdr_idx_init(&txn->hdr_idx);
8683}
8684
8685/* to be used at the end of a transaction */
8686void http_end_txn(struct session *s)
8687{
8688 struct http_txn *txn = &s->txn;
8689
Willy Tarreau75195602014-03-11 15:48:55 +01008690 /* release any possible compression context */
8691 if (s->flags & SN_COMP_READY)
8692 s->comp_algo->end(&s->comp_ctx);
8693 s->comp_algo = NULL;
8694 s->flags &= ~SN_COMP_READY;
8695
Willy Tarreau0937bc42009-12-22 15:03:09 +01008696 /* these ones will have been dynamically allocated */
8697 pool_free2(pool2_requri, txn->uri);
8698 pool_free2(pool2_capture, txn->cli_cookie);
8699 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008700 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008701 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008702
William Lallemanda73203e2012-03-12 12:48:57 +01008703 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008704 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008705 txn->uri = NULL;
8706 txn->srv_cookie = NULL;
8707 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008708
8709 if (txn->req.cap) {
8710 struct cap_hdr *h;
8711 for (h = s->fe->req_cap; h; h = h->next)
8712 pool_free2(h->pool, txn->req.cap[h->index]);
8713 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8714 }
8715
8716 if (txn->rsp.cap) {
8717 struct cap_hdr *h;
8718 for (h = s->fe->rsp_cap; h; h = h->next)
8719 pool_free2(h->pool, txn->rsp.cap[h->index]);
8720 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8721 }
8722
Willy Tarreau0937bc42009-12-22 15:03:09 +01008723}
8724
8725/* to be used at the end of a transaction to prepare a new one */
8726void http_reset_txn(struct session *s)
8727{
8728 http_end_txn(s);
8729 http_init_txn(s);
8730
8731 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008732 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008733 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008734 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008735 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008736 /* re-init store persistence */
8737 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008738 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008739
Willy Tarreau0937bc42009-12-22 15:03:09 +01008740 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008741
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008742 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008743
Willy Tarreau739cfba2010-01-25 23:11:14 +01008744 /* We must trim any excess data from the response buffer, because we
8745 * may have blocked an invalid response from a server that we don't
8746 * want to accidentely forward once we disable the analysers, nor do
8747 * we want those data to come along with next response. A typical
8748 * example of such data would be from a buggy server responding to
8749 * a HEAD with some data, or sending more than the advertised
8750 * content-length.
8751 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008752 if (unlikely(s->rep->buf->i))
8753 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008754
Willy Tarreau0937bc42009-12-22 15:03:09 +01008755 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008756 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008757
Willy Tarreaud04e8582010-05-31 12:31:35 +02008758 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008759 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008760
8761 s->req->rex = TICK_ETERNITY;
8762 s->req->wex = TICK_ETERNITY;
8763 s->req->analyse_exp = TICK_ETERNITY;
8764 s->rep->rex = TICK_ETERNITY;
8765 s->rep->wex = TICK_ETERNITY;
8766 s->rep->analyse_exp = TICK_ETERNITY;
8767}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008768
Sasha Pachev218f0642014-06-16 12:05:59 -06008769void free_http_res_rules(struct list *r)
8770{
8771 struct http_res_rule *tr, *pr;
8772
8773 list_for_each_entry_safe(pr, tr, r, list) {
8774 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008775 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008776 free(pr);
8777 }
8778}
8779
8780void free_http_req_rules(struct list *r)
8781{
Willy Tarreauff011f22011-01-06 17:51:27 +01008782 struct http_req_rule *tr, *pr;
8783
8784 list_for_each_entry_safe(pr, tr, r, list) {
8785 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008786 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008787 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008788
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008789 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008790 free(pr);
8791 }
8792}
8793
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008794/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008795struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8796{
8797 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008798 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008799 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008800 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008801
8802 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8803 if (!rule) {
8804 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008805 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008806 }
8807
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008808 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008809 rule->action = HTTP_REQ_ACT_ALLOW;
8810 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008811 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008812 rule->action = HTTP_REQ_ACT_DENY;
8813 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008814 } else if (!strcmp(args[0], "tarpit")) {
8815 rule->action = HTTP_REQ_ACT_TARPIT;
8816 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008817 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008818 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008819 cur_arg = 1;
8820
8821 while(*args[cur_arg]) {
8822 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008823 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008824 cur_arg+=2;
8825 continue;
8826 } else
8827 break;
8828 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008829 } else if (!strcmp(args[0], "set-nice")) {
8830 rule->action = HTTP_REQ_ACT_SET_NICE;
8831 cur_arg = 1;
8832
8833 if (!*args[cur_arg] ||
8834 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8835 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8836 file, linenum, args[0]);
8837 goto out_err;
8838 }
8839 rule->arg.nice = atoi(args[cur_arg]);
8840 if (rule->arg.nice < -1024)
8841 rule->arg.nice = -1024;
8842 else if (rule->arg.nice > 1024)
8843 rule->arg.nice = 1024;
8844 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008845 } else if (!strcmp(args[0], "set-tos")) {
8846#ifdef IP_TOS
8847 char *err;
8848 rule->action = HTTP_REQ_ACT_SET_TOS;
8849 cur_arg = 1;
8850
8851 if (!*args[cur_arg] ||
8852 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8853 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8854 file, linenum, args[0]);
8855 goto out_err;
8856 }
8857
8858 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8859 if (err && *err != '\0') {
8860 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8861 file, linenum, err, args[0]);
8862 goto out_err;
8863 }
8864 cur_arg++;
8865#else
8866 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8867 goto out_err;
8868#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008869 } else if (!strcmp(args[0], "set-mark")) {
8870#ifdef SO_MARK
8871 char *err;
8872 rule->action = HTTP_REQ_ACT_SET_MARK;
8873 cur_arg = 1;
8874
8875 if (!*args[cur_arg] ||
8876 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8877 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8878 file, linenum, args[0]);
8879 goto out_err;
8880 }
8881
8882 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8883 if (err && *err != '\0') {
8884 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8885 file, linenum, err, args[0]);
8886 goto out_err;
8887 }
8888 cur_arg++;
8889 global.last_checks |= LSTCHK_NETADM;
8890#else
8891 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8892 goto out_err;
8893#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008894 } else if (!strcmp(args[0], "set-log-level")) {
8895 rule->action = HTTP_REQ_ACT_SET_LOGL;
8896 cur_arg = 1;
8897
8898 if (!*args[cur_arg] ||
8899 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8900 bad_log_level:
8901 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8902 file, linenum, args[0]);
8903 goto out_err;
8904 }
8905 if (strcmp(args[cur_arg], "silent") == 0)
8906 rule->arg.loglevel = -1;
8907 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8908 goto bad_log_level;
8909 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008910 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8911 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8912 cur_arg = 1;
8913
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008914 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8915 (*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 +01008916 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8917 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008918 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008919 }
8920
8921 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8922 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8923 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008924
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008925 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008926 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008927 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8928 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008929 free(proxy->conf.lfs_file);
8930 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8931 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008932 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008933 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
8934 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008935 cur_arg = 1;
8936
8937 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann8f249f42014-06-24 11:10:00 +02008938 (*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 -06008939 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8940 file, linenum, args[0]);
8941 goto out_err;
8942 }
8943
8944 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8945 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8946 LIST_INIT(&rule->arg.hdr_add.fmt);
8947
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008948 error = NULL;
8949 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8950 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8951 args[cur_arg + 1], error);
8952 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008953 goto out_err;
8954 }
8955
8956 proxy->conf.args.ctx = ARGC_HRQ;
8957 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
8958 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8959 file, linenum);
8960
8961 free(proxy->conf.lfs_file);
8962 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8963 proxy->conf.lfs_line = proxy->conf.args.line;
8964 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008965 } else if (strcmp(args[0], "del-header") == 0) {
8966 rule->action = HTTP_REQ_ACT_DEL_HDR;
8967 cur_arg = 1;
8968
8969 if (!*args[cur_arg] ||
8970 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8971 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8972 file, linenum, args[0]);
8973 goto out_err;
8974 }
8975
8976 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8977 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8978
8979 proxy->conf.args.ctx = ARGC_HRQ;
8980 free(proxy->conf.lfs_file);
8981 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8982 proxy->conf.lfs_line = proxy->conf.args.line;
8983 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008984 } else if (strcmp(args[0], "redirect") == 0) {
8985 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008986 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008987
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008988 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008989 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8990 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8991 goto out_err;
8992 }
8993
8994 /* this redirect rule might already contain a parsed condition which
8995 * we'll pass to the http-request rule.
8996 */
8997 rule->action = HTTP_REQ_ACT_REDIR;
8998 rule->arg.redir = redir;
8999 rule->cond = redir->cond;
9000 redir->cond = NULL;
9001 cur_arg = 2;
9002 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009003 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9004 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9005 rule->action = HTTP_REQ_ACT_ADD_ACL;
9006 /*
9007 * '+ 8' for 'add-acl('
9008 * '- 9' for 'add-acl(' + trailing ')'
9009 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009010 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009011
9012 cur_arg = 1;
9013
9014 if (!*args[cur_arg] ||
9015 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9016 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9017 file, linenum, args[0]);
9018 goto out_err;
9019 }
9020
9021 LIST_INIT(&rule->arg.map.key);
9022 proxy->conf.args.ctx = ARGC_HRQ;
9023 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9024 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9025 file, linenum);
9026 free(proxy->conf.lfs_file);
9027 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9028 proxy->conf.lfs_line = proxy->conf.args.line;
9029 cur_arg += 1;
9030 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9031 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9032 rule->action = HTTP_REQ_ACT_DEL_ACL;
9033 /*
9034 * '+ 8' for 'del-acl('
9035 * '- 9' for 'del-acl(' + trailing ')'
9036 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009037 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009038
9039 cur_arg = 1;
9040
9041 if (!*args[cur_arg] ||
9042 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9043 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9044 file, linenum, args[0]);
9045 goto out_err;
9046 }
9047
9048 LIST_INIT(&rule->arg.map.key);
9049 proxy->conf.args.ctx = ARGC_HRQ;
9050 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9051 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9052 file, linenum);
9053 free(proxy->conf.lfs_file);
9054 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9055 proxy->conf.lfs_line = proxy->conf.args.line;
9056 cur_arg += 1;
9057 } else if (strncmp(args[0], "del-map", 7) == 0) {
9058 /* http-request del-map(<reference (map name)>) <key pattern> */
9059 rule->action = HTTP_REQ_ACT_DEL_MAP;
9060 /*
9061 * '+ 8' for 'del-map('
9062 * '- 9' for 'del-map(' + trailing ')'
9063 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009064 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009065
9066 cur_arg = 1;
9067
9068 if (!*args[cur_arg] ||
9069 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9070 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9071 file, linenum, args[0]);
9072 goto out_err;
9073 }
9074
9075 LIST_INIT(&rule->arg.map.key);
9076 proxy->conf.args.ctx = ARGC_HRQ;
9077 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9078 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9079 file, linenum);
9080 free(proxy->conf.lfs_file);
9081 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9082 proxy->conf.lfs_line = proxy->conf.args.line;
9083 cur_arg += 1;
9084 } else if (strncmp(args[0], "set-map", 7) == 0) {
9085 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9086 rule->action = HTTP_REQ_ACT_SET_MAP;
9087 /*
9088 * '+ 8' for 'set-map('
9089 * '- 9' for 'set-map(' + trailing ')'
9090 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009091 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009092
9093 cur_arg = 1;
9094
9095 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9096 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9097 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9098 file, linenum, args[0]);
9099 goto out_err;
9100 }
9101
9102 LIST_INIT(&rule->arg.map.key);
9103 LIST_INIT(&rule->arg.map.value);
9104 proxy->conf.args.ctx = ARGC_HRQ;
9105
9106 /* key pattern */
9107 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9108 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9109 file, linenum);
9110
9111 /* value pattern */
9112 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9113 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9114 file, linenum);
9115 free(proxy->conf.lfs_file);
9116 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9117 proxy->conf.lfs_line = proxy->conf.args.line;
9118
9119 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009120 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9121 char *errmsg = NULL;
9122 cur_arg = 1;
9123 /* try in the module list */
9124 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9125 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9126 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9127 free(errmsg);
9128 goto out_err;
9129 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009130 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009131 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 +01009132 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009133 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009134 }
9135
9136 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9137 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009138 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009139
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009140 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9141 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9142 file, linenum, args[0], errmsg);
9143 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009144 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009145 }
9146 rule->cond = cond;
9147 }
9148 else if (*args[cur_arg]) {
9149 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9150 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9151 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009152 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009153 }
9154
9155 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009156 out_err:
9157 free(rule);
9158 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009159}
9160
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009161/* parse an "http-respose" rule */
9162struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9163{
9164 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009165 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009166 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009167 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009168
9169 rule = calloc(1, sizeof(*rule));
9170 if (!rule) {
9171 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9172 goto out_err;
9173 }
9174
9175 if (!strcmp(args[0], "allow")) {
9176 rule->action = HTTP_RES_ACT_ALLOW;
9177 cur_arg = 1;
9178 } else if (!strcmp(args[0], "deny")) {
9179 rule->action = HTTP_RES_ACT_DENY;
9180 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009181 } else if (!strcmp(args[0], "set-nice")) {
9182 rule->action = HTTP_RES_ACT_SET_NICE;
9183 cur_arg = 1;
9184
9185 if (!*args[cur_arg] ||
9186 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9187 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9188 file, linenum, args[0]);
9189 goto out_err;
9190 }
9191 rule->arg.nice = atoi(args[cur_arg]);
9192 if (rule->arg.nice < -1024)
9193 rule->arg.nice = -1024;
9194 else if (rule->arg.nice > 1024)
9195 rule->arg.nice = 1024;
9196 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009197 } else if (!strcmp(args[0], "set-tos")) {
9198#ifdef IP_TOS
9199 char *err;
9200 rule->action = HTTP_RES_ACT_SET_TOS;
9201 cur_arg = 1;
9202
9203 if (!*args[cur_arg] ||
9204 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9205 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9206 file, linenum, args[0]);
9207 goto out_err;
9208 }
9209
9210 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9211 if (err && *err != '\0') {
9212 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9213 file, linenum, err, args[0]);
9214 goto out_err;
9215 }
9216 cur_arg++;
9217#else
9218 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9219 goto out_err;
9220#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009221 } else if (!strcmp(args[0], "set-mark")) {
9222#ifdef SO_MARK
9223 char *err;
9224 rule->action = HTTP_RES_ACT_SET_MARK;
9225 cur_arg = 1;
9226
9227 if (!*args[cur_arg] ||
9228 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9229 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9230 file, linenum, args[0]);
9231 goto out_err;
9232 }
9233
9234 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9235 if (err && *err != '\0') {
9236 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9237 file, linenum, err, args[0]);
9238 goto out_err;
9239 }
9240 cur_arg++;
9241 global.last_checks |= LSTCHK_NETADM;
9242#else
9243 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9244 goto out_err;
9245#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009246 } else if (!strcmp(args[0], "set-log-level")) {
9247 rule->action = HTTP_RES_ACT_SET_LOGL;
9248 cur_arg = 1;
9249
9250 if (!*args[cur_arg] ||
9251 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9252 bad_log_level:
9253 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9254 file, linenum, args[0]);
9255 goto out_err;
9256 }
9257 if (strcmp(args[cur_arg], "silent") == 0)
9258 rule->arg.loglevel = -1;
9259 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9260 goto bad_log_level;
9261 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009262 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9263 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9264 cur_arg = 1;
9265
9266 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9267 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9268 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9269 file, linenum, args[0]);
9270 goto out_err;
9271 }
9272
9273 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9274 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9275 LIST_INIT(&rule->arg.hdr_add.fmt);
9276
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009277 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009278 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009279 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9280 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009281 free(proxy->conf.lfs_file);
9282 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9283 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009284 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009285 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009286 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009287 cur_arg = 1;
9288
9289 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmanna772b942014-08-08 17:29:06 +02009290 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9291 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009292 file, linenum, args[0]);
9293 goto out_err;
9294 }
9295
9296 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9297 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9298 LIST_INIT(&rule->arg.hdr_add.fmt);
9299
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009300 error = NULL;
9301 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9302 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9303 args[cur_arg + 1], error);
9304 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009305 goto out_err;
9306 }
9307
9308 proxy->conf.args.ctx = ARGC_HRQ;
9309 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9310 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9311 file, linenum);
9312
9313 free(proxy->conf.lfs_file);
9314 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9315 proxy->conf.lfs_line = proxy->conf.args.line;
9316 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009317 } else if (strcmp(args[0], "del-header") == 0) {
9318 rule->action = HTTP_RES_ACT_DEL_HDR;
9319 cur_arg = 1;
9320
9321 if (!*args[cur_arg] ||
9322 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9323 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9324 file, linenum, args[0]);
9325 goto out_err;
9326 }
9327
9328 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9329 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9330
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009331 proxy->conf.args.ctx = ARGC_HRS;
9332 free(proxy->conf.lfs_file);
9333 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9334 proxy->conf.lfs_line = proxy->conf.args.line;
9335 cur_arg += 1;
9336 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9337 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9338 rule->action = HTTP_RES_ACT_ADD_ACL;
9339 /*
9340 * '+ 8' for 'add-acl('
9341 * '- 9' for 'add-acl(' + trailing ')'
9342 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009343 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009344
9345 cur_arg = 1;
9346
9347 if (!*args[cur_arg] ||
9348 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9349 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9350 file, linenum, args[0]);
9351 goto out_err;
9352 }
9353
9354 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009355 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009356 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9357 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9358 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009359 free(proxy->conf.lfs_file);
9360 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9361 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009362
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009363 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009364 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9365 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9366 rule->action = HTTP_RES_ACT_DEL_ACL;
9367 /*
9368 * '+ 8' for 'del-acl('
9369 * '- 9' for 'del-acl(' + trailing ')'
9370 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009371 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009372
9373 cur_arg = 1;
9374
9375 if (!*args[cur_arg] ||
9376 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9377 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9378 file, linenum, args[0]);
9379 goto out_err;
9380 }
9381
9382 LIST_INIT(&rule->arg.map.key);
9383 proxy->conf.args.ctx = ARGC_HRS;
9384 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9385 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9386 file, linenum);
9387 free(proxy->conf.lfs_file);
9388 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9389 proxy->conf.lfs_line = proxy->conf.args.line;
9390 cur_arg += 1;
9391 } else if (strncmp(args[0], "del-map", 7) == 0) {
9392 /* http-response del-map(<reference (map name)>) <key pattern> */
9393 rule->action = HTTP_RES_ACT_DEL_MAP;
9394 /*
9395 * '+ 8' for 'del-map('
9396 * '- 9' for 'del-map(' + trailing ')'
9397 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009398 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009399
9400 cur_arg = 1;
9401
9402 if (!*args[cur_arg] ||
9403 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9404 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9405 file, linenum, args[0]);
9406 goto out_err;
9407 }
9408
9409 LIST_INIT(&rule->arg.map.key);
9410 proxy->conf.args.ctx = ARGC_HRS;
9411 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9412 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9413 file, linenum);
9414 free(proxy->conf.lfs_file);
9415 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9416 proxy->conf.lfs_line = proxy->conf.args.line;
9417 cur_arg += 1;
9418 } else if (strncmp(args[0], "set-map", 7) == 0) {
9419 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9420 rule->action = HTTP_RES_ACT_SET_MAP;
9421 /*
9422 * '+ 8' for 'set-map('
9423 * '- 9' for 'set-map(' + trailing ')'
9424 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009425 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009426
9427 cur_arg = 1;
9428
9429 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9430 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9431 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9432 file, linenum, args[0]);
9433 goto out_err;
9434 }
9435
9436 LIST_INIT(&rule->arg.map.key);
9437 LIST_INIT(&rule->arg.map.value);
9438
9439 proxy->conf.args.ctx = ARGC_HRS;
9440
9441 /* key pattern */
9442 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9443 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9444 file, linenum);
9445
9446 /* value pattern */
9447 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9448 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9449 file, linenum);
9450
9451 free(proxy->conf.lfs_file);
9452 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9453 proxy->conf.lfs_line = proxy->conf.args.line;
9454
9455 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009456 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9457 char *errmsg = NULL;
9458 cur_arg = 1;
9459 /* try in the module list */
9460 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9461 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9462 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9463 free(errmsg);
9464 goto out_err;
9465 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009466 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009467 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 +02009468 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9469 goto out_err;
9470 }
9471
9472 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9473 struct acl_cond *cond;
9474 char *errmsg = NULL;
9475
9476 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9477 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9478 file, linenum, args[0], errmsg);
9479 free(errmsg);
9480 goto out_err;
9481 }
9482 rule->cond = cond;
9483 }
9484 else if (*args[cur_arg]) {
9485 Alert("parsing [%s:%d]: 'http-response %s' expects"
9486 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9487 file, linenum, args[0], args[cur_arg]);
9488 goto out_err;
9489 }
9490
9491 return rule;
9492 out_err:
9493 free(rule);
9494 return NULL;
9495}
9496
Willy Tarreau4baae242012-12-27 12:00:31 +01009497/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009498 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9499 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009500 */
9501struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009502 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009503{
9504 struct redirect_rule *rule;
9505 int cur_arg;
9506 int type = REDIRECT_TYPE_NONE;
9507 int code = 302;
9508 const char *destination = NULL;
9509 const char *cookie = NULL;
9510 int cookie_set = 0;
9511 unsigned int flags = REDIRECT_FLAG_NONE;
9512 struct acl_cond *cond = NULL;
9513
9514 cur_arg = 0;
9515 while (*(args[cur_arg])) {
9516 if (strcmp(args[cur_arg], "location") == 0) {
9517 if (!*args[cur_arg + 1])
9518 goto missing_arg;
9519
9520 type = REDIRECT_TYPE_LOCATION;
9521 cur_arg++;
9522 destination = args[cur_arg];
9523 }
9524 else if (strcmp(args[cur_arg], "prefix") == 0) {
9525 if (!*args[cur_arg + 1])
9526 goto missing_arg;
9527
9528 type = REDIRECT_TYPE_PREFIX;
9529 cur_arg++;
9530 destination = args[cur_arg];
9531 }
9532 else if (strcmp(args[cur_arg], "scheme") == 0) {
9533 if (!*args[cur_arg + 1])
9534 goto missing_arg;
9535
9536 type = REDIRECT_TYPE_SCHEME;
9537 cur_arg++;
9538 destination = args[cur_arg];
9539 }
9540 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9541 if (!*args[cur_arg + 1])
9542 goto missing_arg;
9543
9544 cur_arg++;
9545 cookie = args[cur_arg];
9546 cookie_set = 1;
9547 }
9548 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9549 if (!*args[cur_arg + 1])
9550 goto missing_arg;
9551
9552 cur_arg++;
9553 cookie = args[cur_arg];
9554 cookie_set = 0;
9555 }
9556 else if (strcmp(args[cur_arg], "code") == 0) {
9557 if (!*args[cur_arg + 1])
9558 goto missing_arg;
9559
9560 cur_arg++;
9561 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009562 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009563 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009564 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009565 args[cur_arg - 1], args[cur_arg]);
9566 return NULL;
9567 }
9568 }
9569 else if (!strcmp(args[cur_arg],"drop-query")) {
9570 flags |= REDIRECT_FLAG_DROP_QS;
9571 }
9572 else if (!strcmp(args[cur_arg],"append-slash")) {
9573 flags |= REDIRECT_FLAG_APPEND_SLASH;
9574 }
9575 else if (strcmp(args[cur_arg], "if") == 0 ||
9576 strcmp(args[cur_arg], "unless") == 0) {
9577 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9578 if (!cond) {
9579 memprintf(errmsg, "error in condition: %s", *errmsg);
9580 return NULL;
9581 }
9582 break;
9583 }
9584 else {
9585 memprintf(errmsg,
9586 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9587 args[cur_arg]);
9588 return NULL;
9589 }
9590 cur_arg++;
9591 }
9592
9593 if (type == REDIRECT_TYPE_NONE) {
9594 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9595 return NULL;
9596 }
9597
9598 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9599 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009600 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009601
9602 if (!use_fmt) {
9603 /* old-style static redirect rule */
9604 rule->rdr_str = strdup(destination);
9605 rule->rdr_len = strlen(destination);
9606 }
9607 else {
9608 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009609
9610 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9611 * if prefix == "/", we don't want to add anything, otherwise it
9612 * makes it hard for the user to configure a self-redirection.
9613 */
9614 proxy->conf.args.ctx = ARGC_RDR;
9615 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009616 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009617 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9618 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009619 free(curproxy->conf.lfs_file);
9620 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9621 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009622 }
9623 }
9624
Willy Tarreau4baae242012-12-27 12:00:31 +01009625 if (cookie) {
9626 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9627 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9628 */
9629 rule->cookie_len = strlen(cookie);
9630 if (cookie_set) {
9631 rule->cookie_str = malloc(rule->cookie_len + 10);
9632 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9633 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9634 rule->cookie_len += 9;
9635 } else {
9636 rule->cookie_str = malloc(rule->cookie_len + 21);
9637 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9638 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9639 rule->cookie_len += 20;
9640 }
9641 }
9642 rule->type = type;
9643 rule->code = code;
9644 rule->flags = flags;
9645 LIST_INIT(&rule->list);
9646 return rule;
9647
9648 missing_arg:
9649 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9650 return NULL;
9651}
9652
Willy Tarreau8797c062007-05-07 00:55:35 +02009653/************************************************************************/
9654/* The code below is dedicated to ACL parsing and matching */
9655/************************************************************************/
9656
9657
Willy Tarreau14174bc2012-04-16 14:34:04 +02009658/* This function ensures that the prerequisites for an L7 fetch are ready,
9659 * which means that a request or response is ready. If some data is missing,
9660 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009661 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9662 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009663 *
9664 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009665 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9666 * decide whether or not an HTTP message is present ;
9667 * 0 if the requested data cannot be fetched or if it is certain that
9668 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009669 * 1 if an HTTP message is ready
9670 */
9671static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009672smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009673 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009674{
9675 struct http_txn *txn = l7;
9676 struct http_msg *msg = &txn->req;
9677
9678 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9679 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9680 */
9681
9682 if (unlikely(!s || !txn))
9683 return 0;
9684
9685 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009686 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009687
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009688 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009689 if (unlikely(!s->req))
9690 return 0;
9691
Willy Tarreauaae75e32013-03-29 12:31:49 +01009692 /* If the buffer does not leave enough free space at the end,
9693 * we must first realign it.
9694 */
9695 if (s->req->buf->p > s->req->buf->data &&
9696 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9697 buffer_slow_realign(s->req->buf);
9698
Willy Tarreau14174bc2012-04-16 14:34:04 +02009699 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009700 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009701 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009702
9703 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009704 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009705 http_msg_analyzer(msg, &txn->hdr_idx);
9706
9707 /* Still no valid request ? */
9708 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009709 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009710 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009711 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009712 }
9713 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009714 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009715 return 0;
9716 }
9717
9718 /* OK we just got a valid HTTP request. We have some minor
9719 * preparation to perform so that further checks can rely
9720 * on HTTP tests.
9721 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009722
9723 /* If the request was parsed but was too large, we must absolutely
9724 * return an error so that it is not processed. At the moment this
9725 * cannot happen, but if the parsers are to change in the future,
9726 * we want this check to be maintained.
9727 */
9728 if (unlikely(s->req->buf->i + s->req->buf->p >
9729 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9730 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009731 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009732 return 1;
9733 }
9734
Willy Tarreau9b28e032012-10-12 23:49:43 +02009735 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009736 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9737 s->flags |= SN_REDIRECTABLE;
9738
Willy Tarreau506d0502013-07-06 13:29:24 +02009739 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9740 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009741 }
9742
Willy Tarreau506d0502013-07-06 13:29:24 +02009743 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009744 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009745 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009746
9747 /* otherwise everything's ready for the request */
9748 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009749 else {
9750 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009751 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9752 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009753 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009754 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009755 }
9756
9757 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009758 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009759 return 1;
9760}
Willy Tarreau8797c062007-05-07 00:55:35 +02009761
Willy Tarreaua4ba9db2014-06-25 16:56:41 +02009762/* Note: these functinos *do* modify the sample. Even in case of success, at
9763 * least the type and uint value are modified.
9764 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009765#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009766 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 +02009767
Willy Tarreau24e32d82012-04-23 23:55:44 +02009768#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009769 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 +02009770
Willy Tarreau8797c062007-05-07 00:55:35 +02009771
9772/* 1. Check on METHOD
9773 * We use the pre-parsed method if it is known, and store its number as an
9774 * integer. If it is unknown, we use the pointer and the length.
9775 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009776static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009777{
9778 int len, meth;
9779
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009780 len = strlen(text);
9781 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009782
9783 pattern->val.i = meth;
9784 if (meth == HTTP_METH_OTHER) {
Willy Tarreau71196f32014-08-29 15:15:50 +02009785 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009786 pattern->len = len;
9787 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009788 else {
9789 pattern->ptr.str = NULL;
9790 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009791 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009792 return 1;
9793}
9794
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009795/* This function fetches the method of current HTTP request and stores
9796 * it in the global pattern struct as a chunk. There are two possibilities :
9797 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9798 * in <len> and <ptr> is NULL ;
9799 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9800 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009801 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009802 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009803static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009804smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009805 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009806{
9807 int meth;
9808 struct http_txn *txn = l7;
9809
Willy Tarreau24e32d82012-04-23 23:55:44 +02009810 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009811
Willy Tarreau8797c062007-05-07 00:55:35 +02009812 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009813 smp->type = SMP_T_METH;
9814 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009815 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009816 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9817 /* ensure the indexes are not affected */
9818 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009819 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009820 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9821 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009822 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009823 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009824 return 1;
9825}
9826
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009827/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009828static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009829{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009830 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009831 struct pattern_list *lst;
9832 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009833
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009834 list_for_each_entry(lst, &expr->patterns, list) {
9835 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009836
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009837 /* well-known method */
9838 if (pattern->val.i != HTTP_METH_OTHER) {
9839 if (smp->data.meth.meth == pattern->val.i)
9840 return pattern;
9841 else
9842 continue;
9843 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009844
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009845 /* Other method, we must compare the strings */
9846 if (pattern->len != smp->data.meth.str.len)
9847 continue;
9848
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009849 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreauc62a0c62014-08-28 20:42:57 +02009850 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
9851 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009852 return pattern;
9853 }
9854 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009855}
9856
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009857static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009858smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009859 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009860{
9861 struct http_txn *txn = l7;
9862 char *ptr;
9863 int len;
9864
Willy Tarreauc0239e02012-04-16 14:42:55 +02009865 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009866
Willy Tarreau8797c062007-05-07 00:55:35 +02009867 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009868 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009869
9870 while ((len-- > 0) && (*ptr++ != '/'));
9871 if (len <= 0)
9872 return 0;
9873
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009874 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009875 smp->data.str.str = ptr;
9876 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009877
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009878 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009879 return 1;
9880}
9881
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009882static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009883smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009884 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009885{
9886 struct http_txn *txn = l7;
9887 char *ptr;
9888 int len;
9889
Willy Tarreauc0239e02012-04-16 14:42:55 +02009890 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009891
Willy Tarreauf26b2522012-12-14 08:33:14 +01009892 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9893 return 0;
9894
Willy Tarreau8797c062007-05-07 00:55:35 +02009895 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009896 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009897
9898 while ((len-- > 0) && (*ptr++ != '/'));
9899 if (len <= 0)
9900 return 0;
9901
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009902 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009903 smp->data.str.str = ptr;
9904 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009905
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009906 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009907 return 1;
9908}
9909
9910/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009911static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009912smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009913 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009914{
9915 struct http_txn *txn = l7;
9916 char *ptr;
9917 int len;
9918
Willy Tarreauc0239e02012-04-16 14:42:55 +02009919 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009920
Willy Tarreauf26b2522012-12-14 08:33:14 +01009921 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9922 return 0;
9923
Willy Tarreau8797c062007-05-07 00:55:35 +02009924 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009925 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009926
Willy Tarreauf853c462012-04-23 18:53:56 +02009927 smp->type = SMP_T_UINT;
9928 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009929 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009930 return 1;
9931}
9932
9933/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009934static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009935smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009936 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009937{
9938 struct http_txn *txn = l7;
9939
Willy Tarreauc0239e02012-04-16 14:42:55 +02009940 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009941
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009942 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009943 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009944 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009945 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009946 return 1;
9947}
9948
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009949static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009950smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009951 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009952{
9953 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009954 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009955
Willy Tarreauc0239e02012-04-16 14:42:55 +02009956 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009957
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009958 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 +02009959 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009960 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009961
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009962 smp->type = SMP_T_IPV4;
9963 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009964 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009965 return 1;
9966}
9967
9968static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009969smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009970 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009971{
9972 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009973 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009974
Willy Tarreauc0239e02012-04-16 14:42:55 +02009975 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009976
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009977 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 +02009978 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9979 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009980
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009981 smp->type = SMP_T_UINT;
9982 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009983 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009984 return 1;
9985}
9986
Willy Tarreau185b5c42012-04-26 15:11:51 +02009987/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9988 * Accepts an optional argument of type string containing the header field name,
9989 * and an optional argument of type signed or unsigned integer to request an
9990 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009991 * headers are considered from the first one. It does not stop on commas and
9992 * returns full lines instead (useful for User-Agent or Date for example).
9993 */
9994static int
9995smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009996 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009997{
9998 struct http_txn *txn = l7;
9999 struct hdr_idx *idx = &txn->hdr_idx;
10000 struct hdr_ctx *ctx = smp->ctx.a[0];
10001 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10002 int occ = 0;
10003 const char *name_str = NULL;
10004 int name_len = 0;
10005
10006 if (!ctx) {
10007 /* first call */
10008 ctx = &static_hdr_ctx;
10009 ctx->idx = 0;
10010 smp->ctx.a[0] = ctx;
10011 }
10012
10013 if (args) {
10014 if (args[0].type != ARGT_STR)
10015 return 0;
10016 name_str = args[0].data.str.str;
10017 name_len = args[0].data.str.len;
10018
10019 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10020 occ = args[1].data.uint;
10021 }
10022
10023 CHECK_HTTP_MESSAGE_FIRST();
10024
10025 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10026 /* search for header from the beginning */
10027 ctx->idx = 0;
10028
10029 if (!occ && !(opt & SMP_OPT_ITERATE))
10030 /* no explicit occurrence and single fetch => last header by default */
10031 occ = -1;
10032
10033 if (!occ)
10034 /* prepare to report multiple occurrences for ACL fetches */
10035 smp->flags |= SMP_F_NOT_LAST;
10036
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010037 smp->type = SMP_T_STR;
10038 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010039 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10040 return 1;
10041
10042 smp->flags &= ~SMP_F_NOT_LAST;
10043 return 0;
10044}
10045
10046/* 6. Check on HTTP header count. The number of occurrences is returned.
10047 * Accepts exactly 1 argument of type string. It does not stop on commas and
10048 * returns full lines instead (useful for User-Agent or Date for example).
10049 */
10050static int
10051smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010052 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010053{
10054 struct http_txn *txn = l7;
10055 struct hdr_idx *idx = &txn->hdr_idx;
10056 struct hdr_ctx ctx;
10057 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10058 int cnt;
10059
10060 if (!args || args->type != ARGT_STR)
10061 return 0;
10062
10063 CHECK_HTTP_MESSAGE_FIRST();
10064
10065 ctx.idx = 0;
10066 cnt = 0;
10067 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
10068 cnt++;
10069
10070 smp->type = SMP_T_UINT;
10071 smp->data.uint = cnt;
10072 smp->flags = SMP_F_VOL_HDR;
10073 return 1;
10074}
10075
10076/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10077 * Accepts an optional argument of type string containing the header field name,
10078 * and an optional argument of type signed or unsigned integer to request an
10079 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010080 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010081 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010082static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010083smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010084 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010085{
10086 struct http_txn *txn = l7;
10087 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010088 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010089 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010090 int occ = 0;
10091 const char *name_str = NULL;
10092 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010093
Willy Tarreaua890d072013-04-02 12:01:06 +020010094 if (!ctx) {
10095 /* first call */
10096 ctx = &static_hdr_ctx;
10097 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010098 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010099 }
10100
Willy Tarreau185b5c42012-04-26 15:11:51 +020010101 if (args) {
10102 if (args[0].type != ARGT_STR)
10103 return 0;
10104 name_str = args[0].data.str.str;
10105 name_len = args[0].data.str.len;
10106
10107 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10108 occ = args[1].data.uint;
10109 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010110
Willy Tarreaue333ec92012-04-16 16:26:40 +020010111 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010112
Willy Tarreau185b5c42012-04-26 15:11:51 +020010113 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010114 /* search for header from the beginning */
10115 ctx->idx = 0;
10116
Willy Tarreau185b5c42012-04-26 15:11:51 +020010117 if (!occ && !(opt & SMP_OPT_ITERATE))
10118 /* no explicit occurrence and single fetch => last header by default */
10119 occ = -1;
10120
10121 if (!occ)
10122 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010123 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010124
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010125 smp->type = SMP_T_STR;
10126 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010127 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 +020010128 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010129
Willy Tarreau37406352012-04-23 16:16:37 +020010130 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010131 return 0;
10132}
10133
Willy Tarreauc11416f2007-06-17 16:58:38 +020010134/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010135 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010136 */
10137static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010138smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010139 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010140{
10141 struct http_txn *txn = l7;
10142 struct hdr_idx *idx = &txn->hdr_idx;
10143 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010144 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010145 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +020010146
Willy Tarreau24e32d82012-04-23 23:55:44 +020010147 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010148 return 0;
10149
Willy Tarreaue333ec92012-04-16 16:26:40 +020010150 CHECK_HTTP_MESSAGE_FIRST();
10151
Willy Tarreau33a7e692007-06-10 19:45:56 +020010152 ctx.idx = 0;
10153 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010154 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 +020010155 cnt++;
10156
Willy Tarreauf853c462012-04-23 18:53:56 +020010157 smp->type = SMP_T_UINT;
10158 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010159 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010160 return 1;
10161}
10162
Willy Tarreau185b5c42012-04-26 15:11:51 +020010163/* Fetch an HTTP header's integer value. The integer value is returned. It
10164 * takes a mandatory argument of type string and an optional one of type int
10165 * to designate a specific occurrence. It returns an unsigned integer, which
10166 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010167 */
10168static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010169smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010170 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010171{
Willy Tarreauef38c392013-07-22 16:29:32 +020010172 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010173
Willy Tarreauf853c462012-04-23 18:53:56 +020010174 if (ret > 0) {
10175 smp->type = SMP_T_UINT;
10176 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10177 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010178
Willy Tarreaud53e2422012-04-16 17:21:11 +020010179 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010180}
10181
Cyril Bonté69fa9922012-10-25 00:01:06 +020010182/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10183 * and an optional one of type int to designate a specific occurrence.
10184 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010185 */
10186static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010187smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010188 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +020010189{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010190 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010191
Willy Tarreauef38c392013-07-22 16:29:32 +020010192 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010193 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10194 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010195 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010196 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010197 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010198 if (smp->data.str.len < temp->size - 1) {
10199 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10200 temp->str[smp->data.str.len] = '\0';
10201 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10202 smp->type = SMP_T_IPV6;
10203 break;
10204 }
10205 }
10206 }
10207
Willy Tarreaud53e2422012-04-16 17:21:11 +020010208 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010209 if (!(smp->flags & SMP_F_NOT_LAST))
10210 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010211 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010212 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010213}
10214
Willy Tarreau737b0c12007-06-10 21:28:46 +020010215/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10216 * the first '/' after the possible hostname, and ends before the possible '?'.
10217 */
10218static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010219smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010220 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010221{
10222 struct http_txn *txn = l7;
10223 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010224
Willy Tarreauc0239e02012-04-16 14:42:55 +020010225 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010226
Willy Tarreau9b28e032012-10-12 23:49:43 +020010227 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010228 ptr = http_get_path(txn);
10229 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010230 return 0;
10231
10232 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010233 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010234 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010235
10236 while (ptr < end && *ptr != '?')
10237 ptr++;
10238
Willy Tarreauf853c462012-04-23 18:53:56 +020010239 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010240 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010241 return 1;
10242}
10243
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010244/* This produces a concatenation of the first occurrence of the Host header
10245 * followed by the path component if it begins with a slash ('/'). This means
10246 * that '*' will not be added, resulting in exactly the first Host entry.
10247 * If no Host header is found, then the path is returned as-is. The returned
10248 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010249 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010250 */
10251static int
10252smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010253 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010254{
10255 struct http_txn *txn = l7;
10256 char *ptr, *end, *beg;
10257 struct hdr_ctx ctx;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010258 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010259
10260 CHECK_HTTP_MESSAGE_FIRST();
10261
10262 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010263 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +020010264 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010265
10266 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010267 temp = get_trash_chunk();
10268 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010269 smp->type = SMP_T_STR;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010270 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010271 smp->data.str.len = ctx.vlen;
10272
10273 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010274 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010275 beg = http_get_path(txn);
10276 if (!beg)
10277 beg = end;
10278
10279 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10280
10281 if (beg < ptr && *beg == '/') {
10282 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10283 smp->data.str.len += ptr - beg;
10284 }
10285
10286 smp->flags = SMP_F_VOL_1ST;
10287 return 1;
10288}
10289
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010290/* This produces a 32-bit hash of the concatenation of the first occurrence of
10291 * the Host header followed by the path component if it begins with a slash ('/').
10292 * This means that '*' will not be added, resulting in exactly the first Host
10293 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010294 * is hashed using the path hash followed by a full avalanche hash and provides a
10295 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010296 * high-traffic sites without having to store whole paths.
10297 */
10298static int
10299smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010300 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010301{
10302 struct http_txn *txn = l7;
10303 struct hdr_ctx ctx;
10304 unsigned int hash = 0;
10305 char *ptr, *beg, *end;
10306 int len;
10307
10308 CHECK_HTTP_MESSAGE_FIRST();
10309
10310 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010311 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010312 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10313 ptr = ctx.line + ctx.val;
10314 len = ctx.vlen;
10315 while (len--)
10316 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10317 }
10318
10319 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010320 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010321 beg = http_get_path(txn);
10322 if (!beg)
10323 beg = end;
10324
10325 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10326
10327 if (beg < ptr && *beg == '/') {
10328 while (beg < ptr)
10329 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10330 }
10331 hash = full_hash(hash);
10332
10333 smp->type = SMP_T_UINT;
10334 smp->data.uint = hash;
10335 smp->flags = SMP_F_VOL_1ST;
10336 return 1;
10337}
10338
Willy Tarreau4a550602012-12-09 14:53:32 +010010339/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010340 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10341 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10342 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010343 * that in environments where IPv6 is insignificant, truncating the output to
10344 * 8 bytes would still work.
10345 */
10346static int
10347smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010348 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +010010349{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010350 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010351 struct connection *cli_conn = objt_conn(l4->si[0].end);
10352
10353 if (!cli_conn)
10354 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010355
Willy Tarreauef38c392013-07-22 16:29:32 +020010356 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +010010357 return 0;
10358
Willy Tarreau47ca5452012-12-23 20:22:19 +010010359 temp = get_trash_chunk();
Willy Tarreau0dff81c2014-07-15 21:34:06 +020010360 *(unsigned int *)temp->str = htonl(smp->data.uint);
10361 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010362
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010363 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010364 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010365 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010366 temp->len += 4;
10367 break;
10368 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010369 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010370 temp->len += 16;
10371 break;
10372 default:
10373 return 0;
10374 }
10375
10376 smp->data.str = *temp;
10377 smp->type = SMP_T_BIN;
10378 return 1;
10379}
10380
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010381static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010382smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010383 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010384{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010385 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10386 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10387 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010388
Willy Tarreau24e32d82012-04-23 23:55:44 +020010389 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010390
Willy Tarreauf853c462012-04-23 18:53:56 +020010391 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010392 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010393 return 1;
10394}
10395
Willy Tarreau7f18e522010-10-22 20:04:13 +020010396/* return a valid test if the current request is the first one on the connection */
10397static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010398smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010399 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010400{
10401 if (!s)
10402 return 0;
10403
Willy Tarreauf853c462012-04-23 18:53:56 +020010404 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010405 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010406 return 1;
10407}
10408
Willy Tarreau34db1082012-04-19 17:16:54 +020010409/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010410static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010411smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010412 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010413{
10414
Willy Tarreau24e32d82012-04-23 23:55:44 +020010415 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010416 return 0;
10417
Willy Tarreauc0239e02012-04-16 14:42:55 +020010418 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010419
Willy Tarreauc0239e02012-04-16 14:42:55 +020010420 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010421 return 0;
10422
Willy Tarreauf853c462012-04-23 18:53:56 +020010423 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010424 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010425 return 1;
10426}
Willy Tarreau8797c062007-05-07 00:55:35 +020010427
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010428/* Accepts exactly 1 argument of type userlist */
10429static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010430smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010431 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010432{
10433
10434 if (!args || args->type != ARGT_USR)
10435 return 0;
10436
10437 CHECK_HTTP_MESSAGE_FIRST();
10438
10439 if (!get_http_auth(l4))
10440 return 0;
10441
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010442 /* if the user does not belong to the userlist or has a wrong password,
10443 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010444 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010445 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010446 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10447 return 0;
10448
10449 /* pat_match_auth() will need the user list */
10450 smp->ctx.a[0] = args->data.usr;
10451
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010452 smp->type = SMP_T_STR;
10453 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010454 smp->data.str.str = l4->txn.auth.user;
10455 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010456
10457 return 1;
10458}
10459
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010460/* Try to find the next occurrence of a cookie name in a cookie header value.
10461 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10462 * the cookie value is returned into *value and *value_l, and the function
10463 * returns a pointer to the next pointer to search from if the value was found.
10464 * Otherwise if the cookie was not found, NULL is returned and neither value
10465 * nor value_l are touched. The input <hdr> string should first point to the
10466 * header's value, and the <hdr_end> pointer must point to the first character
10467 * not part of the value. <list> must be non-zero if value may represent a list
10468 * of values (cookie headers). This makes it faster to abort parsing when no
10469 * list is expected.
10470 */
10471static char *
10472extract_cookie_value(char *hdr, const char *hdr_end,
10473 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010474 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010475{
10476 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10477 char *next;
10478
10479 /* we search at least a cookie name followed by an equal, and more
10480 * generally something like this :
10481 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10482 */
10483 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10484 /* Iterate through all cookies on this line */
10485
10486 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10487 att_beg++;
10488
10489 /* find att_end : this is the first character after the last non
10490 * space before the equal. It may be equal to hdr_end.
10491 */
10492 equal = att_end = att_beg;
10493
10494 while (equal < hdr_end) {
10495 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10496 break;
10497 if (http_is_spht[(unsigned char)*equal++])
10498 continue;
10499 att_end = equal;
10500 }
10501
10502 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10503 * is between <att_beg> and <equal>, both may be identical.
10504 */
10505
10506 /* look for end of cookie if there is an equal sign */
10507 if (equal < hdr_end && *equal == '=') {
10508 /* look for the beginning of the value */
10509 val_beg = equal + 1;
10510 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10511 val_beg++;
10512
10513 /* find the end of the value, respecting quotes */
10514 next = find_cookie_value_end(val_beg, hdr_end);
10515
10516 /* make val_end point to the first white space or delimitor after the value */
10517 val_end = next;
10518 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10519 val_end--;
10520 } else {
10521 val_beg = val_end = next = equal;
10522 }
10523
10524 /* We have nothing to do with attributes beginning with '$'. However,
10525 * they will automatically be removed if a header before them is removed,
10526 * since they're supposed to be linked together.
10527 */
10528 if (*att_beg == '$')
10529 continue;
10530
10531 /* Ignore cookies with no equal sign */
10532 if (equal == next)
10533 continue;
10534
10535 /* Now we have the cookie name between att_beg and att_end, and
10536 * its value between val_beg and val_end.
10537 */
10538
10539 if (att_end - att_beg == cookie_name_l &&
10540 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10541 /* let's return this value and indicate where to go on from */
10542 *value = val_beg;
10543 *value_l = val_end - val_beg;
10544 return next + 1;
10545 }
10546
10547 /* Set-Cookie headers only have the name in the first attr=value part */
10548 if (!list)
10549 break;
10550 }
10551
10552 return NULL;
10553}
10554
William Lallemanda43ba4e2014-01-28 18:14:25 +010010555/* Fetch a captured HTTP request header. The index is the position of
10556 * the "capture" option in the configuration file
10557 */
10558static int
10559smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10560 const struct arg *args, struct sample *smp, const char *kw)
10561{
10562 struct proxy *fe = l4->fe;
10563 struct http_txn *txn = l7;
10564 int idx;
10565
10566 if (!args || args->type != ARGT_UINT)
10567 return 0;
10568
10569 idx = args->data.uint;
10570
10571 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10572 return 0;
10573
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010574 smp->type = SMP_T_STR;
10575 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010576 smp->data.str.str = txn->req.cap[idx];
10577 smp->data.str.len = strlen(txn->req.cap[idx]);
10578
10579 return 1;
10580}
10581
10582/* Fetch a captured HTTP response header. The index is the position of
10583 * the "capture" option in the configuration file
10584 */
10585static int
10586smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10587 const struct arg *args, struct sample *smp, const char *kw)
10588{
10589 struct proxy *fe = l4->fe;
10590 struct http_txn *txn = l7;
10591 int idx;
10592
10593 if (!args || args->type != ARGT_UINT)
10594 return 0;
10595
10596 idx = args->data.uint;
10597
10598 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10599 return 0;
10600
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010601 smp->type = SMP_T_STR;
10602 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010603 smp->data.str.str = txn->rsp.cap[idx];
10604 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10605
10606 return 1;
10607}
10608
William Lallemand65ad6e12014-01-31 15:08:02 +010010609/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10610static int
10611smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10612 const struct arg *args, struct sample *smp, const char *kw)
10613{
10614 struct chunk *temp;
10615 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010616 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010617
10618 if (!txn->uri)
10619 return 0;
10620
William Lallemand96a77852014-02-05 00:30:02 +010010621 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010622
William Lallemand96a77852014-02-05 00:30:02 +010010623 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10624 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010625
William Lallemand96a77852014-02-05 00:30:02 +010010626 temp = get_trash_chunk();
10627 temp->str = txn->uri;
10628 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010629 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010630 smp->type = SMP_T_STR;
10631 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010632
10633 return 1;
10634
10635}
10636
10637/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10638static int
10639smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10640 const struct arg *args, struct sample *smp, const char *kw)
10641{
10642 struct chunk *temp;
10643 struct http_txn *txn = l7;
10644 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010645
10646 if (!txn->uri)
10647 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010648
William Lallemand65ad6e12014-01-31 15:08:02 +010010649 ptr = txn->uri;
10650
10651 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10652 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010653
William Lallemand65ad6e12014-01-31 15:08:02 +010010654 if (!*ptr)
10655 return 0;
10656
10657 ptr++; /* skip the space */
10658
10659 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010660 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010661 if (!ptr)
10662 return 0;
10663 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10664 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010665
10666 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010667 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010668 smp->type = SMP_T_STR;
10669 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010670
10671 return 1;
10672}
10673
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010674/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10675 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10676 */
10677static int
10678smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10679 const struct arg *args, struct sample *smp, const char *kw)
10680{
10681 struct http_txn *txn = l7;
10682
10683 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10684 return 0;
10685
10686 if (txn->req.flags & HTTP_MSGF_VER_11)
10687 smp->data.str.str = "HTTP/1.1";
10688 else
10689 smp->data.str.str = "HTTP/1.0";
10690
10691 smp->data.str.len = 8;
10692 smp->type = SMP_T_STR;
10693 smp->flags = SMP_F_CONST;
10694 return 1;
10695
10696}
10697
10698/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10699 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10700 */
10701static int
10702smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10703 const struct arg *args, struct sample *smp, const char *kw)
10704{
10705 struct http_txn *txn = l7;
10706
10707 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10708 return 0;
10709
10710 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10711 smp->data.str.str = "HTTP/1.1";
10712 else
10713 smp->data.str.str = "HTTP/1.0";
10714
10715 smp->data.str.len = 8;
10716 smp->type = SMP_T_STR;
10717 smp->flags = SMP_F_CONST;
10718 return 1;
10719
10720}
10721
William Lallemand65ad6e12014-01-31 15:08:02 +010010722
Willy Tarreaue333ec92012-04-16 16:26:40 +020010723/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010724 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010725 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010726 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010727 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010728 * Accepts exactly 1 argument of type string. If the input options indicate
10729 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010730 * The returned sample is of type CSTR. Can be used to parse cookies in other
10731 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010732 */
William Lallemand07c8b242014-05-02 17:11:07 +020010733int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010734 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010735{
10736 struct http_txn *txn = l7;
10737 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010738 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010739 const struct http_msg *msg;
10740 const char *hdr_name;
10741 int hdr_name_len;
10742 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010743 int occ = 0;
10744 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010745
Willy Tarreau24e32d82012-04-23 23:55:44 +020010746 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010747 return 0;
10748
Willy Tarreaua890d072013-04-02 12:01:06 +020010749 if (!ctx) {
10750 /* first call */
10751 ctx = &static_hdr_ctx;
10752 ctx->idx = 0;
10753 smp->ctx.a[2] = ctx;
10754 }
10755
Willy Tarreaue333ec92012-04-16 16:26:40 +020010756 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010757
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010758 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010759 msg = &txn->req;
10760 hdr_name = "Cookie";
10761 hdr_name_len = 6;
10762 } else {
10763 msg = &txn->rsp;
10764 hdr_name = "Set-Cookie";
10765 hdr_name_len = 10;
10766 }
10767
Willy Tarreau28376d62012-04-26 21:26:10 +020010768 if (!occ && !(opt & SMP_OPT_ITERATE))
10769 /* no explicit occurrence and single fetch => last cookie by default */
10770 occ = -1;
10771
10772 /* OK so basically here, either we want only one value and it's the
10773 * last one, or we want to iterate over all of them and we fetch the
10774 * next one.
10775 */
10776
Willy Tarreau9b28e032012-10-12 23:49:43 +020010777 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010778 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010779 /* search for the header from the beginning, we must first initialize
10780 * the search parameters.
10781 */
Willy Tarreau37406352012-04-23 16:16:37 +020010782 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010783 ctx->idx = 0;
10784 }
10785
Willy Tarreau28376d62012-04-26 21:26:10 +020010786 smp->flags |= SMP_F_VOL_HDR;
10787
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010788 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010789 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10790 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010791 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10792 goto out;
10793
Willy Tarreau24e32d82012-04-23 23:55:44 +020010794 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010795 continue;
10796
Willy Tarreau37406352012-04-23 16:16:37 +020010797 smp->ctx.a[0] = ctx->line + ctx->val;
10798 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010799 }
10800
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010801 smp->type = SMP_T_STR;
10802 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010803 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010804 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010805 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010806 &smp->data.str.str,
10807 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010808 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010809 found = 1;
10810 if (occ >= 0) {
10811 /* one value was returned into smp->data.str.{str,len} */
10812 smp->flags |= SMP_F_NOT_LAST;
10813 return 1;
10814 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010815 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010816 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010817 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010818 /* all cookie headers and values were scanned. If we're looking for the
10819 * last occurrence, we may return it now.
10820 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010821 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010822 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010823 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010824}
10825
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010826/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010827 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010828 * multiple cookies may be parsed on the same line. The returned sample is of
10829 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010830 */
10831static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010832smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010833 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010834{
10835 struct http_txn *txn = l7;
10836 struct hdr_idx *idx = &txn->hdr_idx;
10837 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010838 const struct http_msg *msg;
10839 const char *hdr_name;
10840 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010841 int cnt;
10842 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010843 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010844
Willy Tarreau24e32d82012-04-23 23:55:44 +020010845 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010846 return 0;
10847
Willy Tarreaue333ec92012-04-16 16:26:40 +020010848 CHECK_HTTP_MESSAGE_FIRST();
10849
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010850 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010851 msg = &txn->req;
10852 hdr_name = "Cookie";
10853 hdr_name_len = 6;
10854 } else {
10855 msg = &txn->rsp;
10856 hdr_name = "Set-Cookie";
10857 hdr_name_len = 10;
10858 }
10859
Willy Tarreau9b28e032012-10-12 23:49:43 +020010860 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010861 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010862 ctx.idx = 0;
10863 cnt = 0;
10864
10865 while (1) {
10866 /* Note: val_beg == NULL every time we need to fetch a new header */
10867 if (!val_beg) {
10868 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10869 break;
10870
Willy Tarreau24e32d82012-04-23 23:55:44 +020010871 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010872 continue;
10873
10874 val_beg = ctx.line + ctx.val;
10875 val_end = val_beg + ctx.vlen;
10876 }
10877
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010878 smp->type = SMP_T_STR;
10879 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010880 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010881 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010882 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010883 &smp->data.str.str,
10884 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010885 cnt++;
10886 }
10887 }
10888
Willy Tarreaub169eba2013-12-16 15:14:43 +010010889 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010890 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010891 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010892 return 1;
10893}
10894
Willy Tarreau51539362012-05-08 12:46:28 +020010895/* Fetch an cookie's integer value. The integer value is returned. It
10896 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10897 */
10898static int
10899smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010900 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010901{
Willy Tarreauef38c392013-07-22 16:29:32 +020010902 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010903
10904 if (ret > 0) {
10905 smp->type = SMP_T_UINT;
10906 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10907 }
10908
10909 return ret;
10910}
10911
Willy Tarreau8797c062007-05-07 00:55:35 +020010912/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010913/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010914/************************************************************************/
10915
David Cournapeau16023ee2010-12-23 20:55:41 +090010916/*
10917 * Given a path string and its length, find the position of beginning of the
10918 * query string. Returns NULL if no query string is found in the path.
10919 *
10920 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10921 *
10922 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10923 */
bedis4c75cca2012-10-05 08:38:24 +020010924static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010925{
10926 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010927
bedis4c75cca2012-10-05 08:38:24 +020010928 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010929 return p ? p + 1 : NULL;
10930}
10931
bedis4c75cca2012-10-05 08:38:24 +020010932static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010933{
bedis4c75cca2012-10-05 08:38:24 +020010934 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010935}
10936
10937/*
10938 * Given a url parameter, find the starting position of the first occurence,
10939 * or NULL if the parameter is not found.
10940 *
10941 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10942 * the function will return query_string+8.
10943 */
10944static char*
10945find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010946 char* url_param_name, size_t url_param_name_l,
10947 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010948{
10949 char *pos, *last;
10950
10951 pos = query_string;
10952 last = query_string + query_string_l - url_param_name_l - 1;
10953
10954 while (pos <= last) {
10955 if (pos[url_param_name_l] == '=') {
10956 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10957 return pos;
10958 pos += url_param_name_l + 1;
10959 }
bedis4c75cca2012-10-05 08:38:24 +020010960 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010961 pos++;
10962 pos++;
10963 }
10964 return NULL;
10965}
10966
10967/*
10968 * Given a url parameter name, returns its value and size into *value and
10969 * *value_l respectively, and returns non-zero. If the parameter is not found,
10970 * zero is returned and value/value_l are not touched.
10971 */
10972static int
10973find_url_param_value(char* path, size_t path_l,
10974 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010975 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010976{
10977 char *query_string, *qs_end;
10978 char *arg_start;
10979 char *value_start, *value_end;
10980
bedis4c75cca2012-10-05 08:38:24 +020010981 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010982 if (!query_string)
10983 return 0;
10984
10985 qs_end = path + path_l;
10986 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010987 url_param_name, url_param_name_l,
10988 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010989 if (!arg_start)
10990 return 0;
10991
10992 value_start = arg_start + url_param_name_l + 1;
10993 value_end = value_start;
10994
bedis4c75cca2012-10-05 08:38:24 +020010995 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010996 value_end++;
10997
10998 *value = value_start;
10999 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011000 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011001}
11002
11003static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011004smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011005 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090011006{
bedis4c75cca2012-10-05 08:38:24 +020011007 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090011008 struct http_txn *txn = l7;
11009 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011010
bedis4c75cca2012-10-05 08:38:24 +020011011 if (!args || args[0].type != ARGT_STR ||
11012 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011013 return 0;
11014
11015 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011016
bedis4c75cca2012-10-05 08:38:24 +020011017 if (args[1].type)
11018 delim = *args[1].data.str.str;
11019
Willy Tarreau9b28e032012-10-12 23:49:43 +020011020 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011021 args->data.str.str, args->data.str.len,
11022 &smp->data.str.str, &smp->data.str.len,
11023 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011024 return 0;
11025
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011026 smp->type = SMP_T_STR;
11027 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011028 return 1;
11029}
11030
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011031/* Return the signed integer value for the specified url parameter (see url_param
11032 * above).
11033 */
11034static int
11035smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011036 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011037{
Willy Tarreauef38c392013-07-22 16:29:32 +020011038 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011039
11040 if (ret > 0) {
11041 smp->type = SMP_T_UINT;
11042 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11043 }
11044
11045 return ret;
11046}
11047
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011048/* This produces a 32-bit hash of the concatenation of the first occurrence of
11049 * the Host header followed by the path component if it begins with a slash ('/').
11050 * This means that '*' will not be added, resulting in exactly the first Host
11051 * entry. If no Host header is found, then the path is used. The resulting value
11052 * is hashed using the url hash followed by a full avalanche hash and provides a
11053 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11054 * high-traffic sites without having to store whole paths.
11055 * this differs from the base32 functions in that it includes the url parameters
11056 * as well as the path
11057 */
11058static int
11059smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011060 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011061{
11062 struct http_txn *txn = l7;
11063 struct hdr_ctx ctx;
11064 unsigned int hash = 0;
11065 char *ptr, *beg, *end;
11066 int len;
11067
11068 CHECK_HTTP_MESSAGE_FIRST();
11069
11070 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011071 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011072 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11073 ptr = ctx.line + ctx.val;
11074 len = ctx.vlen;
11075 while (len--)
11076 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11077 }
11078
11079 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011080 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 +000011081 beg = http_get_path(txn);
11082 if (!beg)
11083 beg = end;
11084
11085 for (ptr = beg; ptr < end ; ptr++);
11086
11087 if (beg < ptr && *beg == '/') {
11088 while (beg < ptr)
11089 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11090 }
11091 hash = full_hash(hash);
11092
11093 smp->type = SMP_T_UINT;
11094 smp->data.uint = hash;
11095 smp->flags = SMP_F_VOL_1ST;
11096 return 1;
11097}
11098
11099/* This concatenates the source address with the 32-bit hash of the Host and
11100 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11101 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11102 * on the source address length. The URL hash is stored before the address so
11103 * that in environments where IPv6 is insignificant, truncating the output to
11104 * 8 bytes would still work.
11105 */
11106static int
11107smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011108 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011109{
11110 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011111 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011112
Willy Tarreaue155ec22013-11-18 18:33:22 +010011113 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011114 return 0;
11115
11116 temp = get_trash_chunk();
11117 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11118 temp->len += sizeof(smp->data.uint);
11119
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011120 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011121 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011122 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011123 temp->len += 4;
11124 break;
11125 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011126 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011127 temp->len += 16;
11128 break;
11129 default:
11130 return 0;
11131 }
11132
11133 smp->data.str = *temp;
11134 smp->type = SMP_T_BIN;
11135 return 1;
11136}
11137
Willy Tarreau185b5c42012-04-26 15:11:51 +020011138/* This function is used to validate the arguments passed to any "hdr" fetch
11139 * keyword. These keywords support an optional positive or negative occurrence
11140 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11141 * is assumed that the types are already the correct ones. Returns 0 on error,
11142 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11143 * error message in case of error, that the caller is responsible for freeing.
11144 * The initial location must either be freeable or NULL.
11145 */
11146static int val_hdr(struct arg *arg, char **err_msg)
11147{
11148 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011149 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011150 return 0;
11151 }
11152 return 1;
11153}
11154
Willy Tarreau276fae92013-07-25 14:36:01 +020011155/* takes an UINT value on input supposed to represent the time since EPOCH,
11156 * adds an optional offset found in args[0] and emits a string representing
11157 * the date in RFC-1123/5322 format.
11158 */
11159static int sample_conv_http_date(const struct arg *args, struct sample *smp)
11160{
11161 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11162 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11163 struct chunk *temp;
11164 struct tm *tm;
11165 time_t curr_date = smp->data.uint;
11166
11167 /* add offset */
11168 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11169 curr_date += args[0].data.sint;
11170
11171 tm = gmtime(&curr_date);
11172
11173 temp = get_trash_chunk();
11174 temp->len = snprintf(temp->str, temp->size - temp->len,
11175 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11176 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11177 tm->tm_hour, tm->tm_min, tm->tm_sec);
11178
11179 smp->data.str = *temp;
11180 smp->type = SMP_T_STR;
11181 return 1;
11182}
11183
Thierry FOURNIERad903512014-04-11 17:51:01 +020011184/* Match language range with language tag. RFC2616 14.4:
11185 *
11186 * A language-range matches a language-tag if it exactly equals
11187 * the tag, or if it exactly equals a prefix of the tag such
11188 * that the first tag character following the prefix is "-".
11189 *
11190 * Return 1 if the strings match, else return 0.
11191 */
11192static inline int language_range_match(const char *range, int range_len,
11193 const char *tag, int tag_len)
11194{
11195 const char *end = range + range_len;
11196 const char *tend = tag + tag_len;
11197 while (range < end) {
11198 if (*range == '-' && tag == tend)
11199 return 1;
11200 if (*range != *tag || tag == tend)
11201 return 0;
11202 range++;
11203 tag++;
11204 }
11205 /* Return true only if the last char of the tag is matched. */
11206 return tag == tend;
11207}
11208
11209/* Arguments: The list of expected value, the number of parts returned and the separator */
11210static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
11211{
11212 const char *al = smp->data.str.str;
11213 const char *end = al + smp->data.str.len;
11214 const char *token;
11215 int toklen;
11216 int qvalue;
11217 const char *str;
11218 const char *w;
11219 int best_q = 0;
11220
11221 /* Set the constant to the sample, because the output of the
11222 * function will be peek in the constant configuration string.
11223 */
11224 smp->flags |= SMP_F_CONST;
11225 smp->data.str.size = 0;
11226 smp->data.str.str = "";
11227 smp->data.str.len = 0;
11228
11229 /* Parse the accept language */
11230 while (1) {
11231
11232 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011233 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011234 al++;
11235 if (al >= end)
11236 break;
11237
11238 /* Start of the fisrt word. */
11239 token = al;
11240
11241 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011242 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011243 al++;
11244 if (al == token)
11245 goto expect_comma;
11246
11247 /* Length of the token. */
11248 toklen = al - token;
11249 qvalue = 1000;
11250
11251 /* Check if the token exists in the list. If the token not exists,
11252 * jump to the next token.
11253 */
11254 str = args[0].data.str.str;
11255 w = str;
11256 while (1) {
11257 if (*str == ';' || *str == '\0') {
11258 if (language_range_match(token, toklen, w, str-w))
11259 goto look_for_q;
11260 if (*str == '\0')
11261 goto expect_comma;
11262 w = str + 1;
11263 }
11264 str++;
11265 }
11266 goto expect_comma;
11267
11268look_for_q:
11269
11270 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011271 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011272 al++;
11273 if (al >= end)
11274 goto process_value;
11275
11276 /* If ',' is found, process the result */
11277 if (*al == ',')
11278 goto process_value;
11279
11280 /* If the character is different from ';', look
11281 * for the end of the header part in best effort.
11282 */
11283 if (*al != ';')
11284 goto expect_comma;
11285
11286 /* Assumes that the char is ';', now expect "q=". */
11287 al++;
11288
11289 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011290 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011291 al++;
11292 if (al >= end)
11293 goto process_value;
11294
11295 /* Expect 'q'. If no 'q', continue in best effort */
11296 if (*al != 'q')
11297 goto process_value;
11298 al++;
11299
11300 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011301 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011302 al++;
11303 if (al >= end)
11304 goto process_value;
11305
11306 /* Expect '='. If no '=', continue in best effort */
11307 if (*al != '=')
11308 goto process_value;
11309 al++;
11310
11311 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011312 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011313 al++;
11314 if (al >= end)
11315 goto process_value;
11316
11317 /* Parse the q value. */
11318 qvalue = parse_qvalue(al, &al);
11319
11320process_value:
11321
11322 /* If the new q value is the best q value, then store the associated
11323 * language in the response. If qvalue is the biggest value (1000),
11324 * break the process.
11325 */
11326 if (qvalue > best_q) {
11327 smp->data.str.str = (char *)w;
11328 smp->data.str.len = str - w;
11329 if (qvalue >= 1000)
11330 break;
11331 best_q = qvalue;
11332 }
11333
11334expect_comma:
11335
11336 /* Expect comma or end. If the end is detected, quit the loop. */
11337 while (al < end && *al != ',')
11338 al++;
11339 if (al >= end)
11340 break;
11341
11342 /* Comma is found, jump it and restart the analyzer. */
11343 al++;
11344 }
11345
11346 /* Set default value if required. */
11347 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11348 smp->data.str.str = args[1].data.str.str;
11349 smp->data.str.len = args[1].data.str.len;
11350 }
11351
11352 /* Return true only if a matching language was found. */
11353 return smp->data.str.len != 0;
11354}
11355
William Lallemand73025dd2014-04-24 14:38:37 +020011356/*
11357 * Return the struct http_req_action_kw associated to a keyword.
11358 */
11359struct http_req_action_kw *action_http_req_custom(const char *kw)
11360{
11361 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11362 struct http_req_action_kw_list *kw_list;
11363 int i;
11364
11365 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11366 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11367 if (!strcmp(kw, kw_list->kw[i].kw))
11368 return &kw_list->kw[i];
11369 }
11370 }
11371 }
11372 return NULL;
11373}
11374
11375/*
11376 * Return the struct http_res_action_kw associated to a keyword.
11377 */
11378struct http_res_action_kw *action_http_res_custom(const char *kw)
11379{
11380 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11381 struct http_res_action_kw_list *kw_list;
11382 int i;
11383
11384 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11385 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11386 if (!strcmp(kw, kw_list->kw[i].kw))
11387 return &kw_list->kw[i];
11388 }
11389 }
11390 }
11391 return NULL;
11392}
11393
Willy Tarreau4a568972010-05-12 08:08:50 +020011394/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011395/* All supported ACL keywords must be declared here. */
11396/************************************************************************/
11397
11398/* Note: must not be declared <const> as its list will be overwritten.
11399 * Please take care of keeping this list alphabetically sorted.
11400 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011401static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011402 { "base", "base", PAT_MATCH_STR },
11403 { "base_beg", "base", PAT_MATCH_BEG },
11404 { "base_dir", "base", PAT_MATCH_DIR },
11405 { "base_dom", "base", PAT_MATCH_DOM },
11406 { "base_end", "base", PAT_MATCH_END },
11407 { "base_len", "base", PAT_MATCH_LEN },
11408 { "base_reg", "base", PAT_MATCH_REG },
11409 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011410
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011411 { "cook", "req.cook", PAT_MATCH_STR },
11412 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11413 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11414 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11415 { "cook_end", "req.cook", PAT_MATCH_END },
11416 { "cook_len", "req.cook", PAT_MATCH_LEN },
11417 { "cook_reg", "req.cook", PAT_MATCH_REG },
11418 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011419
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011420 { "hdr", "req.hdr", PAT_MATCH_STR },
11421 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11422 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11423 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11424 { "hdr_end", "req.hdr", PAT_MATCH_END },
11425 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11426 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11427 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011428
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011429 /* these two declarations uses strings with list storage (in place
11430 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11431 * and delete functions are relative to the list management. The parse
11432 * and match method are related to the corresponding fetch methods. This
11433 * is very particular ACL declaration mode.
11434 */
11435 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11436 { "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 +020011437
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011438 { "path", "path", PAT_MATCH_STR },
11439 { "path_beg", "path", PAT_MATCH_BEG },
11440 { "path_dir", "path", PAT_MATCH_DIR },
11441 { "path_dom", "path", PAT_MATCH_DOM },
11442 { "path_end", "path", PAT_MATCH_END },
11443 { "path_len", "path", PAT_MATCH_LEN },
11444 { "path_reg", "path", PAT_MATCH_REG },
11445 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011446
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011447 { "req_ver", "req.ver", PAT_MATCH_STR },
11448 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011449
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011450 { "scook", "res.cook", PAT_MATCH_STR },
11451 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11452 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11453 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11454 { "scook_end", "res.cook", PAT_MATCH_END },
11455 { "scook_len", "res.cook", PAT_MATCH_LEN },
11456 { "scook_reg", "res.cook", PAT_MATCH_REG },
11457 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011458
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011459 { "shdr", "res.hdr", PAT_MATCH_STR },
11460 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11461 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11462 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11463 { "shdr_end", "res.hdr", PAT_MATCH_END },
11464 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11465 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11466 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011467
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011468 { "url", "url", PAT_MATCH_STR },
11469 { "url_beg", "url", PAT_MATCH_BEG },
11470 { "url_dir", "url", PAT_MATCH_DIR },
11471 { "url_dom", "url", PAT_MATCH_DOM },
11472 { "url_end", "url", PAT_MATCH_END },
11473 { "url_len", "url", PAT_MATCH_LEN },
11474 { "url_reg", "url", PAT_MATCH_REG },
11475 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011476
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011477 { "urlp", "urlp", PAT_MATCH_STR },
11478 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11479 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11480 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11481 { "urlp_end", "urlp", PAT_MATCH_END },
11482 { "urlp_len", "urlp", PAT_MATCH_LEN },
11483 { "urlp_reg", "urlp", PAT_MATCH_REG },
11484 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011485
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011486 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011487}};
11488
11489/************************************************************************/
11490/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011491/************************************************************************/
11492/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011493static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011494 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011495 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11496 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11497
William Lallemanda43ba4e2014-01-28 18:14:25 +010011498 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011499 { "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 +020011500
11501 /* retrieve these captures from the HTTP logs */
11502 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11503 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11504 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11505
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011506 { "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 +020011507 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011508
Willy Tarreau409bcde2013-01-08 00:31:00 +010011509 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11510 * are only here to match the ACL's name, are request-only and are used
11511 * for ACL compatibility only.
11512 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011513 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11514 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011515 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11516 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11517
11518 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11519 * only here to match the ACL's name, are request-only and are used for
11520 * ACL compatibility only.
11521 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011522 { "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 +010011523 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11524 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11525 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11526
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011527 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011528 { "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 +010011529 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011530 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011531 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011532
11533 /* HTTP protocol on the request path */
11534 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011535 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011536
11537 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011538 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11539 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011540
11541 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011542 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11543 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011544
Willy Tarreau18ed2562013-01-14 15:56:36 +010011545 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011546 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011547 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11548 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11549
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011550 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011551 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011552 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011553 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11554 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11555 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11556
11557 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011558 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011559 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11560 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11561
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011562 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011563 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011564 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011565 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11566 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11567 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11568
Willy Tarreau409bcde2013-01-08 00:31:00 +010011569 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011570 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011571 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11572 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011573 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011574
11575 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011576 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011577 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11578 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11579 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11580
11581 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011582 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011583 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11584 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011585 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11586 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011587 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11588 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011589 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11590 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011591}};
11592
Willy Tarreau8797c062007-05-07 00:55:35 +020011593
Willy Tarreau276fae92013-07-25 14:36:01 +020011594/* Note: must not be declared <const> as its list will be overwritten */
11595static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011596 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11597 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011598 { NULL, NULL, 0, 0, 0 },
11599}};
11600
Willy Tarreau8797c062007-05-07 00:55:35 +020011601__attribute__((constructor))
11602static void __http_protocol_init(void)
11603{
11604 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011605 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011606 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020011607}
11608
11609
Willy Tarreau58f10d72006-12-04 02:26:12 +010011610/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011611 * Local variables:
11612 * c-indent-level: 8
11613 * c-basic-offset: 8
11614 * End:
11615 */