blob: 6b9665c55b7917344c9bd1da2d22864e1443d76d [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreau522d6c02009-12-06 18:49:18 +010069const char HTTP_100[] =
70 "HTTP/1.1 100 Continue\r\n\r\n";
71
72const struct chunk http_100_chunk = {
73 .str = (char *)&HTTP_100,
74 .len = sizeof(HTTP_100)-1
75};
76
Willy Tarreaua9679ac2010-01-03 17:32:57 +010077/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020081 "Location: "; /* not terminated since it will be concatenated with the URL */
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
89/* same as 302 except that the browser MUST retry with the GET method */
90const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Location: "; /* not terminated since it will be concatenated with the URL */
95
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040096
97/* same as 302 except that the browser MUST retry with the same method */
98const char *HTTP_307 =
99 "HTTP/1.1 307 Temporary Redirect\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Content-length: 0\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
104/* same as 301 except that the browser MUST retry with the same method */
105const char *HTTP_308 =
106 "HTTP/1.1 308 Permanent Redirect\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
111const char *HTTP_401_fmt =
112 "HTTP/1.0 401 Unauthorized\r\n"
113 "Cache-Control: no-cache\r\n"
114 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200115 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
117 "\r\n"
118 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
119
Willy Tarreau844a7e72010-01-31 21:46:18 +0100120const char *HTTP_407_fmt =
121 "HTTP/1.0 407 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
124 "Content-Type: text/html\r\n"
125 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
Godbachb3016542014-12-17 16:32:05 +0800127 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100128
Willy Tarreau0f772532006-12-23 20:51:41 +0100129
130const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200131 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100132 [HTTP_ERR_400] = 400,
133 [HTTP_ERR_403] = 403,
134 [HTTP_ERR_408] = 408,
135 [HTTP_ERR_500] = 500,
136 [HTTP_ERR_502] = 502,
137 [HTTP_ERR_503] = 503,
138 [HTTP_ERR_504] = 504,
139};
140
Willy Tarreau80587432006-12-24 17:47:20 +0100141static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200142 [HTTP_ERR_200] =
143 "HTTP/1.0 200 OK\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
149
Willy Tarreau0f772532006-12-23 20:51:41 +0100150 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100151 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
157
158 [HTTP_ERR_403] =
159 "HTTP/1.0 403 Forbidden\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
165
166 [HTTP_ERR_408] =
167 "HTTP/1.0 408 Request Time-out\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
173
174 [HTTP_ERR_500] =
175 "HTTP/1.0 500 Server Error\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
181
182 [HTTP_ERR_502] =
183 "HTTP/1.0 502 Bad Gateway\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
189
190 [HTTP_ERR_503] =
191 "HTTP/1.0 503 Service Unavailable\r\n"
192 "Cache-Control: no-cache\r\n"
193 "Connection: close\r\n"
194 "Content-Type: text/html\r\n"
195 "\r\n"
196 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
197
198 [HTTP_ERR_504] =
199 "HTTP/1.0 504 Gateway Time-out\r\n"
200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
204 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
205
206};
207
Cyril Bonté19979e12012-04-04 12:57:21 +0200208/* status codes available for the stats admin page (strictly 4 chars length) */
209const char *stat_status_codes[STAT_STATUS_SIZE] = {
210 [STAT_STATUS_DENY] = "DENY",
211 [STAT_STATUS_DONE] = "DONE",
212 [STAT_STATUS_ERRP] = "ERRP",
213 [STAT_STATUS_EXCD] = "EXCD",
214 [STAT_STATUS_NONE] = "NONE",
215 [STAT_STATUS_PART] = "PART",
216 [STAT_STATUS_UNKN] = "UNKN",
217};
218
219
William Lallemand73025dd2014-04-24 14:38:37 +0200220/* List head of all known action keywords for "http-request" */
221struct http_req_action_kw_list http_req_keywords = {
222 .list = LIST_HEAD_INIT(http_req_keywords.list)
223};
224
225/* List head of all known action keywords for "http-response" */
226struct http_res_action_kw_list http_res_keywords = {
227 .list = LIST_HEAD_INIT(http_res_keywords.list)
228};
229
Willy Tarreau80587432006-12-24 17:47:20 +0100230/* We must put the messages here since GCC cannot initialize consts depending
231 * on strlen().
232 */
233struct chunk http_err_chunks[HTTP_ERR_SIZE];
234
Willy Tarreaua890d072013-04-02 12:01:06 +0200235/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
236static struct hdr_ctx static_hdr_ctx;
237
Willy Tarreau42250582007-04-01 01:30:43 +0200238#define FD_SETS_ARE_BITFIELDS
239#ifdef FD_SETS_ARE_BITFIELDS
240/*
241 * This map is used with all the FD_* macros to check whether a particular bit
242 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
243 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
244 * byte should be encoded. Be careful to always pass bytes from 0 to 255
245 * exclusively to the macros.
246 */
247fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
248fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100249fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200250
251#else
252#error "Check if your OS uses bitfields for fd_sets"
253#endif
254
Willy Tarreau0b748332014-04-29 00:13:29 +0200255static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn);
256
Willy Tarreau80587432006-12-24 17:47:20 +0100257void init_proto_http()
258{
Willy Tarreau42250582007-04-01 01:30:43 +0200259 int i;
260 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100261 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200262
Willy Tarreau80587432006-12-24 17:47:20 +0100263 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
264 if (!http_err_msgs[msg]) {
265 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
266 abort();
267 }
268
269 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
270 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
271 }
Willy Tarreau42250582007-04-01 01:30:43 +0200272
273 /* initialize the log header encoding map : '{|}"#' should be encoded with
274 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
275 * URL encoding only requires '"', '#' to be encoded as well as non-
276 * printable characters above.
277 */
278 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
279 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100280 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200281 for (i = 0; i < 32; i++) {
282 FD_SET(i, hdr_encode_map);
283 FD_SET(i, url_encode_map);
284 }
285 for (i = 127; i < 256; i++) {
286 FD_SET(i, hdr_encode_map);
287 FD_SET(i, url_encode_map);
288 }
289
290 tmp = "\"#{|}";
291 while (*tmp) {
292 FD_SET(*tmp, hdr_encode_map);
293 tmp++;
294 }
295
296 tmp = "\"#";
297 while (*tmp) {
298 FD_SET(*tmp, url_encode_map);
299 tmp++;
300 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200301
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100302 /* initialize the http header encoding map. The draft httpbis define the
303 * header content as:
304 *
305 * HTTP-message = start-line
306 * *( header-field CRLF )
307 * CRLF
308 * [ message-body ]
309 * header-field = field-name ":" OWS field-value OWS
310 * field-value = *( field-content / obs-fold )
311 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
312 * obs-fold = CRLF 1*( SP / HTAB )
313 * field-vchar = VCHAR / obs-text
314 * VCHAR = %x21-7E
315 * obs-text = %x80-FF
316 *
317 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
318 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
319 * "obs-fold" is volontary forgotten because haproxy remove this.
320 */
321 memset(http_encode_map, 0, sizeof(http_encode_map));
322 for (i = 0x00; i <= 0x08; i++)
323 FD_SET(i, http_encode_map);
324 for (i = 0x0a; i <= 0x1f; i++)
325 FD_SET(i, http_encode_map);
326 FD_SET(0x7f, http_encode_map);
327
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200328 /* memory allocations */
329 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100330 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100331}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332
Willy Tarreau53b6c742006-12-17 13:37:46 +0100333/*
334 * We have 26 list of methods (1 per first letter), each of which can have
335 * up to 3 entries (2 valid, 1 null).
336 */
337struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100338 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100339 int len;
340 const char text[8];
341};
342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100343const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100344 ['C' - 'A'] = {
345 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
346 },
347 ['D' - 'A'] = {
348 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
349 },
350 ['G' - 'A'] = {
351 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
352 },
353 ['H' - 'A'] = {
354 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
355 },
356 ['P' - 'A'] = {
357 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
358 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
359 },
360 ['T' - 'A'] = {
361 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
362 },
363 /* rest is empty like this :
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 */
Willy Tarreau14acc652015-01-07 17:23:50 +0100734 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100735 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 Tarreau8767b132014-10-21 19:36:09 +02002549 msg->msg_state >= HTTP_MSG_BODY)) {
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 Tarreau4db603d2015-05-01 10:05:17 +02002888 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2889 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002890 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002891 * The length of a message body is determined by one of the following
2892 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002893 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002894 * 1. Any response to a HEAD request and any response with a 1xx
2895 * (Informational), 204 (No Content), or 304 (Not Modified) status
2896 * code is always terminated by the first empty line after the
2897 * header fields, regardless of the header fields present in the
2898 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002899 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002900 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2901 * the connection will become a tunnel immediately after the empty
2902 * line that concludes the header fields. A client MUST ignore any
2903 * Content-Length or Transfer-Encoding header fields received in
2904 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002905 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002906 * 3. If a Transfer-Encoding header field is present and the chunked
2907 * transfer coding (Section 4.1) is the final encoding, the message
2908 * body length is determined by reading and decoding the chunked
2909 * data until the transfer coding indicates the data is complete.
2910 *
2911 * If a Transfer-Encoding header field is present in a response and
2912 * the chunked transfer coding is not the final encoding, the
2913 * message body length is determined by reading the connection until
2914 * it is closed by the server. If a Transfer-Encoding header field
2915 * is present in a request and the chunked transfer coding is not
2916 * the final encoding, the message body length cannot be determined
2917 * reliably; the server MUST respond with the 400 (Bad Request)
2918 * status code and then close the connection.
2919 *
2920 * If a message is received with both a Transfer-Encoding and a
2921 * Content-Length header field, the Transfer-Encoding overrides the
2922 * Content-Length. Such a message might indicate an attempt to
2923 * perform request smuggling (Section 9.5) or response splitting
2924 * (Section 9.4) and ought to be handled as an error. A sender MUST
2925 * remove the received Content-Length field prior to forwarding such
2926 * a message downstream.
2927 *
2928 * 4. If a message is received without Transfer-Encoding and with
2929 * either multiple Content-Length header fields having differing
2930 * field-values or a single Content-Length header field having an
2931 * invalid value, then the message framing is invalid and the
2932 * recipient MUST treat it as an unrecoverable error. If this is a
2933 * request message, the server MUST respond with a 400 (Bad Request)
2934 * status code and then close the connection. If this is a response
2935 * message received by a proxy, the proxy MUST close the connection
2936 * to the server, discard the received response, and send a 502 (Bad
2937 * Gateway) response to the client. If this is a response message
2938 * received by a user agent, the user agent MUST close the
2939 * connection to the server and discard the received response.
2940 *
2941 * 5. If a valid Content-Length header field is present without
2942 * Transfer-Encoding, its decimal value defines the expected message
2943 * body length in octets. If the sender closes the connection or
2944 * the recipient times out before the indicated number of octets are
2945 * received, the recipient MUST consider the message to be
2946 * incomplete and close the connection.
2947 *
2948 * 6. If this is a request message and none of the above are true, then
2949 * the message body length is zero (no message body is present).
2950 *
2951 * 7. Otherwise, this is a response message without a declared message
2952 * body length, so the message body length is determined by the
2953 * number of octets received prior to the server closing the
2954 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002955 */
2956
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002957 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002958 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002959 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaue77bc172015-05-01 10:06:30 +02002960 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002961 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002962 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2963 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002964 /* bad transfer-encoding (chunked followed by something else) */
2965 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002966 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002967 break;
2968 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002969 }
2970
Willy Tarreaudfa3d922015-04-30 10:57:51 +02002971 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002972 ctx.idx = 0;
Willy Tarreaudfa3d922015-04-30 10:57:51 +02002973 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2974 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
2975 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2976 }
2977 else while (!use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002978 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002979 signed long long cl;
2980
Willy Tarreauad14f752011-09-02 20:33:27 +02002981 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002982 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002983 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002984 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002985
Willy Tarreauad14f752011-09-02 20:33:27 +02002986 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002987 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002988 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002989 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002990
Willy Tarreauad14f752011-09-02 20:33:27 +02002991 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002992 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002993 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002994 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002995
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002996 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002997 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002998 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002999 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003000
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003001 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003002 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003003 }
3004
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003005 /* bodyless requests have a known length */
3006 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003007 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003008
Willy Tarreau179085c2014-04-28 16:48:56 +02003009 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3010 * only change if both the request and the config reference something else.
3011 * Option httpclose by itself sets tunnel mode where headers are mangled.
3012 * However, if another mode is set, it will affect it (eg: server-close/
3013 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3014 * if FE and BE have the same settings (common). The method consists in
3015 * checking if options changed between the two calls (implying that either
3016 * one is non-null, or one of them is non-null and we are there for the first
3017 * time.
3018 */
3019 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau2e47a3a2014-09-30 18:44:22 +02003020 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
3021 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003022
Willy Tarreaud787e662009-07-07 10:14:51 +02003023 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003024 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003025 req->analyse_exp = TICK_ETERNITY;
3026 return 1;
3027
3028 return_bad_req:
3029 /* We centralize bad requests processing here */
3030 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3031 /* we detected a parsing error. We want to archive this request
3032 * in the dedicated proxy area for later troubleshooting.
3033 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003034 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003035 }
3036
3037 txn->req.msg_state = HTTP_MSG_ERROR;
3038 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003039 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003040
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003041 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003042 if (s->listener->counters)
3043 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003044
3045 return_prx_cond:
3046 if (!(s->flags & SN_ERR_MASK))
3047 s->flags |= SN_ERR_PRXCOND;
3048 if (!(s->flags & SN_FINST_MASK))
3049 s->flags |= SN_FINST_R;
3050
3051 req->analysers = 0;
3052 req->analyse_exp = TICK_ETERNITY;
3053 return 0;
3054}
3055
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003056
Willy Tarreau347a35d2013-11-22 17:51:09 +01003057/* This function prepares an applet to handle the stats. It can deal with the
3058 * "100-continue" expectation, check that admin rules are met for POST requests,
3059 * and program a response message if something was unexpected. It cannot fail
3060 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003061 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003062 * s->target which is supposed to already point to the stats applet. The caller
3063 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003064 */
3065int http_handle_stats(struct session *s, struct channel *req)
3066{
3067 struct stats_admin_rule *stats_admin_rule;
3068 struct stream_interface *si = s->rep->prod;
3069 struct http_txn *txn = &s->txn;
3070 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003071 struct uri_auth *uri_auth = s->be->uri_auth;
3072 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003073 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003074
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003075 appctx = si_appctx(si);
3076 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3077 appctx->st1 = appctx->st2 = 0;
3078 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3079 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003080 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3081 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003082
3083 uri = msg->chn->buf->p + msg->sl.rq.u;
3084 lookup = uri + uri_auth->uri_len;
3085
3086 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3087 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003088 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003089 break;
3090 }
3091 }
3092
3093 if (uri_auth->refresh) {
3094 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3095 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003096 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003097 break;
3098 }
3099 }
3100 }
3101
3102 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3103 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003104 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003105 break;
3106 }
3107 }
3108
3109 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3110 if (memcmp(h, ";st=", 4) == 0) {
3111 int i;
3112 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003113 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003114 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3115 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003116 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003117 break;
3118 }
3119 }
3120 break;
3121 }
3122 }
3123
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003124 appctx->ctx.stats.scope_str = 0;
3125 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003126 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3127 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3128 int itx = 0;
3129 const char *h2;
3130 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3131 const char *err;
3132
3133 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3134 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003135 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003136 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3137 itx++;
3138 h++;
3139 }
3140
3141 if (itx > STAT_SCOPE_TXT_MAXLEN)
3142 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003143 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003144
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003145 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003146 memcpy(scope_txt, h2, itx);
3147 scope_txt[itx] = '\0';
3148 err = invalid_char(scope_txt);
3149 if (err) {
3150 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003151 appctx->ctx.stats.scope_str = 0;
3152 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003153 }
3154 break;
3155 }
3156 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003157
3158 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003159 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003160 int ret = 1;
3161
3162 if (stats_admin_rule->cond) {
3163 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3164 ret = acl_pass(ret);
3165 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3166 ret = !ret;
3167 }
3168
3169 if (ret) {
3170 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003171 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003172 break;
3173 }
3174 }
3175
3176 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003177 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003178 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003179 /* we'll need the request body, possibly after sending 100-continue */
3180 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003181 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003182 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003183 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003184 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3185 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003186 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003187 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003188 else {
3189 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003190 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003191 }
3192
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003193 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003194 return 1;
3195}
3196
Lukas Tribus67db8df2013-06-23 17:37:13 +02003197/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3198 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3199 */
3200static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3201{
3202#ifdef IP_TOS
3203 if (from.ss_family == AF_INET)
3204 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3205#endif
3206#ifdef IPV6_TCLASS
3207 if (from.ss_family == AF_INET6) {
3208 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3209 /* v4-mapped addresses need IP_TOS */
3210 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3211 else
3212 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3213 }
3214#endif
3215}
3216
Sasha Pachev218f0642014-06-16 12:05:59 -06003217static int http_transform_header(struct session* s, struct http_msg *msg, const char* name, uint name_len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003218 char* buf, struct hdr_idx* idx, struct list *fmt, struct my_regex *re,
Sasha Pachev218f0642014-06-16 12:05:59 -06003219 struct hdr_ctx* ctx, int action)
3220{
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003221 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3222 struct hdr_idx *idx, struct hdr_ctx *ctx);
3223 struct chunk *replace = get_trash_chunk();
3224 struct chunk *output = get_trash_chunk();
3225
3226 replace->len = build_logline(s, replace->str, replace->size, fmt);
3227 if (replace->len >= replace->size - 1)
3228 return -1;
3229
Sasha Pachev218f0642014-06-16 12:05:59 -06003230 ctx->idx = 0;
3231
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003232 /* Choose the header browsing function. */
3233 switch (action) {
3234 case HTTP_REQ_ACT_REPLACE_VAL:
3235 case HTTP_RES_ACT_REPLACE_VAL:
3236 http_find_hdr_func = http_find_header2;
3237 break;
3238 case HTTP_REQ_ACT_REPLACE_HDR:
3239 case HTTP_RES_ACT_REPLACE_HDR:
3240 http_find_hdr_func = http_find_full_header2;
3241 break;
3242 default: /* impossible */
3243 return -1;
3244 }
3245
3246 while (http_find_hdr_func(name, name_len, buf, idx, ctx)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06003247 struct hdr_idx_elem *hdr = idx->v + ctx->idx;
3248 int delta;
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003249 char *val = ctx->line + ctx->val;
3250 char* val_end = val + ctx->vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003251
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003252 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch))
3253 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003254
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003255 output->len = exp_replace(output->str, output->size, val, replace->str, pmatch);
3256 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003257 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003258
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003259 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003260
3261 hdr->len += delta;
3262 http_msg_move_end(msg, delta);
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003263
3264 /* Adjust the length of the current value of the index. */
3265 ctx->vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003266 }
3267
3268 return 0;
3269}
3270
Willy Tarreau20b0de52012-12-24 15:45:22 +01003271/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003272 * transaction <txn>. Returns the verdict of the first rule that prevents
3273 * further processing of the request (auth, deny, ...), and defaults to
3274 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3275 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3276 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003277 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003278enum rule_result
Willy Tarreau96257ec2012-12-27 10:46:37 +01003279http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003280{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003281 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003282 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003283 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003284 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003285
Willy Tarreauff011f22011-01-06 17:51:27 +01003286 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003287 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003288 continue;
3289
Willy Tarreau96257ec2012-12-27 10:46:37 +01003290 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003291 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003292 int ret;
3293
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003294 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003295 ret = acl_pass(ret);
3296
Willy Tarreauff011f22011-01-06 17:51:27 +01003297 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003298 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003299
3300 if (!ret) /* condition not matched */
3301 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003302 }
3303
Willy Tarreau20b0de52012-12-24 15:45:22 +01003304
Willy Tarreau96257ec2012-12-27 10:46:37 +01003305 switch (rule->action) {
3306 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003307 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003308
3309 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003310 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003311
Willy Tarreauccbcc372012-12-27 12:37:57 +01003312 case HTTP_REQ_ACT_TARPIT:
3313 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003314 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003315
Willy Tarreau96257ec2012-12-27 10:46:37 +01003316 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003317 /* Auth might be performed on regular http-req rules as well as on stats */
3318 auth_realm = rule->arg.auth.realm;
3319 if (!auth_realm) {
3320 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3321 auth_realm = STATS_DEFAULT_REALM;
3322 else
3323 auth_realm = px->id;
3324 }
3325 /* send 401/407 depending on whether we use a proxy or not. We still
3326 * count one error, because normal browsing won't significantly
3327 * increase the counter but brute force attempts will.
3328 */
3329 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3330 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3331 stream_int_retnclose(&s->si[0], &trash);
3332 session_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003333 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003334
Willy Tarreau81499eb2012-12-27 12:19:02 +01003335 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003336 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3337 return HTTP_RULE_RES_BADREQ;
3338 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003339
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003340 case HTTP_REQ_ACT_SET_NICE:
3341 s->task->nice = rule->arg.nice;
3342 break;
3343
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003344 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003345 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003346 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003347 break;
3348
Willy Tarreau51347ed2013-06-11 19:34:13 +02003349 case HTTP_REQ_ACT_SET_MARK:
3350#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003351 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003352 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003353#endif
3354 break;
3355
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003356 case HTTP_REQ_ACT_SET_LOGL:
3357 s->logs.level = rule->arg.loglevel;
3358 break;
3359
Sasha Pachev218f0642014-06-16 12:05:59 -06003360 case HTTP_REQ_ACT_REPLACE_HDR:
3361 case HTTP_REQ_ACT_REPLACE_VAL:
3362 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3363 txn->req.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003364 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003365 return HTTP_RULE_RES_BADREQ;
3366 break;
3367
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003368 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003369 ctx.idx = 0;
3370 /* remove all occurrences of the header */
3371 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3372 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3373 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003374 }
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003375 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003376
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003377 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003378 case HTTP_REQ_ACT_ADD_HDR:
3379 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3380 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3381 trash.len = rule->arg.hdr_add.name_len;
3382 trash.str[trash.len++] = ':';
3383 trash.str[trash.len++] = ' ';
3384 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003385
3386 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3387 /* remove all occurrences of the header */
3388 ctx.idx = 0;
3389 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3390 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3391 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3392 }
3393 }
3394
Willy Tarreau96257ec2012-12-27 10:46:37 +01003395 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3396 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003397
3398 case HTTP_REQ_ACT_DEL_ACL:
3399 case HTTP_REQ_ACT_DEL_MAP: {
3400 struct pat_ref *ref;
3401 char *key;
3402 int len;
3403
3404 /* collect reference */
3405 ref = pat_ref_lookup(rule->arg.map.ref);
3406 if (!ref)
3407 continue;
3408
3409 /* collect key */
3410 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3411 key = trash.str;
3412 key[len] = '\0';
3413
3414 /* perform update */
3415 /* returned code: 1=ok, 0=ko */
3416 pat_ref_delete(ref, key);
3417
3418 break;
3419 }
3420
3421 case HTTP_REQ_ACT_ADD_ACL: {
3422 struct pat_ref *ref;
3423 char *key;
3424 struct chunk *trash_key;
3425 int len;
3426
3427 trash_key = get_trash_chunk();
3428
3429 /* collect reference */
3430 ref = pat_ref_lookup(rule->arg.map.ref);
3431 if (!ref)
3432 continue;
3433
3434 /* collect key */
3435 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3436 key = trash_key->str;
3437 key[len] = '\0';
3438
3439 /* perform update */
3440 /* add entry only if it does not already exist */
3441 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003442 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003443
3444 break;
3445 }
3446
3447 case HTTP_REQ_ACT_SET_MAP: {
3448 struct pat_ref *ref;
3449 char *key, *value;
3450 struct chunk *trash_key, *trash_value;
3451 int len;
3452
3453 trash_key = get_trash_chunk();
3454 trash_value = get_trash_chunk();
3455
3456 /* collect reference */
3457 ref = pat_ref_lookup(rule->arg.map.ref);
3458 if (!ref)
3459 continue;
3460
3461 /* collect key */
3462 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3463 key = trash_key->str;
3464 key[len] = '\0';
3465
3466 /* collect value */
3467 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3468 value = trash_value->str;
3469 value[len] = '\0';
3470
3471 /* perform update */
3472 if (pat_ref_find_elt(ref, key) != NULL)
3473 /* update entry if it exists */
3474 pat_ref_set(ref, key, value, NULL);
3475 else
3476 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003477 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003478
3479 break;
3480 }
William Lallemand73025dd2014-04-24 14:38:37 +02003481
3482 case HTTP_REQ_ACT_CUSTOM_CONT:
3483 rule->action_ptr(rule, px, s, txn);
3484 break;
3485
3486 case HTTP_REQ_ACT_CUSTOM_STOP:
3487 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003488 return HTTP_RULE_RES_DONE;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003489 }
3490 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003491
3492 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003493 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003494}
3495
Willy Tarreau71241ab2012-12-27 11:30:54 +01003496
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003497/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3498 * transaction <txn>. Returns the first rule that prevents further processing
3499 * of the response (deny, ...) or NULL if it executed all rules or stopped
3500 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3501 * rule.
3502 */
3503static struct http_res_rule *
3504http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3505{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003506 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003507 struct http_res_rule *rule;
3508 struct hdr_ctx ctx;
3509
3510 list_for_each_entry(rule, rules, list) {
3511 if (rule->action >= HTTP_RES_ACT_MAX)
3512 continue;
3513
3514 /* check optional condition */
3515 if (rule->cond) {
3516 int ret;
3517
3518 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3519 ret = acl_pass(ret);
3520
3521 if (rule->cond->pol == ACL_COND_UNLESS)
3522 ret = !ret;
3523
3524 if (!ret) /* condition not matched */
3525 continue;
3526 }
3527
3528
3529 switch (rule->action) {
3530 case HTTP_RES_ACT_ALLOW:
3531 return NULL; /* "allow" rules are OK */
3532
3533 case HTTP_RES_ACT_DENY:
3534 txn->flags |= TX_SVDENY;
3535 return rule;
3536
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003537 case HTTP_RES_ACT_SET_NICE:
3538 s->task->nice = rule->arg.nice;
3539 break;
3540
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003541 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003542 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003543 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003544 break;
3545
Willy Tarreau51347ed2013-06-11 19:34:13 +02003546 case HTTP_RES_ACT_SET_MARK:
3547#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003548 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003549 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003550#endif
3551 break;
3552
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003553 case HTTP_RES_ACT_SET_LOGL:
3554 s->logs.level = rule->arg.loglevel;
3555 break;
3556
Sasha Pachev218f0642014-06-16 12:05:59 -06003557 case HTTP_RES_ACT_REPLACE_HDR:
3558 case HTTP_RES_ACT_REPLACE_VAL:
3559 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3560 txn->rsp.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003561 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003562 return NULL; /* note: we should report an error here */
3563 break;
3564
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003565 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003566 ctx.idx = 0;
3567 /* remove all occurrences of the header */
3568 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3569 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3570 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3571 }
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003572 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003573
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003574 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003575 case HTTP_RES_ACT_ADD_HDR:
3576 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3577 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3578 trash.len = rule->arg.hdr_add.name_len;
3579 trash.str[trash.len++] = ':';
3580 trash.str[trash.len++] = ' ';
3581 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003582
3583 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3584 /* remove all occurrences of the header */
3585 ctx.idx = 0;
3586 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3587 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3588 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3589 }
3590 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003591 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3592 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003593
3594 case HTTP_RES_ACT_DEL_ACL:
3595 case HTTP_RES_ACT_DEL_MAP: {
3596 struct pat_ref *ref;
3597 char *key;
3598 int len;
3599
3600 /* collect reference */
3601 ref = pat_ref_lookup(rule->arg.map.ref);
3602 if (!ref)
3603 continue;
3604
3605 /* collect key */
3606 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3607 key = trash.str;
3608 key[len] = '\0';
3609
3610 /* perform update */
3611 /* returned code: 1=ok, 0=ko */
3612 pat_ref_delete(ref, key);
3613
3614 break;
3615 }
3616
3617 case HTTP_RES_ACT_ADD_ACL: {
3618 struct pat_ref *ref;
3619 char *key;
3620 struct chunk *trash_key;
3621 int len;
3622
3623 trash_key = get_trash_chunk();
3624
3625 /* collect reference */
3626 ref = pat_ref_lookup(rule->arg.map.ref);
3627 if (!ref)
3628 continue;
3629
3630 /* collect key */
3631 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3632 key = trash_key->str;
3633 key[len] = '\0';
3634
3635 /* perform update */
3636 /* check if the entry already exists */
3637 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003638 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003639
3640 break;
3641 }
3642
3643 case HTTP_RES_ACT_SET_MAP: {
3644 struct pat_ref *ref;
3645 char *key, *value;
3646 struct chunk *trash_key, *trash_value;
3647 int len;
3648
3649 trash_key = get_trash_chunk();
3650 trash_value = get_trash_chunk();
3651
3652 /* collect reference */
3653 ref = pat_ref_lookup(rule->arg.map.ref);
3654 if (!ref)
3655 continue;
3656
3657 /* collect key */
3658 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3659 key = trash_key->str;
3660 key[len] = '\0';
3661
3662 /* collect value */
3663 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3664 value = trash_value->str;
3665 value[len] = '\0';
3666
3667 /* perform update */
3668 if (pat_ref_find_elt(ref, key) != NULL)
3669 /* update entry if it exists */
3670 pat_ref_set(ref, key, value, NULL);
3671 else
3672 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003673 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003674
3675 break;
3676 }
William Lallemand73025dd2014-04-24 14:38:37 +02003677
3678 case HTTP_RES_ACT_CUSTOM_CONT:
3679 rule->action_ptr(rule, px, s, txn);
3680 break;
3681
3682 case HTTP_RES_ACT_CUSTOM_STOP:
3683 rule->action_ptr(rule, px, s, txn);
3684 return rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003685 }
3686 }
3687
3688 /* we reached the end of the rules, nothing to report */
3689 return NULL;
3690}
3691
3692
Willy Tarreau71241ab2012-12-27 11:30:54 +01003693/* Perform an HTTP redirect based on the information in <rule>. The function
3694 * returns non-zero on success, or zero in case of a, irrecoverable error such
3695 * as too large a request to build a valid response.
3696 */
3697static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3698{
3699 struct http_msg *msg = &txn->req;
3700 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003701 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003702
3703 /* build redirect message */
3704 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003705 case 308:
3706 msg_fmt = HTTP_308;
3707 break;
3708 case 307:
3709 msg_fmt = HTTP_307;
3710 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003711 case 303:
3712 msg_fmt = HTTP_303;
3713 break;
3714 case 301:
3715 msg_fmt = HTTP_301;
3716 break;
3717 case 302:
3718 default:
3719 msg_fmt = HTTP_302;
3720 break;
3721 }
3722
3723 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3724 return 0;
3725
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003726 location = trash.str + trash.len;
3727
Willy Tarreau71241ab2012-12-27 11:30:54 +01003728 switch(rule->type) {
3729 case REDIRECT_TYPE_SCHEME: {
3730 const char *path;
3731 const char *host;
3732 struct hdr_ctx ctx;
3733 int pathlen;
3734 int hostlen;
3735
3736 host = "";
3737 hostlen = 0;
3738 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003739 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003740 host = ctx.line + ctx.val;
3741 hostlen = ctx.vlen;
3742 }
3743
3744 path = http_get_path(txn);
3745 /* build message using path */
3746 if (path) {
3747 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3748 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3749 int qs = 0;
3750 while (qs < pathlen) {
3751 if (path[qs] == '?') {
3752 pathlen = qs;
3753 break;
3754 }
3755 qs++;
3756 }
3757 }
3758 } else {
3759 path = "/";
3760 pathlen = 1;
3761 }
3762
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003763 if (rule->rdr_str) { /* this is an old "redirect" rule */
3764 /* check if we can add scheme + "://" + host + path */
3765 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3766 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003767
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003768 /* add scheme */
3769 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3770 trash.len += rule->rdr_len;
3771 }
3772 else {
3773 /* add scheme with executing log format */
3774 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003775
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003776 /* check if we can add scheme + "://" + host + path */
3777 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3778 return 0;
3779 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003780 /* add "://" */
3781 memcpy(trash.str + trash.len, "://", 3);
3782 trash.len += 3;
3783
3784 /* add host */
3785 memcpy(trash.str + trash.len, host, hostlen);
3786 trash.len += hostlen;
3787
3788 /* add path */
3789 memcpy(trash.str + trash.len, path, pathlen);
3790 trash.len += pathlen;
3791
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003792 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003793 if (trash.len && trash.str[trash.len - 1] != '/' &&
3794 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3795 if (trash.len > trash.size - 5)
3796 return 0;
3797 trash.str[trash.len] = '/';
3798 trash.len++;
3799 }
3800
3801 break;
3802 }
3803 case REDIRECT_TYPE_PREFIX: {
3804 const char *path;
3805 int pathlen;
3806
3807 path = http_get_path(txn);
3808 /* build message using path */
3809 if (path) {
3810 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3811 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3812 int qs = 0;
3813 while (qs < pathlen) {
3814 if (path[qs] == '?') {
3815 pathlen = qs;
3816 break;
3817 }
3818 qs++;
3819 }
3820 }
3821 } else {
3822 path = "/";
3823 pathlen = 1;
3824 }
3825
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003826 if (rule->rdr_str) { /* this is an old "redirect" rule */
3827 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3828 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003829
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003830 /* add prefix. Note that if prefix == "/", we don't want to
3831 * add anything, otherwise it makes it hard for the user to
3832 * configure a self-redirection.
3833 */
3834 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3835 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3836 trash.len += rule->rdr_len;
3837 }
3838 }
3839 else {
3840 /* add prefix with executing log format */
3841 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3842
3843 /* Check length */
3844 if (trash.len + pathlen > trash.size - 4)
3845 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003846 }
3847
3848 /* add path */
3849 memcpy(trash.str + trash.len, path, pathlen);
3850 trash.len += pathlen;
3851
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003852 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003853 if (trash.len && trash.str[trash.len - 1] != '/' &&
3854 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3855 if (trash.len > trash.size - 5)
3856 return 0;
3857 trash.str[trash.len] = '/';
3858 trash.len++;
3859 }
3860
3861 break;
3862 }
3863 case REDIRECT_TYPE_LOCATION:
3864 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003865 if (rule->rdr_str) { /* this is an old "redirect" rule */
3866 if (trash.len + rule->rdr_len > trash.size - 4)
3867 return 0;
3868
3869 /* add location */
3870 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3871 trash.len += rule->rdr_len;
3872 }
3873 else {
3874 /* add location with executing log format */
3875 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003876
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003877 /* Check left length */
3878 if (trash.len > trash.size - 4)
3879 return 0;
3880 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003881 break;
3882 }
3883
3884 if (rule->cookie_len) {
3885 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3886 trash.len += 14;
3887 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3888 trash.len += rule->cookie_len;
3889 memcpy(trash.str + trash.len, "\r\n", 2);
3890 trash.len += 2;
3891 }
3892
3893 /* add end of headers and the keep-alive/close status.
3894 * We may choose to set keep-alive if the Location begins
3895 * with a slash, because the client will come back to the
3896 * same server.
3897 */
3898 txn->status = rule->code;
3899 /* let's log the request time */
3900 s->logs.tv_request = now;
3901
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003902 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003903 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3904 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3905 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3906 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3907 /* keep-alive possible */
3908 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3909 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3910 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3911 trash.len += 30;
3912 } else {
3913 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3914 trash.len += 24;
3915 }
3916 }
3917 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3918 trash.len += 4;
3919 bo_inject(txn->rsp.chn, trash.str, trash.len);
3920 /* "eat" the request */
3921 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02003922 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003923 msg->sov = 0;
3924 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3925 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3926 txn->req.msg_state = HTTP_MSG_CLOSED;
3927 txn->rsp.msg_state = HTTP_MSG_DONE;
3928 } else {
3929 /* keep-alive not possible */
3930 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3931 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3932 trash.len += 29;
3933 } else {
3934 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3935 trash.len += 23;
3936 }
3937 stream_int_retnclose(txn->req.chn->prod, &trash);
3938 txn->req.chn->analysers = 0;
3939 }
3940
3941 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003942 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003943 if (!(s->flags & SN_FINST_MASK))
3944 s->flags |= SN_FINST_R;
3945
3946 return 1;
3947}
3948
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003949/* This stream analyser runs all HTTP request processing which is common to
3950 * frontends and backends, which means blocking ACLs, filters, connection-close,
3951 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003952 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003953 * either needs more data or wants to immediately abort the request (eg: deny,
3954 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003955 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003956int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003957{
Willy Tarreaud787e662009-07-07 10:14:51 +02003958 struct http_txn *txn = &s->txn;
3959 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003960 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003961 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003962 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02003963
Willy Tarreau655dce92009-11-08 13:10:58 +01003964 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003965 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003966 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003967 return 0;
3968 }
3969
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003970 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 +02003971 now_ms, __FUNCTION__,
3972 s,
3973 req,
3974 req->rex, req->wex,
3975 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003976 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003977 req->analysers);
3978
Willy Tarreau65410832014-04-28 21:25:43 +02003979 /* just in case we have some per-backend tracking */
3980 session_inc_be_http_req_ctr(s);
3981
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003982 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003983 if (!LIST_ISEMPTY(&px->http_req_rules)) {
3984 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003985
Willy Tarreau0b748332014-04-29 00:13:29 +02003986 switch (verdict) {
3987 case HTTP_RULE_RES_CONT:
3988 case HTTP_RULE_RES_STOP: /* nothing to do */
3989 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003990
Willy Tarreau0b748332014-04-29 00:13:29 +02003991 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3992 if (txn->flags & TX_CLTARPIT)
3993 goto tarpit;
3994 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003995
Willy Tarreau0b748332014-04-29 00:13:29 +02003996 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3997 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003998
Willy Tarreau0b748332014-04-29 00:13:29 +02003999 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004000 goto done;
4001
Willy Tarreau0b748332014-04-29 00:13:29 +02004002 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4003 goto return_bad_req;
4004 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004005 }
4006
Willy Tarreau52542592014-04-28 18:33:26 +02004007 /* OK at this stage, we know that the request was accepted according to
4008 * the http-request rules, we can check for the stats. Note that the
4009 * URI is detected *before* the req* rules in order not to be affected
4010 * by a possible reqrep, while they are processed *after* so that a
4011 * reqdeny can still block them. This clearly needs to change in 1.6!
4012 */
4013 if (stats_check_uri(s->rep->prod, txn, px)) {
4014 s->target = &http_stats_applet.obj_type;
4015 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
4016 txn->status = 500;
4017 s->logs.tv_request = now;
4018 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004019
Willy Tarreau52542592014-04-28 18:33:26 +02004020 if (!(s->flags & SN_ERR_MASK))
4021 s->flags |= SN_ERR_RESOURCE;
4022 goto return_prx_cond;
4023 }
4024
4025 /* parse the whole stats request and extract the relevant information */
4026 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004027 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4028 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004029
Willy Tarreau0b748332014-04-29 00:13:29 +02004030 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4031 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004032
Willy Tarreau0b748332014-04-29 00:13:29 +02004033 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4034 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004035 }
4036
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004037 /* evaluate the req* rules except reqadd */
4038 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004039 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004040 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004041
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004042 if (txn->flags & TX_CLDENY)
4043 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004044
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004045 if (txn->flags & TX_CLTARPIT)
4046 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004047 }
Willy Tarreau06619262006-12-17 08:37:22 +01004048
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004049 /* add request headers from the rule sets in the same order */
4050 list_for_each_entry(wl, &px->req_add, list) {
4051 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004052 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004053 ret = acl_pass(ret);
4054 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4055 ret = !ret;
4056 if (!ret)
4057 continue;
4058 }
4059
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004060 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004061 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004062 }
4063
Willy Tarreau52542592014-04-28 18:33:26 +02004064
4065 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004066 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004067 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004068 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
4069 s->fe->fe_counters.intercepted_req++;
4070
4071 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
4072 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
4073 if (!(s->flags & SN_FINST_MASK))
4074 s->flags |= SN_FINST_R;
4075
Willy Tarreau70730dd2014-04-24 18:06:27 +02004076 /* we may want to compress the stats page */
4077 if (s->fe->comp || s->be->comp)
4078 select_compression_request_header(s, req->buf);
4079
4080 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreaude8ca962014-11-20 22:23:10 +01004081 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004082 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004083 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004084
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004085 /* check whether we have some ACLs set to redirect this request */
4086 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004087 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004088 int ret;
4089
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004090 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004091 ret = acl_pass(ret);
4092 if (rule->cond->pol == ACL_COND_UNLESS)
4093 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004094 if (!ret)
4095 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004096 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004097 if (!http_apply_redirect_rule(rule, s, txn))
4098 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004099 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004100 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004101
Willy Tarreau2be39392010-01-03 17:24:51 +01004102 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4103 * If this happens, then the data will not come immediately, so we must
4104 * send all what we have without waiting. Note that due to the small gain
4105 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004106 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004107 * itself once used.
4108 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004109 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004110
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004111 done: /* done with this analyser, continue with next ones that the calling
4112 * points will have set, if any.
4113 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004114 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004115 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4116 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004117 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004118
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004119 tarpit:
4120 /* When a connection is tarpitted, we use the tarpit timeout,
4121 * which may be the same as the connect timeout if unspecified.
4122 * If unset, then set it to zero because we really want it to
4123 * eventually expire. We build the tarpit as an analyser.
4124 */
4125 channel_erase(s->req);
4126
4127 /* wipe the request out so that we can drop the connection early
4128 * if the client closes first.
4129 */
4130 channel_dont_connect(req);
4131 req->analysers = 0; /* remove switching rules etc... */
4132 req->analysers |= AN_REQ_HTTP_TARPIT;
4133 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4134 if (!req->analyse_exp)
4135 req->analyse_exp = tick_add(now_ms, 0);
4136 session_inc_http_err_ctr(s);
4137 s->fe->fe_counters.denied_req++;
4138 if (s->fe != s->be)
4139 s->be->be_counters.denied_req++;
4140 if (s->listener->counters)
4141 s->listener->counters->denied_req++;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004142 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004143
4144 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004145 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004146 txn->status = 403;
4147 s->logs.tv_request = now;
4148 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4149 session_inc_http_err_ctr(s);
4150 s->fe->fe_counters.denied_req++;
4151 if (s->fe != s->be)
4152 s->be->be_counters.denied_req++;
4153 if (s->listener->counters)
4154 s->listener->counters->denied_req++;
4155 goto return_prx_cond;
4156
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004157 return_bad_req:
4158 /* We centralize bad requests processing here */
4159 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4160 /* we detected a parsing error. We want to archive this request
4161 * in the dedicated proxy area for later troubleshooting.
4162 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004163 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004164 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004165
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004166 txn->req.msg_state = HTTP_MSG_ERROR;
4167 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004168 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004169
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004170 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004171 if (s->listener->counters)
4172 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004173
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004174 return_prx_cond:
4175 if (!(s->flags & SN_ERR_MASK))
4176 s->flags |= SN_ERR_PRXCOND;
4177 if (!(s->flags & SN_FINST_MASK))
4178 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004179
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004180 req->analysers = 0;
4181 req->analyse_exp = TICK_ETERNITY;
4182 return 0;
4183}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004184
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004185/* This function performs all the processing enabled for the current request.
4186 * It returns 1 if the processing can continue on next analysers, or zero if it
4187 * needs more data, encounters an error, or wants to immediately abort the
4188 * request. It relies on buffers flags, and updates s->req->analysers.
4189 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004190int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004191{
4192 struct http_txn *txn = &s->txn;
4193 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004194 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004195
Willy Tarreau655dce92009-11-08 13:10:58 +01004196 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004197 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004198 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004199 return 0;
4200 }
4201
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004202 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 +02004203 now_ms, __FUNCTION__,
4204 s,
4205 req,
4206 req->rex, req->wex,
4207 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004208 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004209 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004210
William Lallemand82fe75c2012-10-23 10:25:10 +02004211 if (s->fe->comp || s->be->comp)
4212 select_compression_request_header(s, req->buf);
4213
Willy Tarreau59234e92008-11-30 23:51:27 +01004214 /*
4215 * Right now, we know that we have processed the entire headers
4216 * and that unwanted requests have been filtered out. We can do
4217 * whatever we want with the remaining request. Also, now we
4218 * may have separate values for ->fe, ->be.
4219 */
Willy Tarreau06619262006-12-17 08:37:22 +01004220
Willy Tarreau59234e92008-11-30 23:51:27 +01004221 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004222 * If HTTP PROXY is set we simply get remote server address parsing
4223 * incoming request. Note that this requires that a connection is
4224 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004225 */
4226 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004227 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004228 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004229
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004230 /* Note that for now we don't reuse existing proxy connections */
4231 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004232 txn->req.msg_state = HTTP_MSG_ERROR;
4233 txn->status = 500;
4234 req->analysers = 0;
4235 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4236
4237 if (!(s->flags & SN_ERR_MASK))
4238 s->flags |= SN_ERR_RESOURCE;
4239 if (!(s->flags & SN_FINST_MASK))
4240 s->flags |= SN_FINST_R;
4241
4242 return 0;
4243 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004244
4245 path = http_get_path(txn);
4246 url2sa(req->buf->p + msg->sl.rq.u,
4247 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004248 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004249 /* if the path was found, we have to remove everything between
4250 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4251 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4252 * u_l characters by a single "/".
4253 */
4254 if (path) {
4255 char *cur_ptr = req->buf->p;
4256 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4257 int delta;
4258
4259 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4260 http_msg_move_end(&txn->req, delta);
4261 cur_end += delta;
4262 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4263 goto return_bad_req;
4264 }
4265 else {
4266 char *cur_ptr = req->buf->p;
4267 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4268 int delta;
4269
4270 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4271 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4272 http_msg_move_end(&txn->req, delta);
4273 cur_end += delta;
4274 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4275 goto return_bad_req;
4276 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004277 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004278
Willy Tarreau59234e92008-11-30 23:51:27 +01004279 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004280 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004281 * Note that doing so might move headers in the request, but
4282 * the fields will stay coherent and the URI will not move.
4283 * This should only be performed in the backend.
4284 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004285 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004286 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4287 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004288
Willy Tarreau59234e92008-11-30 23:51:27 +01004289 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004290 * 8: the appsession cookie was looked up very early in 1.2,
4291 * so let's do the same now.
4292 */
4293
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004294 /* It needs to look into the URI unless persistence must be ignored */
4295 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004296 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 +01004297 }
4298
William Lallemanda73203e2012-03-12 12:48:57 +01004299 /* add unique-id if "header-unique-id" is specified */
4300
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004301 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4302 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4303 goto return_bad_req;
4304 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004305 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004306 }
William Lallemanda73203e2012-03-12 12:48:57 +01004307
4308 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004309 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4310 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004311 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004312 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004313 goto return_bad_req;
4314 }
4315
Cyril Bontéb21570a2009-11-29 20:04:48 +01004316 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004317 * 9: add X-Forwarded-For if either the frontend or the backend
4318 * asks for it.
4319 */
4320 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004321 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004322 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004323 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4324 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004325 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004326 /* The header is set to be added only if none is present
4327 * and we found it, so don't do anything.
4328 */
4329 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004330 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004331 /* Add an X-Forwarded-For header unless the source IP is
4332 * in the 'except' network range.
4333 */
4334 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004335 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004336 != s->fe->except_net.s_addr) &&
4337 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004338 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004339 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004340 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004341 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004342 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004343
4344 /* Note: we rely on the backend to get the header name to be used for
4345 * x-forwarded-for, because the header is really meant for the backends.
4346 * However, if the backend did not specify any option, we have to rely
4347 * on the frontend's header name.
4348 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004349 if (s->be->fwdfor_hdr_len) {
4350 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004351 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004352 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004353 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004354 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004355 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004356 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 +01004357
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004358 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004359 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004360 }
4361 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004362 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004363 /* FIXME: for the sake of completeness, we should also support
4364 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004365 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004366 int len;
4367 char pn[INET6_ADDRSTRLEN];
4368 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004369 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004370 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004371
Willy Tarreau59234e92008-11-30 23:51:27 +01004372 /* Note: we rely on the backend to get the header name to be used for
4373 * x-forwarded-for, because the header is really meant for the backends.
4374 * However, if the backend did not specify any option, we have to rely
4375 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004376 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004377 if (s->be->fwdfor_hdr_len) {
4378 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004379 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004380 } else {
4381 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004382 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004383 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004384 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004385
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004386 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004387 goto return_bad_req;
4388 }
4389 }
4390
4391 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004392 * 10: add X-Original-To if either the frontend or the backend
4393 * asks for it.
4394 */
4395 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4396
4397 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004398 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004399 /* Add an X-Original-To header unless the destination IP is
4400 * in the 'except' network range.
4401 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004402 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004403
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004404 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004405 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004406 (((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 +02004407 != s->fe->except_to.s_addr) &&
4408 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004409 (((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 +02004410 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004411 int len;
4412 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004413 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004414
4415 /* Note: we rely on the backend to get the header name to be used for
4416 * x-original-to, because the header is really meant for the backends.
4417 * However, if the backend did not specify any option, we have to rely
4418 * on the frontend's header name.
4419 */
4420 if (s->be->orgto_hdr_len) {
4421 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004422 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004423 } else {
4424 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004425 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004426 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004427 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 +02004428
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004429 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004430 goto return_bad_req;
4431 }
4432 }
4433 }
4434
Willy Tarreau50fc7772012-11-11 22:19:57 +01004435 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4436 * If an "Upgrade" token is found, the header is left untouched in order not to have
4437 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4438 * "Upgrade" is present in the Connection header.
4439 */
4440 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4441 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004442 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4443 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004444 unsigned int want_flags = 0;
4445
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004446 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004447 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004448 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4449 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004450 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004451 want_flags |= TX_CON_CLO_SET;
4452 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004453 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004454 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4455 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004456 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004457 want_flags |= TX_CON_KAL_SET;
4458 }
4459
4460 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004461 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004462 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004463
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004464
Willy Tarreau522d6c02009-12-06 18:49:18 +01004465 /* If we have no server assigned yet and we're balancing on url_param
4466 * with a POST request, we may be interested in checking the body for
4467 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004468 */
4469 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4470 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004471 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004472 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004473 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004474 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004475
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004476 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004477 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004478#ifdef TCP_QUICKACK
4479 /* We expect some data from the client. Unless we know for sure
4480 * we already have a full request, we have to re-enable quick-ack
4481 * in case we previously disabled it, otherwise we might cause
4482 * the client to delay further data.
4483 */
4484 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004485 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004486 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004487 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004488 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004489#endif
4490 }
Willy Tarreau03945942009-12-22 16:50:27 +01004491
Willy Tarreau59234e92008-11-30 23:51:27 +01004492 /*************************************************************
4493 * OK, that's finished for the headers. We have done what we *
4494 * could. Let's switch to the DATA state. *
4495 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004496 req->analyse_exp = TICK_ETERNITY;
4497 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004498
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004499 /* if the server closes the connection, we want to immediately react
4500 * and close the socket to save packets and syscalls.
4501 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004502 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4503 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004504
Willy Tarreau59234e92008-11-30 23:51:27 +01004505 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004506 /* OK let's go on with the BODY now */
4507 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004508
Willy Tarreau59234e92008-11-30 23:51:27 +01004509 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004510 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004511 /* we detected a parsing error. We want to archive this request
4512 * in the dedicated proxy area for later troubleshooting.
4513 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004514 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004515 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004516
Willy Tarreau59234e92008-11-30 23:51:27 +01004517 txn->req.msg_state = HTTP_MSG_ERROR;
4518 txn->status = 400;
4519 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004520 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004521
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004522 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004523 if (s->listener->counters)
4524 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004525
Willy Tarreau59234e92008-11-30 23:51:27 +01004526 if (!(s->flags & SN_ERR_MASK))
4527 s->flags |= SN_ERR_PRXCOND;
4528 if (!(s->flags & SN_FINST_MASK))
4529 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004530 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004531}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004532
Willy Tarreau60b85b02008-11-30 23:28:40 +01004533/* This function is an analyser which processes the HTTP tarpit. It always
4534 * returns zero, at the beginning because it prevents any other processing
4535 * from occurring, and at the end because it terminates the request.
4536 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004537int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004538{
4539 struct http_txn *txn = &s->txn;
4540
4541 /* This connection is being tarpitted. The CLIENT side has
4542 * already set the connect expiration date to the right
4543 * timeout. We just have to check that the client is still
4544 * there and that the timeout has not expired.
4545 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004546 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004547 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004548 !tick_is_expired(req->analyse_exp, now_ms))
4549 return 0;
4550
4551 /* We will set the queue timer to the time spent, just for
4552 * logging purposes. We fake a 500 server error, so that the
4553 * attacker will not suspect his connection has been tarpitted.
4554 * It will not cause trouble to the logs because we can exclude
4555 * the tarpitted connections by filtering on the 'PT' status flags.
4556 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004557 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4558
4559 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004560 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004561 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004562
4563 req->analysers = 0;
4564 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004565
Willy Tarreau60b85b02008-11-30 23:28:40 +01004566 if (!(s->flags & SN_ERR_MASK))
4567 s->flags |= SN_ERR_PRXCOND;
4568 if (!(s->flags & SN_FINST_MASK))
4569 s->flags |= SN_FINST_T;
4570 return 0;
4571}
4572
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004573/* This function is an analyser which waits for the HTTP request body. It waits
4574 * for either the buffer to be full, or the full advertised contents to have
4575 * reached the buffer. It must only be called after the standard HTTP request
4576 * processing has occurred, because it expects the request to be parsed and will
4577 * look for the Expect header. It may send a 100-Continue interim response. It
4578 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4579 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4580 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004581 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004582int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004583{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004584 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004585 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004586
4587 /* We have to parse the HTTP request body to find any required data.
4588 * "balance url_param check_post" should have been the only way to get
4589 * into this. We were brought here after HTTP header analysis, so all
4590 * related structures are ready.
4591 */
4592
Willy Tarreau890988f2014-04-10 11:59:33 +02004593 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4594 /* This is the first call */
4595 if (msg->msg_state < HTTP_MSG_BODY)
4596 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004597
Willy Tarreau890988f2014-04-10 11:59:33 +02004598 if (msg->msg_state < HTTP_MSG_100_SENT) {
4599 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4600 * send an HTTP/1.1 100 Continue intermediate response.
4601 */
4602 if (msg->flags & HTTP_MSGF_VER_11) {
4603 struct hdr_ctx ctx;
4604 ctx.idx = 0;
4605 /* Expect is allowed in 1.1, look for it */
4606 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4607 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4608 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4609 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004610 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004611 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004612 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004613
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004614 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004615 * req->buf->p still points to the beginning of the message. We
4616 * must save the body in msg->next because it survives buffer
4617 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004618 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004619 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004620
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004621 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004622 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4623 else
4624 msg->msg_state = HTTP_MSG_DATA;
4625 }
4626
Willy Tarreau890988f2014-04-10 11:59:33 +02004627 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4628 /* We're in content-length mode, we just have to wait for enough data. */
4629 if (req->buf->i - msg->sov < msg->body_len)
4630 goto missing_data;
4631
4632 /* OK we have everything we need now */
4633 goto http_end;
4634 }
4635
4636 /* OK here we're parsing a chunked-encoded message */
4637
Willy Tarreau522d6c02009-12-06 18:49:18 +01004638 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004639 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004640 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004641 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004642 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004643 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004644
Willy Tarreau115acb92009-12-26 13:56:06 +01004645 if (!ret)
4646 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004647 else if (ret < 0) {
4648 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004649 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004650 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004651 }
4652
Willy Tarreaud98cf932009-12-27 22:54:55 +01004653 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004654 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004655 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004656 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004657 if (msg->msg_state == HTTP_MSG_TRAILERS)
4658 goto http_end;
4659
Willy Tarreau226071e2014-04-10 11:55:45 +02004660 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004661 goto http_end;
4662
4663 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004664 /* we get here if we need to wait for more data. If the buffer is full,
4665 * we have the maximum we can expect.
4666 */
4667 if (buffer_full(req->buf, global.tune.maxrewrite))
4668 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004669
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004670 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004671 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004672 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004673
4674 if (!(s->flags & SN_ERR_MASK))
4675 s->flags |= SN_ERR_CLITO;
4676 if (!(s->flags & SN_FINST_MASK))
4677 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004678 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004679 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004680
4681 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004682 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004683 /* Not enough data. We'll re-use the http-request
4684 * timeout here. Ideally, we should set the timeout
4685 * relative to the accept() date. We just set the
4686 * request timeout once at the beginning of the
4687 * request.
4688 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004689 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004690 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004691 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004692 return 0;
4693 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004694
4695 http_end:
4696 /* The situation will not evolve, so let's give up on the analysis. */
4697 s->logs.tv_request = now; /* update the request timer to reflect full request */
4698 req->analysers &= ~an_bit;
4699 req->analyse_exp = TICK_ETERNITY;
4700 return 1;
4701
4702 return_bad_req: /* let's centralize all bad requests */
4703 txn->req.msg_state = HTTP_MSG_ERROR;
4704 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004705 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004706
Willy Tarreau79ebac62010-06-07 13:47:49 +02004707 if (!(s->flags & SN_ERR_MASK))
4708 s->flags |= SN_ERR_PRXCOND;
4709 if (!(s->flags & SN_FINST_MASK))
4710 s->flags |= SN_FINST_R;
4711
Willy Tarreau522d6c02009-12-06 18:49:18 +01004712 return_err_msg:
4713 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004714 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004715 if (s->listener->counters)
4716 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004717 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004718}
4719
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004720/* send a server's name with an outgoing request over an established connection.
4721 * Note: this function is designed to be called once the request has been scheduled
4722 * for being forwarded. This is the reason why it rewinds the buffer before
4723 * proceeding.
4724 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004725int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004726
4727 struct hdr_ctx ctx;
4728
Mark Lamourinec2247f02012-01-04 13:02:01 -05004729 char *hdr_name = be->server_id_hdr_name;
4730 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004731 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004732 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004733 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004734
William Lallemandd9e90662012-01-30 17:27:17 +01004735 ctx.idx = 0;
4736
Willy Tarreau211cdec2014-04-17 20:18:08 +02004737 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004738 if (old_o) {
4739 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004740 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004741 txn->req.next += old_o;
4742 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004743 }
4744
Willy Tarreau9b28e032012-10-12 23:49:43 +02004745 old_i = chn->buf->i;
4746 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 -05004747 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004748 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004749 }
4750
4751 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004752 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004753 memcpy(hdr_val, hdr_name, hdr_name_len);
4754 hdr_val += hdr_name_len;
4755 *hdr_val++ = ':';
4756 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004757 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4758 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004759
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004760 if (old_o) {
4761 /* If this was a forwarded request, we must readjust the amount of
4762 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004763 * variations. Note that the current state is >= HTTP_MSG_BODY,
4764 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004765 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004766 old_o += chn->buf->i - old_i;
4767 b_adv(chn->buf, old_o);
4768 txn->req.next -= old_o;
4769 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004770 }
4771
Mark Lamourinec2247f02012-01-04 13:02:01 -05004772 return 0;
4773}
4774
Willy Tarreau610ecce2010-01-04 21:15:02 +01004775/* Terminate current transaction and prepare a new one. This is very tricky
4776 * right now but it works.
4777 */
4778void http_end_txn_clean_session(struct session *s)
4779{
Willy Tarreau068621e2013-12-23 15:11:25 +01004780 int prev_status = s->txn.status;
4781
Willy Tarreau610ecce2010-01-04 21:15:02 +01004782 /* FIXME: We need a more portable way of releasing a backend's and a
4783 * server's connections. We need a safer way to reinitialize buffer
4784 * flags. We also need a more accurate method for computing per-request
4785 * data.
4786 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004787
Willy Tarreau4213a112013-12-15 10:25:42 +01004788 /* unless we're doing keep-alive, we want to quickly close the connection
4789 * to the server.
4790 */
4791 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4792 !si_conn_ready(s->req->cons)) {
4793 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4794 si_shutr(s->req->cons);
4795 si_shutw(s->req->cons);
4796 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004797
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004798 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004799 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004800 if (unlikely(s->srv_conn))
4801 sess_change_server(s, NULL);
4802 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004803
4804 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4805 session_process_counters(s);
4806
4807 if (s->txn.status) {
4808 int n;
4809
4810 n = s->txn.status / 100;
4811 if (n < 1 || n > 5)
4812 n = 0;
4813
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004814 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004815 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004816 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004817 s->fe->fe_counters.p.http.comp_rsp++;
4818 }
Willy Tarreau24657792010-02-26 10:30:28 +01004819 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004820 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004821 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004822 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004823 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004824 s->be->be_counters.p.http.comp_rsp++;
4825 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004826 }
4827
4828 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004829 s->logs.bytes_in -= s->req->buf->i;
4830 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004831
4832 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004833 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004834 !(s->flags & SN_MONITOR) &&
4835 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4836 s->do_log(s);
4837 }
4838
Willy Tarreauc177ea72014-06-25 15:36:04 +02004839 /* stop tracking content-based counters */
4840 session_stop_content_counters(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004841 session_update_time_stats(s);
4842
Willy Tarreau610ecce2010-01-04 21:15:02 +01004843 s->logs.accept_date = date; /* user-visible date for logging */
4844 s->logs.tv_accept = now; /* corrected date for internal use */
4845 tv_zero(&s->logs.tv_request);
4846 s->logs.t_queue = -1;
4847 s->logs.t_connect = -1;
4848 s->logs.t_data = -1;
4849 s->logs.t_close = 0;
4850 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4851 s->logs.srv_queue_size = 0; /* we will get this number soon */
4852
Willy Tarreau9b28e032012-10-12 23:49:43 +02004853 s->logs.bytes_in = s->req->total = s->req->buf->i;
4854 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004855
4856 if (s->pend_pos)
4857 pendconn_free(s->pend_pos);
4858
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004859 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004860 if (s->flags & SN_CURR_SESS) {
4861 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004862 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004863 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004864 if (may_dequeue_tasks(objt_server(s->target), s->be))
4865 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004866 }
4867
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004868 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004869
Willy Tarreau4213a112013-12-15 10:25:42 +01004870 /* only release our endpoint if we don't intend to reuse the
4871 * connection.
4872 */
4873 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4874 !si_conn_ready(s->req->cons)) {
4875 si_release_endpoint(s->req->cons);
4876 }
4877
Willy Tarreau610ecce2010-01-04 21:15:02 +01004878 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004879 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004880 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004881 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004882 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreaub4d05092014-09-01 20:35:55 +02004883 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);
4884 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 +02004885 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 +01004886 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Cyril Bonté17f77072014-10-22 22:30:13 +02004887 s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004888
Willy Tarreau610ecce2010-01-04 21:15:02 +01004889 s->txn.meth = 0;
4890 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004891 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004892
4893 if (prev_status == 401 || prev_status == 407) {
4894 /* In HTTP keep-alive mode, if we receive a 401, we still have
4895 * a chance of being able to send the visitor again to the same
4896 * server over the same connection. This is required by some
4897 * broken protocols such as NTLM, and anyway whenever there is
4898 * an opportunity for sending the challenge to the proper place,
4899 * it's better to do it (at least it helps with debugging).
4900 */
4901 s->txn.flags |= TX_PREFER_LAST;
4902 }
4903
Willy Tarreauee55dc02010-06-01 10:56:34 +02004904 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004905 s->req->cons->flags |= SI_FL_INDEP_STR;
4906
Willy Tarreau96e31212011-05-30 18:10:30 +02004907 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004908 s->req->flags |= CF_NEVER_WAIT;
4909 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004910 }
4911
Willy Tarreau610ecce2010-01-04 21:15:02 +01004912 /* if the request buffer is not empty, it means we're
4913 * about to process another request, so send pending
4914 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004915 * Just don't do this if the buffer is close to be full,
4916 * because the request will wait for it to flush a little
4917 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004918 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004919 if (s->req->buf->i) {
4920 if (s->rep->buf->o &&
4921 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4922 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004923 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004924 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004925
4926 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004927 channel_auto_read(s->req);
4928 channel_auto_close(s->req);
4929 channel_auto_read(s->rep);
4930 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004931
Willy Tarreau27375622013-12-17 00:00:28 +01004932 /* we're in keep-alive with an idle connection, monitor it */
4933 si_idle_conn(s->req->cons);
4934
Willy Tarreau342b11c2010-11-24 16:22:09 +01004935 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004936 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004937}
4938
4939
4940/* This function updates the request state machine according to the response
4941 * state machine and buffer flags. It returns 1 if it changes anything (flag
4942 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4943 * it is only used to find when a request/response couple is complete. Both
4944 * this function and its equivalent should loop until both return zero. It
4945 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4946 */
4947int http_sync_req_state(struct session *s)
4948{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004949 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004950 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004951 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004952 unsigned int old_state = txn->req.msg_state;
4953
Willy Tarreau610ecce2010-01-04 21:15:02 +01004954 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4955 return 0;
4956
4957 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004958 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004959 * We can shut the read side unless we want to abort_on_close,
4960 * or we have a POST request. The issue with POST requests is
4961 * that some browsers still send a CRLF after the request, and
4962 * this CRLF must be read so that it does not remain in the kernel
4963 * buffers, otherwise a close could cause an RST on some systems
4964 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004965 * Note that if we're using keep-alive on the client side, we'd
4966 * rather poll now and keep the polling enabled for the whole
4967 * session's life than enabling/disabling it between each
4968 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004969 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004970 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4971 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4972 !(s->be->options & PR_O_ABRT_CLOSE) &&
4973 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004974 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004975
Willy Tarreau40f151a2012-12-20 12:10:09 +01004976 /* if the server closes the connection, we want to immediately react
4977 * and close the socket to save packets and syscalls.
4978 */
4979 chn->cons->flags |= SI_FL_NOHALF;
4980
Willy Tarreau610ecce2010-01-04 21:15:02 +01004981 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4982 goto wait_other_side;
4983
4984 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4985 /* The server has not finished to respond, so we
4986 * don't want to move in order not to upset it.
4987 */
4988 goto wait_other_side;
4989 }
4990
4991 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4992 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004993 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004994 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004995 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004996 goto wait_other_side;
4997 }
4998
4999 /* When we get here, it means that both the request and the
5000 * response have finished receiving. Depending on the connection
5001 * mode, we'll have to wait for the last bytes to leave in either
5002 * direction, and sometimes for a close to be effective.
5003 */
5004
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005005 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5006 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005007 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5008 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005009 }
5010 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5011 /* Option forceclose is set, or either side wants to close,
5012 * let's enforce it now that we're not expecting any new
5013 * data to come. The caller knows the session is complete
5014 * once both states are CLOSED.
5015 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005016 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5017 channel_shutr_now(chn);
5018 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005019 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005020 }
5021 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005022 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5023 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005024 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005025 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5026 channel_auto_read(chn);
5027 txn->req.msg_state = HTTP_MSG_TUNNEL;
5028 chn->flags |= CF_NEVER_WAIT;
5029 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005030 }
5031
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005032 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005033 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005034 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005035
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005036 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005037 txn->req.msg_state = HTTP_MSG_CLOSING;
5038 goto http_msg_closing;
5039 }
5040 else {
5041 txn->req.msg_state = HTTP_MSG_CLOSED;
5042 goto http_msg_closed;
5043 }
5044 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005045 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005046 }
5047
5048 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5049 http_msg_closing:
5050 /* nothing else to forward, just waiting for the output buffer
5051 * to be empty and for the shutw_now to take effect.
5052 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005053 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005054 txn->req.msg_state = HTTP_MSG_CLOSED;
5055 goto http_msg_closed;
5056 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005057 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005058 txn->req.msg_state = HTTP_MSG_ERROR;
5059 goto wait_other_side;
5060 }
5061 }
5062
5063 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5064 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005065 /* see above in MSG_DONE why we only do this in these states */
5066 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5067 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5068 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005069 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005070 goto wait_other_side;
5071 }
5072
5073 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005074 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005075}
5076
5077
5078/* This function updates the response state machine according to the request
5079 * state machine and buffer flags. It returns 1 if it changes anything (flag
5080 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5081 * it is only used to find when a request/response couple is complete. Both
5082 * this function and its equivalent should loop until both return zero. It
5083 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5084 */
5085int http_sync_res_state(struct session *s)
5086{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005087 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005088 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005089 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005090 unsigned int old_state = txn->rsp.msg_state;
5091
Willy Tarreau610ecce2010-01-04 21:15:02 +01005092 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5093 return 0;
5094
5095 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5096 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005097 * still monitor the server connection for a possible close
5098 * while the request is being uploaded, so we don't disable
5099 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005100 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005101 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005102
5103 if (txn->req.msg_state == HTTP_MSG_ERROR)
5104 goto wait_other_side;
5105
5106 if (txn->req.msg_state < HTTP_MSG_DONE) {
5107 /* The client seems to still be sending data, probably
5108 * because we got an error response during an upload.
5109 * We have the choice of either breaking the connection
5110 * or letting it pass through. Let's do the later.
5111 */
5112 goto wait_other_side;
5113 }
5114
5115 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5116 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005117 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005118 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005119 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005120 goto wait_other_side;
5121 }
5122
5123 /* When we get here, it means that both the request and the
5124 * response have finished receiving. Depending on the connection
5125 * mode, we'll have to wait for the last bytes to leave in either
5126 * direction, and sometimes for a close to be effective.
5127 */
5128
5129 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5130 /* Server-close mode : shut read and wait for the request
5131 * side to close its output buffer. The caller will detect
5132 * when we're in DONE and the other is in CLOSED and will
5133 * catch that for the final cleanup.
5134 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005135 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5136 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005137 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005138 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5139 /* Option forceclose is set, or either side wants to close,
5140 * let's enforce it now that we're not expecting any new
5141 * data to come. The caller knows the session is complete
5142 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005143 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005144 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5145 channel_shutr_now(chn);
5146 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005147 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005148 }
5149 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005150 /* The last possible modes are keep-alive and tunnel. Tunnel will
5151 * need to forward remaining data. Keep-alive will need to monitor
5152 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005153 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005154 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005155 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005156 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5157 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005158 }
5159
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005160 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005161 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005162 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005163 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5164 goto http_msg_closing;
5165 }
5166 else {
5167 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5168 goto http_msg_closed;
5169 }
5170 }
5171 goto wait_other_side;
5172 }
5173
5174 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5175 http_msg_closing:
5176 /* nothing else to forward, just waiting for the output buffer
5177 * to be empty and for the shutw_now to take effect.
5178 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005179 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005180 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5181 goto http_msg_closed;
5182 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005183 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005184 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005185 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005186 if (objt_server(s->target))
5187 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005188 goto wait_other_side;
5189 }
5190 }
5191
5192 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5193 http_msg_closed:
5194 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005195 bi_erase(chn);
5196 channel_auto_close(chn);
5197 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005198 goto wait_other_side;
5199 }
5200
5201 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005202 /* We force the response to leave immediately if we're waiting for the
5203 * other side, since there is no pending shutdown to push it out.
5204 */
5205 if (!channel_is_empty(chn))
5206 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005207 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005208}
5209
5210
5211/* Resync the request and response state machines. Return 1 if either state
5212 * changes.
5213 */
5214int http_resync_states(struct session *s)
5215{
5216 struct http_txn *txn = &s->txn;
5217 int old_req_state = txn->req.msg_state;
5218 int old_res_state = txn->rsp.msg_state;
5219
Willy Tarreau610ecce2010-01-04 21:15:02 +01005220 http_sync_req_state(s);
5221 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005222 if (!http_sync_res_state(s))
5223 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005224 if (!http_sync_req_state(s))
5225 break;
5226 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005227
Willy Tarreau610ecce2010-01-04 21:15:02 +01005228 /* OK, both state machines agree on a compatible state.
5229 * There are a few cases we're interested in :
5230 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5231 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5232 * directions, so let's simply disable both analysers.
5233 * - HTTP_MSG_CLOSED on the response only means we must abort the
5234 * request.
5235 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5236 * with server-close mode means we've completed one request and we
5237 * must re-initialize the server connection.
5238 */
5239
5240 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5241 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5242 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5243 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5244 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005245 channel_auto_close(s->req);
5246 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005247 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005248 channel_auto_close(s->rep);
5249 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005250 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005251 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5252 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005253 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005254 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +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 Tarreau90deb182010-01-07 00:20:41 +01005258 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005259 channel_abort(s->req);
5260 channel_auto_close(s->req);
5261 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005262 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005263 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005264 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5265 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005266 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005267 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5268 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5269 /* server-close/keep-alive: terminate this transaction,
5270 * possibly killing the server connection and reinitialize
5271 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005272 */
5273 http_end_txn_clean_session(s);
5274 }
5275
Willy Tarreau610ecce2010-01-04 21:15:02 +01005276 return txn->req.msg_state != old_req_state ||
5277 txn->rsp.msg_state != old_res_state;
5278}
5279
Willy Tarreaud98cf932009-12-27 22:54:55 +01005280/* This function is an analyser which forwards request body (including chunk
5281 * sizes if any). It is called as soon as we must forward, even if we forward
5282 * zero byte. The only situation where it must not be called is when we're in
5283 * tunnel mode and we want to forward till the close. It's used both to forward
5284 * remaining data and to resync after end of body. It expects the msg_state to
5285 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5286 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005287 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005288 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005289 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005290int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005291{
5292 struct http_txn *txn = &s->txn;
5293 struct http_msg *msg = &s->txn.req;
5294
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005295 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5296 return 0;
5297
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005298 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005299 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005300 /* Output closed while we were sending data. We must abort and
5301 * wake the other side up.
5302 */
5303 msg->msg_state = HTTP_MSG_ERROR;
5304 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005305 return 1;
5306 }
5307
Willy Tarreaud98cf932009-12-27 22:54:55 +01005308 /* Note that we don't have to send 100-continue back because we don't
5309 * need the data to complete our job, and it's up to the server to
5310 * decide whether to return 100, 417 or anything else in return of
5311 * an "Expect: 100-continue" header.
5312 */
5313
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005314 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005315 /* we have msg->sov which points to the first byte of message
5316 * body, and req->buf.p still points to the beginning of the
5317 * message. We forward the headers now, as we don't need them
5318 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005319 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005320 b_adv(req->buf, msg->sov);
5321 msg->next -= msg->sov;
5322 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005323
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005324 /* The previous analysers guarantee that the state is somewhere
5325 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5326 * msg->next are always correct.
5327 */
5328 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5329 if (msg->flags & HTTP_MSGF_TE_CHNK)
5330 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5331 else
5332 msg->msg_state = HTTP_MSG_DATA;
5333 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005334 }
5335
Willy Tarreau7ba23542014-04-17 21:50:00 +02005336 /* Some post-connect processing might want us to refrain from starting to
5337 * forward data. Currently, the only reason for this is "balance url_param"
5338 * whichs need to parse/process the request after we've enabled forwarding.
5339 */
5340 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5341 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5342 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005343 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005344 goto missing_data;
5345 }
5346 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5347 }
5348
Willy Tarreau80a92c02014-03-12 10:41:13 +01005349 /* in most states, we should abort in case of early close */
5350 channel_auto_close(req);
5351
Willy Tarreauefdf0942014-04-24 20:08:57 +02005352 if (req->to_forward) {
5353 /* We can't process the buffer's contents yet */
5354 req->flags |= CF_WAKE_WRITE;
5355 goto missing_data;
5356 }
5357
Willy Tarreaud98cf932009-12-27 22:54:55 +01005358 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005359 if (msg->msg_state == HTTP_MSG_DATA) {
5360 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005361 /* we may have some pending data starting at req->buf->p */
5362 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005363 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005364 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005365 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005366 msg->next += msg->chunk_len;
5367 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005368
5369 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005370 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005371 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005372 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005373 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005374 }
5375 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005376 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005377 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005378 * TRAILERS state.
5379 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005380 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005381
Willy Tarreau54d23df2012-10-25 19:04:45 +02005382 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005383 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005384 else if (ret < 0) {
5385 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005386 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005387 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005388 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005389 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005390 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005391 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005392 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005393 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005394 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005395
5396 if (ret == 0)
5397 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005398 else if (ret < 0) {
5399 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005400 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005401 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005402 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005403 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005404 /* we're in MSG_CHUNK_SIZE now */
5405 }
5406 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005407 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005408
5409 if (ret == 0)
5410 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005411 else if (ret < 0) {
5412 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005413 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005414 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005415 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005416 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005417 /* we're in HTTP_MSG_DONE now */
5418 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005419 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005420 int old_state = msg->msg_state;
5421
Willy Tarreau610ecce2010-01-04 21:15:02 +01005422 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005423
5424 /* we may have some pending data starting at req->buf->p
5425 * such as last chunk of data or trailers.
5426 */
5427 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005428 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005429 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005430 msg->next = 0;
5431
Willy Tarreau4fe41902010-06-07 22:27:41 +02005432 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005433 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5434 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005435 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005436 if (http_resync_states(s)) {
5437 /* some state changes occurred, maybe the analyser
5438 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005439 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005440 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005441 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005442 /* request errors are most likely due to
5443 * the server aborting the transfer.
5444 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005445 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005446 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005447 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005448 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005449 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005450 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005451 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005452 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005453
5454 /* If "option abortonclose" is set on the backend, we
5455 * want to monitor the client's connection and forward
5456 * any shutdown notification to the server, which will
5457 * decide whether to close or to go on processing the
5458 * request.
5459 */
5460 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005461 channel_auto_read(req);
5462 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005463 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005464 else if (s->txn.meth == HTTP_METH_POST) {
5465 /* POST requests may require to read extra CRLF
5466 * sent by broken browsers and which could cause
5467 * an RST to be sent upon close on some systems
5468 * (eg: Linux).
5469 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005470 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005471 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005472
Willy Tarreau610ecce2010-01-04 21:15:02 +01005473 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005474 }
5475 }
5476
Willy Tarreaud98cf932009-12-27 22:54:55 +01005477 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005478 /* we may have some pending data starting at req->buf->p */
5479 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005480 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005481 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5482
Willy Tarreaubed410e2014-04-22 08:19:34 +02005483 msg->next = 0;
5484 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5485
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005486 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005487 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005488 if (!(s->flags & SN_ERR_MASK))
5489 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005490 if (!(s->flags & SN_FINST_MASK)) {
5491 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5492 s->flags |= SN_FINST_H;
5493 else
5494 s->flags |= SN_FINST_D;
5495 }
5496
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005497 s->fe->fe_counters.cli_aborts++;
5498 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005499 if (objt_server(s->target))
5500 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005501
5502 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005503 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005504
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005505 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005506 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005507 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005508
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005509 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005510 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005511 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005512 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005513 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005514
Willy Tarreau5c620922011-05-11 19:56:11 +02005515 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005516 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005517 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005518 * modes are already handled by the stream sock layer. We must not do
5519 * this in content-length mode because it could present the MSG_MORE
5520 * flag with the last block of forwarded data, which would cause an
5521 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005522 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005523 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005524 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005525
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005526 return 0;
5527
Willy Tarreaud98cf932009-12-27 22:54:55 +01005528 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005529 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005530 if (s->listener->counters)
5531 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005532
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005533 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005534 /* we may have some pending data starting at req->buf->p */
5535 b_adv(req->buf, msg->next);
5536 msg->next = 0;
5537
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005538 txn->req.msg_state = HTTP_MSG_ERROR;
5539 if (txn->status) {
5540 /* Note: we don't send any error if some data were already sent */
5541 stream_int_retnclose(req->prod, NULL);
5542 } else {
5543 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005544 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005545 }
5546 req->analysers = 0;
5547 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005548
5549 if (!(s->flags & SN_ERR_MASK))
5550 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005551 if (!(s->flags & SN_FINST_MASK)) {
5552 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5553 s->flags |= SN_FINST_H;
5554 else
5555 s->flags |= SN_FINST_D;
5556 }
5557 return 0;
5558
5559 aborted_xfer:
5560 txn->req.msg_state = HTTP_MSG_ERROR;
5561 if (txn->status) {
5562 /* Note: we don't send any error if some data were already sent */
5563 stream_int_retnclose(req->prod, NULL);
5564 } else {
5565 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005566 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005567 }
5568 req->analysers = 0;
5569 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5570
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005571 s->fe->fe_counters.srv_aborts++;
5572 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005573 if (objt_server(s->target))
5574 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005575
5576 if (!(s->flags & SN_ERR_MASK))
5577 s->flags |= SN_ERR_SRVCL;
5578 if (!(s->flags & SN_FINST_MASK)) {
5579 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5580 s->flags |= SN_FINST_H;
5581 else
5582 s->flags |= SN_FINST_D;
5583 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005584 return 0;
5585}
5586
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005587/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5588 * processing can continue on next analysers, or zero if it either needs more
5589 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5590 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5591 * when it has nothing left to do, and may remove any analyser when it wants to
5592 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005593 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005594int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005595{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005596 struct http_txn *txn = &s->txn;
5597 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005598 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005599 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005600 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005601 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005602
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005603 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 +02005604 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005605 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005606 rep,
5607 rep->rex, rep->wex,
5608 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005609 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005610 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005611
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005612 /*
5613 * Now parse the partial (or complete) lines.
5614 * We will check the response syntax, and also join multi-line
5615 * headers. An index of all the lines will be elaborated while
5616 * parsing.
5617 *
5618 * For the parsing, we use a 28 states FSM.
5619 *
5620 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005621 * rep->buf->p = beginning of response
5622 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5623 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005624 * msg->eol = end of current header or line (LF or CRLF)
5625 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005626 */
5627
Willy Tarreau628c40c2014-04-24 19:11:26 +02005628 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005629 /* There's a protected area at the end of the buffer for rewriting
5630 * purposes. We don't want to start to parse the request if the
5631 * protected area is affected, because we may have to move processed
5632 * data later, which is much more complicated.
5633 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005634 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005635 if (unlikely(!channel_reserved(rep))) {
5636 /* some data has still not left the buffer, wake us once that's done */
5637 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5638 goto abort_response;
5639 channel_dont_close(rep);
5640 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005641 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005642 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005643 }
5644
Willy Tarreau379357a2013-06-08 12:55:46 +02005645 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5646 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5647 buffer_slow_realign(rep->buf);
5648
Willy Tarreau9b28e032012-10-12 23:49:43 +02005649 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005650 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005651 }
5652
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005653 /* 1: we might have to print this header in debug mode */
5654 if (unlikely((global.mode & MODE_DEBUG) &&
5655 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau8767b132014-10-21 19:36:09 +02005656 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005657 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005658
Willy Tarreau9b28e032012-10-12 23:49:43 +02005659 sol = rep->buf->p;
5660 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005661 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005662
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005663 sol += hdr_idx_first_pos(&txn->hdr_idx);
5664 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005665
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005666 while (cur_idx) {
5667 eol = sol + txn->hdr_idx.v[cur_idx].len;
5668 debug_hdr("srvhdr", s, sol, eol);
5669 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5670 cur_idx = txn->hdr_idx.v[cur_idx].next;
5671 }
5672 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005673
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005674 /*
5675 * Now we quickly check if we have found a full valid response.
5676 * If not so, we check the FD and buffer states before leaving.
5677 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005678 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005679 * responses are checked first.
5680 *
5681 * Depending on whether the client is still there or not, we
5682 * may send an error response back or not. Note that normally
5683 * we should only check for HTTP status there, and check I/O
5684 * errors somewhere else.
5685 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005686
Willy Tarreau655dce92009-11-08 13:10:58 +01005687 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005688 /* Invalid response */
5689 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5690 /* we detected a parsing error. We want to archive this response
5691 * in the dedicated proxy area for later troubleshooting.
5692 */
5693 hdr_response_bad:
5694 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005695 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005696
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005697 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005698 if (objt_server(s->target)) {
5699 objt_server(s->target)->counters.failed_resp++;
5700 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005701 }
Willy Tarreau64648412010-03-05 10:41:54 +01005702 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005703 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005704 rep->analysers = 0;
5705 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005706 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005707 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005708 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005709
5710 if (!(s->flags & SN_ERR_MASK))
5711 s->flags |= SN_ERR_PRXCOND;
5712 if (!(s->flags & SN_FINST_MASK))
5713 s->flags |= SN_FINST_H;
5714
5715 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005716 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005717
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005718 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005719 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005720 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005721 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005722 goto hdr_response_bad;
5723 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005724
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005725 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005726 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005727 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005728 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005729 else if (txn->flags & TX_NOT_FIRST)
5730 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005731
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005732 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005733 if (objt_server(s->target)) {
5734 objt_server(s->target)->counters.failed_resp++;
5735 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005736 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005737
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005738 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005739 rep->analysers = 0;
5740 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005741 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005742 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005743 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005744
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005745 if (!(s->flags & SN_ERR_MASK))
5746 s->flags |= SN_ERR_SRVCL;
5747 if (!(s->flags & SN_FINST_MASK))
5748 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005749 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005750 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005751
Willy Tarreau2a4f5112014-06-23 15:22:31 +02005752 /* read timeout : return a 504 to the client. */
5753 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005754 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005755 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005756 else if (txn->flags & TX_NOT_FIRST)
5757 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005758
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005759 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005760 if (objt_server(s->target)) {
5761 objt_server(s->target)->counters.failed_resp++;
5762 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005763 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005764
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005765 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005766 rep->analysers = 0;
5767 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005768 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005769 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005770 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005771
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005772 if (!(s->flags & SN_ERR_MASK))
5773 s->flags |= SN_ERR_SRVTO;
5774 if (!(s->flags & SN_FINST_MASK))
5775 s->flags |= SN_FINST_H;
5776 return 0;
5777 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005778
Willy Tarreauf003d372012-11-26 13:35:37 +01005779 /* client abort with an abortonclose */
5780 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5781 s->fe->fe_counters.cli_aborts++;
5782 s->be->be_counters.cli_aborts++;
5783 if (objt_server(s->target))
5784 objt_server(s->target)->counters.cli_aborts++;
5785
5786 rep->analysers = 0;
5787 channel_auto_close(rep);
5788
5789 txn->status = 400;
5790 bi_erase(rep);
5791 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5792
5793 if (!(s->flags & SN_ERR_MASK))
5794 s->flags |= SN_ERR_CLICL;
5795 if (!(s->flags & SN_FINST_MASK))
5796 s->flags |= SN_FINST_H;
5797
5798 /* process_session() will take care of the error */
5799 return 0;
5800 }
5801
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005802 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005803 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005804 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005805 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005806 else if (txn->flags & TX_NOT_FIRST)
5807 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005808
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005809 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005810 if (objt_server(s->target)) {
5811 objt_server(s->target)->counters.failed_resp++;
5812 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005813 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005814
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005815 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005816 rep->analysers = 0;
5817 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005818 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005819 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005820 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005821
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005822 if (!(s->flags & SN_ERR_MASK))
5823 s->flags |= SN_ERR_SRVCL;
5824 if (!(s->flags & SN_FINST_MASK))
5825 s->flags |= SN_FINST_H;
5826 return 0;
5827 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005828
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005829 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005830 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005831 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005832 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005833 else if (txn->flags & TX_NOT_FIRST)
5834 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005835
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005836 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005837 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005838 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005839
5840 if (!(s->flags & SN_ERR_MASK))
5841 s->flags |= SN_ERR_CLICL;
5842 if (!(s->flags & SN_FINST_MASK))
5843 s->flags |= SN_FINST_H;
5844
5845 /* process_session() will take care of the error */
5846 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005847 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005848
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005849 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005850 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005851 return 0;
5852 }
5853
5854 /* More interesting part now : we know that we have a complete
5855 * response which at least looks like HTTP. We have an indicator
5856 * of each header's length, so we can parse them quickly.
5857 */
5858
5859 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005860 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005861
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005862 /*
5863 * 1: get the status code
5864 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005865 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005866 if (n < 1 || n > 5)
5867 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005868 /* when the client triggers a 4xx from the server, it's most often due
5869 * to a missing object or permission. These events should be tracked
5870 * because if they happen often, it may indicate a brute force or a
5871 * vulnerability scan.
5872 */
5873 if (n == 4)
5874 session_inc_http_err_ctr(s);
5875
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005876 if (objt_server(s->target))
5877 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005878
Willy Tarreau5b154472009-12-21 20:11:07 +01005879 /* check if the response is HTTP/1.1 or above */
5880 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005881 ((rep->buf->p[5] > '1') ||
5882 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005883 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005884
5885 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005886 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 +01005887
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005888 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005889 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005890
Willy Tarreau9b28e032012-10-12 23:49:43 +02005891 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005892
Willy Tarreau39650402010-03-15 19:44:39 +01005893 /* Adjust server's health based on status code. Note: status codes 501
5894 * and 505 are triggered on demand by client request, so we must not
5895 * count them as server failures.
5896 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005897 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005898 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005899 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005900 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005901 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005902 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005903
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005904 /*
5905 * 2: check for cacheability.
5906 */
5907
5908 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005909 case 100:
5910 /*
5911 * We may be facing a 100-continue response, in which case this
5912 * is not the right response, and we're waiting for the next one.
5913 * Let's allow this response to go to the client and wait for the
5914 * next one.
5915 */
5916 hdr_idx_init(&txn->hdr_idx);
5917 msg->next -= channel_forward(rep, msg->next);
5918 msg->msg_state = HTTP_MSG_RPBEFORE;
5919 txn->status = 0;
5920 s->logs.t_data = -1; /* was not a response yet */
5921 goto next_one;
5922
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005923 case 200:
5924 case 203:
5925 case 206:
5926 case 300:
5927 case 301:
5928 case 410:
5929 /* RFC2616 @13.4:
5930 * "A response received with a status code of
5931 * 200, 203, 206, 300, 301 or 410 MAY be stored
5932 * by a cache (...) unless a cache-control
5933 * directive prohibits caching."
5934 *
5935 * RFC2616 @9.5: POST method :
5936 * "Responses to this method are not cacheable,
5937 * unless the response includes appropriate
5938 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005939 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005940 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005941 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005942 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5943 break;
5944 default:
5945 break;
5946 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005947
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005948 /*
5949 * 3: we may need to capture headers
5950 */
5951 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005952 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005953 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005954 txn->rsp.cap, s->fe->rsp_cap);
5955
Willy Tarreau4db603d2015-05-01 10:05:17 +02005956 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5957 * by RFC7230#3.3.3 :
5958 *
5959 * The length of a message body is determined by one of the following
5960 * (in order of precedence):
5961 *
5962 * 1. Any response to a HEAD request and any response with a 1xx
5963 * (Informational), 204 (No Content), or 304 (Not Modified) status
5964 * code is always terminated by the first empty line after the
5965 * header fields, regardless of the header fields present in the
5966 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005967 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02005968 * 2. Any 2xx (Successful) response to a CONNECT request implies that
5969 * the connection will become a tunnel immediately after the empty
5970 * line that concludes the header fields. A client MUST ignore any
5971 * Content-Length or Transfer-Encoding header fields received in
5972 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005973 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02005974 * 3. If a Transfer-Encoding header field is present and the chunked
5975 * transfer coding (Section 4.1) is the final encoding, the message
5976 * body length is determined by reading and decoding the chunked
5977 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005978 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02005979 * If a Transfer-Encoding header field is present in a response and
5980 * the chunked transfer coding is not the final encoding, the
5981 * message body length is determined by reading the connection until
5982 * it is closed by the server. If a Transfer-Encoding header field
5983 * is present in a request and the chunked transfer coding is not
5984 * the final encoding, the message body length cannot be determined
5985 * reliably; the server MUST respond with the 400 (Bad Request)
5986 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005987 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02005988 * If a message is received with both a Transfer-Encoding and a
5989 * Content-Length header field, the Transfer-Encoding overrides the
5990 * Content-Length. Such a message might indicate an attempt to
5991 * perform request smuggling (Section 9.5) or response splitting
5992 * (Section 9.4) and ought to be handled as an error. A sender MUST
5993 * remove the received Content-Length field prior to forwarding such
5994 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005995 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02005996 * 4. If a message is received without Transfer-Encoding and with
5997 * either multiple Content-Length header fields having differing
5998 * field-values or a single Content-Length header field having an
5999 * invalid value, then the message framing is invalid and the
6000 * recipient MUST treat it as an unrecoverable error. If this is a
6001 * request message, the server MUST respond with a 400 (Bad Request)
6002 * status code and then close the connection. If this is a response
6003 * message received by a proxy, the proxy MUST close the connection
6004 * to the server, discard the received response, and send a 502 (Bad
6005 * Gateway) response to the client. If this is a response message
6006 * received by a user agent, the user agent MUST close the
6007 * connection to the server and discard the received response.
6008 *
6009 * 5. If a valid Content-Length header field is present without
6010 * Transfer-Encoding, its decimal value defines the expected message
6011 * body length in octets. If the sender closes the connection or
6012 * the recipient times out before the indicated number of octets are
6013 * received, the recipient MUST consider the message to be
6014 * incomplete and close the connection.
6015 *
6016 * 6. If this is a request message and none of the above are true, then
6017 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006018 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006019 * 7. Otherwise, this is a response message without a declared message
6020 * body length, so the message body length is determined by the
6021 * number of octets received prior to the server closing the
6022 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006023 */
6024
6025 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006026 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006027 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006028 * FIXME: should we parse anyway and return an error on chunked encoding ?
6029 */
6030 if (txn->meth == HTTP_METH_HEAD ||
6031 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006032 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006033 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006034 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006035 goto skip_content_length;
6036 }
6037
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006038 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006039 ctx.idx = 0;
Willy Tarreaue77bc172015-05-01 10:06:30 +02006040 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006041 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006042 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6043 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006044 /* bad transfer-encoding (chunked followed by something else) */
6045 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006046 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006047 break;
6048 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006049 }
6050
Willy Tarreaudfa3d922015-04-30 10:57:51 +02006051 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006052 ctx.idx = 0;
Willy Tarreaudfa3d922015-04-30 10:57:51 +02006053 if (msg->flags & HTTP_MSGF_TE_CHNK) {
6054 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6055 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6056 }
6057 else while (!use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006058 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006059 signed long long cl;
6060
Willy Tarreauad14f752011-09-02 20:33:27 +02006061 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006062 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006063 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006064 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006065
Willy Tarreauad14f752011-09-02 20:33:27 +02006066 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006067 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006068 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006069 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006070
Willy Tarreauad14f752011-09-02 20:33:27 +02006071 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006072 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006073 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006074 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006075
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006076 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006077 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006078 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006079 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006080
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006081 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006082 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006083 }
6084
William Lallemand82fe75c2012-10-23 10:25:10 +02006085 if (s->fe->comp || s->be->comp)
6086 select_compression_response_header(s, rep->buf);
6087
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006088skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006089 /* Now we have to check if we need to modify the Connection header.
6090 * This is more difficult on the response than it is on the request,
6091 * because we can have two different HTTP versions and we don't know
6092 * how the client will interprete a response. For instance, let's say
6093 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6094 * HTTP/1.1 response without any header. Maybe it will bound itself to
6095 * HTTP/1.0 because it only knows about it, and will consider the lack
6096 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6097 * the lack of header as a keep-alive. Thus we will use two flags
6098 * indicating how a request MAY be understood by the client. In case
6099 * of multiple possibilities, we'll fix the header to be explicit. If
6100 * ambiguous cases such as both close and keepalive are seen, then we
6101 * will fall back to explicit close. Note that we won't take risks with
6102 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006103 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006104 */
6105
Willy Tarreaudc008c52010-02-01 16:20:08 +01006106 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6107 txn->status == 101)) {
6108 /* Either we've established an explicit tunnel, or we're
6109 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006110 * to understand the next protocols. We have to switch to tunnel
6111 * mode, so that we transfer the request and responses then let
6112 * this protocol pass unmodified. When we later implement specific
6113 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006114 * header which contains information about that protocol for
6115 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006116 */
6117 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6118 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006119 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6120 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006121 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6122 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006123 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006124
Willy Tarreau70dffda2014-01-30 03:07:23 +01006125 /* this situation happens when combining pretend-keepalive with httpclose. */
6126 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006127 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6128 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006129 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6130
Willy Tarreau60466522010-01-18 19:08:45 +01006131 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006132 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006133 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6134 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006135
Willy Tarreau60466522010-01-18 19:08:45 +01006136 /* now adjust header transformations depending on current state */
6137 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6138 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6139 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006140 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006141 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006142 }
Willy Tarreau60466522010-01-18 19:08:45 +01006143 else { /* SCL / KAL */
6144 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006145 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006146 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006147 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006148
Willy Tarreau60466522010-01-18 19:08:45 +01006149 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006150 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006151
Willy Tarreau60466522010-01-18 19:08:45 +01006152 /* Some keep-alive responses are converted to Server-close if
6153 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006154 */
Willy Tarreau60466522010-01-18 19:08:45 +01006155 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6156 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006157 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006158 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006159 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006160 }
6161
Willy Tarreau7959a552013-09-23 16:44:27 +02006162 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006163 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006164
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006165 /* end of job, return OK */
6166 rep->analysers &= ~an_bit;
6167 rep->analyse_exp = TICK_ETERNITY;
6168 channel_auto_close(rep);
6169 return 1;
6170
6171 abort_keep_alive:
6172 /* A keep-alive request to the server failed on a network error.
6173 * The client is required to retry. We need to close without returning
6174 * any other information so that the client retries.
6175 */
6176 txn->status = 0;
6177 rep->analysers = 0;
6178 s->req->analysers = 0;
6179 channel_auto_close(rep);
6180 s->logs.logwait = 0;
6181 s->logs.level = 0;
6182 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
6183 bi_erase(rep);
6184 stream_int_retnclose(rep->cons, NULL);
6185 return 0;
6186}
6187
6188/* This function performs all the processing enabled for the current response.
6189 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6190 * and updates s->rep->analysers. It might make sense to explode it into several
6191 * other functions. It works like process_request (see indications above).
6192 */
6193int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6194{
6195 struct http_txn *txn = &s->txn;
6196 struct http_msg *msg = &txn->rsp;
6197 struct proxy *cur_proxy;
6198 struct cond_wordlist *wl;
6199 struct http_res_rule *http_res_last_rule = NULL;
6200
6201 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6202 now_ms, __FUNCTION__,
6203 s,
6204 rep,
6205 rep->rex, rep->wex,
6206 rep->flags,
6207 rep->buf->i,
6208 rep->analysers);
6209
6210 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6211 return 0;
6212
6213 rep->analysers &= ~an_bit;
6214 rep->analyse_exp = TICK_ETERNITY;
6215
Willy Tarreau70730dd2014-04-24 18:06:27 +02006216 /* The stats applet needs to adjust the Connection header but we don't
6217 * apply any filter there.
6218 */
6219 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6220 goto skip_filters;
6221
Willy Tarreau58975672014-04-24 21:13:57 +02006222 /*
6223 * We will have to evaluate the filters.
6224 * As opposed to version 1.2, now they will be evaluated in the
6225 * filters order and not in the header order. This means that
6226 * each filter has to be validated among all headers.
6227 *
6228 * Filters are tried with ->be first, then with ->fe if it is
6229 * different from ->be.
6230 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006231
Willy Tarreau58975672014-04-24 21:13:57 +02006232 cur_proxy = s->be;
6233 while (1) {
6234 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006235
Willy Tarreau58975672014-04-24 21:13:57 +02006236 /* evaluate http-response rules */
6237 if (!http_res_last_rule)
6238 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 +02006239
Willy Tarreau58975672014-04-24 21:13:57 +02006240 /* try headers filters */
6241 if (rule_set->rsp_exp != NULL) {
6242 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6243 return_bad_resp:
6244 if (objt_server(s->target)) {
6245 objt_server(s->target)->counters.failed_resp++;
6246 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006247 }
Willy Tarreau58975672014-04-24 21:13:57 +02006248 s->be->be_counters.failed_resp++;
6249 return_srv_prx_502:
6250 rep->analysers = 0;
6251 txn->status = 502;
6252 s->logs.t_data = -1; /* was not a valid response */
6253 rep->prod->flags |= SI_FL_NOLINGER;
6254 bi_erase(rep);
6255 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6256 if (!(s->flags & SN_ERR_MASK))
6257 s->flags |= SN_ERR_PRXCOND;
6258 if (!(s->flags & SN_FINST_MASK))
6259 s->flags |= SN_FINST_H;
6260 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006261 }
Willy Tarreau58975672014-04-24 21:13:57 +02006262 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006263
Willy Tarreau58975672014-04-24 21:13:57 +02006264 /* has the response been denied ? */
6265 if (txn->flags & TX_SVDENY) {
6266 if (objt_server(s->target))
6267 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006268
Willy Tarreau58975672014-04-24 21:13:57 +02006269 s->be->be_counters.denied_resp++;
6270 s->fe->fe_counters.denied_resp++;
6271 if (s->listener->counters)
6272 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006273
Willy Tarreau58975672014-04-24 21:13:57 +02006274 goto return_srv_prx_502;
6275 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006276
Willy Tarreau58975672014-04-24 21:13:57 +02006277 /* add response headers from the rule sets in the same order */
6278 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006279 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006280 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006281 if (wl->cond) {
6282 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6283 ret = acl_pass(ret);
6284 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6285 ret = !ret;
6286 if (!ret)
6287 continue;
6288 }
6289 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6290 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006291 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006292
Willy Tarreau58975672014-04-24 21:13:57 +02006293 /* check whether we're already working on the frontend */
6294 if (cur_proxy == s->fe)
6295 break;
6296 cur_proxy = s->fe;
6297 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006298
Willy Tarreau58975672014-04-24 21:13:57 +02006299 /* OK that's all we can do for 1xx responses */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006300 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006301 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006302
Willy Tarreau58975672014-04-24 21:13:57 +02006303 /*
6304 * Now check for a server cookie.
6305 */
6306 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6307 (s->be->options & PR_O_CHK_CACHE))
6308 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006309
Willy Tarreau58975672014-04-24 21:13:57 +02006310 /*
6311 * Check for cache-control or pragma headers if required.
6312 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006313 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 +02006314 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006315
Willy Tarreau58975672014-04-24 21:13:57 +02006316 /*
6317 * Add server cookie in the response if needed
6318 */
6319 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6320 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6321 (!(s->flags & SN_DIRECT) ||
6322 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6323 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6324 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6325 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6326 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6327 !(s->flags & SN_IGNORE_PRST)) {
6328 /* the server is known, it's not the one the client requested, or the
6329 * cookie's last seen date needs to be refreshed. We have to
6330 * insert a set-cookie here, except if we want to insert only on POST
6331 * requests and this one isn't. Note that servers which don't have cookies
6332 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006333 */
Willy Tarreau58975672014-04-24 21:13:57 +02006334 if (!objt_server(s->target)->cookie) {
6335 chunk_printf(&trash,
6336 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6337 s->be->cookie_name);
6338 }
6339 else {
6340 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006341
Willy Tarreau58975672014-04-24 21:13:57 +02006342 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6343 /* emit last_date, which is mandatory */
6344 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6345 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6346 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006347
Willy Tarreau58975672014-04-24 21:13:57 +02006348 if (s->be->cookie_maxlife) {
6349 /* emit first_date, which is either the original one or
6350 * the current date.
6351 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006352 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006353 s30tob64(txn->cookie_first_date ?
6354 txn->cookie_first_date >> 2 :
6355 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006356 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006357 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006358 }
Willy Tarreau58975672014-04-24 21:13:57 +02006359 chunk_appendf(&trash, "; path=/");
6360 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006361
Willy Tarreau58975672014-04-24 21:13:57 +02006362 if (s->be->cookie_domain)
6363 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006364
Willy Tarreau58975672014-04-24 21:13:57 +02006365 if (s->be->ck_opts & PR_CK_HTTPONLY)
6366 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006367
Willy Tarreau58975672014-04-24 21:13:57 +02006368 if (s->be->ck_opts & PR_CK_SECURE)
6369 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006370
Willy Tarreau58975672014-04-24 21:13:57 +02006371 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6372 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006373
Willy Tarreau58975672014-04-24 21:13:57 +02006374 txn->flags &= ~TX_SCK_MASK;
6375 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6376 /* the server did not change, only the date was updated */
6377 txn->flags |= TX_SCK_UPDATED;
6378 else
6379 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006380
Willy Tarreau58975672014-04-24 21:13:57 +02006381 /* Here, we will tell an eventual cache on the client side that we don't
6382 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6383 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6384 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006385 */
Willy Tarreau58975672014-04-24 21:13:57 +02006386 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006387
Willy Tarreau58975672014-04-24 21:13:57 +02006388 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006389
Willy Tarreau58975672014-04-24 21:13:57 +02006390 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6391 "Cache-control: private", 22) < 0))
6392 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006393 }
Willy Tarreau58975672014-04-24 21:13:57 +02006394 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006395
Willy Tarreau58975672014-04-24 21:13:57 +02006396 /*
6397 * Check if result will be cacheable with a cookie.
6398 * We'll block the response if security checks have caught
6399 * nasty things such as a cacheable cookie.
6400 */
6401 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6402 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6403 (s->be->options & PR_O_CHK_CACHE)) {
6404 /* we're in presence of a cacheable response containing
6405 * a set-cookie header. We'll block it as requested by
6406 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006407 */
Willy Tarreau58975672014-04-24 21:13:57 +02006408 if (objt_server(s->target))
6409 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006410
Willy Tarreau58975672014-04-24 21:13:57 +02006411 s->be->be_counters.denied_resp++;
6412 s->fe->fe_counters.denied_resp++;
6413 if (s->listener->counters)
6414 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006415
Willy Tarreau58975672014-04-24 21:13:57 +02006416 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6417 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6418 send_log(s->be, LOG_ALERT,
6419 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6420 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6421 goto return_srv_prx_502;
6422 }
Willy Tarreau03945942009-12-22 16:50:27 +01006423
Willy Tarreau70730dd2014-04-24 18:06:27 +02006424 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006425 /*
6426 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6427 * If an "Upgrade" token is found, the header is left untouched in order
6428 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006429 * if anything but "Upgrade" is present in the Connection header. We don't
6430 * want to touch any 101 response either since it's switching to another
6431 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006432 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006433 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006434 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6435 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6436 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6437 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006438
Willy Tarreau58975672014-04-24 21:13:57 +02006439 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6440 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6441 /* we want a keep-alive response here. Keep-alive header
6442 * required if either side is not 1.1.
6443 */
6444 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6445 want_flags |= TX_CON_KAL_SET;
6446 }
6447 else {
6448 /* we want a close response here. Close header required if
6449 * the server is 1.1, regardless of the client.
6450 */
6451 if (msg->flags & HTTP_MSGF_VER_11)
6452 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006453 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006454
Willy Tarreau58975672014-04-24 21:13:57 +02006455 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6456 http_change_connection_header(txn, msg, want_flags);
6457 }
6458
6459 skip_header_mangling:
6460 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6461 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6462 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006463
Willy Tarreau58975672014-04-24 21:13:57 +02006464 /* if the user wants to log as soon as possible, without counting
6465 * bytes from the server, then this is the right moment. We have
6466 * to temporarily assign bytes_out to log what we currently have.
6467 */
6468 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6469 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6470 s->logs.bytes_out = txn->rsp.eoh;
6471 s->do_log(s);
6472 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006473 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006474 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006475}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006476
Willy Tarreaud98cf932009-12-27 22:54:55 +01006477/* This function is an analyser which forwards response body (including chunk
6478 * sizes if any). It is called as soon as we must forward, even if we forward
6479 * zero byte. The only situation where it must not be called is when we're in
6480 * tunnel mode and we want to forward till the close. It's used both to forward
6481 * remaining data and to resync after end of body. It expects the msg_state to
6482 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6483 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006484 *
6485 * It is capable of compressing response data both in content-length mode and
6486 * in chunked mode. The state machines follows different flows depending on
6487 * whether content-length and chunked modes are used, since there are no
6488 * trailers in content-length :
6489 *
6490 * chk-mode cl-mode
6491 * ,----- BODY -----.
6492 * / \
6493 * V size > 0 V chk-mode
6494 * .--> SIZE -------------> DATA -------------> CRLF
6495 * | | size == 0 | last byte |
6496 * | v final crlf v inspected |
6497 * | TRAILERS -----------> DONE |
6498 * | |
6499 * `----------------------------------------------'
6500 *
6501 * Compression only happens in the DATA state, and must be flushed in final
6502 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6503 * is performed at once on final states for all bytes parsed, or when leaving
6504 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006505 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006506int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006507{
6508 struct http_txn *txn = &s->txn;
6509 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006510 static struct buffer *tmpbuf = NULL;
6511 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006512 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006513
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006514 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6515 return 0;
6516
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006517 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006518 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006519 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006520 /* Output closed while we were sending data. We must abort and
6521 * wake the other side up.
6522 */
6523 msg->msg_state = HTTP_MSG_ERROR;
6524 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006525 return 1;
6526 }
6527
Willy Tarreau4fe41902010-06-07 22:27:41 +02006528 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006529 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006530
Willy Tarreau5bebcd02014-07-10 19:06:10 +02006531 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006532 /* we have msg->sov which points to the first byte of message
6533 * body, and res->buf.p still points to the beginning of the
6534 * message. We forward the headers now, as we don't need them
6535 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006536 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006537 b_adv(res->buf, msg->sov);
6538 msg->next -= msg->sov;
6539 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006540
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006541 /* The previous analysers guarantee that the state is somewhere
6542 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6543 * msg->next are always correct.
6544 */
6545 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6546 if (msg->flags & HTTP_MSGF_TE_CHNK)
6547 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6548 else
6549 msg->msg_state = HTTP_MSG_DATA;
6550 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006551 }
6552
Willy Tarreauefdf0942014-04-24 20:08:57 +02006553 if (res->to_forward) {
6554 /* We can't process the buffer's contents yet */
6555 res->flags |= CF_WAKE_WRITE;
6556 goto missing_data;
6557 }
6558
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006559 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6560 /* We need a compression buffer in the DATA state to put the
6561 * output of compressed data, and in CRLF state to let the
6562 * TRAILERS state finish the job of removing the trailing CRLF.
6563 */
6564 if (unlikely(tmpbuf == NULL)) {
6565 /* this is the first time we need the compression buffer */
6566 tmpbuf = pool_alloc2(pool2_buffer);
6567 if (tmpbuf == NULL)
6568 goto aborted_xfer; /* no memory */
6569 }
6570
6571 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006572 if (ret < 0) {
6573 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006574 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006575 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006576 compressing = 1;
6577 }
6578
Willy Tarreaud98cf932009-12-27 22:54:55 +01006579 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006580 switch (msg->msg_state - HTTP_MSG_DATA) {
6581 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006582 /* we may have some pending data starting at res->buf->p */
6583 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006584 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006585 if (ret < 0)
6586 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006587
Willy Tarreaud5a67832014-04-21 10:54:27 +02006588 if (msg->chunk_len) {
6589 /* input empty or output full */
6590 if (res->buf->i > msg->next)
6591 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006592 goto missing_data;
6593 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006594 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006595 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006596 if (msg->chunk_len > res->buf->i - msg->next) {
6597 /* output full */
6598 res->flags |= CF_WAKE_WRITE;
6599 goto missing_data;
6600 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006601 msg->next += msg->chunk_len;
6602 msg->chunk_len = 0;
6603 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006604
6605 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006606 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006607 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006608 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006609 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006610 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006611 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006612 /* fall through for HTTP_MSG_CHUNK_CRLF */
6613
6614 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6615 /* we want the CRLF after the data */
6616
6617 ret = http_skip_chunk_crlf(msg);
6618 if (ret == 0)
6619 goto missing_data;
6620 else if (ret < 0) {
6621 if (msg->err_pos >= 0)
6622 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6623 goto return_bad_res;
6624 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006625 /* we're in MSG_CHUNK_SIZE now, fall through */
6626
6627 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006628 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006629 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006630 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006631 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006632
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006633 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006634 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006635 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006636 else if (ret < 0) {
6637 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006638 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006639 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006640 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006641 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006642 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006643
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006644 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006645 if (unlikely(compressing)) {
6646 /* we need to flush output contents before syncing FSMs */
6647 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6648 compressing = 0;
6649 }
6650
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006651 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006652 if (ret == 0)
6653 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006654 else if (ret < 0) {
6655 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006656 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006657 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006658 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006659 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006660
6661 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006662 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006663 if (unlikely(compressing)) {
6664 /* we need to flush output contents before syncing FSMs */
6665 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6666 compressing = 0;
6667 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006668
Willy Tarreauc623c172014-04-18 09:53:50 +02006669 /* we may have some pending data starting at res->buf->p
6670 * such as a last chunk of data or trailers.
6671 */
6672 b_adv(res->buf, msg->next);
6673 msg->next = 0;
6674
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006675 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006676 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006677 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6678 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006679 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006680
Willy Tarreau610ecce2010-01-04 21:15:02 +01006681 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006682 /* some state changes occurred, maybe the analyser
6683 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006684 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006685 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006686 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006687 /* response errors are most likely due to
6688 * the client aborting the transfer.
6689 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006690 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006691 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006692 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006693 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006694 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006695 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006696 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006697 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006698 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006699 }
6700 }
6701
Willy Tarreaud98cf932009-12-27 22:54:55 +01006702 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006703 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006704 if (unlikely(compressing)) {
6705 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006706 compressing = 0;
6707 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006708
Willy Tarreauc623c172014-04-18 09:53:50 +02006709 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6710 b_adv(res->buf, msg->next);
6711 msg->next = 0;
6712 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6713 }
6714
Willy Tarreauf003d372012-11-26 13:35:37 +01006715 if (res->flags & CF_SHUTW)
6716 goto aborted_xfer;
6717
6718 /* stop waiting for data if the input is closed before the end. If the
6719 * client side was already closed, it means that the client has aborted,
6720 * so we don't want to count this as a server abort. Otherwise it's a
6721 * server abort.
6722 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006723 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006724 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006725 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006726 if (!(s->flags & SN_ERR_MASK))
6727 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006728 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006729 if (objt_server(s->target))
6730 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006731 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006732 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006733
Willy Tarreau40dba092010-03-04 18:14:51 +01006734 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006735 if (!s->req->analysers)
6736 goto return_bad_res;
6737
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006738 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006739 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006740 * Similarly, with keep-alive on the client side, we don't want to forward a
6741 * close.
6742 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006743 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006744 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6745 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006746 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006747
Willy Tarreau5c620922011-05-11 19:56:11 +02006748 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006749 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006750 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006751 * modes are already handled by the stream sock layer. We must not do
6752 * this in content-length mode because it could present the MSG_MORE
6753 * flag with the last block of forwarded data, which would cause an
6754 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006755 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006756 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006757 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006758
Willy Tarreaud98cf932009-12-27 22:54:55 +01006759 /* the session handler will take care of timeouts and errors */
6760 return 0;
6761
Willy Tarreau40dba092010-03-04 18:14:51 +01006762 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006763 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006764 if (objt_server(s->target))
6765 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006766
6767 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006768 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006769 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006770 compressing = 0;
6771 }
6772
Willy Tarreauc623c172014-04-18 09:53:50 +02006773 /* we may have some pending data starting at res->buf->p */
6774 if (s->comp_algo == NULL) {
6775 b_adv(res->buf, msg->next);
6776 msg->next = 0;
6777 }
6778
Willy Tarreaud98cf932009-12-27 22:54:55 +01006779 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006780 /* don't send any error message as we're in the body */
6781 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006782 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006783 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006784 if (objt_server(s->target))
6785 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006786
6787 if (!(s->flags & SN_ERR_MASK))
6788 s->flags |= SN_ERR_PRXCOND;
6789 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006790 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006791 return 0;
6792
6793 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006794 if (unlikely(compressing)) {
6795 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6796 compressing = 0;
6797 }
6798
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006799 txn->rsp.msg_state = HTTP_MSG_ERROR;
6800 /* don't send any error message as we're in the body */
6801 stream_int_retnclose(res->cons, NULL);
6802 res->analysers = 0;
6803 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6804
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006805 s->fe->fe_counters.cli_aborts++;
6806 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006807 if (objt_server(s->target))
6808 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006809
6810 if (!(s->flags & SN_ERR_MASK))
6811 s->flags |= SN_ERR_CLICL;
6812 if (!(s->flags & SN_FINST_MASK))
6813 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006814 return 0;
6815}
6816
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006817/* Iterate the same filter through all request headers.
6818 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006819 * Since it can manage the switch to another backend, it updates the per-proxy
6820 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006821 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006822int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006823{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006824 char *cur_ptr, *cur_end, *cur_next;
6825 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006826 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006827 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006828 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006829
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006830 last_hdr = 0;
6831
Willy Tarreau9b28e032012-10-12 23:49:43 +02006832 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006833 old_idx = 0;
6834
6835 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006836 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006837 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006838 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006839 (exp->action == ACT_ALLOW ||
6840 exp->action == ACT_DENY ||
6841 exp->action == ACT_TARPIT))
6842 return 0;
6843
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006844 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006845 if (!cur_idx)
6846 break;
6847
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006848 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006849 cur_ptr = cur_next;
6850 cur_end = cur_ptr + cur_hdr->len;
6851 cur_next = cur_end + cur_hdr->cr + 1;
6852
6853 /* Now we have one header between cur_ptr and cur_end,
6854 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006855 */
6856
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02006857 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006858 switch (exp->action) {
6859 case ACT_SETBE:
6860 /* It is not possible to jump a second time.
6861 * FIXME: should we return an HTTP/500 here so that
6862 * the admin knows there's a problem ?
6863 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006864 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006865 break;
6866
6867 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006868 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006869 last_hdr = 1;
6870 break;
6871
6872 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006873 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006874 last_hdr = 1;
6875 break;
6876
6877 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006878 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006879 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006880 break;
6881
6882 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006883 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006884 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006885 break;
6886
6887 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006888 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6889 if (trash.len < 0)
6890 return -1;
6891
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006892 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006893 /* FIXME: if the user adds a newline in the replacement, the
6894 * index will not be recalculated for now, and the new line
6895 * will not be counted as a new header.
6896 */
6897
6898 cur_end += delta;
6899 cur_next += delta;
6900 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006901 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006902 break;
6903
6904 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006905 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006906 cur_next += delta;
6907
Willy Tarreaufa355d42009-11-29 18:12:29 +01006908 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006909 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6910 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006911 cur_hdr->len = 0;
6912 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006913 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006914 break;
6915
6916 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006917 }
6918
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006919 /* keep the link from this header to next one in case of later
6920 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006921 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006922 old_idx = cur_idx;
6923 }
6924 return 0;
6925}
6926
6927
6928/* Apply the filter to the request line.
6929 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6930 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006931 * Since it can manage the switch to another backend, it updates the per-proxy
6932 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006933 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006934int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006935{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006936 char *cur_ptr, *cur_end;
6937 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006938 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006939 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006940
Willy Tarreau3d300592007-03-18 18:34:41 +01006941 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006942 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006943 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006944 (exp->action == ACT_ALLOW ||
6945 exp->action == ACT_DENY ||
6946 exp->action == ACT_TARPIT))
6947 return 0;
6948 else if (exp->action == ACT_REMOVE)
6949 return 0;
6950
6951 done = 0;
6952
Willy Tarreau9b28e032012-10-12 23:49:43 +02006953 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006954 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006955
6956 /* Now we have the request line between cur_ptr and cur_end */
6957
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02006958 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006959 switch (exp->action) {
6960 case ACT_SETBE:
6961 /* It is not possible to jump a second time.
6962 * FIXME: should we return an HTTP/500 here so that
6963 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006964 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006965 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006966 break;
6967
6968 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006969 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006970 done = 1;
6971 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006972
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006973 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006974 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006975 done = 1;
6976 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006977
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006978 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006979 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006980 done = 1;
6981 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006982
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006983 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006984 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006985 done = 1;
6986 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006987
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006988 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006989 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6990 if (trash.len < 0)
6991 return -1;
6992
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006993 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006994 /* FIXME: if the user adds a newline in the replacement, the
6995 * index will not be recalculated for now, and the new line
6996 * will not be counted as a new header.
6997 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006998
Willy Tarreaufa355d42009-11-29 18:12:29 +01006999 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007000 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007001 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007002 HTTP_MSG_RQMETH,
7003 cur_ptr, cur_end + 1,
7004 NULL, NULL);
7005 if (unlikely(!cur_end))
7006 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007007
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007008 /* we have a full request and we know that we have either a CR
7009 * or an LF at <ptr>.
7010 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007011 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7012 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007013 /* there is no point trying this regex on headers */
7014 return 1;
7015 }
7016 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007017 return done;
7018}
Willy Tarreau97de6242006-12-27 17:18:38 +01007019
Willy Tarreau58f10d72006-12-04 02:26:12 +01007020
Willy Tarreau58f10d72006-12-04 02:26:12 +01007021
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007022/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01007023 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007024 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007025 * unparsable request. Since it can manage the switch to another backend, it
7026 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007027 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007028int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007029{
Willy Tarreau6c123b12010-01-28 20:22:06 +01007030 struct http_txn *txn = &s->txn;
7031 struct hdr_exp *exp;
7032
7033 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007034 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007035
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007036 /*
7037 * The interleaving of transformations and verdicts
7038 * makes it difficult to decide to continue or stop
7039 * the evaluation.
7040 */
7041
Willy Tarreau6c123b12010-01-28 20:22:06 +01007042 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7043 break;
7044
Willy Tarreau3d300592007-03-18 18:34:41 +01007045 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007046 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007047 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007048 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007049
7050 /* if this filter had a condition, evaluate it now and skip to
7051 * next filter if the condition does not match.
7052 */
7053 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007054 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007055 ret = acl_pass(ret);
7056 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7057 ret = !ret;
7058
7059 if (!ret)
7060 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007061 }
7062
7063 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007064 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007065 if (unlikely(ret < 0))
7066 return -1;
7067
7068 if (likely(ret == 0)) {
7069 /* The filter did not match the request, it can be
7070 * iterated through all headers.
7071 */
Willy Tarreaua256fae2015-01-30 20:58:58 +01007072 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7073 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007074 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007075 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007076 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007077}
7078
7079
Willy Tarreaua15645d2007-03-18 16:22:39 +01007080
Willy Tarreau58f10d72006-12-04 02:26:12 +01007081/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007082 * Try to retrieve the server associated to the appsession.
7083 * If the server is found, it's assigned to the session.
7084 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007085void manage_client_side_appsession(struct session *s, const char *buf, int len) {
7086 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007087 appsess *asession = NULL;
7088 char *sessid_temp = NULL;
7089
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007090 if (len > s->be->appsession_len) {
7091 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007092 }
7093
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007094 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007095 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007096 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007097 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007098 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007099 }
7100
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007101 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007102 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007103 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007104 return;
7105 }
7106
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007107 memcpy(txn->sessid, buf, len);
7108 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007109 }
7110
7111 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7112 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007113 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007114 return;
7115 }
7116
Cyril Bontéb21570a2009-11-29 20:04:48 +01007117 memcpy(sessid_temp, buf, len);
7118 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007119
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007120 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007121 /* free previously allocated memory */
7122 pool_free2(apools.sessid, sessid_temp);
7123
7124 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007125 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7126 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007127 asession->request_count++;
7128
7129 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007130 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007131
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007132 while (srv) {
7133 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007134 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007135 (s->be->options & PR_O_PERSIST) ||
7136 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007137 /* we found the server and it's usable */
7138 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007139 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007140 s->flags |= SN_DIRECT | SN_ASSIGNED;
7141 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007142
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007143 break;
7144 } else {
7145 txn->flags &= ~TX_CK_MASK;
7146 txn->flags |= TX_CK_DOWN;
7147 }
7148 }
7149 srv = srv->next;
7150 }
7151 }
7152 }
7153}
7154
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007155/* Find the end of a cookie value contained between <s> and <e>. It works the
7156 * same way as with headers above except that the semi-colon also ends a token.
7157 * See RFC2965 for more information. Note that it requires a valid header to
7158 * return a valid result.
7159 */
7160char *find_cookie_value_end(char *s, const char *e)
7161{
7162 int quoted, qdpair;
7163
7164 quoted = qdpair = 0;
7165 for (; s < e; s++) {
7166 if (qdpair) qdpair = 0;
7167 else if (quoted) {
7168 if (*s == '\\') qdpair = 1;
7169 else if (*s == '"') quoted = 0;
7170 }
7171 else if (*s == '"') quoted = 1;
7172 else if (*s == ',' || *s == ';') return s;
7173 }
7174 return s;
7175}
7176
7177/* Delete a value in a header between delimiters <from> and <next> in buffer
7178 * <buf>. The number of characters displaced is returned, and the pointer to
7179 * the first delimiter is updated if required. The function tries as much as
7180 * possible to respect the following principles :
7181 * - replace <from> delimiter by the <next> one unless <from> points to a
7182 * colon, in which case <next> is simply removed
7183 * - set exactly one space character after the new first delimiter, unless
7184 * there are not enough characters in the block being moved to do so.
7185 * - remove unneeded spaces before the previous delimiter and after the new
7186 * one.
7187 *
7188 * It is the caller's responsibility to ensure that :
7189 * - <from> points to a valid delimiter or the colon ;
7190 * - <next> points to a valid delimiter or the final CR/LF ;
7191 * - there are non-space chars before <from> ;
7192 * - there is a CR/LF at or after <next>.
7193 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007194int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007195{
7196 char *prev = *from;
7197
7198 if (*prev == ':') {
7199 /* We're removing the first value, preserve the colon and add a
7200 * space if possible.
7201 */
7202 if (!http_is_crlf[(unsigned char)*next])
7203 next++;
7204 prev++;
7205 if (prev < next)
7206 *prev++ = ' ';
7207
7208 while (http_is_spht[(unsigned char)*next])
7209 next++;
7210 } else {
7211 /* Remove useless spaces before the old delimiter. */
7212 while (http_is_spht[(unsigned char)*(prev-1)])
7213 prev--;
7214 *from = prev;
7215
7216 /* copy the delimiter and if possible a space if we're
7217 * not at the end of the line.
7218 */
7219 if (!http_is_crlf[(unsigned char)*next]) {
7220 *prev++ = *next++;
7221 if (prev + 1 < next)
7222 *prev++ = ' ';
7223 while (http_is_spht[(unsigned char)*next])
7224 next++;
7225 }
7226 }
7227 return buffer_replace2(buf, prev, next, NULL, 0);
7228}
7229
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007230/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007231 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007232 * desirable to call it only when needed. This code is quite complex because
7233 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7234 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007235 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007236void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007237{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007238 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007239 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007240 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007241 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7242 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007243
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007244 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007245 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007246 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007247
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007248 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007249 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007250 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007251
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007252 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007253 hdr_beg = hdr_next;
7254 hdr_end = hdr_beg + cur_hdr->len;
7255 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007256
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007257 /* We have one full header between hdr_beg and hdr_end, and the
7258 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007259 * "Cookie:" headers.
7260 */
7261
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007262 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007263 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007264 old_idx = cur_idx;
7265 continue;
7266 }
7267
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007268 del_from = NULL; /* nothing to be deleted */
7269 preserve_hdr = 0; /* assume we may kill the whole header */
7270
Willy Tarreau58f10d72006-12-04 02:26:12 +01007271 /* Now look for cookies. Conforming to RFC2109, we have to support
7272 * attributes whose name begin with a '$', and associate them with
7273 * the right cookie, if we want to delete this cookie.
7274 * So there are 3 cases for each cookie read :
7275 * 1) it's a special attribute, beginning with a '$' : ignore it.
7276 * 2) it's a server id cookie that we *MAY* want to delete : save
7277 * some pointers on it (last semi-colon, beginning of cookie...)
7278 * 3) it's an application cookie : we *MAY* have to delete a previous
7279 * "special" cookie.
7280 * At the end of loop, if a "special" cookie remains, we may have to
7281 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007282 * *MUST* delete it.
7283 *
7284 * Note: RFC2965 is unclear about the processing of spaces around
7285 * the equal sign in the ATTR=VALUE form. A careful inspection of
7286 * the RFC explicitly allows spaces before it, and not within the
7287 * tokens (attrs or values). An inspection of RFC2109 allows that
7288 * too but section 10.1.3 lets one think that spaces may be allowed
7289 * after the equal sign too, resulting in some (rare) buggy
7290 * implementations trying to do that. So let's do what servers do.
7291 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7292 * allowed quoted strings in values, with any possible character
7293 * after a backslash, including control chars and delimitors, which
7294 * causes parsing to become ambiguous. Browsers also allow spaces
7295 * within values even without quotes.
7296 *
7297 * We have to keep multiple pointers in order to support cookie
7298 * removal at the beginning, middle or end of header without
7299 * corrupting the header. All of these headers are valid :
7300 *
7301 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7302 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7303 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7304 * | | | | | | | | |
7305 * | | | | | | | | hdr_end <--+
7306 * | | | | | | | +--> next
7307 * | | | | | | +----> val_end
7308 * | | | | | +-----------> val_beg
7309 * | | | | +--------------> equal
7310 * | | | +----------------> att_end
7311 * | | +---------------------> att_beg
7312 * | +--------------------------> prev
7313 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007314 */
7315
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007316 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7317 /* Iterate through all cookies on this line */
7318
7319 /* find att_beg */
7320 att_beg = prev + 1;
7321 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7322 att_beg++;
7323
7324 /* find att_end : this is the first character after the last non
7325 * space before the equal. It may be equal to hdr_end.
7326 */
7327 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007328
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007329 while (equal < hdr_end) {
7330 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007331 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007332 if (http_is_spht[(unsigned char)*equal++])
7333 continue;
7334 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007335 }
7336
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007337 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7338 * is between <att_beg> and <equal>, both may be identical.
7339 */
7340
7341 /* look for end of cookie if there is an equal sign */
7342 if (equal < hdr_end && *equal == '=') {
7343 /* look for the beginning of the value */
7344 val_beg = equal + 1;
7345 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7346 val_beg++;
7347
7348 /* find the end of the value, respecting quotes */
7349 next = find_cookie_value_end(val_beg, hdr_end);
7350
7351 /* make val_end point to the first white space or delimitor after the value */
7352 val_end = next;
7353 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7354 val_end--;
7355 } else {
7356 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007357 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007358
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007359 /* We have nothing to do with attributes beginning with '$'. However,
7360 * they will automatically be removed if a header before them is removed,
7361 * since they're supposed to be linked together.
7362 */
7363 if (*att_beg == '$')
7364 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007365
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007366 /* Ignore cookies with no equal sign */
7367 if (equal == next) {
7368 /* This is not our cookie, so we must preserve it. But if we already
7369 * scheduled another cookie for removal, we cannot remove the
7370 * complete header, but we can remove the previous block itself.
7371 */
7372 preserve_hdr = 1;
7373 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007374 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007375 val_end += delta;
7376 next += delta;
7377 hdr_end += delta;
7378 hdr_next += delta;
7379 cur_hdr->len += delta;
7380 http_msg_move_end(&txn->req, delta);
7381 prev = del_from;
7382 del_from = NULL;
7383 }
7384 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007385 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007386
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007387 /* if there are spaces around the equal sign, we need to
7388 * strip them otherwise we'll get trouble for cookie captures,
7389 * or even for rewrites. Since this happens extremely rarely,
7390 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007391 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007392 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7393 int stripped_before = 0;
7394 int stripped_after = 0;
7395
7396 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007397 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007398 equal += stripped_before;
7399 val_beg += stripped_before;
7400 }
7401
7402 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007403 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007404 val_beg += stripped_after;
7405 stripped_before += stripped_after;
7406 }
7407
7408 val_end += stripped_before;
7409 next += stripped_before;
7410 hdr_end += stripped_before;
7411 hdr_next += stripped_before;
7412 cur_hdr->len += stripped_before;
7413 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007414 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007415 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007416
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007417 /* First, let's see if we want to capture this cookie. We check
7418 * that we don't already have a client side cookie, because we
7419 * can only capture one. Also as an optimisation, we ignore
7420 * cookies shorter than the declared name.
7421 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007422 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7423 (val_end - att_beg >= s->fe->capture_namelen) &&
7424 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007425 int log_len = val_end - att_beg;
7426
7427 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7428 Alert("HTTP logging : out of memory.\n");
7429 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007430 if (log_len > s->fe->capture_len)
7431 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007432 memcpy(txn->cli_cookie, att_beg, log_len);
7433 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007434 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007435 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007436
Willy Tarreaubca99692010-10-06 19:25:55 +02007437 /* Persistence cookies in passive, rewrite or insert mode have the
7438 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007439 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007440 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007441 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007442 * For cookies in prefix mode, the form is :
7443 *
7444 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007445 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007446 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7447 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7448 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007449 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007450
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007451 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7452 * have the server ID between val_beg and delim, and the original cookie between
7453 * delim+1 and val_end. Otherwise, delim==val_end :
7454 *
7455 * Cookie: NAME=SRV; # in all but prefix modes
7456 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7457 * | || || | |+-> next
7458 * | || || | +--> val_end
7459 * | || || +---------> delim
7460 * | || |+------------> val_beg
7461 * | || +-------------> att_end = equal
7462 * | |+-----------------> att_beg
7463 * | +------------------> prev
7464 * +-------------------------> hdr_beg
7465 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007466
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007467 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007468 for (delim = val_beg; delim < val_end; delim++)
7469 if (*delim == COOKIE_DELIM)
7470 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007471 } else {
7472 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007473 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007474 /* Now check if the cookie contains a date field, which would
7475 * appear after a vertical bar ('|') just after the server name
7476 * and before the delimiter.
7477 */
7478 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7479 if (vbar1) {
7480 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007481 * right is the last seen date. It is a base64 encoded
7482 * 30-bit value representing the UNIX date since the
7483 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007484 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007485 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007486 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007487 if (val_end - vbar1 >= 5) {
7488 val = b64tos30(vbar1);
7489 if (val > 0)
7490 txn->cookie_last_date = val << 2;
7491 }
7492 /* look for a second vertical bar */
7493 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7494 if (vbar1 && (val_end - vbar1 > 5)) {
7495 val = b64tos30(vbar1 + 1);
7496 if (val > 0)
7497 txn->cookie_first_date = val << 2;
7498 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007499 }
7500 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007501
Willy Tarreauf64d1412010-10-07 20:06:11 +02007502 /* if the cookie has an expiration date and the proxy wants to check
7503 * it, then we do that now. We first check if the cookie is too old,
7504 * then only if it has expired. We detect strict overflow because the
7505 * time resolution here is not great (4 seconds). Cookies with dates
7506 * in the future are ignored if their offset is beyond one day. This
7507 * allows an admin to fix timezone issues without expiring everyone
7508 * and at the same time avoids keeping unwanted side effects for too
7509 * long.
7510 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007511 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7512 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007513 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007514 txn->flags &= ~TX_CK_MASK;
7515 txn->flags |= TX_CK_OLD;
7516 delim = val_beg; // let's pretend we have not found the cookie
7517 txn->cookie_first_date = 0;
7518 txn->cookie_last_date = 0;
7519 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007520 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7521 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007522 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007523 txn->flags &= ~TX_CK_MASK;
7524 txn->flags |= TX_CK_EXPIRED;
7525 delim = val_beg; // let's pretend we have not found the cookie
7526 txn->cookie_first_date = 0;
7527 txn->cookie_last_date = 0;
7528 }
7529
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007530 /* Here, we'll look for the first running server which supports the cookie.
7531 * This allows to share a same cookie between several servers, for example
7532 * to dedicate backup servers to specific servers only.
7533 * However, to prevent clients from sticking to cookie-less backup server
7534 * when they have incidentely learned an empty cookie, we simply ignore
7535 * empty cookies and mark them as invalid.
7536 * The same behaviour is applied when persistence must be ignored.
7537 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007538 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007539 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007540
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007541 while (srv) {
7542 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7543 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007544 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007545 (s->be->options & PR_O_PERSIST) ||
7546 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007547 /* we found the server and we can use it */
7548 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007549 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007550 s->flags |= SN_DIRECT | SN_ASSIGNED;
7551 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007552 break;
7553 } else {
7554 /* we found a server, but it's down,
7555 * mark it as such and go on in case
7556 * another one is available.
7557 */
7558 txn->flags &= ~TX_CK_MASK;
7559 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007560 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007561 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007562 srv = srv->next;
7563 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007564
Willy Tarreauf64d1412010-10-07 20:06:11 +02007565 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007566 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007567 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007568 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007569 txn->flags |= TX_CK_UNUSED;
7570 else
7571 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007572 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007573
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007574 /* depending on the cookie mode, we may have to either :
7575 * - delete the complete cookie if we're in insert+indirect mode, so that
7576 * the server never sees it ;
7577 * - remove the server id from the cookie value, and tag the cookie as an
7578 * application cookie so that it does not get accidentely removed later,
7579 * if we're in cookie prefix mode
7580 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007581 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007582 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007583
Willy Tarreau9b28e032012-10-12 23:49:43 +02007584 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007585 val_end += delta;
7586 next += delta;
7587 hdr_end += delta;
7588 hdr_next += delta;
7589 cur_hdr->len += delta;
7590 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007591
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007592 del_from = NULL;
7593 preserve_hdr = 1; /* we want to keep this cookie */
7594 }
7595 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007596 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007597 del_from = prev;
7598 }
7599 } else {
7600 /* This is not our cookie, so we must preserve it. But if we already
7601 * scheduled another cookie for removal, we cannot remove the
7602 * complete header, but we can remove the previous block itself.
7603 */
7604 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007605
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007606 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007607 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007608 if (att_beg >= del_from)
7609 att_beg += delta;
7610 if (att_end >= del_from)
7611 att_end += delta;
7612 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007613 val_end += delta;
7614 next += delta;
7615 hdr_end += delta;
7616 hdr_next += delta;
7617 cur_hdr->len += delta;
7618 http_msg_move_end(&txn->req, delta);
7619 prev = del_from;
7620 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007621 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007622 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007623
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007624 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007625 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007626 int cmp_len, value_len;
7627 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007628
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007629 if (s->be->options2 & PR_O2_AS_PFX) {
7630 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7631 value_begin = att_beg + s->be->appsession_name_len;
7632 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007633 } else {
7634 cmp_len = att_end - att_beg;
7635 value_begin = val_beg;
7636 value_len = val_end - val_beg;
7637 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007638
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007639 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007640 if (cmp_len == s->be->appsession_name_len &&
7641 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7642 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007643 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007644 }
7645
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007646 /* continue with next cookie on this header line */
7647 att_beg = next;
7648 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007649
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007650 /* There are no more cookies on this line.
7651 * We may still have one (or several) marked for deletion at the
7652 * end of the line. We must do this now in two ways :
7653 * - if some cookies must be preserved, we only delete from the
7654 * mark to the end of line ;
7655 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007656 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007657 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007658 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007659 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007660 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007661 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007662 cur_hdr->len += delta;
7663 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007664 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007665
7666 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007667 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7668 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007669 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007670 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007671 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007672 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007673 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007674 }
7675
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007676 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007677 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007678 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007679}
7680
7681
Willy Tarreaua15645d2007-03-18 16:22:39 +01007682/* Iterate the same filter through all response headers contained in <rtr>.
7683 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7684 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007685int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007686{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007687 char *cur_ptr, *cur_end, *cur_next;
7688 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007689 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007690 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007691 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007692
7693 last_hdr = 0;
7694
Willy Tarreau9b28e032012-10-12 23:49:43 +02007695 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007696 old_idx = 0;
7697
7698 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007699 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007700 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007701 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007702 (exp->action == ACT_ALLOW ||
7703 exp->action == ACT_DENY))
7704 return 0;
7705
7706 cur_idx = txn->hdr_idx.v[old_idx].next;
7707 if (!cur_idx)
7708 break;
7709
7710 cur_hdr = &txn->hdr_idx.v[cur_idx];
7711 cur_ptr = cur_next;
7712 cur_end = cur_ptr + cur_hdr->len;
7713 cur_next = cur_end + cur_hdr->cr + 1;
7714
7715 /* Now we have one header between cur_ptr and cur_end,
7716 * and the next header starts at cur_next.
7717 */
7718
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007719 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007720 switch (exp->action) {
7721 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007722 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007723 last_hdr = 1;
7724 break;
7725
7726 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007727 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007728 last_hdr = 1;
7729 break;
7730
7731 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007732 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7733 if (trash.len < 0)
7734 return -1;
7735
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007736 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007737 /* FIXME: if the user adds a newline in the replacement, the
7738 * index will not be recalculated for now, and the new line
7739 * will not be counted as a new header.
7740 */
7741
7742 cur_end += delta;
7743 cur_next += delta;
7744 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007745 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007746 break;
7747
7748 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007749 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007750 cur_next += delta;
7751
Willy Tarreaufa355d42009-11-29 18:12:29 +01007752 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007753 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7754 txn->hdr_idx.used--;
7755 cur_hdr->len = 0;
7756 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007757 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007758 break;
7759
7760 }
7761 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007762
7763 /* keep the link from this header to next one in case of later
7764 * removal of next header.
7765 */
7766 old_idx = cur_idx;
7767 }
7768 return 0;
7769}
7770
7771
7772/* Apply the filter to the status line in the response buffer <rtr>.
7773 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7774 * or -1 if a replacement resulted in an invalid status line.
7775 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007776int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007777{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007778 char *cur_ptr, *cur_end;
7779 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007780 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007781 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007782
7783
Willy Tarreau3d300592007-03-18 18:34:41 +01007784 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007785 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007786 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007787 (exp->action == ACT_ALLOW ||
7788 exp->action == ACT_DENY))
7789 return 0;
7790 else if (exp->action == ACT_REMOVE)
7791 return 0;
7792
7793 done = 0;
7794
Willy Tarreau9b28e032012-10-12 23:49:43 +02007795 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007796 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007797
7798 /* Now we have the status line between cur_ptr and cur_end */
7799
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007800 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007801 switch (exp->action) {
7802 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007803 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007804 done = 1;
7805 break;
7806
7807 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007808 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007809 done = 1;
7810 break;
7811
7812 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007813 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7814 if (trash.len < 0)
7815 return -1;
7816
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007817 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007818 /* FIXME: if the user adds a newline in the replacement, the
7819 * index will not be recalculated for now, and the new line
7820 * will not be counted as a new header.
7821 */
7822
Willy Tarreaufa355d42009-11-29 18:12:29 +01007823 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007824 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007825 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007826 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007827 cur_ptr, cur_end + 1,
7828 NULL, NULL);
7829 if (unlikely(!cur_end))
7830 return -1;
7831
7832 /* we have a full respnse and we know that we have either a CR
7833 * or an LF at <ptr>.
7834 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007835 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007836 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007837 /* there is no point trying this regex on headers */
7838 return 1;
7839 }
7840 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007841 return done;
7842}
7843
7844
7845
7846/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007847 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007848 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7849 * unparsable response.
7850 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007851int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007852{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007853 struct http_txn *txn = &s->txn;
7854 struct hdr_exp *exp;
7855
7856 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007857 int ret;
7858
7859 /*
7860 * The interleaving of transformations and verdicts
7861 * makes it difficult to decide to continue or stop
7862 * the evaluation.
7863 */
7864
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007865 if (txn->flags & TX_SVDENY)
7866 break;
7867
Willy Tarreau3d300592007-03-18 18:34:41 +01007868 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007869 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7870 exp->action == ACT_PASS)) {
7871 exp = exp->next;
7872 continue;
7873 }
7874
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007875 /* if this filter had a condition, evaluate it now and skip to
7876 * next filter if the condition does not match.
7877 */
7878 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007879 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007880 ret = acl_pass(ret);
7881 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7882 ret = !ret;
7883 if (!ret)
7884 continue;
7885 }
7886
Willy Tarreaua15645d2007-03-18 16:22:39 +01007887 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007888 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007889 if (unlikely(ret < 0))
7890 return -1;
7891
7892 if (likely(ret == 0)) {
7893 /* The filter did not match the response, it can be
7894 * iterated through all headers.
7895 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007896 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7897 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007898 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007899 }
7900 return 0;
7901}
7902
7903
Willy Tarreaua15645d2007-03-18 16:22:39 +01007904/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007905 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007906 * desirable to call it only when needed. This function is also used when we
7907 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007908 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007909void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007910{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007911 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007912 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007913 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007914 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007915 char *hdr_beg, *hdr_end, *hdr_next;
7916 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007917
Willy Tarreaua15645d2007-03-18 16:22:39 +01007918 /* Iterate through the headers.
7919 * we start with the start line.
7920 */
7921 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007922 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007923
7924 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7925 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007926 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007927
7928 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007929 hdr_beg = hdr_next;
7930 hdr_end = hdr_beg + cur_hdr->len;
7931 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007932
Willy Tarreau24581ba2010-08-31 22:39:35 +02007933 /* We have one full header between hdr_beg and hdr_end, and the
7934 * next header starts at hdr_next. We're only interested in
7935 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007936 */
7937
Willy Tarreau24581ba2010-08-31 22:39:35 +02007938 is_cookie2 = 0;
7939 prev = hdr_beg + 10;
7940 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007941 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007942 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7943 if (!val) {
7944 old_idx = cur_idx;
7945 continue;
7946 }
7947 is_cookie2 = 1;
7948 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007949 }
7950
Willy Tarreau24581ba2010-08-31 22:39:35 +02007951 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7952 * <prev> points to the colon.
7953 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007954 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007955
Willy Tarreau24581ba2010-08-31 22:39:35 +02007956 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7957 * check-cache is enabled) and we are not interested in checking
7958 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007959 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007960 if (s->be->cookie_name == NULL &&
7961 s->be->appsession_name == NULL &&
7962 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007963 return;
7964
Willy Tarreau24581ba2010-08-31 22:39:35 +02007965 /* OK so now we know we have to process this response cookie.
7966 * The format of the Set-Cookie header is slightly different
7967 * from the format of the Cookie header in that it does not
7968 * support the comma as a cookie delimiter (thus the header
7969 * cannot be folded) because the Expires attribute described in
7970 * the original Netscape's spec may contain an unquoted date
7971 * with a comma inside. We have to live with this because
7972 * many browsers don't support Max-Age and some browsers don't
7973 * support quoted strings. However the Set-Cookie2 header is
7974 * clean.
7975 *
7976 * We have to keep multiple pointers in order to support cookie
7977 * removal at the beginning, middle or end of header without
7978 * corrupting the header (in case of set-cookie2). A special
7979 * pointer, <scav> points to the beginning of the set-cookie-av
7980 * fields after the first semi-colon. The <next> pointer points
7981 * either to the end of line (set-cookie) or next unquoted comma
7982 * (set-cookie2). All of these headers are valid :
7983 *
7984 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7985 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7986 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7987 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7988 * | | | | | | | | | |
7989 * | | | | | | | | +-> next hdr_end <--+
7990 * | | | | | | | +------------> scav
7991 * | | | | | | +--------------> val_end
7992 * | | | | | +--------------------> val_beg
7993 * | | | | +----------------------> equal
7994 * | | | +------------------------> att_end
7995 * | | +----------------------------> att_beg
7996 * | +------------------------------> prev
7997 * +-----------------------------------------> hdr_beg
7998 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007999
Willy Tarreau24581ba2010-08-31 22:39:35 +02008000 for (; prev < hdr_end; prev = next) {
8001 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008002
Willy Tarreau24581ba2010-08-31 22:39:35 +02008003 /* find att_beg */
8004 att_beg = prev + 1;
8005 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8006 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008007
Willy Tarreau24581ba2010-08-31 22:39:35 +02008008 /* find att_end : this is the first character after the last non
8009 * space before the equal. It may be equal to hdr_end.
8010 */
8011 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008012
Willy Tarreau24581ba2010-08-31 22:39:35 +02008013 while (equal < hdr_end) {
8014 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8015 break;
8016 if (http_is_spht[(unsigned char)*equal++])
8017 continue;
8018 att_end = equal;
8019 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008020
Willy Tarreau24581ba2010-08-31 22:39:35 +02008021 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8022 * is between <att_beg> and <equal>, both may be identical.
8023 */
8024
8025 /* look for end of cookie if there is an equal sign */
8026 if (equal < hdr_end && *equal == '=') {
8027 /* look for the beginning of the value */
8028 val_beg = equal + 1;
8029 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8030 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008031
Willy Tarreau24581ba2010-08-31 22:39:35 +02008032 /* find the end of the value, respecting quotes */
8033 next = find_cookie_value_end(val_beg, hdr_end);
8034
8035 /* make val_end point to the first white space or delimitor after the value */
8036 val_end = next;
8037 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8038 val_end--;
8039 } else {
8040 /* <equal> points to next comma, semi-colon or EOL */
8041 val_beg = val_end = next = equal;
8042 }
8043
8044 if (next < hdr_end) {
8045 /* Set-Cookie2 supports multiple cookies, and <next> points to
8046 * a colon or semi-colon before the end. So skip all attr-value
8047 * pairs and look for the next comma. For Set-Cookie, since
8048 * commas are permitted in values, skip to the end.
8049 */
8050 if (is_cookie2)
8051 next = find_hdr_value_end(next, hdr_end);
8052 else
8053 next = hdr_end;
8054 }
8055
8056 /* Now everything is as on the diagram above */
8057
8058 /* Ignore cookies with no equal sign */
8059 if (equal == val_end)
8060 continue;
8061
8062 /* If there are spaces around the equal sign, we need to
8063 * strip them otherwise we'll get trouble for cookie captures,
8064 * or even for rewrites. Since this happens extremely rarely,
8065 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008066 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008067 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8068 int stripped_before = 0;
8069 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008070
Willy Tarreau24581ba2010-08-31 22:39:35 +02008071 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008072 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008073 equal += stripped_before;
8074 val_beg += stripped_before;
8075 }
8076
8077 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008078 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008079 val_beg += stripped_after;
8080 stripped_before += stripped_after;
8081 }
8082
8083 val_end += stripped_before;
8084 next += stripped_before;
8085 hdr_end += stripped_before;
8086 hdr_next += stripped_before;
8087 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008088 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008089 }
8090
8091 /* First, let's see if we want to capture this cookie. We check
8092 * that we don't already have a server side cookie, because we
8093 * can only capture one. Also as an optimisation, we ignore
8094 * cookies shorter than the declared name.
8095 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008096 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008097 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008098 (val_end - att_beg >= s->fe->capture_namelen) &&
8099 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008100 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008101 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008102 Alert("HTTP logging : out of memory.\n");
8103 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008104 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008105 if (log_len > s->fe->capture_len)
8106 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008107 memcpy(txn->srv_cookie, att_beg, log_len);
8108 txn->srv_cookie[log_len] = 0;
8109 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008110 }
8111
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008112 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008113 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008114 if (!(s->flags & SN_IGNORE_PRST) &&
8115 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8116 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008117 /* assume passive cookie by default */
8118 txn->flags &= ~TX_SCK_MASK;
8119 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008120
8121 /* If the cookie is in insert mode on a known server, we'll delete
8122 * this occurrence because we'll insert another one later.
8123 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008124 * a direct access.
8125 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008126 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008127 /* The "preserve" flag was set, we don't want to touch the
8128 * server's cookie.
8129 */
8130 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008131 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8132 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008133 /* this cookie must be deleted */
8134 if (*prev == ':' && next == hdr_end) {
8135 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008136 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008137 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8138 txn->hdr_idx.used--;
8139 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008140 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008141 hdr_next += delta;
8142 http_msg_move_end(&txn->rsp, delta);
8143 /* note: while both invalid now, <next> and <hdr_end>
8144 * are still equal, so the for() will stop as expected.
8145 */
8146 } else {
8147 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008148 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008149 next = prev;
8150 hdr_end += delta;
8151 hdr_next += delta;
8152 cur_hdr->len += delta;
8153 http_msg_move_end(&txn->rsp, delta);
8154 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008155 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008156 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008157 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008158 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008159 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008160 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008161 * with this server since we know it.
8162 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008163 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008164 next += delta;
8165 hdr_end += delta;
8166 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008167 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008168 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008169
Willy Tarreauf1348312010-10-07 15:54:11 +02008170 txn->flags &= ~TX_SCK_MASK;
8171 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008172 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008173 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008174 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008175 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008176 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008177 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008178 next += delta;
8179 hdr_end += delta;
8180 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008181 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008182 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008183
Willy Tarreau827aee92011-03-10 16:55:02 +01008184 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008185 txn->flags &= ~TX_SCK_MASK;
8186 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008187 }
8188 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008189 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008190 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008191 int cmp_len, value_len;
8192 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008193
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008194 if (s->be->options2 & PR_O2_AS_PFX) {
8195 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8196 value_begin = att_beg + s->be->appsession_name_len;
8197 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008198 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008199 cmp_len = att_end - att_beg;
8200 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008201 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008202 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008203
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008204 if ((cmp_len == s->be->appsession_name_len) &&
8205 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008206 /* free a possibly previously allocated memory */
8207 pool_free2(apools.sessid, txn->sessid);
8208
Cyril Bontéb21570a2009-11-29 20:04:48 +01008209 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008210 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008211 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008212 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008213 return;
8214 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008215 memcpy(txn->sessid, value_begin, value_len);
8216 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008217 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008218 }
8219 /* that's done for this cookie, check the next one on the same
8220 * line when next != hdr_end (only if is_cookie2).
8221 */
8222 }
8223 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008224 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008225 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008226
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008227 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008228 appsess *asession = NULL;
8229 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008230 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008231 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008232 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008233 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8234 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008235 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008236 return;
8237 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008238 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8239
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008240 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8241 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008242 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8243 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008244 return;
8245 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008246 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8247 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008248
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008249 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008250 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008251 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008252 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8253 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008254 return;
8255 }
8256 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008257 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008258
8259 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008260 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008261 }
8262
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008263 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008264 asession->request_count++;
8265 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008266}
8267
8268
Willy Tarreaua15645d2007-03-18 16:22:39 +01008269/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008270 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008271 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008272void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008273{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008274 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008275 char *p1, *p2;
8276
8277 char *cur_ptr, *cur_end, *cur_next;
8278 int cur_idx;
8279
Willy Tarreau5df51872007-11-25 16:20:08 +01008280 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008281 return;
8282
8283 /* Iterate through the headers.
8284 * we start with the start line.
8285 */
8286 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008287 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008288
8289 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8290 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008291 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008292
8293 cur_hdr = &txn->hdr_idx.v[cur_idx];
8294 cur_ptr = cur_next;
8295 cur_end = cur_ptr + cur_hdr->len;
8296 cur_next = cur_end + cur_hdr->cr + 1;
8297
8298 /* We have one full header between cur_ptr and cur_end, and the
8299 * next header starts at cur_next. We're only interested in
8300 * "Cookie:" headers.
8301 */
8302
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008303 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8304 if (val) {
8305 if ((cur_end - (cur_ptr + val) >= 8) &&
8306 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8307 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8308 return;
8309 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008310 }
8311
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008312 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8313 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008314 continue;
8315
8316 /* OK, right now we know we have a cache-control header at cur_ptr */
8317
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008318 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008319
8320 if (p1 >= cur_end) /* no more info */
8321 continue;
8322
8323 /* p1 is at the beginning of the value */
8324 p2 = p1;
8325
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008326 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008327 p2++;
8328
8329 /* we have a complete value between p1 and p2 */
8330 if (p2 < cur_end && *p2 == '=') {
8331 /* we have something of the form no-cache="set-cookie" */
8332 if ((cur_end - p1 >= 21) &&
8333 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8334 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008335 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008336 continue;
8337 }
8338
8339 /* OK, so we know that either p2 points to the end of string or to a comma */
8340 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008341 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008342 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8343 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8344 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008345 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008346 return;
8347 }
8348
8349 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008350 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008351 continue;
8352 }
8353 }
8354}
8355
8356
Willy Tarreau58f10d72006-12-04 02:26:12 +01008357/*
8358 * Try to retrieve a known appsession in the URI, then the associated server.
8359 * If the server is found, it's assigned to the session.
8360 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008361void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008362{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008363 char *end_params, *first_param, *cur_param, *next_param;
8364 char separator;
8365 int value_len;
8366
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008367 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008368
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008369 if (s->be->appsession_name == NULL ||
8370 (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 +01008371 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008372 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008373
Cyril Bontéb21570a2009-11-29 20:04:48 +01008374 first_param = NULL;
8375 switch (mode) {
8376 case PR_O2_AS_M_PP:
8377 first_param = memchr(begin, ';', len);
8378 break;
8379 case PR_O2_AS_M_QS:
8380 first_param = memchr(begin, '?', len);
8381 break;
8382 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008383
Cyril Bontéb21570a2009-11-29 20:04:48 +01008384 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008385 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008386 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008387
Cyril Bontéb21570a2009-11-29 20:04:48 +01008388 switch (mode) {
8389 case PR_O2_AS_M_PP:
8390 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8391 end_params = (char *) begin + len;
8392 }
8393 separator = ';';
8394 break;
8395 case PR_O2_AS_M_QS:
8396 end_params = (char *) begin + len;
8397 separator = '&';
8398 break;
8399 default:
8400 /* unknown mode, shouldn't happen */
8401 return;
8402 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008403
Cyril Bontéb21570a2009-11-29 20:04:48 +01008404 cur_param = next_param = end_params;
8405 while (cur_param > first_param) {
8406 cur_param--;
8407 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8408 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008409 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8410 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8411 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008412 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008413 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8414 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008415 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008416 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008417 }
8418 break;
8419 }
8420 next_param = cur_param;
8421 }
8422 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008423#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008424 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008425 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008426#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008427}
8428
Willy Tarreaub2513902006-12-17 14:52:38 +01008429/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008430 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008431 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008432 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008433 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008434 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008435 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008436 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008437 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008438int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008439{
8440 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008441 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008442 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008443
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008444 if (!uri_auth)
8445 return 0;
8446
Cyril Bonté70be45d2010-10-12 00:14:35 +02008447 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008448 return 0;
8449
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008450 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008451 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008452 return 0;
8453
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008454 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008455 return 0;
8456
Willy Tarreaub2513902006-12-17 14:52:38 +01008457 return 1;
8458}
8459
Willy Tarreau4076a152009-04-02 15:18:36 +02008460/*
8461 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008462 * By default it tries to report the error position as msg->err_pos. However if
8463 * this one is not set, it will then report msg->next, which is the last known
8464 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008465 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008466 */
8467void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008468 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008469 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008470{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008471 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008472 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008473
Willy Tarreau9b28e032012-10-12 23:49:43 +02008474 es->len = MIN(chn->buf->i, sizeof(es->buf));
8475 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008476 len1 = MIN(len1, es->len);
8477 len2 = es->len - len1; /* remaining data if buffer wraps */
8478
Willy Tarreau9b28e032012-10-12 23:49:43 +02008479 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008480 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008481 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008482
Willy Tarreau4076a152009-04-02 15:18:36 +02008483 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008484 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008485 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008486 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008487
Willy Tarreau4076a152009-04-02 15:18:36 +02008488 es->when = date; // user-visible date
8489 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008490 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008491 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008492 if (objt_conn(s->req->prod->end))
8493 es->src = __objt_conn(s->req->prod->end)->addr.from;
8494 else
8495 memset(&es->src, 0, sizeof(es->src));
8496
Willy Tarreau078272e2010-12-12 12:46:33 +01008497 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008498 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008499 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008500 es->s_flags = s->flags;
8501 es->t_flags = s->txn.flags;
8502 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008503 es->b_out = chn->buf->o;
8504 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008505 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008506 es->m_clen = msg->chunk_len;
8507 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008508}
Willy Tarreaub2513902006-12-17 14:52:38 +01008509
Willy Tarreau294c4732011-12-16 21:35:50 +01008510/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8511 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8512 * performed over the whole headers. Otherwise it must contain a valid header
8513 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8514 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8515 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8516 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008517 * -1. The value fetch stops at commas, so this function is suited for use with
8518 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008519 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008520 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008521unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008522 struct hdr_idx *idx, int occ,
8523 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008524{
Willy Tarreau294c4732011-12-16 21:35:50 +01008525 struct hdr_ctx local_ctx;
8526 char *ptr_hist[MAX_HDR_HISTORY];
8527 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008528 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008529 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008530
Willy Tarreau294c4732011-12-16 21:35:50 +01008531 if (!ctx) {
8532 local_ctx.idx = 0;
8533 ctx = &local_ctx;
8534 }
8535
Willy Tarreaubce70882009-09-07 11:51:47 +02008536 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008537 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008538 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008539 occ--;
8540 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008541 *vptr = ctx->line + ctx->val;
8542 *vlen = ctx->vlen;
8543 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008544 }
8545 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008546 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008547 }
8548
8549 /* negative occurrence, we scan all the list then walk back */
8550 if (-occ > MAX_HDR_HISTORY)
8551 return 0;
8552
Willy Tarreau294c4732011-12-16 21:35:50 +01008553 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008554 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008555 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8556 len_hist[hist_ptr] = ctx->vlen;
8557 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008558 hist_ptr = 0;
8559 found++;
8560 }
8561 if (-occ > found)
8562 return 0;
8563 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008564 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8565 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8566 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008567 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008568 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008569 if (hist_ptr >= MAX_HDR_HISTORY)
8570 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008571 *vptr = ptr_hist[hist_ptr];
8572 *vlen = len_hist[hist_ptr];
8573 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008574}
8575
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008576/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8577 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8578 * performed over the whole headers. Otherwise it must contain a valid header
8579 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8580 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8581 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8582 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8583 * -1. This function differs from http_get_hdr() in that it only returns full
8584 * line header values and does not stop at commas.
8585 * The return value is 0 if nothing was found, or non-zero otherwise.
8586 */
8587unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8588 struct hdr_idx *idx, int occ,
8589 struct hdr_ctx *ctx, char **vptr, int *vlen)
8590{
8591 struct hdr_ctx local_ctx;
8592 char *ptr_hist[MAX_HDR_HISTORY];
8593 int len_hist[MAX_HDR_HISTORY];
8594 unsigned int hist_ptr;
8595 int found;
8596
8597 if (!ctx) {
8598 local_ctx.idx = 0;
8599 ctx = &local_ctx;
8600 }
8601
8602 if (occ >= 0) {
8603 /* search from the beginning */
8604 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8605 occ--;
8606 if (occ <= 0) {
8607 *vptr = ctx->line + ctx->val;
8608 *vlen = ctx->vlen;
8609 return 1;
8610 }
8611 }
8612 return 0;
8613 }
8614
8615 /* negative occurrence, we scan all the list then walk back */
8616 if (-occ > MAX_HDR_HISTORY)
8617 return 0;
8618
8619 found = hist_ptr = 0;
8620 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8621 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8622 len_hist[hist_ptr] = ctx->vlen;
8623 if (++hist_ptr >= MAX_HDR_HISTORY)
8624 hist_ptr = 0;
8625 found++;
8626 }
8627 if (-occ > found)
8628 return 0;
8629 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8630 * find occurrence -occ, so we have to check [hist_ptr+occ].
8631 */
8632 hist_ptr += occ;
8633 if (hist_ptr >= MAX_HDR_HISTORY)
8634 hist_ptr -= MAX_HDR_HISTORY;
8635 *vptr = ptr_hist[hist_ptr];
8636 *vlen = len_hist[hist_ptr];
8637 return 1;
8638}
8639
Willy Tarreaubaaee002006-06-26 02:48:02 +02008640/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008641 * Print a debug line with a header. Always stop at the first CR or LF char,
8642 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8643 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008644 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008645void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008646{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008647 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008648 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008649 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008650 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8651 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 +02008652
8653 for (max = 0; start + max < end; max++)
8654 if (start[max] == '\r' || start[max] == '\n')
8655 break;
8656
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008657 UBOUND(max, trash.size - trash.len - 3);
8658 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8659 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008660 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008661}
8662
Willy Tarreau0937bc42009-12-22 15:03:09 +01008663/*
8664 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8665 * the required fields are properly allocated and that we only need to (re)init
8666 * them. This should be used before processing any new request.
8667 */
8668void http_init_txn(struct session *s)
8669{
8670 struct http_txn *txn = &s->txn;
8671 struct proxy *fe = s->fe;
8672
8673 txn->flags = 0;
8674 txn->status = -1;
8675
Willy Tarreauf64d1412010-10-07 20:06:11 +02008676 txn->cookie_first_date = 0;
8677 txn->cookie_last_date = 0;
8678
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008679 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008680 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008681 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008682 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008683 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008684 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008685 txn->req.chunk_len = 0LL;
8686 txn->req.body_len = 0LL;
8687 txn->rsp.chunk_len = 0LL;
8688 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008689 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8690 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008691 txn->req.chn = s->req;
8692 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008693
8694 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008695
8696 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8697 if (fe->options2 & PR_O2_REQBUG_OK)
8698 txn->req.err_pos = -1; /* let buggy requests pass */
8699
Willy Tarreau46023632010-01-07 22:51:47 +01008700 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008701 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8702
Willy Tarreau46023632010-01-07 22:51:47 +01008703 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008704 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8705
8706 if (txn->hdr_idx.v)
8707 hdr_idx_init(&txn->hdr_idx);
8708}
8709
8710/* to be used at the end of a transaction */
8711void http_end_txn(struct session *s)
8712{
8713 struct http_txn *txn = &s->txn;
8714
Willy Tarreau75195602014-03-11 15:48:55 +01008715 /* release any possible compression context */
8716 if (s->flags & SN_COMP_READY)
8717 s->comp_algo->end(&s->comp_ctx);
8718 s->comp_algo = NULL;
8719 s->flags &= ~SN_COMP_READY;
8720
Willy Tarreau0937bc42009-12-22 15:03:09 +01008721 /* these ones will have been dynamically allocated */
8722 pool_free2(pool2_requri, txn->uri);
8723 pool_free2(pool2_capture, txn->cli_cookie);
8724 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008725 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008726 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008727
William Lallemanda73203e2012-03-12 12:48:57 +01008728 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008729 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008730 txn->uri = NULL;
8731 txn->srv_cookie = NULL;
8732 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008733
8734 if (txn->req.cap) {
8735 struct cap_hdr *h;
8736 for (h = s->fe->req_cap; h; h = h->next)
8737 pool_free2(h->pool, txn->req.cap[h->index]);
8738 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8739 }
8740
8741 if (txn->rsp.cap) {
8742 struct cap_hdr *h;
8743 for (h = s->fe->rsp_cap; h; h = h->next)
8744 pool_free2(h->pool, txn->rsp.cap[h->index]);
8745 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8746 }
8747
Willy Tarreau0937bc42009-12-22 15:03:09 +01008748}
8749
8750/* to be used at the end of a transaction to prepare a new one */
8751void http_reset_txn(struct session *s)
8752{
8753 http_end_txn(s);
8754 http_init_txn(s);
8755
8756 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008757 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008758 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008759 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008760 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008761 /* re-init store persistence */
8762 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008763 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008764
Willy Tarreau0937bc42009-12-22 15:03:09 +01008765 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008766
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008767 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008768
Willy Tarreau739cfba2010-01-25 23:11:14 +01008769 /* We must trim any excess data from the response buffer, because we
8770 * may have blocked an invalid response from a server that we don't
8771 * want to accidentely forward once we disable the analysers, nor do
8772 * we want those data to come along with next response. A typical
8773 * example of such data would be from a buggy server responding to
8774 * a HEAD with some data, or sending more than the advertised
8775 * content-length.
8776 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008777 if (unlikely(s->rep->buf->i))
8778 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008779
Willy Tarreau0937bc42009-12-22 15:03:09 +01008780 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008781 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008782
Willy Tarreaud04e8582010-05-31 12:31:35 +02008783 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008784 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008785
8786 s->req->rex = TICK_ETERNITY;
8787 s->req->wex = TICK_ETERNITY;
8788 s->req->analyse_exp = TICK_ETERNITY;
8789 s->rep->rex = TICK_ETERNITY;
8790 s->rep->wex = TICK_ETERNITY;
8791 s->rep->analyse_exp = TICK_ETERNITY;
8792}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008793
Sasha Pachev218f0642014-06-16 12:05:59 -06008794void free_http_res_rules(struct list *r)
8795{
8796 struct http_res_rule *tr, *pr;
8797
8798 list_for_each_entry_safe(pr, tr, r, list) {
8799 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008800 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008801 free(pr);
8802 }
8803}
8804
8805void free_http_req_rules(struct list *r)
8806{
Willy Tarreauff011f22011-01-06 17:51:27 +01008807 struct http_req_rule *tr, *pr;
8808
8809 list_for_each_entry_safe(pr, tr, r, list) {
8810 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008811 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008812 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008813
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008814 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008815 free(pr);
8816 }
8817}
8818
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008819/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008820struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8821{
8822 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008823 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008824 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008825 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008826
8827 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8828 if (!rule) {
8829 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008830 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008831 }
8832
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008833 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008834 rule->action = HTTP_REQ_ACT_ALLOW;
8835 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008836 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008837 rule->action = HTTP_REQ_ACT_DENY;
8838 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008839 } else if (!strcmp(args[0], "tarpit")) {
8840 rule->action = HTTP_REQ_ACT_TARPIT;
8841 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008842 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008843 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008844 cur_arg = 1;
8845
8846 while(*args[cur_arg]) {
8847 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008848 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008849 cur_arg+=2;
8850 continue;
8851 } else
8852 break;
8853 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008854 } else if (!strcmp(args[0], "set-nice")) {
8855 rule->action = HTTP_REQ_ACT_SET_NICE;
8856 cur_arg = 1;
8857
8858 if (!*args[cur_arg] ||
8859 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8860 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8861 file, linenum, args[0]);
8862 goto out_err;
8863 }
8864 rule->arg.nice = atoi(args[cur_arg]);
8865 if (rule->arg.nice < -1024)
8866 rule->arg.nice = -1024;
8867 else if (rule->arg.nice > 1024)
8868 rule->arg.nice = 1024;
8869 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008870 } else if (!strcmp(args[0], "set-tos")) {
8871#ifdef IP_TOS
8872 char *err;
8873 rule->action = HTTP_REQ_ACT_SET_TOS;
8874 cur_arg = 1;
8875
8876 if (!*args[cur_arg] ||
8877 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8878 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8879 file, linenum, args[0]);
8880 goto out_err;
8881 }
8882
8883 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8884 if (err && *err != '\0') {
8885 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8886 file, linenum, err, args[0]);
8887 goto out_err;
8888 }
8889 cur_arg++;
8890#else
8891 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8892 goto out_err;
8893#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008894 } else if (!strcmp(args[0], "set-mark")) {
8895#ifdef SO_MARK
8896 char *err;
8897 rule->action = HTTP_REQ_ACT_SET_MARK;
8898 cur_arg = 1;
8899
8900 if (!*args[cur_arg] ||
8901 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8902 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8903 file, linenum, args[0]);
8904 goto out_err;
8905 }
8906
8907 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8908 if (err && *err != '\0') {
8909 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8910 file, linenum, err, args[0]);
8911 goto out_err;
8912 }
8913 cur_arg++;
8914 global.last_checks |= LSTCHK_NETADM;
8915#else
8916 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8917 goto out_err;
8918#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008919 } else if (!strcmp(args[0], "set-log-level")) {
8920 rule->action = HTTP_REQ_ACT_SET_LOGL;
8921 cur_arg = 1;
8922
8923 if (!*args[cur_arg] ||
8924 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8925 bad_log_level:
8926 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8927 file, linenum, args[0]);
8928 goto out_err;
8929 }
8930 if (strcmp(args[cur_arg], "silent") == 0)
8931 rule->arg.loglevel = -1;
8932 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8933 goto bad_log_level;
8934 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008935 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8936 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8937 cur_arg = 1;
8938
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008939 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8940 (*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 +01008941 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8942 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008943 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008944 }
8945
8946 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8947 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8948 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008949
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008950 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008951 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008952 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8953 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008954 free(proxy->conf.lfs_file);
8955 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8956 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008957 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008958 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
8959 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008960 cur_arg = 1;
8961
8962 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann8f249f42014-06-24 11:10:00 +02008963 (*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 -06008964 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8965 file, linenum, args[0]);
8966 goto out_err;
8967 }
8968
8969 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8970 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8971 LIST_INIT(&rule->arg.hdr_add.fmt);
8972
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008973 error = NULL;
8974 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8975 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8976 args[cur_arg + 1], error);
8977 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008978 goto out_err;
8979 }
8980
8981 proxy->conf.args.ctx = ARGC_HRQ;
8982 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
8983 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8984 file, linenum);
8985
8986 free(proxy->conf.lfs_file);
8987 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8988 proxy->conf.lfs_line = proxy->conf.args.line;
8989 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008990 } else if (strcmp(args[0], "del-header") == 0) {
8991 rule->action = HTTP_REQ_ACT_DEL_HDR;
8992 cur_arg = 1;
8993
8994 if (!*args[cur_arg] ||
8995 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8996 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8997 file, linenum, args[0]);
8998 goto out_err;
8999 }
9000
9001 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9002 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9003
9004 proxy->conf.args.ctx = ARGC_HRQ;
9005 free(proxy->conf.lfs_file);
9006 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9007 proxy->conf.lfs_line = proxy->conf.args.line;
9008 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009009 } else if (strcmp(args[0], "redirect") == 0) {
9010 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009011 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009012
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009013 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009014 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9015 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9016 goto out_err;
9017 }
9018
9019 /* this redirect rule might already contain a parsed condition which
9020 * we'll pass to the http-request rule.
9021 */
9022 rule->action = HTTP_REQ_ACT_REDIR;
9023 rule->arg.redir = redir;
9024 rule->cond = redir->cond;
9025 redir->cond = NULL;
9026 cur_arg = 2;
9027 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009028 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9029 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9030 rule->action = HTTP_REQ_ACT_ADD_ACL;
9031 /*
9032 * '+ 8' for 'add-acl('
9033 * '- 9' for 'add-acl(' + trailing ')'
9034 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009035 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009036
9037 cur_arg = 1;
9038
9039 if (!*args[cur_arg] ||
9040 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9041 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9042 file, linenum, args[0]);
9043 goto out_err;
9044 }
9045
9046 LIST_INIT(&rule->arg.map.key);
9047 proxy->conf.args.ctx = ARGC_HRQ;
9048 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9049 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9050 file, linenum);
9051 free(proxy->conf.lfs_file);
9052 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9053 proxy->conf.lfs_line = proxy->conf.args.line;
9054 cur_arg += 1;
9055 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9056 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9057 rule->action = HTTP_REQ_ACT_DEL_ACL;
9058 /*
9059 * '+ 8' for 'del-acl('
9060 * '- 9' for 'del-acl(' + trailing ')'
9061 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009062 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009063
9064 cur_arg = 1;
9065
9066 if (!*args[cur_arg] ||
9067 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9068 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9069 file, linenum, args[0]);
9070 goto out_err;
9071 }
9072
9073 LIST_INIT(&rule->arg.map.key);
9074 proxy->conf.args.ctx = ARGC_HRQ;
9075 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9076 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9077 file, linenum);
9078 free(proxy->conf.lfs_file);
9079 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9080 proxy->conf.lfs_line = proxy->conf.args.line;
9081 cur_arg += 1;
9082 } else if (strncmp(args[0], "del-map", 7) == 0) {
9083 /* http-request del-map(<reference (map name)>) <key pattern> */
9084 rule->action = HTTP_REQ_ACT_DEL_MAP;
9085 /*
9086 * '+ 8' for 'del-map('
9087 * '- 9' for 'del-map(' + trailing ')'
9088 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009089 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009090
9091 cur_arg = 1;
9092
9093 if (!*args[cur_arg] ||
9094 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9095 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9096 file, linenum, args[0]);
9097 goto out_err;
9098 }
9099
9100 LIST_INIT(&rule->arg.map.key);
9101 proxy->conf.args.ctx = ARGC_HRQ;
9102 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9103 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9104 file, linenum);
9105 free(proxy->conf.lfs_file);
9106 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9107 proxy->conf.lfs_line = proxy->conf.args.line;
9108 cur_arg += 1;
9109 } else if (strncmp(args[0], "set-map", 7) == 0) {
9110 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9111 rule->action = HTTP_REQ_ACT_SET_MAP;
9112 /*
9113 * '+ 8' for 'set-map('
9114 * '- 9' for 'set-map(' + trailing ')'
9115 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009116 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009117
9118 cur_arg = 1;
9119
9120 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9121 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9122 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9123 file, linenum, args[0]);
9124 goto out_err;
9125 }
9126
9127 LIST_INIT(&rule->arg.map.key);
9128 LIST_INIT(&rule->arg.map.value);
9129 proxy->conf.args.ctx = ARGC_HRQ;
9130
9131 /* key pattern */
9132 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9133 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9134 file, linenum);
9135
9136 /* value pattern */
9137 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9138 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9139 file, linenum);
9140 free(proxy->conf.lfs_file);
9141 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9142 proxy->conf.lfs_line = proxy->conf.args.line;
9143
9144 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009145 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9146 char *errmsg = NULL;
9147 cur_arg = 1;
9148 /* try in the module list */
9149 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9150 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9151 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9152 free(errmsg);
9153 goto out_err;
9154 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009155 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009156 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 +01009157 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009158 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009159 }
9160
9161 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9162 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009163 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009164
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009165 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9166 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9167 file, linenum, args[0], errmsg);
9168 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009169 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009170 }
9171 rule->cond = cond;
9172 }
9173 else if (*args[cur_arg]) {
9174 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9175 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9176 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009177 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009178 }
9179
9180 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009181 out_err:
9182 free(rule);
9183 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009184}
9185
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009186/* parse an "http-respose" rule */
9187struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9188{
9189 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009190 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009191 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009192 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009193
9194 rule = calloc(1, sizeof(*rule));
9195 if (!rule) {
9196 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9197 goto out_err;
9198 }
9199
9200 if (!strcmp(args[0], "allow")) {
9201 rule->action = HTTP_RES_ACT_ALLOW;
9202 cur_arg = 1;
9203 } else if (!strcmp(args[0], "deny")) {
9204 rule->action = HTTP_RES_ACT_DENY;
9205 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009206 } else if (!strcmp(args[0], "set-nice")) {
9207 rule->action = HTTP_RES_ACT_SET_NICE;
9208 cur_arg = 1;
9209
9210 if (!*args[cur_arg] ||
9211 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9212 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9213 file, linenum, args[0]);
9214 goto out_err;
9215 }
9216 rule->arg.nice = atoi(args[cur_arg]);
9217 if (rule->arg.nice < -1024)
9218 rule->arg.nice = -1024;
9219 else if (rule->arg.nice > 1024)
9220 rule->arg.nice = 1024;
9221 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009222 } else if (!strcmp(args[0], "set-tos")) {
9223#ifdef IP_TOS
9224 char *err;
9225 rule->action = HTTP_RES_ACT_SET_TOS;
9226 cur_arg = 1;
9227
9228 if (!*args[cur_arg] ||
9229 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9230 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9231 file, linenum, args[0]);
9232 goto out_err;
9233 }
9234
9235 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9236 if (err && *err != '\0') {
9237 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9238 file, linenum, err, args[0]);
9239 goto out_err;
9240 }
9241 cur_arg++;
9242#else
9243 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9244 goto out_err;
9245#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009246 } else if (!strcmp(args[0], "set-mark")) {
9247#ifdef SO_MARK
9248 char *err;
9249 rule->action = HTTP_RES_ACT_SET_MARK;
9250 cur_arg = 1;
9251
9252 if (!*args[cur_arg] ||
9253 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9254 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9255 file, linenum, args[0]);
9256 goto out_err;
9257 }
9258
9259 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9260 if (err && *err != '\0') {
9261 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9262 file, linenum, err, args[0]);
9263 goto out_err;
9264 }
9265 cur_arg++;
9266 global.last_checks |= LSTCHK_NETADM;
9267#else
9268 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9269 goto out_err;
9270#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009271 } else if (!strcmp(args[0], "set-log-level")) {
9272 rule->action = HTTP_RES_ACT_SET_LOGL;
9273 cur_arg = 1;
9274
9275 if (!*args[cur_arg] ||
9276 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9277 bad_log_level:
9278 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9279 file, linenum, args[0]);
9280 goto out_err;
9281 }
9282 if (strcmp(args[cur_arg], "silent") == 0)
9283 rule->arg.loglevel = -1;
9284 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9285 goto bad_log_level;
9286 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009287 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9288 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9289 cur_arg = 1;
9290
9291 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9292 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9293 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9294 file, linenum, args[0]);
9295 goto out_err;
9296 }
9297
9298 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9299 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9300 LIST_INIT(&rule->arg.hdr_add.fmt);
9301
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009302 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009303 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009304 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9305 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009306 free(proxy->conf.lfs_file);
9307 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9308 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009309 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009310 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009311 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009312 cur_arg = 1;
9313
9314 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmanna772b942014-08-08 17:29:06 +02009315 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9316 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009317 file, linenum, args[0]);
9318 goto out_err;
9319 }
9320
9321 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9322 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9323 LIST_INIT(&rule->arg.hdr_add.fmt);
9324
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009325 error = NULL;
9326 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9327 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9328 args[cur_arg + 1], error);
9329 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009330 goto out_err;
9331 }
9332
9333 proxy->conf.args.ctx = ARGC_HRQ;
9334 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9335 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9336 file, linenum);
9337
9338 free(proxy->conf.lfs_file);
9339 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9340 proxy->conf.lfs_line = proxy->conf.args.line;
9341 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009342 } else if (strcmp(args[0], "del-header") == 0) {
9343 rule->action = HTTP_RES_ACT_DEL_HDR;
9344 cur_arg = 1;
9345
9346 if (!*args[cur_arg] ||
9347 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9348 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9349 file, linenum, args[0]);
9350 goto out_err;
9351 }
9352
9353 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9354 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9355
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009356 proxy->conf.args.ctx = ARGC_HRS;
9357 free(proxy->conf.lfs_file);
9358 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9359 proxy->conf.lfs_line = proxy->conf.args.line;
9360 cur_arg += 1;
9361 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9362 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9363 rule->action = HTTP_RES_ACT_ADD_ACL;
9364 /*
9365 * '+ 8' for 'add-acl('
9366 * '- 9' for 'add-acl(' + trailing ')'
9367 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009368 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009369
9370 cur_arg = 1;
9371
9372 if (!*args[cur_arg] ||
9373 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9374 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9375 file, linenum, args[0]);
9376 goto out_err;
9377 }
9378
9379 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009380 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009381 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9382 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9383 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009384 free(proxy->conf.lfs_file);
9385 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9386 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009387
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009388 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009389 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9390 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9391 rule->action = HTTP_RES_ACT_DEL_ACL;
9392 /*
9393 * '+ 8' for 'del-acl('
9394 * '- 9' for 'del-acl(' + trailing ')'
9395 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009396 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009397
9398 cur_arg = 1;
9399
9400 if (!*args[cur_arg] ||
9401 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9402 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9403 file, linenum, args[0]);
9404 goto out_err;
9405 }
9406
9407 LIST_INIT(&rule->arg.map.key);
9408 proxy->conf.args.ctx = ARGC_HRS;
9409 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9410 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9411 file, linenum);
9412 free(proxy->conf.lfs_file);
9413 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9414 proxy->conf.lfs_line = proxy->conf.args.line;
9415 cur_arg += 1;
9416 } else if (strncmp(args[0], "del-map", 7) == 0) {
9417 /* http-response del-map(<reference (map name)>) <key pattern> */
9418 rule->action = HTTP_RES_ACT_DEL_MAP;
9419 /*
9420 * '+ 8' for 'del-map('
9421 * '- 9' for 'del-map(' + trailing ')'
9422 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009423 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009424
9425 cur_arg = 1;
9426
9427 if (!*args[cur_arg] ||
9428 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9429 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9430 file, linenum, args[0]);
9431 goto out_err;
9432 }
9433
9434 LIST_INIT(&rule->arg.map.key);
9435 proxy->conf.args.ctx = ARGC_HRS;
9436 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9437 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9438 file, linenum);
9439 free(proxy->conf.lfs_file);
9440 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9441 proxy->conf.lfs_line = proxy->conf.args.line;
9442 cur_arg += 1;
9443 } else if (strncmp(args[0], "set-map", 7) == 0) {
9444 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9445 rule->action = HTTP_RES_ACT_SET_MAP;
9446 /*
9447 * '+ 8' for 'set-map('
9448 * '- 9' for 'set-map(' + trailing ')'
9449 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009450 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009451
9452 cur_arg = 1;
9453
9454 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9455 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9456 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9457 file, linenum, args[0]);
9458 goto out_err;
9459 }
9460
9461 LIST_INIT(&rule->arg.map.key);
9462 LIST_INIT(&rule->arg.map.value);
9463
9464 proxy->conf.args.ctx = ARGC_HRS;
9465
9466 /* key pattern */
9467 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9468 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9469 file, linenum);
9470
9471 /* value pattern */
9472 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9473 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9474 file, linenum);
9475
9476 free(proxy->conf.lfs_file);
9477 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9478 proxy->conf.lfs_line = proxy->conf.args.line;
9479
9480 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009481 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9482 char *errmsg = NULL;
9483 cur_arg = 1;
9484 /* try in the module list */
9485 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9486 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9487 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9488 free(errmsg);
9489 goto out_err;
9490 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009491 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009492 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 +02009493 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9494 goto out_err;
9495 }
9496
9497 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9498 struct acl_cond *cond;
9499 char *errmsg = NULL;
9500
9501 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9502 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9503 file, linenum, args[0], errmsg);
9504 free(errmsg);
9505 goto out_err;
9506 }
9507 rule->cond = cond;
9508 }
9509 else if (*args[cur_arg]) {
9510 Alert("parsing [%s:%d]: 'http-response %s' expects"
9511 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9512 file, linenum, args[0], args[cur_arg]);
9513 goto out_err;
9514 }
9515
9516 return rule;
9517 out_err:
9518 free(rule);
9519 return NULL;
9520}
9521
Willy Tarreau4baae242012-12-27 12:00:31 +01009522/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009523 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9524 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009525 */
9526struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009527 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009528{
9529 struct redirect_rule *rule;
9530 int cur_arg;
9531 int type = REDIRECT_TYPE_NONE;
9532 int code = 302;
9533 const char *destination = NULL;
9534 const char *cookie = NULL;
9535 int cookie_set = 0;
9536 unsigned int flags = REDIRECT_FLAG_NONE;
9537 struct acl_cond *cond = NULL;
9538
9539 cur_arg = 0;
9540 while (*(args[cur_arg])) {
9541 if (strcmp(args[cur_arg], "location") == 0) {
9542 if (!*args[cur_arg + 1])
9543 goto missing_arg;
9544
9545 type = REDIRECT_TYPE_LOCATION;
9546 cur_arg++;
9547 destination = args[cur_arg];
9548 }
9549 else if (strcmp(args[cur_arg], "prefix") == 0) {
9550 if (!*args[cur_arg + 1])
9551 goto missing_arg;
9552
9553 type = REDIRECT_TYPE_PREFIX;
9554 cur_arg++;
9555 destination = args[cur_arg];
9556 }
9557 else if (strcmp(args[cur_arg], "scheme") == 0) {
9558 if (!*args[cur_arg + 1])
9559 goto missing_arg;
9560
9561 type = REDIRECT_TYPE_SCHEME;
9562 cur_arg++;
9563 destination = args[cur_arg];
9564 }
9565 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9566 if (!*args[cur_arg + 1])
9567 goto missing_arg;
9568
9569 cur_arg++;
9570 cookie = args[cur_arg];
9571 cookie_set = 1;
9572 }
9573 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9574 if (!*args[cur_arg + 1])
9575 goto missing_arg;
9576
9577 cur_arg++;
9578 cookie = args[cur_arg];
9579 cookie_set = 0;
9580 }
9581 else if (strcmp(args[cur_arg], "code") == 0) {
9582 if (!*args[cur_arg + 1])
9583 goto missing_arg;
9584
9585 cur_arg++;
9586 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009587 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009588 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009589 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009590 args[cur_arg - 1], args[cur_arg]);
9591 return NULL;
9592 }
9593 }
9594 else if (!strcmp(args[cur_arg],"drop-query")) {
9595 flags |= REDIRECT_FLAG_DROP_QS;
9596 }
9597 else if (!strcmp(args[cur_arg],"append-slash")) {
9598 flags |= REDIRECT_FLAG_APPEND_SLASH;
9599 }
9600 else if (strcmp(args[cur_arg], "if") == 0 ||
9601 strcmp(args[cur_arg], "unless") == 0) {
9602 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9603 if (!cond) {
9604 memprintf(errmsg, "error in condition: %s", *errmsg);
9605 return NULL;
9606 }
9607 break;
9608 }
9609 else {
9610 memprintf(errmsg,
9611 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9612 args[cur_arg]);
9613 return NULL;
9614 }
9615 cur_arg++;
9616 }
9617
9618 if (type == REDIRECT_TYPE_NONE) {
9619 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9620 return NULL;
9621 }
9622
9623 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9624 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009625 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009626
9627 if (!use_fmt) {
9628 /* old-style static redirect rule */
9629 rule->rdr_str = strdup(destination);
9630 rule->rdr_len = strlen(destination);
9631 }
9632 else {
9633 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009634
9635 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9636 * if prefix == "/", we don't want to add anything, otherwise it
9637 * makes it hard for the user to configure a self-redirection.
9638 */
Godbach543b9782014-12-18 15:44:58 +08009639 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009640 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009641 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009642 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9643 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009644 free(curproxy->conf.lfs_file);
9645 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9646 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009647 }
9648 }
9649
Willy Tarreau4baae242012-12-27 12:00:31 +01009650 if (cookie) {
9651 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9652 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9653 */
9654 rule->cookie_len = strlen(cookie);
9655 if (cookie_set) {
9656 rule->cookie_str = malloc(rule->cookie_len + 10);
9657 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9658 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9659 rule->cookie_len += 9;
9660 } else {
9661 rule->cookie_str = malloc(rule->cookie_len + 21);
9662 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9663 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9664 rule->cookie_len += 20;
9665 }
9666 }
9667 rule->type = type;
9668 rule->code = code;
9669 rule->flags = flags;
9670 LIST_INIT(&rule->list);
9671 return rule;
9672
9673 missing_arg:
9674 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9675 return NULL;
9676}
9677
Willy Tarreau8797c062007-05-07 00:55:35 +02009678/************************************************************************/
9679/* The code below is dedicated to ACL parsing and matching */
9680/************************************************************************/
9681
9682
Willy Tarreau14174bc2012-04-16 14:34:04 +02009683/* This function ensures that the prerequisites for an L7 fetch are ready,
9684 * which means that a request or response is ready. If some data is missing,
9685 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009686 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9687 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009688 *
9689 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009690 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9691 * decide whether or not an HTTP message is present ;
9692 * 0 if the requested data cannot be fetched or if it is certain that
9693 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009694 * 1 if an HTTP message is ready
9695 */
9696static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009697smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009698 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009699{
9700 struct http_txn *txn = l7;
9701 struct http_msg *msg = &txn->req;
9702
9703 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9704 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9705 */
9706
9707 if (unlikely(!s || !txn))
9708 return 0;
9709
9710 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009711 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009712
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009713 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009714 if (unlikely(!s->req))
9715 return 0;
9716
Willy Tarreauaae75e32013-03-29 12:31:49 +01009717 /* If the buffer does not leave enough free space at the end,
9718 * we must first realign it.
9719 */
9720 if (s->req->buf->p > s->req->buf->data &&
9721 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9722 buffer_slow_realign(s->req->buf);
9723
Willy Tarreau14174bc2012-04-16 14:34:04 +02009724 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009725 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009726 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009727
9728 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009729 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009730 http_msg_analyzer(msg, &txn->hdr_idx);
9731
9732 /* Still no valid request ? */
9733 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009734 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009735 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009736 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009737 }
9738 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009739 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009740 return 0;
9741 }
9742
9743 /* OK we just got a valid HTTP request. We have some minor
9744 * preparation to perform so that further checks can rely
9745 * on HTTP tests.
9746 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009747
9748 /* If the request was parsed but was too large, we must absolutely
9749 * return an error so that it is not processed. At the moment this
9750 * cannot happen, but if the parsers are to change in the future,
9751 * we want this check to be maintained.
9752 */
9753 if (unlikely(s->req->buf->i + s->req->buf->p >
9754 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9755 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009756 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009757 return 1;
9758 }
9759
Willy Tarreau9b28e032012-10-12 23:49:43 +02009760 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009761 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9762 s->flags |= SN_REDIRECTABLE;
9763
Willy Tarreau506d0502013-07-06 13:29:24 +02009764 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9765 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009766 }
9767
Willy Tarreau506d0502013-07-06 13:29:24 +02009768 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009769 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009770 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009771
9772 /* otherwise everything's ready for the request */
9773 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009774 else {
9775 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009776 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9777 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009778 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009779 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009780 }
9781
9782 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009783 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009784 return 1;
9785}
Willy Tarreau8797c062007-05-07 00:55:35 +02009786
Willy Tarreaua4ba9db2014-06-25 16:56:41 +02009787/* Note: these functinos *do* modify the sample. Even in case of success, at
9788 * least the type and uint value are modified.
9789 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009790#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009791 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 +02009792
Willy Tarreau24e32d82012-04-23 23:55:44 +02009793#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009794 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 +02009795
Willy Tarreau8797c062007-05-07 00:55:35 +02009796
9797/* 1. Check on METHOD
9798 * We use the pre-parsed method if it is known, and store its number as an
9799 * integer. If it is unknown, we use the pointer and the length.
9800 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009801static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009802{
9803 int len, meth;
9804
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009805 len = strlen(text);
9806 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009807
9808 pattern->val.i = meth;
9809 if (meth == HTTP_METH_OTHER) {
Willy Tarreau71196f32014-08-29 15:15:50 +02009810 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009811 pattern->len = len;
9812 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009813 else {
9814 pattern->ptr.str = NULL;
9815 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009816 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009817 return 1;
9818}
9819
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009820/* This function fetches the method of current HTTP request and stores
9821 * it in the global pattern struct as a chunk. There are two possibilities :
9822 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9823 * in <len> and <ptr> is NULL ;
9824 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9825 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009826 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009827 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009828static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009829smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009830 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009831{
9832 int meth;
9833 struct http_txn *txn = l7;
9834
Willy Tarreau24e32d82012-04-23 23:55:44 +02009835 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009836
Willy Tarreau8797c062007-05-07 00:55:35 +02009837 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009838 smp->type = SMP_T_METH;
9839 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009840 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009841 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9842 /* ensure the indexes are not affected */
9843 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009844 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009845 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9846 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009847 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009848 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009849 return 1;
9850}
9851
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009852/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009853static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009854{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009855 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009856 struct pattern_list *lst;
9857 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009858
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009859 list_for_each_entry(lst, &expr->patterns, list) {
9860 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009861
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009862 /* well-known method */
9863 if (pattern->val.i != HTTP_METH_OTHER) {
9864 if (smp->data.meth.meth == pattern->val.i)
9865 return pattern;
9866 else
9867 continue;
9868 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009869
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009870 /* Other method, we must compare the strings */
9871 if (pattern->len != smp->data.meth.str.len)
9872 continue;
9873
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009874 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreauc62a0c62014-08-28 20:42:57 +02009875 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
9876 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009877 return pattern;
9878 }
9879 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009880}
9881
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009882static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009883smp_fetch_rqver(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 Tarreau8797c062007-05-07 00:55:35 +02009892 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009893 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009894
9895 while ((len-- > 0) && (*ptr++ != '/'));
9896 if (len <= 0)
9897 return 0;
9898
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009899 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009900 smp->data.str.str = ptr;
9901 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009902
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009903 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009904 return 1;
9905}
9906
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009907static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009908smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009909 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009910{
9911 struct http_txn *txn = l7;
9912 char *ptr;
9913 int len;
9914
Willy Tarreauc0239e02012-04-16 14:42:55 +02009915 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009916
Willy Tarreauf26b2522012-12-14 08:33:14 +01009917 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9918 return 0;
9919
Willy Tarreau8797c062007-05-07 00:55:35 +02009920 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009921 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009922
9923 while ((len-- > 0) && (*ptr++ != '/'));
9924 if (len <= 0)
9925 return 0;
9926
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009927 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009928 smp->data.str.str = ptr;
9929 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009930
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009931 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009932 return 1;
9933}
9934
9935/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009936static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009937smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009938 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009939{
9940 struct http_txn *txn = l7;
9941 char *ptr;
9942 int len;
9943
Willy Tarreauc0239e02012-04-16 14:42:55 +02009944 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009945
Willy Tarreauf26b2522012-12-14 08:33:14 +01009946 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9947 return 0;
9948
Willy Tarreau8797c062007-05-07 00:55:35 +02009949 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009950 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009951
Willy Tarreauf853c462012-04-23 18:53:56 +02009952 smp->type = SMP_T_UINT;
9953 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009954 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009955 return 1;
9956}
9957
9958/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009959static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009960smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009961 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009962{
9963 struct http_txn *txn = l7;
9964
Willy Tarreauc0239e02012-04-16 14:42:55 +02009965 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009966
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009967 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009968 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009969 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009970 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009971 return 1;
9972}
9973
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009974static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009975smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009976 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009977{
9978 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009979 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009980
Willy Tarreauc0239e02012-04-16 14:42:55 +02009981 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009982
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009983 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 +02009984 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009985 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009986
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009987 smp->type = SMP_T_IPV4;
9988 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009989 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009990 return 1;
9991}
9992
9993static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009994smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009995 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009996{
9997 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009998 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009999
Willy Tarreauc0239e02012-04-16 14:42:55 +020010000 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010001
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010002 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 +020010003 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10004 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010005
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010006 smp->type = SMP_T_UINT;
10007 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010008 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010009 return 1;
10010}
10011
Willy Tarreau185b5c42012-04-26 15:11:51 +020010012/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10013 * Accepts an optional argument of type string containing the header field name,
10014 * and an optional argument of type signed or unsigned integer to request an
10015 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010016 * headers are considered from the first one. It does not stop on commas and
10017 * returns full lines instead (useful for User-Agent or Date for example).
10018 */
10019static int
10020smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010021 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010022{
10023 struct http_txn *txn = l7;
10024 struct hdr_idx *idx = &txn->hdr_idx;
10025 struct hdr_ctx *ctx = smp->ctx.a[0];
10026 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10027 int occ = 0;
10028 const char *name_str = NULL;
10029 int name_len = 0;
10030
10031 if (!ctx) {
10032 /* first call */
10033 ctx = &static_hdr_ctx;
10034 ctx->idx = 0;
10035 smp->ctx.a[0] = ctx;
10036 }
10037
10038 if (args) {
10039 if (args[0].type != ARGT_STR)
10040 return 0;
10041 name_str = args[0].data.str.str;
10042 name_len = args[0].data.str.len;
10043
10044 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10045 occ = args[1].data.uint;
10046 }
10047
10048 CHECK_HTTP_MESSAGE_FIRST();
10049
10050 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10051 /* search for header from the beginning */
10052 ctx->idx = 0;
10053
10054 if (!occ && !(opt & SMP_OPT_ITERATE))
10055 /* no explicit occurrence and single fetch => last header by default */
10056 occ = -1;
10057
10058 if (!occ)
10059 /* prepare to report multiple occurrences for ACL fetches */
10060 smp->flags |= SMP_F_NOT_LAST;
10061
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010062 smp->type = SMP_T_STR;
10063 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010064 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10065 return 1;
10066
10067 smp->flags &= ~SMP_F_NOT_LAST;
10068 return 0;
10069}
10070
10071/* 6. Check on HTTP header count. The number of occurrences is returned.
10072 * Accepts exactly 1 argument of type string. It does not stop on commas and
10073 * returns full lines instead (useful for User-Agent or Date for example).
10074 */
10075static int
10076smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010077 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010078{
10079 struct http_txn *txn = l7;
10080 struct hdr_idx *idx = &txn->hdr_idx;
10081 struct hdr_ctx ctx;
10082 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10083 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010084 const char *name = NULL;
10085 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010086
Willy Tarreau29437342015-04-01 19:16:09 +020010087 if (args && args->type == ARGT_STR) {
10088 name = args->data.str.str;
10089 len = args->data.str.len;
10090 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010091
10092 CHECK_HTTP_MESSAGE_FIRST();
10093
10094 ctx.idx = 0;
10095 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010096 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010097 cnt++;
10098
10099 smp->type = SMP_T_UINT;
10100 smp->data.uint = cnt;
10101 smp->flags = SMP_F_VOL_HDR;
10102 return 1;
10103}
10104
10105/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10106 * Accepts an optional argument of type string containing the header field name,
10107 * and an optional argument of type signed or unsigned integer to request an
10108 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010109 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010110 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010111static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010112smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010113 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010114{
10115 struct http_txn *txn = l7;
10116 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010117 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010118 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010119 int occ = 0;
10120 const char *name_str = NULL;
10121 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010122
Willy Tarreaua890d072013-04-02 12:01:06 +020010123 if (!ctx) {
10124 /* first call */
10125 ctx = &static_hdr_ctx;
10126 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010127 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010128 }
10129
Willy Tarreau185b5c42012-04-26 15:11:51 +020010130 if (args) {
10131 if (args[0].type != ARGT_STR)
10132 return 0;
10133 name_str = args[0].data.str.str;
10134 name_len = args[0].data.str.len;
10135
10136 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10137 occ = args[1].data.uint;
10138 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010139
Willy Tarreaue333ec92012-04-16 16:26:40 +020010140 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010141
Willy Tarreau185b5c42012-04-26 15:11:51 +020010142 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010143 /* search for header from the beginning */
10144 ctx->idx = 0;
10145
Willy Tarreau185b5c42012-04-26 15:11:51 +020010146 if (!occ && !(opt & SMP_OPT_ITERATE))
10147 /* no explicit occurrence and single fetch => last header by default */
10148 occ = -1;
10149
10150 if (!occ)
10151 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010152 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010153
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010154 smp->type = SMP_T_STR;
10155 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010156 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 +020010157 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010158
Willy Tarreau37406352012-04-23 16:16:37 +020010159 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010160 return 0;
10161}
10162
Willy Tarreauc11416f2007-06-17 16:58:38 +020010163/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010164 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010165 */
10166static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010167smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010168 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010169{
10170 struct http_txn *txn = l7;
10171 struct hdr_idx *idx = &txn->hdr_idx;
10172 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010173 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010174 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010175 const char *name = NULL;
10176 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010177
Willy Tarreau29437342015-04-01 19:16:09 +020010178 if (args && args->type == ARGT_STR) {
10179 name = args->data.str.str;
10180 len = args->data.str.len;
10181 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010182
Willy Tarreaue333ec92012-04-16 16:26:40 +020010183 CHECK_HTTP_MESSAGE_FIRST();
10184
Willy Tarreau33a7e692007-06-10 19:45:56 +020010185 ctx.idx = 0;
10186 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010187 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010188 cnt++;
10189
Willy Tarreauf853c462012-04-23 18:53:56 +020010190 smp->type = SMP_T_UINT;
10191 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010192 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010193 return 1;
10194}
10195
Willy Tarreau185b5c42012-04-26 15:11:51 +020010196/* Fetch an HTTP header's integer value. The integer value is returned. It
10197 * takes a mandatory argument of type string and an optional one of type int
10198 * to designate a specific occurrence. It returns an unsigned integer, which
10199 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010200 */
10201static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010202smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010203 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010204{
Willy Tarreauef38c392013-07-22 16:29:32 +020010205 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010206
Willy Tarreauf853c462012-04-23 18:53:56 +020010207 if (ret > 0) {
10208 smp->type = SMP_T_UINT;
10209 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10210 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010211
Willy Tarreaud53e2422012-04-16 17:21:11 +020010212 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010213}
10214
Cyril Bonté69fa9922012-10-25 00:01:06 +020010215/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10216 * and an optional one of type int to designate a specific occurrence.
10217 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010218 */
10219static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010220smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010221 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +020010222{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010223 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010224
Willy Tarreauef38c392013-07-22 16:29:32 +020010225 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010226 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10227 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010228 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010229 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010230 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010231 if (smp->data.str.len < temp->size - 1) {
10232 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10233 temp->str[smp->data.str.len] = '\0';
10234 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10235 smp->type = SMP_T_IPV6;
10236 break;
10237 }
10238 }
10239 }
10240
Willy Tarreaud53e2422012-04-16 17:21:11 +020010241 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010242 if (!(smp->flags & SMP_F_NOT_LAST))
10243 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010244 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010245 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010246}
10247
Willy Tarreau737b0c12007-06-10 21:28:46 +020010248/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10249 * the first '/' after the possible hostname, and ends before the possible '?'.
10250 */
10251static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010252smp_fetch_path(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 Tarreau737b0c12007-06-10 21:28:46 +020010254{
10255 struct http_txn *txn = l7;
10256 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010257
Willy Tarreauc0239e02012-04-16 14:42:55 +020010258 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010259
Willy Tarreau9b28e032012-10-12 23:49:43 +020010260 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010261 ptr = http_get_path(txn);
10262 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010263 return 0;
10264
10265 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010266 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010267 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010268
10269 while (ptr < end && *ptr != '?')
10270 ptr++;
10271
Willy Tarreauf853c462012-04-23 18:53:56 +020010272 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010273 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010274 return 1;
10275}
10276
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010277/* This produces a concatenation of the first occurrence of the Host header
10278 * followed by the path component if it begins with a slash ('/'). This means
10279 * that '*' will not be added, resulting in exactly the first Host entry.
10280 * If no Host header is found, then the path is returned as-is. The returned
10281 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010282 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010283 */
10284static int
10285smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010286 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010287{
10288 struct http_txn *txn = l7;
10289 char *ptr, *end, *beg;
10290 struct hdr_ctx ctx;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010291 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010292
10293 CHECK_HTTP_MESSAGE_FIRST();
10294
10295 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010296 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +020010297 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010298
10299 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010300 temp = get_trash_chunk();
10301 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010302 smp->type = SMP_T_STR;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010303 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010304 smp->data.str.len = ctx.vlen;
10305
10306 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010307 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010308 beg = http_get_path(txn);
10309 if (!beg)
10310 beg = end;
10311
10312 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10313
10314 if (beg < ptr && *beg == '/') {
10315 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10316 smp->data.str.len += ptr - beg;
10317 }
10318
10319 smp->flags = SMP_F_VOL_1ST;
10320 return 1;
10321}
10322
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010323/* This produces a 32-bit hash of the concatenation of the first occurrence of
10324 * the Host header followed by the path component if it begins with a slash ('/').
10325 * This means that '*' will not be added, resulting in exactly the first Host
10326 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010327 * is hashed using the path hash followed by a full avalanche hash and provides a
10328 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010329 * high-traffic sites without having to store whole paths.
10330 */
10331static int
10332smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010333 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010334{
10335 struct http_txn *txn = l7;
10336 struct hdr_ctx ctx;
10337 unsigned int hash = 0;
10338 char *ptr, *beg, *end;
10339 int len;
10340
10341 CHECK_HTTP_MESSAGE_FIRST();
10342
10343 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010344 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010345 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10346 ptr = ctx.line + ctx.val;
10347 len = ctx.vlen;
10348 while (len--)
10349 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10350 }
10351
10352 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010353 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010354 beg = http_get_path(txn);
10355 if (!beg)
10356 beg = end;
10357
10358 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10359
10360 if (beg < ptr && *beg == '/') {
10361 while (beg < ptr)
10362 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10363 }
10364 hash = full_hash(hash);
10365
10366 smp->type = SMP_T_UINT;
10367 smp->data.uint = hash;
10368 smp->flags = SMP_F_VOL_1ST;
10369 return 1;
10370}
10371
Willy Tarreau4a550602012-12-09 14:53:32 +010010372/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010373 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10374 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10375 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010376 * that in environments where IPv6 is insignificant, truncating the output to
10377 * 8 bytes would still work.
10378 */
10379static int
10380smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010381 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +010010382{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010383 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010384 struct connection *cli_conn = objt_conn(l4->si[0].end);
10385
10386 if (!cli_conn)
10387 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010388
Willy Tarreauef38c392013-07-22 16:29:32 +020010389 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +010010390 return 0;
10391
Willy Tarreau47ca5452012-12-23 20:22:19 +010010392 temp = get_trash_chunk();
Willy Tarreau0dff81c2014-07-15 21:34:06 +020010393 *(unsigned int *)temp->str = htonl(smp->data.uint);
10394 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010395
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010396 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010397 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010398 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010399 temp->len += 4;
10400 break;
10401 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010402 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010403 temp->len += 16;
10404 break;
10405 default:
10406 return 0;
10407 }
10408
10409 smp->data.str = *temp;
10410 smp->type = SMP_T_BIN;
10411 return 1;
10412}
10413
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010414static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010415smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010416 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010417{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010418 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10419 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10420 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010421
Willy Tarreau24e32d82012-04-23 23:55:44 +020010422 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010423
Willy Tarreauf853c462012-04-23 18:53:56 +020010424 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010425 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010426 return 1;
10427}
10428
Willy Tarreau7f18e522010-10-22 20:04:13 +020010429/* return a valid test if the current request is the first one on the connection */
10430static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010431smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010432 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010433{
10434 if (!s)
10435 return 0;
10436
Willy Tarreauf853c462012-04-23 18:53:56 +020010437 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010438 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010439 return 1;
10440}
10441
Willy Tarreau34db1082012-04-19 17:16:54 +020010442/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010443static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010444smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010445 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010446{
10447
Willy Tarreau24e32d82012-04-23 23:55:44 +020010448 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010449 return 0;
10450
Willy Tarreauc0239e02012-04-16 14:42:55 +020010451 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010452
Willy Tarreauc0239e02012-04-16 14:42:55 +020010453 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010454 return 0;
10455
Willy Tarreauf853c462012-04-23 18:53:56 +020010456 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010457 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010458 return 1;
10459}
Willy Tarreau8797c062007-05-07 00:55:35 +020010460
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010461/* Accepts exactly 1 argument of type userlist */
10462static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010463smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010464 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010465{
10466
10467 if (!args || args->type != ARGT_USR)
10468 return 0;
10469
10470 CHECK_HTTP_MESSAGE_FIRST();
10471
10472 if (!get_http_auth(l4))
10473 return 0;
10474
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010475 /* if the user does not belong to the userlist or has a wrong password,
10476 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010477 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010478 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010479 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10480 return 0;
10481
10482 /* pat_match_auth() will need the user list */
10483 smp->ctx.a[0] = args->data.usr;
10484
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010485 smp->type = SMP_T_STR;
10486 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010487 smp->data.str.str = l4->txn.auth.user;
10488 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010489
10490 return 1;
10491}
10492
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010493/* Try to find the next occurrence of a cookie name in a cookie header value.
10494 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10495 * the cookie value is returned into *value and *value_l, and the function
10496 * returns a pointer to the next pointer to search from if the value was found.
10497 * Otherwise if the cookie was not found, NULL is returned and neither value
10498 * nor value_l are touched. The input <hdr> string should first point to the
10499 * header's value, and the <hdr_end> pointer must point to the first character
10500 * not part of the value. <list> must be non-zero if value may represent a list
10501 * of values (cookie headers). This makes it faster to abort parsing when no
10502 * list is expected.
10503 */
10504static char *
10505extract_cookie_value(char *hdr, const char *hdr_end,
10506 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010507 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010508{
10509 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10510 char *next;
10511
10512 /* we search at least a cookie name followed by an equal, and more
10513 * generally something like this :
10514 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10515 */
10516 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10517 /* Iterate through all cookies on this line */
10518
10519 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10520 att_beg++;
10521
10522 /* find att_end : this is the first character after the last non
10523 * space before the equal. It may be equal to hdr_end.
10524 */
10525 equal = att_end = att_beg;
10526
10527 while (equal < hdr_end) {
10528 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10529 break;
10530 if (http_is_spht[(unsigned char)*equal++])
10531 continue;
10532 att_end = equal;
10533 }
10534
10535 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10536 * is between <att_beg> and <equal>, both may be identical.
10537 */
10538
10539 /* look for end of cookie if there is an equal sign */
10540 if (equal < hdr_end && *equal == '=') {
10541 /* look for the beginning of the value */
10542 val_beg = equal + 1;
10543 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10544 val_beg++;
10545
10546 /* find the end of the value, respecting quotes */
10547 next = find_cookie_value_end(val_beg, hdr_end);
10548
10549 /* make val_end point to the first white space or delimitor after the value */
10550 val_end = next;
10551 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10552 val_end--;
10553 } else {
10554 val_beg = val_end = next = equal;
10555 }
10556
10557 /* We have nothing to do with attributes beginning with '$'. However,
10558 * they will automatically be removed if a header before them is removed,
10559 * since they're supposed to be linked together.
10560 */
10561 if (*att_beg == '$')
10562 continue;
10563
10564 /* Ignore cookies with no equal sign */
10565 if (equal == next)
10566 continue;
10567
10568 /* Now we have the cookie name between att_beg and att_end, and
10569 * its value between val_beg and val_end.
10570 */
10571
10572 if (att_end - att_beg == cookie_name_l &&
10573 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10574 /* let's return this value and indicate where to go on from */
10575 *value = val_beg;
10576 *value_l = val_end - val_beg;
10577 return next + 1;
10578 }
10579
10580 /* Set-Cookie headers only have the name in the first attr=value part */
10581 if (!list)
10582 break;
10583 }
10584
10585 return NULL;
10586}
10587
William Lallemanda43ba4e2014-01-28 18:14:25 +010010588/* Fetch a captured HTTP request header. The index is the position of
10589 * the "capture" option in the configuration file
10590 */
10591static int
10592smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10593 const struct arg *args, struct sample *smp, const char *kw)
10594{
10595 struct proxy *fe = l4->fe;
10596 struct http_txn *txn = l7;
10597 int idx;
10598
10599 if (!args || args->type != ARGT_UINT)
10600 return 0;
10601
10602 idx = args->data.uint;
10603
10604 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10605 return 0;
10606
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010607 smp->type = SMP_T_STR;
10608 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010609 smp->data.str.str = txn->req.cap[idx];
10610 smp->data.str.len = strlen(txn->req.cap[idx]);
10611
10612 return 1;
10613}
10614
10615/* Fetch a captured HTTP response header. The index is the position of
10616 * the "capture" option in the configuration file
10617 */
10618static int
10619smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10620 const struct arg *args, struct sample *smp, const char *kw)
10621{
10622 struct proxy *fe = l4->fe;
10623 struct http_txn *txn = l7;
10624 int idx;
10625
10626 if (!args || args->type != ARGT_UINT)
10627 return 0;
10628
10629 idx = args->data.uint;
10630
10631 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10632 return 0;
10633
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010634 smp->type = SMP_T_STR;
10635 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010636 smp->data.str.str = txn->rsp.cap[idx];
10637 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10638
10639 return 1;
10640}
10641
William Lallemand65ad6e12014-01-31 15:08:02 +010010642/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10643static int
10644smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10645 const struct arg *args, struct sample *smp, const char *kw)
10646{
10647 struct chunk *temp;
10648 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010649 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010650
10651 if (!txn->uri)
10652 return 0;
10653
William Lallemand96a77852014-02-05 00:30:02 +010010654 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010655
William Lallemand96a77852014-02-05 00:30:02 +010010656 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10657 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010658
William Lallemand96a77852014-02-05 00:30:02 +010010659 temp = get_trash_chunk();
10660 temp->str = txn->uri;
10661 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010662 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010663 smp->type = SMP_T_STR;
10664 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010665
10666 return 1;
10667
10668}
10669
10670/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10671static int
10672smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10673 const struct arg *args, struct sample *smp, const char *kw)
10674{
10675 struct chunk *temp;
10676 struct http_txn *txn = l7;
10677 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010678
10679 if (!txn->uri)
10680 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010681
William Lallemand65ad6e12014-01-31 15:08:02 +010010682 ptr = txn->uri;
10683
10684 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10685 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010686
William Lallemand65ad6e12014-01-31 15:08:02 +010010687 if (!*ptr)
10688 return 0;
10689
10690 ptr++; /* skip the space */
10691
10692 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010693 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010694 if (!ptr)
10695 return 0;
10696 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10697 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010698
10699 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010700 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010701 smp->type = SMP_T_STR;
10702 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010703
10704 return 1;
10705}
10706
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010707/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10708 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10709 */
10710static int
10711smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10712 const struct arg *args, struct sample *smp, const char *kw)
10713{
10714 struct http_txn *txn = l7;
10715
10716 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10717 return 0;
10718
10719 if (txn->req.flags & HTTP_MSGF_VER_11)
10720 smp->data.str.str = "HTTP/1.1";
10721 else
10722 smp->data.str.str = "HTTP/1.0";
10723
10724 smp->data.str.len = 8;
10725 smp->type = SMP_T_STR;
10726 smp->flags = SMP_F_CONST;
10727 return 1;
10728
10729}
10730
10731/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10732 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10733 */
10734static int
10735smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10736 const struct arg *args, struct sample *smp, const char *kw)
10737{
10738 struct http_txn *txn = l7;
10739
10740 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10741 return 0;
10742
10743 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10744 smp->data.str.str = "HTTP/1.1";
10745 else
10746 smp->data.str.str = "HTTP/1.0";
10747
10748 smp->data.str.len = 8;
10749 smp->type = SMP_T_STR;
10750 smp->flags = SMP_F_CONST;
10751 return 1;
10752
10753}
10754
William Lallemand65ad6e12014-01-31 15:08:02 +010010755
Willy Tarreaue333ec92012-04-16 16:26:40 +020010756/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010757 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010758 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010759 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010760 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010761 * Accepts exactly 1 argument of type string. If the input options indicate
10762 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010763 * The returned sample is of type CSTR. Can be used to parse cookies in other
10764 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010765 */
William Lallemand07c8b242014-05-02 17:11:07 +020010766int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010767 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010768{
10769 struct http_txn *txn = l7;
10770 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010771 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010772 const struct http_msg *msg;
10773 const char *hdr_name;
10774 int hdr_name_len;
10775 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010776 int occ = 0;
10777 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010778
Willy Tarreau24e32d82012-04-23 23:55:44 +020010779 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010780 return 0;
10781
Willy Tarreaua890d072013-04-02 12:01:06 +020010782 if (!ctx) {
10783 /* first call */
10784 ctx = &static_hdr_ctx;
10785 ctx->idx = 0;
10786 smp->ctx.a[2] = ctx;
10787 }
10788
Willy Tarreaue333ec92012-04-16 16:26:40 +020010789 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010790
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010791 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010792 msg = &txn->req;
10793 hdr_name = "Cookie";
10794 hdr_name_len = 6;
10795 } else {
10796 msg = &txn->rsp;
10797 hdr_name = "Set-Cookie";
10798 hdr_name_len = 10;
10799 }
10800
Willy Tarreau28376d62012-04-26 21:26:10 +020010801 if (!occ && !(opt & SMP_OPT_ITERATE))
10802 /* no explicit occurrence and single fetch => last cookie by default */
10803 occ = -1;
10804
10805 /* OK so basically here, either we want only one value and it's the
10806 * last one, or we want to iterate over all of them and we fetch the
10807 * next one.
10808 */
10809
Willy Tarreau9b28e032012-10-12 23:49:43 +020010810 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010811 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010812 /* search for the header from the beginning, we must first initialize
10813 * the search parameters.
10814 */
Willy Tarreau37406352012-04-23 16:16:37 +020010815 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010816 ctx->idx = 0;
10817 }
10818
Willy Tarreau28376d62012-04-26 21:26:10 +020010819 smp->flags |= SMP_F_VOL_HDR;
10820
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010821 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010822 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10823 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010824 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10825 goto out;
10826
Willy Tarreau24e32d82012-04-23 23:55:44 +020010827 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010828 continue;
10829
Willy Tarreau37406352012-04-23 16:16:37 +020010830 smp->ctx.a[0] = ctx->line + ctx->val;
10831 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010832 }
10833
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010834 smp->type = SMP_T_STR;
10835 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010836 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010837 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010838 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010839 &smp->data.str.str,
10840 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010841 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010842 found = 1;
10843 if (occ >= 0) {
10844 /* one value was returned into smp->data.str.{str,len} */
10845 smp->flags |= SMP_F_NOT_LAST;
10846 return 1;
10847 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010848 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010849 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010850 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010851 /* all cookie headers and values were scanned. If we're looking for the
10852 * last occurrence, we may return it now.
10853 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010854 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010855 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010856 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010857}
10858
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010859/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010860 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010861 * multiple cookies may be parsed on the same line. The returned sample is of
10862 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010863 */
10864static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010865smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010866 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010867{
10868 struct http_txn *txn = l7;
10869 struct hdr_idx *idx = &txn->hdr_idx;
10870 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010871 const struct http_msg *msg;
10872 const char *hdr_name;
10873 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010874 int cnt;
10875 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010876 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010877
Willy Tarreau24e32d82012-04-23 23:55:44 +020010878 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010879 return 0;
10880
Willy Tarreaue333ec92012-04-16 16:26:40 +020010881 CHECK_HTTP_MESSAGE_FIRST();
10882
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010883 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010884 msg = &txn->req;
10885 hdr_name = "Cookie";
10886 hdr_name_len = 6;
10887 } else {
10888 msg = &txn->rsp;
10889 hdr_name = "Set-Cookie";
10890 hdr_name_len = 10;
10891 }
10892
Willy Tarreau9b28e032012-10-12 23:49:43 +020010893 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010894 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010895 ctx.idx = 0;
10896 cnt = 0;
10897
10898 while (1) {
10899 /* Note: val_beg == NULL every time we need to fetch a new header */
10900 if (!val_beg) {
10901 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10902 break;
10903
Willy Tarreau24e32d82012-04-23 23:55:44 +020010904 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010905 continue;
10906
10907 val_beg = ctx.line + ctx.val;
10908 val_end = val_beg + ctx.vlen;
10909 }
10910
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010911 smp->type = SMP_T_STR;
10912 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010913 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010914 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010915 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010916 &smp->data.str.str,
10917 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010918 cnt++;
10919 }
10920 }
10921
Willy Tarreaub169eba2013-12-16 15:14:43 +010010922 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010923 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010924 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010925 return 1;
10926}
10927
Willy Tarreau51539362012-05-08 12:46:28 +020010928/* Fetch an cookie's integer value. The integer value is returned. It
10929 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10930 */
10931static int
10932smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010933 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010934{
Willy Tarreauef38c392013-07-22 16:29:32 +020010935 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010936
10937 if (ret > 0) {
10938 smp->type = SMP_T_UINT;
10939 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10940 }
10941
10942 return ret;
10943}
10944
Willy Tarreau8797c062007-05-07 00:55:35 +020010945/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010946/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010947/************************************************************************/
10948
David Cournapeau16023ee2010-12-23 20:55:41 +090010949/*
10950 * Given a path string and its length, find the position of beginning of the
10951 * query string. Returns NULL if no query string is found in the path.
10952 *
10953 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10954 *
10955 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10956 */
bedis4c75cca2012-10-05 08:38:24 +020010957static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010958{
10959 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010960
bedis4c75cca2012-10-05 08:38:24 +020010961 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010962 return p ? p + 1 : NULL;
10963}
10964
bedis4c75cca2012-10-05 08:38:24 +020010965static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010966{
bedis4c75cca2012-10-05 08:38:24 +020010967 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010968}
10969
10970/*
10971 * Given a url parameter, find the starting position of the first occurence,
10972 * or NULL if the parameter is not found.
10973 *
10974 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10975 * the function will return query_string+8.
10976 */
10977static char*
10978find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010979 char* url_param_name, size_t url_param_name_l,
10980 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010981{
10982 char *pos, *last;
10983
10984 pos = query_string;
10985 last = query_string + query_string_l - url_param_name_l - 1;
10986
10987 while (pos <= last) {
10988 if (pos[url_param_name_l] == '=') {
10989 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10990 return pos;
10991 pos += url_param_name_l + 1;
10992 }
bedis4c75cca2012-10-05 08:38:24 +020010993 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010994 pos++;
10995 pos++;
10996 }
10997 return NULL;
10998}
10999
11000/*
11001 * Given a url parameter name, returns its value and size into *value and
11002 * *value_l respectively, and returns non-zero. If the parameter is not found,
11003 * zero is returned and value/value_l are not touched.
11004 */
11005static int
11006find_url_param_value(char* path, size_t path_l,
11007 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011008 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011009{
11010 char *query_string, *qs_end;
11011 char *arg_start;
11012 char *value_start, *value_end;
11013
bedis4c75cca2012-10-05 08:38:24 +020011014 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011015 if (!query_string)
11016 return 0;
11017
11018 qs_end = path + path_l;
11019 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011020 url_param_name, url_param_name_l,
11021 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011022 if (!arg_start)
11023 return 0;
11024
11025 value_start = arg_start + url_param_name_l + 1;
11026 value_end = value_start;
11027
bedis4c75cca2012-10-05 08:38:24 +020011028 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011029 value_end++;
11030
11031 *value = value_start;
11032 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011033 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011034}
11035
11036static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011037smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011038 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090011039{
bedis4c75cca2012-10-05 08:38:24 +020011040 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090011041 struct http_txn *txn = l7;
11042 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011043
bedis4c75cca2012-10-05 08:38:24 +020011044 if (!args || args[0].type != ARGT_STR ||
11045 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011046 return 0;
11047
11048 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011049
bedis4c75cca2012-10-05 08:38:24 +020011050 if (args[1].type)
11051 delim = *args[1].data.str.str;
11052
Willy Tarreau9b28e032012-10-12 23:49:43 +020011053 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011054 args->data.str.str, args->data.str.len,
11055 &smp->data.str.str, &smp->data.str.len,
11056 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011057 return 0;
11058
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011059 smp->type = SMP_T_STR;
11060 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011061 return 1;
11062}
11063
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011064/* Return the signed integer value for the specified url parameter (see url_param
11065 * above).
11066 */
11067static int
11068smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011069 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011070{
Willy Tarreauef38c392013-07-22 16:29:32 +020011071 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011072
11073 if (ret > 0) {
11074 smp->type = SMP_T_UINT;
11075 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11076 }
11077
11078 return ret;
11079}
11080
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011081/* This produces a 32-bit hash of the concatenation of the first occurrence of
11082 * the Host header followed by the path component if it begins with a slash ('/').
11083 * This means that '*' will not be added, resulting in exactly the first Host
11084 * entry. If no Host header is found, then the path is used. The resulting value
11085 * is hashed using the url hash followed by a full avalanche hash and provides a
11086 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11087 * high-traffic sites without having to store whole paths.
11088 * this differs from the base32 functions in that it includes the url parameters
11089 * as well as the path
11090 */
11091static int
11092smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011093 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011094{
11095 struct http_txn *txn = l7;
11096 struct hdr_ctx ctx;
11097 unsigned int hash = 0;
11098 char *ptr, *beg, *end;
11099 int len;
11100
11101 CHECK_HTTP_MESSAGE_FIRST();
11102
11103 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011104 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011105 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11106 ptr = ctx.line + ctx.val;
11107 len = ctx.vlen;
11108 while (len--)
11109 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11110 }
11111
11112 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011113 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 +000011114 beg = http_get_path(txn);
11115 if (!beg)
11116 beg = end;
11117
11118 for (ptr = beg; ptr < end ; ptr++);
11119
11120 if (beg < ptr && *beg == '/') {
11121 while (beg < ptr)
11122 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11123 }
11124 hash = full_hash(hash);
11125
11126 smp->type = SMP_T_UINT;
11127 smp->data.uint = hash;
11128 smp->flags = SMP_F_VOL_1ST;
11129 return 1;
11130}
11131
11132/* This concatenates the source address with the 32-bit hash of the Host and
11133 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11134 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11135 * on the source address length. The URL hash is stored before the address so
11136 * that in environments where IPv6 is insignificant, truncating the output to
11137 * 8 bytes would still work.
11138 */
11139static int
11140smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011141 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011142{
11143 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011144 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011145
Willy Tarreaue155ec22013-11-18 18:33:22 +010011146 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011147 return 0;
11148
11149 temp = get_trash_chunk();
11150 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11151 temp->len += sizeof(smp->data.uint);
11152
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011153 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011154 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011155 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011156 temp->len += 4;
11157 break;
11158 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011159 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011160 temp->len += 16;
11161 break;
11162 default:
11163 return 0;
11164 }
11165
11166 smp->data.str = *temp;
11167 smp->type = SMP_T_BIN;
11168 return 1;
11169}
11170
Willy Tarreau185b5c42012-04-26 15:11:51 +020011171/* This function is used to validate the arguments passed to any "hdr" fetch
11172 * keyword. These keywords support an optional positive or negative occurrence
11173 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11174 * is assumed that the types are already the correct ones. Returns 0 on error,
11175 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11176 * error message in case of error, that the caller is responsible for freeing.
11177 * The initial location must either be freeable or NULL.
11178 */
11179static int val_hdr(struct arg *arg, char **err_msg)
11180{
11181 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011182 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011183 return 0;
11184 }
11185 return 1;
11186}
11187
Willy Tarreau276fae92013-07-25 14:36:01 +020011188/* takes an UINT value on input supposed to represent the time since EPOCH,
11189 * adds an optional offset found in args[0] and emits a string representing
11190 * the date in RFC-1123/5322 format.
11191 */
11192static int sample_conv_http_date(const struct arg *args, struct sample *smp)
11193{
11194 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11195 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11196 struct chunk *temp;
11197 struct tm *tm;
11198 time_t curr_date = smp->data.uint;
11199
11200 /* add offset */
11201 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11202 curr_date += args[0].data.sint;
11203
11204 tm = gmtime(&curr_date);
11205
11206 temp = get_trash_chunk();
11207 temp->len = snprintf(temp->str, temp->size - temp->len,
11208 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11209 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11210 tm->tm_hour, tm->tm_min, tm->tm_sec);
11211
11212 smp->data.str = *temp;
11213 smp->type = SMP_T_STR;
11214 return 1;
11215}
11216
Thierry FOURNIERad903512014-04-11 17:51:01 +020011217/* Match language range with language tag. RFC2616 14.4:
11218 *
11219 * A language-range matches a language-tag if it exactly equals
11220 * the tag, or if it exactly equals a prefix of the tag such
11221 * that the first tag character following the prefix is "-".
11222 *
11223 * Return 1 if the strings match, else return 0.
11224 */
11225static inline int language_range_match(const char *range, int range_len,
11226 const char *tag, int tag_len)
11227{
11228 const char *end = range + range_len;
11229 const char *tend = tag + tag_len;
11230 while (range < end) {
11231 if (*range == '-' && tag == tend)
11232 return 1;
11233 if (*range != *tag || tag == tend)
11234 return 0;
11235 range++;
11236 tag++;
11237 }
11238 /* Return true only if the last char of the tag is matched. */
11239 return tag == tend;
11240}
11241
11242/* Arguments: The list of expected value, the number of parts returned and the separator */
11243static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
11244{
11245 const char *al = smp->data.str.str;
11246 const char *end = al + smp->data.str.len;
11247 const char *token;
11248 int toklen;
11249 int qvalue;
11250 const char *str;
11251 const char *w;
11252 int best_q = 0;
11253
11254 /* Set the constant to the sample, because the output of the
11255 * function will be peek in the constant configuration string.
11256 */
11257 smp->flags |= SMP_F_CONST;
11258 smp->data.str.size = 0;
11259 smp->data.str.str = "";
11260 smp->data.str.len = 0;
11261
11262 /* Parse the accept language */
11263 while (1) {
11264
11265 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011266 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011267 al++;
11268 if (al >= end)
11269 break;
11270
11271 /* Start of the fisrt word. */
11272 token = al;
11273
11274 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011275 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011276 al++;
11277 if (al == token)
11278 goto expect_comma;
11279
11280 /* Length of the token. */
11281 toklen = al - token;
11282 qvalue = 1000;
11283
11284 /* Check if the token exists in the list. If the token not exists,
11285 * jump to the next token.
11286 */
11287 str = args[0].data.str.str;
11288 w = str;
11289 while (1) {
11290 if (*str == ';' || *str == '\0') {
11291 if (language_range_match(token, toklen, w, str-w))
11292 goto look_for_q;
11293 if (*str == '\0')
11294 goto expect_comma;
11295 w = str + 1;
11296 }
11297 str++;
11298 }
11299 goto expect_comma;
11300
11301look_for_q:
11302
11303 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011304 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011305 al++;
11306 if (al >= end)
11307 goto process_value;
11308
11309 /* If ',' is found, process the result */
11310 if (*al == ',')
11311 goto process_value;
11312
11313 /* If the character is different from ';', look
11314 * for the end of the header part in best effort.
11315 */
11316 if (*al != ';')
11317 goto expect_comma;
11318
11319 /* Assumes that the char is ';', now expect "q=". */
11320 al++;
11321
11322 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011323 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011324 al++;
11325 if (al >= end)
11326 goto process_value;
11327
11328 /* Expect 'q'. If no 'q', continue in best effort */
11329 if (*al != 'q')
11330 goto process_value;
11331 al++;
11332
11333 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011334 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011335 al++;
11336 if (al >= end)
11337 goto process_value;
11338
11339 /* Expect '='. If no '=', continue in best effort */
11340 if (*al != '=')
11341 goto process_value;
11342 al++;
11343
11344 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011345 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011346 al++;
11347 if (al >= end)
11348 goto process_value;
11349
11350 /* Parse the q value. */
11351 qvalue = parse_qvalue(al, &al);
11352
11353process_value:
11354
11355 /* If the new q value is the best q value, then store the associated
11356 * language in the response. If qvalue is the biggest value (1000),
11357 * break the process.
11358 */
11359 if (qvalue > best_q) {
11360 smp->data.str.str = (char *)w;
11361 smp->data.str.len = str - w;
11362 if (qvalue >= 1000)
11363 break;
11364 best_q = qvalue;
11365 }
11366
11367expect_comma:
11368
11369 /* Expect comma or end. If the end is detected, quit the loop. */
11370 while (al < end && *al != ',')
11371 al++;
11372 if (al >= end)
11373 break;
11374
11375 /* Comma is found, jump it and restart the analyzer. */
11376 al++;
11377 }
11378
11379 /* Set default value if required. */
11380 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11381 smp->data.str.str = args[1].data.str.str;
11382 smp->data.str.len = args[1].data.str.len;
11383 }
11384
11385 /* Return true only if a matching language was found. */
11386 return smp->data.str.len != 0;
11387}
11388
William Lallemand73025dd2014-04-24 14:38:37 +020011389/*
11390 * Return the struct http_req_action_kw associated to a keyword.
11391 */
11392struct http_req_action_kw *action_http_req_custom(const char *kw)
11393{
11394 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11395 struct http_req_action_kw_list *kw_list;
11396 int i;
11397
11398 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11399 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11400 if (!strcmp(kw, kw_list->kw[i].kw))
11401 return &kw_list->kw[i];
11402 }
11403 }
11404 }
11405 return NULL;
11406}
11407
11408/*
11409 * Return the struct http_res_action_kw associated to a keyword.
11410 */
11411struct http_res_action_kw *action_http_res_custom(const char *kw)
11412{
11413 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11414 struct http_res_action_kw_list *kw_list;
11415 int i;
11416
11417 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11418 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11419 if (!strcmp(kw, kw_list->kw[i].kw))
11420 return &kw_list->kw[i];
11421 }
11422 }
11423 }
11424 return NULL;
11425}
11426
Willy Tarreau4a568972010-05-12 08:08:50 +020011427/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011428/* All supported ACL keywords must be declared here. */
11429/************************************************************************/
11430
11431/* Note: must not be declared <const> as its list will be overwritten.
11432 * Please take care of keeping this list alphabetically sorted.
11433 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011434static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011435 { "base", "base", PAT_MATCH_STR },
11436 { "base_beg", "base", PAT_MATCH_BEG },
11437 { "base_dir", "base", PAT_MATCH_DIR },
11438 { "base_dom", "base", PAT_MATCH_DOM },
11439 { "base_end", "base", PAT_MATCH_END },
11440 { "base_len", "base", PAT_MATCH_LEN },
11441 { "base_reg", "base", PAT_MATCH_REG },
11442 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011443
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011444 { "cook", "req.cook", PAT_MATCH_STR },
11445 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11446 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11447 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11448 { "cook_end", "req.cook", PAT_MATCH_END },
11449 { "cook_len", "req.cook", PAT_MATCH_LEN },
11450 { "cook_reg", "req.cook", PAT_MATCH_REG },
11451 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011452
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011453 { "hdr", "req.hdr", PAT_MATCH_STR },
11454 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11455 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11456 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11457 { "hdr_end", "req.hdr", PAT_MATCH_END },
11458 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11459 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11460 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011461
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011462 /* these two declarations uses strings with list storage (in place
11463 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11464 * and delete functions are relative to the list management. The parse
11465 * and match method are related to the corresponding fetch methods. This
11466 * is very particular ACL declaration mode.
11467 */
11468 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11469 { "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 +020011470
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011471 { "path", "path", PAT_MATCH_STR },
11472 { "path_beg", "path", PAT_MATCH_BEG },
11473 { "path_dir", "path", PAT_MATCH_DIR },
11474 { "path_dom", "path", PAT_MATCH_DOM },
11475 { "path_end", "path", PAT_MATCH_END },
11476 { "path_len", "path", PAT_MATCH_LEN },
11477 { "path_reg", "path", PAT_MATCH_REG },
11478 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011479
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011480 { "req_ver", "req.ver", PAT_MATCH_STR },
11481 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011482
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011483 { "scook", "res.cook", PAT_MATCH_STR },
11484 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11485 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11486 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11487 { "scook_end", "res.cook", PAT_MATCH_END },
11488 { "scook_len", "res.cook", PAT_MATCH_LEN },
11489 { "scook_reg", "res.cook", PAT_MATCH_REG },
11490 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011491
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011492 { "shdr", "res.hdr", PAT_MATCH_STR },
11493 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11494 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11495 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11496 { "shdr_end", "res.hdr", PAT_MATCH_END },
11497 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11498 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11499 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011500
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011501 { "url", "url", PAT_MATCH_STR },
11502 { "url_beg", "url", PAT_MATCH_BEG },
11503 { "url_dir", "url", PAT_MATCH_DIR },
11504 { "url_dom", "url", PAT_MATCH_DOM },
11505 { "url_end", "url", PAT_MATCH_END },
11506 { "url_len", "url", PAT_MATCH_LEN },
11507 { "url_reg", "url", PAT_MATCH_REG },
11508 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011509
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011510 { "urlp", "urlp", PAT_MATCH_STR },
11511 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11512 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11513 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11514 { "urlp_end", "urlp", PAT_MATCH_END },
11515 { "urlp_len", "urlp", PAT_MATCH_LEN },
11516 { "urlp_reg", "urlp", PAT_MATCH_REG },
11517 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011518
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011519 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011520}};
11521
11522/************************************************************************/
11523/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011524/************************************************************************/
11525/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011526static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011527 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011528 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11529 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11530
William Lallemanda43ba4e2014-01-28 18:14:25 +010011531 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011532 { "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 +020011533
11534 /* retrieve these captures from the HTTP logs */
11535 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11536 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11537 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11538
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011539 { "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 +020011540 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011541
Willy Tarreau409bcde2013-01-08 00:31:00 +010011542 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11543 * are only here to match the ACL's name, are request-only and are used
11544 * for ACL compatibility only.
11545 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011546 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11547 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011548 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11549 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11550
11551 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11552 * only here to match the ACL's name, are request-only and are used for
11553 * ACL compatibility only.
11554 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011555 { "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 +010011556 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11557 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11558 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11559
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011560 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011561 { "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 +010011562 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011563 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011564 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011565
11566 /* HTTP protocol on the request path */
11567 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011568 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011569
11570 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011571 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11572 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011573
11574 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011575 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11576 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011577
Willy Tarreau18ed2562013-01-14 15:56:36 +010011578 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011579 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011580 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11581 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11582
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011583 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011584 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011585 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011586 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11587 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11588 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11589
11590 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011591 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011592 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11593 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11594
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011595 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011596 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011597 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011598 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11599 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11600 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11601
Willy Tarreau409bcde2013-01-08 00:31:00 +010011602 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011603 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011604 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11605 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011606 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011607
11608 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011609 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011610 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11611 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11612 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11613
11614 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011615 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011616 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11617 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011618 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11619 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011620 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11621 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011622 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11623 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011624}};
11625
Willy Tarreau8797c062007-05-07 00:55:35 +020011626
Willy Tarreau276fae92013-07-25 14:36:01 +020011627/* Note: must not be declared <const> as its list will be overwritten */
11628static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011629 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11630 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011631 { NULL, NULL, 0, 0, 0 },
11632}};
11633
Willy Tarreau8797c062007-05-07 00:55:35 +020011634__attribute__((constructor))
11635static void __http_protocol_init(void)
11636{
11637 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011638 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011639 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020011640}
11641
11642
Willy Tarreau58f10d72006-12-04 02:26:12 +010011643/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011644 * Local variables:
11645 * c-indent-level: 8
11646 * c-basic-offset: 8
11647 * End:
11648 */