blob: 33aaf67ec95870f23698a8719033d0cb164b1db6 [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;
2481 struct http_txn *txn = &s->txn;
2482 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002483 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002484
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002485 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 +01002486 now_ms, __FUNCTION__,
2487 s,
2488 req,
2489 req->rex, req->wex,
2490 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002491 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002492 req->analysers);
2493
Willy Tarreau52a0c602009-08-16 22:45:38 +02002494 /* we're speaking HTTP here, so let's speak HTTP to the client */
2495 s->srv_error = http_return_srv_error;
2496
Willy Tarreau83e3af02009-12-28 17:39:57 +01002497 /* There's a protected area at the end of the buffer for rewriting
2498 * purposes. We don't want to start to parse the request if the
2499 * protected area is affected, because we may have to move processed
2500 * data later, which is much more complicated.
2501 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002502 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002503 if (txn->flags & TX_NOT_FIRST) {
2504 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002505 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002506 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002507 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002508 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002509 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002510 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002511 return 0;
2512 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002513 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2514 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2515 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002516 }
2517
Willy Tarreau065e8332010-01-08 00:30:20 +01002518 /* Note that we have the same problem with the response ; we
2519 * may want to send a redirect, error or anything which requires
2520 * some spare space. So we'll ensure that we have at least
2521 * maxrewrite bytes available in the response buffer before
2522 * processing that one. This will only affect pipelined
2523 * keep-alive requests.
2524 */
2525 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002526 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002527 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2528 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2529 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002530 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002531 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002532 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002533 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002534 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002535 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002536 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002537 return 0;
2538 }
2539 }
2540
Willy Tarreau9b28e032012-10-12 23:49:43 +02002541 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002542 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002543 }
2544
Willy Tarreau59234e92008-11-30 23:51:27 +01002545 /* 1: we might have to print this header in debug mode */
2546 if (unlikely((global.mode & MODE_DEBUG) &&
2547 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau8767b132014-10-21 19:36:09 +02002548 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002549 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002550
Willy Tarreau9b28e032012-10-12 23:49:43 +02002551 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002552 /* this is a bit complex : in case of error on the request line,
2553 * we know that rq.l is still zero, so we display only the part
2554 * up to the end of the line (truncated by debug_hdr).
2555 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002556 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002557 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002558
Willy Tarreau59234e92008-11-30 23:51:27 +01002559 sol += hdr_idx_first_pos(&txn->hdr_idx);
2560 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002561
Willy Tarreau59234e92008-11-30 23:51:27 +01002562 while (cur_idx) {
2563 eol = sol + txn->hdr_idx.v[cur_idx].len;
2564 debug_hdr("clihdr", s, sol, eol);
2565 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2566 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002567 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002568 }
2569
Willy Tarreau58f10d72006-12-04 02:26:12 +01002570
Willy Tarreau59234e92008-11-30 23:51:27 +01002571 /*
2572 * Now we quickly check if we have found a full valid request.
2573 * If not so, we check the FD and buffer states before leaving.
2574 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002575 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002576 * requests are checked first. When waiting for a second request
2577 * on a keep-alive session, if we encounter and error, close, t/o,
2578 * we note the error in the session flags but don't set any state.
2579 * Since the error will be noted there, it will not be counted by
2580 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002581 * Last, we may increase some tracked counters' http request errors on
2582 * the cases that are deliberately the client's fault. For instance,
2583 * a timeout or connection reset is not counted as an error. However
2584 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002585 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002586
Willy Tarreau655dce92009-11-08 13:10:58 +01002587 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002588 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002589 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002590 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002591 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002592 session_inc_http_req_ctr(s);
2593 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002594 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002595 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002596 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002597
Willy Tarreau59234e92008-11-30 23:51:27 +01002598 /* 1: Since we are in header mode, if there's no space
2599 * left for headers, we won't be able to free more
2600 * later, so the session will never terminate. We
2601 * must terminate it now.
2602 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002603 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 /* FIXME: check if URI is set and return Status
2605 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002606 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002607 session_inc_http_req_ctr(s);
2608 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002609 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002610 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002611 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002612 goto return_bad_req;
2613 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002614
Willy Tarreau59234e92008-11-30 23:51:27 +01002615 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002616 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002617 if (!(s->flags & SN_ERR_MASK))
2618 s->flags |= SN_ERR_CLICL;
2619
Willy Tarreaufcffa692010-01-10 14:21:19 +01002620 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002621 goto failed_keep_alive;
2622
Willy Tarreau59234e92008-11-30 23:51:27 +01002623 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002624 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002625 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002626 session_inc_http_err_ctr(s);
2627 }
2628
Willy Tarreaudc979f22012-12-04 10:39:01 +01002629 txn->status = 400;
2630 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002631 msg->msg_state = HTTP_MSG_ERROR;
2632 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002633
Willy Tarreauda7ff642010-06-23 11:44:09 +02002634 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002635 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002636 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002637 if (s->listener->counters)
2638 s->listener->counters->failed_req++;
2639
Willy Tarreau59234e92008-11-30 23:51:27 +01002640 if (!(s->flags & SN_FINST_MASK))
2641 s->flags |= SN_FINST_R;
2642 return 0;
2643 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002644
Willy Tarreau59234e92008-11-30 23:51:27 +01002645 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002646 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002647 if (!(s->flags & SN_ERR_MASK))
2648 s->flags |= SN_ERR_CLITO;
2649
Willy Tarreaufcffa692010-01-10 14:21:19 +01002650 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002651 goto failed_keep_alive;
2652
Willy Tarreau59234e92008-11-30 23:51:27 +01002653 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002654 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002655 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002656 session_inc_http_err_ctr(s);
2657 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002658 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002659 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002660 msg->msg_state = HTTP_MSG_ERROR;
2661 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002662
Willy Tarreauda7ff642010-06-23 11:44:09 +02002663 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002664 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002665 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002666 if (s->listener->counters)
2667 s->listener->counters->failed_req++;
2668
Willy Tarreau59234e92008-11-30 23:51:27 +01002669 if (!(s->flags & SN_FINST_MASK))
2670 s->flags |= SN_FINST_R;
2671 return 0;
2672 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002673
Willy Tarreau59234e92008-11-30 23:51:27 +01002674 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002675 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002676 if (!(s->flags & SN_ERR_MASK))
2677 s->flags |= SN_ERR_CLICL;
2678
Willy Tarreaufcffa692010-01-10 14:21:19 +01002679 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002680 goto failed_keep_alive;
2681
Willy Tarreau4076a152009-04-02 15:18:36 +02002682 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002683 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002685 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002686 msg->msg_state = HTTP_MSG_ERROR;
2687 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002688
Willy Tarreauda7ff642010-06-23 11:44:09 +02002689 session_inc_http_err_ctr(s);
2690 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002691 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002692 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002693 if (s->listener->counters)
2694 s->listener->counters->failed_req++;
2695
Willy Tarreau59234e92008-11-30 23:51:27 +01002696 if (!(s->flags & SN_FINST_MASK))
2697 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002698 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002699 }
2700
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002701 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002702 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2703 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002704#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002705 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 +01002706 /* We need more data, we have to re-enable quick-ack in case we
2707 * previously disabled it, otherwise we might cause the client
2708 * to delay next data.
2709 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002710 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002711 }
2712#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002713
Willy Tarreaufcffa692010-01-10 14:21:19 +01002714 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2715 /* If the client starts to talk, let's fall back to
2716 * request timeout processing.
2717 */
2718 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002719 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002720 }
2721
Willy Tarreau59234e92008-11-30 23:51:27 +01002722 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002723 if (!tick_isset(req->analyse_exp)) {
2724 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2725 (txn->flags & TX_WAIT_NEXT_RQ) &&
2726 tick_isset(s->be->timeout.httpka))
2727 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2728 else
2729 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2730 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002731
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 /* we're not ready yet */
2733 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002734
2735 failed_keep_alive:
2736 /* Here we process low-level errors for keep-alive requests. In
2737 * short, if the request is not the first one and it experiences
2738 * a timeout, read error or shutdown, we just silently close so
2739 * that the client can try again.
2740 */
2741 txn->status = 0;
2742 msg->msg_state = HTTP_MSG_RQBEFORE;
2743 req->analysers = 0;
2744 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002745 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002746 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002747 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002748 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002749 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002750
Willy Tarreaud787e662009-07-07 10:14:51 +02002751 /* OK now we have a complete HTTP request with indexed headers. Let's
2752 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002753 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002754 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002755 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002756 * byte after the last LF. msg->sov points to the first byte of data.
2757 * msg->eol cannot be trusted because it may have been left uninitialized
2758 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002759 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002760
Willy Tarreauda7ff642010-06-23 11:44:09 +02002761 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002762 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2763
Willy Tarreaub16a5742010-01-10 14:46:16 +01002764 if (txn->flags & TX_WAIT_NEXT_RQ) {
2765 /* kill the pending keep-alive timeout */
2766 txn->flags &= ~TX_WAIT_NEXT_RQ;
2767 req->analyse_exp = TICK_ETERNITY;
2768 }
2769
2770
Willy Tarreaud787e662009-07-07 10:14:51 +02002771 /* Maybe we found in invalid header name while we were configured not
2772 * to block on that, so we have to capture it now.
2773 */
2774 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002775 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002776
Willy Tarreau59234e92008-11-30 23:51:27 +01002777 /*
2778 * 1: identify the method
2779 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002780 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002781
2782 /* we can make use of server redirect on GET and HEAD */
2783 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2784 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002785
Willy Tarreau59234e92008-11-30 23:51:27 +01002786 /*
2787 * 2: check if the URI matches the monitor_uri.
2788 * We have to do this for every request which gets in, because
2789 * the monitor-uri is defined by the frontend.
2790 */
2791 if (unlikely((s->fe->monitor_uri_len != 0) &&
2792 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002793 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002794 s->fe->monitor_uri,
2795 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002796 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002797 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002798 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002799 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002800
Willy Tarreau59234e92008-11-30 23:51:27 +01002801 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002802 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002803
Willy Tarreau59234e92008-11-30 23:51:27 +01002804 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002805 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002806 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002807
Willy Tarreau59234e92008-11-30 23:51:27 +01002808 ret = acl_pass(ret);
2809 if (cond->pol == ACL_COND_UNLESS)
2810 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002811
Willy Tarreau59234e92008-11-30 23:51:27 +01002812 if (ret) {
2813 /* we fail this request, let's return 503 service unavail */
2814 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002815 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002816 if (!(s->flags & SN_ERR_MASK))
2817 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002818 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002819 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002820 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002821
Willy Tarreau59234e92008-11-30 23:51:27 +01002822 /* nothing to fail, let's reply normaly */
2823 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002824 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002825 if (!(s->flags & SN_ERR_MASK))
2826 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002827 goto return_prx_cond;
2828 }
2829
2830 /*
2831 * 3: Maybe we have to copy the original REQURI for the logs ?
2832 * Note: we cannot log anymore if the request has been
2833 * classified as invalid.
2834 */
2835 if (unlikely(s->logs.logwait & LW_REQ)) {
2836 /* we have a complete HTTP request that we must log */
2837 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2838 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002839
Willy Tarreau59234e92008-11-30 23:51:27 +01002840 if (urilen >= REQURI_LEN)
2841 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002842 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002843 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002844
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002845 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002846 s->do_log(s);
2847 } else {
2848 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002849 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002850 }
Willy Tarreau06619262006-12-17 08:37:22 +01002851
Willy Tarreau59234e92008-11-30 23:51:27 +01002852 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002853 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002854 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002855
Willy Tarreau55645552015-05-01 13:26:00 +02002856 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2857 * exactly one digit "." one digit. This check may be disabled using
2858 * option accept-invalid-http-request.
2859 */
2860 if (!(s->fe->options2 & PR_O2_REQBUG_OK)) {
2861 if (msg->sl.rq.v_l != 8) {
2862 msg->err_pos = msg->sl.rq.v;
2863 goto return_bad_req;
2864 }
2865
2866 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2867 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2868 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2869 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2870 msg->err_pos = msg->sl.rq.v + 4;
2871 goto return_bad_req;
2872 }
2873 }
2874
Willy Tarreau5b154472009-12-21 20:11:07 +01002875 /* ... and check if the request is HTTP/1.1 or above */
2876 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002877 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2878 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2879 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002880 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002881
2882 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002883 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 +01002884
Willy Tarreau88d349d2010-01-25 12:15:43 +01002885 /* if the frontend has "option http-use-proxy-header", we'll check if
2886 * we have what looks like a proxied connection instead of a connection,
2887 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2888 * Note that this is *not* RFC-compliant, however browsers and proxies
2889 * happen to do that despite being non-standard :-(
2890 * We consider that a request not beginning with either '/' or '*' is
2891 * a proxied connection, which covers both "scheme://location" and
2892 * CONNECT ip:port.
2893 */
2894 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002895 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002896 txn->flags |= TX_USE_PX_CONN;
2897
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002898 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002899 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002900
Willy Tarreau59234e92008-11-30 23:51:27 +01002901 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002902 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002903 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002904 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002905
Willy Tarreau4db603d2015-05-01 10:05:17 +02002906 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2907 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002908 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002909 * The length of a message body is determined by one of the following
2910 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002911 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002912 * 1. Any response to a HEAD request and any response with a 1xx
2913 * (Informational), 204 (No Content), or 304 (Not Modified) status
2914 * code is always terminated by the first empty line after the
2915 * header fields, regardless of the header fields present in the
2916 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002917 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002918 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2919 * the connection will become a tunnel immediately after the empty
2920 * line that concludes the header fields. A client MUST ignore any
2921 * Content-Length or Transfer-Encoding header fields received in
2922 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002923 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002924 * 3. If a Transfer-Encoding header field is present and the chunked
2925 * transfer coding (Section 4.1) is the final encoding, the message
2926 * body length is determined by reading and decoding the chunked
2927 * data until the transfer coding indicates the data is complete.
2928 *
2929 * If a Transfer-Encoding header field is present in a response and
2930 * the chunked transfer coding is not the final encoding, the
2931 * message body length is determined by reading the connection until
2932 * it is closed by the server. If a Transfer-Encoding header field
2933 * is present in a request and the chunked transfer coding is not
2934 * the final encoding, the message body length cannot be determined
2935 * reliably; the server MUST respond with the 400 (Bad Request)
2936 * status code and then close the connection.
2937 *
2938 * If a message is received with both a Transfer-Encoding and a
2939 * Content-Length header field, the Transfer-Encoding overrides the
2940 * Content-Length. Such a message might indicate an attempt to
2941 * perform request smuggling (Section 9.5) or response splitting
2942 * (Section 9.4) and ought to be handled as an error. A sender MUST
2943 * remove the received Content-Length field prior to forwarding such
2944 * a message downstream.
2945 *
2946 * 4. If a message is received without Transfer-Encoding and with
2947 * either multiple Content-Length header fields having differing
2948 * field-values or a single Content-Length header field having an
2949 * invalid value, then the message framing is invalid and the
2950 * recipient MUST treat it as an unrecoverable error. If this is a
2951 * request message, the server MUST respond with a 400 (Bad Request)
2952 * status code and then close the connection. If this is a response
2953 * message received by a proxy, the proxy MUST close the connection
2954 * to the server, discard the received response, and send a 502 (Bad
2955 * Gateway) response to the client. If this is a response message
2956 * received by a user agent, the user agent MUST close the
2957 * connection to the server and discard the received response.
2958 *
2959 * 5. If a valid Content-Length header field is present without
2960 * Transfer-Encoding, its decimal value defines the expected message
2961 * body length in octets. If the sender closes the connection or
2962 * the recipient times out before the indicated number of octets are
2963 * received, the recipient MUST consider the message to be
2964 * incomplete and close the connection.
2965 *
2966 * 6. If this is a request message and none of the above are true, then
2967 * the message body length is zero (no message body is present).
2968 *
2969 * 7. Otherwise, this is a response message without a declared message
2970 * body length, so the message body length is determined by the
2971 * number of octets received prior to the server closing the
2972 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002973 */
2974
Willy Tarreau32b47f42009-10-18 20:55:02 +02002975 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002976 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaue77bc172015-05-01 10:06:30 +02002977 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002978 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002979 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2980 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau49efa262015-05-01 10:09:49 +02002981 /* chunked not last, return badreq */
2982 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002983 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002984 }
2985
Willy Tarreaudfa3d922015-04-30 10:57:51 +02002986 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002987 ctx.idx = 0;
Willy Tarreaudfa3d922015-04-30 10:57:51 +02002988 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2989 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
2990 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2991 }
Willy Tarreau49efa262015-05-01 10:09:49 +02002992 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002993 signed long long cl;
2994
Willy Tarreauad14f752011-09-02 20:33:27 +02002995 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002996 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002997 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002998 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002999
Willy Tarreauad14f752011-09-02 20:33:27 +02003000 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003001 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003002 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003003 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003004
Willy Tarreauad14f752011-09-02 20:33:27 +02003005 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003006 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003007 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003008 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003009
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003010 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003011 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003012 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003013 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003014
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003015 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003016 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003017 }
3018
Willy Tarreau49efa262015-05-01 10:09:49 +02003019 /* even bodyless requests have a known length */
3020 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003021
Willy Tarreau179085c2014-04-28 16:48:56 +02003022 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3023 * only change if both the request and the config reference something else.
3024 * Option httpclose by itself sets tunnel mode where headers are mangled.
3025 * However, if another mode is set, it will affect it (eg: server-close/
3026 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3027 * if FE and BE have the same settings (common). The method consists in
3028 * checking if options changed between the two calls (implying that either
3029 * one is non-null, or one of them is non-null and we are there for the first
3030 * time.
3031 */
3032 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau2e47a3a2014-09-30 18:44:22 +02003033 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
3034 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003035
Willy Tarreaud787e662009-07-07 10:14:51 +02003036 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003037 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003038 req->analyse_exp = TICK_ETERNITY;
3039 return 1;
3040
3041 return_bad_req:
3042 /* We centralize bad requests processing here */
3043 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3044 /* we detected a parsing error. We want to archive this request
3045 * in the dedicated proxy area for later troubleshooting.
3046 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003047 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003048 }
3049
3050 txn->req.msg_state = HTTP_MSG_ERROR;
3051 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003052 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003053
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003054 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003055 if (s->listener->counters)
3056 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003057
3058 return_prx_cond:
3059 if (!(s->flags & SN_ERR_MASK))
3060 s->flags |= SN_ERR_PRXCOND;
3061 if (!(s->flags & SN_FINST_MASK))
3062 s->flags |= SN_FINST_R;
3063
3064 req->analysers = 0;
3065 req->analyse_exp = TICK_ETERNITY;
3066 return 0;
3067}
3068
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003069
Willy Tarreau347a35d2013-11-22 17:51:09 +01003070/* This function prepares an applet to handle the stats. It can deal with the
3071 * "100-continue" expectation, check that admin rules are met for POST requests,
3072 * and program a response message if something was unexpected. It cannot fail
3073 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003074 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003075 * s->target which is supposed to already point to the stats applet. The caller
3076 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003077 */
3078int http_handle_stats(struct session *s, struct channel *req)
3079{
3080 struct stats_admin_rule *stats_admin_rule;
3081 struct stream_interface *si = s->rep->prod;
3082 struct http_txn *txn = &s->txn;
3083 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003084 struct uri_auth *uri_auth = s->be->uri_auth;
3085 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003086 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003087
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003088 appctx = si_appctx(si);
3089 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3090 appctx->st1 = appctx->st2 = 0;
3091 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3092 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003093 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3094 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003095
3096 uri = msg->chn->buf->p + msg->sl.rq.u;
3097 lookup = uri + uri_auth->uri_len;
3098
3099 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3100 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003101 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003102 break;
3103 }
3104 }
3105
3106 if (uri_auth->refresh) {
3107 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3108 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003109 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003110 break;
3111 }
3112 }
3113 }
3114
3115 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3116 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003117 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003118 break;
3119 }
3120 }
3121
3122 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3123 if (memcmp(h, ";st=", 4) == 0) {
3124 int i;
3125 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003126 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003127 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3128 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003129 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003130 break;
3131 }
3132 }
3133 break;
3134 }
3135 }
3136
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003137 appctx->ctx.stats.scope_str = 0;
3138 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003139 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3140 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3141 int itx = 0;
3142 const char *h2;
3143 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3144 const char *err;
3145
3146 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3147 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003148 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003149 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3150 itx++;
3151 h++;
3152 }
3153
3154 if (itx > STAT_SCOPE_TXT_MAXLEN)
3155 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003156 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003157
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003158 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003159 memcpy(scope_txt, h2, itx);
3160 scope_txt[itx] = '\0';
3161 err = invalid_char(scope_txt);
3162 if (err) {
3163 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003164 appctx->ctx.stats.scope_str = 0;
3165 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003166 }
3167 break;
3168 }
3169 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003170
3171 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003172 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003173 int ret = 1;
3174
3175 if (stats_admin_rule->cond) {
3176 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3177 ret = acl_pass(ret);
3178 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3179 ret = !ret;
3180 }
3181
3182 if (ret) {
3183 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003184 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003185 break;
3186 }
3187 }
3188
3189 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003190 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003191 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003192 /* we'll need the request body, possibly after sending 100-continue */
3193 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003194 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003195 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003196 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003197 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3198 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003199 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003200 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003201 else {
3202 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003203 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003204 }
3205
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003206 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003207 return 1;
3208}
3209
Lukas Tribus67db8df2013-06-23 17:37:13 +02003210/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3211 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3212 */
3213static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3214{
3215#ifdef IP_TOS
3216 if (from.ss_family == AF_INET)
3217 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3218#endif
3219#ifdef IPV6_TCLASS
3220 if (from.ss_family == AF_INET6) {
3221 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3222 /* v4-mapped addresses need IP_TOS */
3223 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3224 else
3225 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3226 }
3227#endif
3228}
3229
Sasha Pachev218f0642014-06-16 12:05:59 -06003230static int http_transform_header(struct session* s, struct http_msg *msg, const char* name, uint name_len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003231 char* buf, struct hdr_idx* idx, struct list *fmt, struct my_regex *re,
Sasha Pachev218f0642014-06-16 12:05:59 -06003232 struct hdr_ctx* ctx, int action)
3233{
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003234 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3235 struct hdr_idx *idx, struct hdr_ctx *ctx);
3236 struct chunk *replace = get_trash_chunk();
3237 struct chunk *output = get_trash_chunk();
3238
3239 replace->len = build_logline(s, replace->str, replace->size, fmt);
3240 if (replace->len >= replace->size - 1)
3241 return -1;
3242
Sasha Pachev218f0642014-06-16 12:05:59 -06003243 ctx->idx = 0;
3244
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003245 /* Choose the header browsing function. */
3246 switch (action) {
3247 case HTTP_REQ_ACT_REPLACE_VAL:
3248 case HTTP_RES_ACT_REPLACE_VAL:
3249 http_find_hdr_func = http_find_header2;
3250 break;
3251 case HTTP_REQ_ACT_REPLACE_HDR:
3252 case HTTP_RES_ACT_REPLACE_HDR:
3253 http_find_hdr_func = http_find_full_header2;
3254 break;
3255 default: /* impossible */
3256 return -1;
3257 }
3258
3259 while (http_find_hdr_func(name, name_len, buf, idx, ctx)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06003260 struct hdr_idx_elem *hdr = idx->v + ctx->idx;
3261 int delta;
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003262 char *val = ctx->line + ctx->val;
3263 char* val_end = val + ctx->vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003264
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003265 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch))
3266 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003267
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003268 output->len = exp_replace(output->str, output->size, val, replace->str, pmatch);
3269 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003270 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003271
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003272 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003273
3274 hdr->len += delta;
3275 http_msg_move_end(msg, delta);
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003276
3277 /* Adjust the length of the current value of the index. */
3278 ctx->vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003279 }
3280
3281 return 0;
3282}
3283
Willy Tarreau20b0de52012-12-24 15:45:22 +01003284/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003285 * transaction <txn>. Returns the verdict of the first rule that prevents
3286 * further processing of the request (auth, deny, ...), and defaults to
3287 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3288 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3289 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003290 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003291enum rule_result
Willy Tarreau96257ec2012-12-27 10:46:37 +01003292http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003293{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003294 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003295 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003296 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003297 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003298
Willy Tarreauff011f22011-01-06 17:51:27 +01003299 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003300 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003301 continue;
3302
Willy Tarreau96257ec2012-12-27 10:46:37 +01003303 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003304 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003305 int ret;
3306
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003307 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003308 ret = acl_pass(ret);
3309
Willy Tarreauff011f22011-01-06 17:51:27 +01003310 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003311 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003312
3313 if (!ret) /* condition not matched */
3314 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003315 }
3316
Willy Tarreau20b0de52012-12-24 15:45:22 +01003317
Willy Tarreau96257ec2012-12-27 10:46:37 +01003318 switch (rule->action) {
3319 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003320 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003321
3322 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003323 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003324
Willy Tarreauccbcc372012-12-27 12:37:57 +01003325 case HTTP_REQ_ACT_TARPIT:
3326 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003327 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003328
Willy Tarreau96257ec2012-12-27 10:46:37 +01003329 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003330 /* Auth might be performed on regular http-req rules as well as on stats */
3331 auth_realm = rule->arg.auth.realm;
3332 if (!auth_realm) {
3333 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3334 auth_realm = STATS_DEFAULT_REALM;
3335 else
3336 auth_realm = px->id;
3337 }
3338 /* send 401/407 depending on whether we use a proxy or not. We still
3339 * count one error, because normal browsing won't significantly
3340 * increase the counter but brute force attempts will.
3341 */
3342 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3343 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3344 stream_int_retnclose(&s->si[0], &trash);
3345 session_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003346 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003347
Willy Tarreau81499eb2012-12-27 12:19:02 +01003348 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003349 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3350 return HTTP_RULE_RES_BADREQ;
3351 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003352
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003353 case HTTP_REQ_ACT_SET_NICE:
3354 s->task->nice = rule->arg.nice;
3355 break;
3356
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003357 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003358 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003359 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003360 break;
3361
Willy Tarreau51347ed2013-06-11 19:34:13 +02003362 case HTTP_REQ_ACT_SET_MARK:
3363#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003364 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003365 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003366#endif
3367 break;
3368
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003369 case HTTP_REQ_ACT_SET_LOGL:
3370 s->logs.level = rule->arg.loglevel;
3371 break;
3372
Sasha Pachev218f0642014-06-16 12:05:59 -06003373 case HTTP_REQ_ACT_REPLACE_HDR:
3374 case HTTP_REQ_ACT_REPLACE_VAL:
3375 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3376 txn->req.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003377 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003378 return HTTP_RULE_RES_BADREQ;
3379 break;
3380
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003381 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003382 ctx.idx = 0;
3383 /* remove all occurrences of the header */
3384 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3385 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3386 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003387 }
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003388 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003389
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003390 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003391 case HTTP_REQ_ACT_ADD_HDR:
3392 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3393 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3394 trash.len = rule->arg.hdr_add.name_len;
3395 trash.str[trash.len++] = ':';
3396 trash.str[trash.len++] = ' ';
3397 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 +01003398
3399 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3400 /* remove all occurrences of the header */
3401 ctx.idx = 0;
3402 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3403 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3404 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3405 }
3406 }
3407
Willy Tarreau96257ec2012-12-27 10:46:37 +01003408 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3409 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003410
3411 case HTTP_REQ_ACT_DEL_ACL:
3412 case HTTP_REQ_ACT_DEL_MAP: {
3413 struct pat_ref *ref;
3414 char *key;
3415 int len;
3416
3417 /* collect reference */
3418 ref = pat_ref_lookup(rule->arg.map.ref);
3419 if (!ref)
3420 continue;
3421
3422 /* collect key */
3423 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3424 key = trash.str;
3425 key[len] = '\0';
3426
3427 /* perform update */
3428 /* returned code: 1=ok, 0=ko */
3429 pat_ref_delete(ref, key);
3430
3431 break;
3432 }
3433
3434 case HTTP_REQ_ACT_ADD_ACL: {
3435 struct pat_ref *ref;
3436 char *key;
3437 struct chunk *trash_key;
3438 int len;
3439
3440 trash_key = get_trash_chunk();
3441
3442 /* collect reference */
3443 ref = pat_ref_lookup(rule->arg.map.ref);
3444 if (!ref)
3445 continue;
3446
3447 /* collect key */
3448 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3449 key = trash_key->str;
3450 key[len] = '\0';
3451
3452 /* perform update */
3453 /* add entry only if it does not already exist */
3454 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003455 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003456
3457 break;
3458 }
3459
3460 case HTTP_REQ_ACT_SET_MAP: {
3461 struct pat_ref *ref;
3462 char *key, *value;
3463 struct chunk *trash_key, *trash_value;
3464 int len;
3465
3466 trash_key = get_trash_chunk();
3467 trash_value = get_trash_chunk();
3468
3469 /* collect reference */
3470 ref = pat_ref_lookup(rule->arg.map.ref);
3471 if (!ref)
3472 continue;
3473
3474 /* collect key */
3475 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3476 key = trash_key->str;
3477 key[len] = '\0';
3478
3479 /* collect value */
3480 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3481 value = trash_value->str;
3482 value[len] = '\0';
3483
3484 /* perform update */
3485 if (pat_ref_find_elt(ref, key) != NULL)
3486 /* update entry if it exists */
3487 pat_ref_set(ref, key, value, NULL);
3488 else
3489 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003490 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003491
3492 break;
3493 }
William Lallemand73025dd2014-04-24 14:38:37 +02003494
3495 case HTTP_REQ_ACT_CUSTOM_CONT:
3496 rule->action_ptr(rule, px, s, txn);
3497 break;
3498
3499 case HTTP_REQ_ACT_CUSTOM_STOP:
3500 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003501 return HTTP_RULE_RES_DONE;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003502 }
3503 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003504
3505 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003506 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003507}
3508
Willy Tarreau71241ab2012-12-27 11:30:54 +01003509
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003510/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3511 * transaction <txn>. Returns the first rule that prevents further processing
3512 * of the response (deny, ...) or NULL if it executed all rules or stopped
3513 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3514 * rule.
3515 */
3516static struct http_res_rule *
3517http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3518{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003519 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003520 struct http_res_rule *rule;
3521 struct hdr_ctx ctx;
3522
3523 list_for_each_entry(rule, rules, list) {
3524 if (rule->action >= HTTP_RES_ACT_MAX)
3525 continue;
3526
3527 /* check optional condition */
3528 if (rule->cond) {
3529 int ret;
3530
3531 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3532 ret = acl_pass(ret);
3533
3534 if (rule->cond->pol == ACL_COND_UNLESS)
3535 ret = !ret;
3536
3537 if (!ret) /* condition not matched */
3538 continue;
3539 }
3540
3541
3542 switch (rule->action) {
3543 case HTTP_RES_ACT_ALLOW:
3544 return NULL; /* "allow" rules are OK */
3545
3546 case HTTP_RES_ACT_DENY:
3547 txn->flags |= TX_SVDENY;
3548 return rule;
3549
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003550 case HTTP_RES_ACT_SET_NICE:
3551 s->task->nice = rule->arg.nice;
3552 break;
3553
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003554 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003555 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003556 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003557 break;
3558
Willy Tarreau51347ed2013-06-11 19:34:13 +02003559 case HTTP_RES_ACT_SET_MARK:
3560#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003561 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003562 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003563#endif
3564 break;
3565
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003566 case HTTP_RES_ACT_SET_LOGL:
3567 s->logs.level = rule->arg.loglevel;
3568 break;
3569
Sasha Pachev218f0642014-06-16 12:05:59 -06003570 case HTTP_RES_ACT_REPLACE_HDR:
3571 case HTTP_RES_ACT_REPLACE_VAL:
3572 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3573 txn->rsp.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003574 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003575 return NULL; /* note: we should report an error here */
3576 break;
3577
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003578 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003579 ctx.idx = 0;
3580 /* remove all occurrences of the header */
3581 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3582 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3583 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3584 }
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003585 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003586
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003587 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003588 case HTTP_RES_ACT_ADD_HDR:
3589 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3590 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3591 trash.len = rule->arg.hdr_add.name_len;
3592 trash.str[trash.len++] = ':';
3593 trash.str[trash.len++] = ' ';
3594 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 +01003595
3596 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3597 /* remove all occurrences of the header */
3598 ctx.idx = 0;
3599 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3600 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3601 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3602 }
3603 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003604 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3605 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003606
3607 case HTTP_RES_ACT_DEL_ACL:
3608 case HTTP_RES_ACT_DEL_MAP: {
3609 struct pat_ref *ref;
3610 char *key;
3611 int len;
3612
3613 /* collect reference */
3614 ref = pat_ref_lookup(rule->arg.map.ref);
3615 if (!ref)
3616 continue;
3617
3618 /* collect key */
3619 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3620 key = trash.str;
3621 key[len] = '\0';
3622
3623 /* perform update */
3624 /* returned code: 1=ok, 0=ko */
3625 pat_ref_delete(ref, key);
3626
3627 break;
3628 }
3629
3630 case HTTP_RES_ACT_ADD_ACL: {
3631 struct pat_ref *ref;
3632 char *key;
3633 struct chunk *trash_key;
3634 int len;
3635
3636 trash_key = get_trash_chunk();
3637
3638 /* collect reference */
3639 ref = pat_ref_lookup(rule->arg.map.ref);
3640 if (!ref)
3641 continue;
3642
3643 /* collect key */
3644 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3645 key = trash_key->str;
3646 key[len] = '\0';
3647
3648 /* perform update */
3649 /* check if the entry already exists */
3650 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003651 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003652
3653 break;
3654 }
3655
3656 case HTTP_RES_ACT_SET_MAP: {
3657 struct pat_ref *ref;
3658 char *key, *value;
3659 struct chunk *trash_key, *trash_value;
3660 int len;
3661
3662 trash_key = get_trash_chunk();
3663 trash_value = get_trash_chunk();
3664
3665 /* collect reference */
3666 ref = pat_ref_lookup(rule->arg.map.ref);
3667 if (!ref)
3668 continue;
3669
3670 /* collect key */
3671 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3672 key = trash_key->str;
3673 key[len] = '\0';
3674
3675 /* collect value */
3676 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3677 value = trash_value->str;
3678 value[len] = '\0';
3679
3680 /* perform update */
3681 if (pat_ref_find_elt(ref, key) != NULL)
3682 /* update entry if it exists */
3683 pat_ref_set(ref, key, value, NULL);
3684 else
3685 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003686 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003687
3688 break;
3689 }
William Lallemand73025dd2014-04-24 14:38:37 +02003690
3691 case HTTP_RES_ACT_CUSTOM_CONT:
3692 rule->action_ptr(rule, px, s, txn);
3693 break;
3694
3695 case HTTP_RES_ACT_CUSTOM_STOP:
3696 rule->action_ptr(rule, px, s, txn);
3697 return rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003698 }
3699 }
3700
3701 /* we reached the end of the rules, nothing to report */
3702 return NULL;
3703}
3704
3705
Willy Tarreau71241ab2012-12-27 11:30:54 +01003706/* Perform an HTTP redirect based on the information in <rule>. The function
3707 * returns non-zero on success, or zero in case of a, irrecoverable error such
3708 * as too large a request to build a valid response.
3709 */
3710static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3711{
3712 struct http_msg *msg = &txn->req;
3713 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003714 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003715
3716 /* build redirect message */
3717 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003718 case 308:
3719 msg_fmt = HTTP_308;
3720 break;
3721 case 307:
3722 msg_fmt = HTTP_307;
3723 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003724 case 303:
3725 msg_fmt = HTTP_303;
3726 break;
3727 case 301:
3728 msg_fmt = HTTP_301;
3729 break;
3730 case 302:
3731 default:
3732 msg_fmt = HTTP_302;
3733 break;
3734 }
3735
3736 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3737 return 0;
3738
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003739 location = trash.str + trash.len;
3740
Willy Tarreau71241ab2012-12-27 11:30:54 +01003741 switch(rule->type) {
3742 case REDIRECT_TYPE_SCHEME: {
3743 const char *path;
3744 const char *host;
3745 struct hdr_ctx ctx;
3746 int pathlen;
3747 int hostlen;
3748
3749 host = "";
3750 hostlen = 0;
3751 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003752 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003753 host = ctx.line + ctx.val;
3754 hostlen = ctx.vlen;
3755 }
3756
3757 path = http_get_path(txn);
3758 /* build message using path */
3759 if (path) {
3760 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3761 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3762 int qs = 0;
3763 while (qs < pathlen) {
3764 if (path[qs] == '?') {
3765 pathlen = qs;
3766 break;
3767 }
3768 qs++;
3769 }
3770 }
3771 } else {
3772 path = "/";
3773 pathlen = 1;
3774 }
3775
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003776 if (rule->rdr_str) { /* this is an old "redirect" rule */
3777 /* check if we can add scheme + "://" + host + path */
3778 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3779 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003780
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003781 /* add scheme */
3782 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3783 trash.len += rule->rdr_len;
3784 }
3785 else {
3786 /* add scheme with executing log format */
3787 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003788
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003789 /* check if we can add scheme + "://" + host + path */
3790 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3791 return 0;
3792 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003793 /* add "://" */
3794 memcpy(trash.str + trash.len, "://", 3);
3795 trash.len += 3;
3796
3797 /* add host */
3798 memcpy(trash.str + trash.len, host, hostlen);
3799 trash.len += hostlen;
3800
3801 /* add path */
3802 memcpy(trash.str + trash.len, path, pathlen);
3803 trash.len += pathlen;
3804
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003805 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003806 if (trash.len && trash.str[trash.len - 1] != '/' &&
3807 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3808 if (trash.len > trash.size - 5)
3809 return 0;
3810 trash.str[trash.len] = '/';
3811 trash.len++;
3812 }
3813
3814 break;
3815 }
3816 case REDIRECT_TYPE_PREFIX: {
3817 const char *path;
3818 int pathlen;
3819
3820 path = http_get_path(txn);
3821 /* build message using path */
3822 if (path) {
3823 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3824 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3825 int qs = 0;
3826 while (qs < pathlen) {
3827 if (path[qs] == '?') {
3828 pathlen = qs;
3829 break;
3830 }
3831 qs++;
3832 }
3833 }
3834 } else {
3835 path = "/";
3836 pathlen = 1;
3837 }
3838
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003839 if (rule->rdr_str) { /* this is an old "redirect" rule */
3840 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3841 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003842
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003843 /* add prefix. Note that if prefix == "/", we don't want to
3844 * add anything, otherwise it makes it hard for the user to
3845 * configure a self-redirection.
3846 */
3847 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3848 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3849 trash.len += rule->rdr_len;
3850 }
3851 }
3852 else {
3853 /* add prefix with executing log format */
3854 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3855
3856 /* Check length */
3857 if (trash.len + pathlen > trash.size - 4)
3858 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003859 }
3860
3861 /* add path */
3862 memcpy(trash.str + trash.len, path, pathlen);
3863 trash.len += pathlen;
3864
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003865 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003866 if (trash.len && trash.str[trash.len - 1] != '/' &&
3867 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3868 if (trash.len > trash.size - 5)
3869 return 0;
3870 trash.str[trash.len] = '/';
3871 trash.len++;
3872 }
3873
3874 break;
3875 }
3876 case REDIRECT_TYPE_LOCATION:
3877 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003878 if (rule->rdr_str) { /* this is an old "redirect" rule */
3879 if (trash.len + rule->rdr_len > trash.size - 4)
3880 return 0;
3881
3882 /* add location */
3883 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3884 trash.len += rule->rdr_len;
3885 }
3886 else {
3887 /* add location with executing log format */
3888 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003889
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003890 /* Check left length */
3891 if (trash.len > trash.size - 4)
3892 return 0;
3893 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003894 break;
3895 }
3896
3897 if (rule->cookie_len) {
3898 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3899 trash.len += 14;
3900 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3901 trash.len += rule->cookie_len;
3902 memcpy(trash.str + trash.len, "\r\n", 2);
3903 trash.len += 2;
3904 }
3905
3906 /* add end of headers and the keep-alive/close status.
3907 * We may choose to set keep-alive if the Location begins
3908 * with a slash, because the client will come back to the
3909 * same server.
3910 */
3911 txn->status = rule->code;
3912 /* let's log the request time */
3913 s->logs.tv_request = now;
3914
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003915 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003916 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3917 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3918 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3919 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3920 /* keep-alive possible */
3921 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3922 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3923 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3924 trash.len += 30;
3925 } else {
3926 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3927 trash.len += 24;
3928 }
3929 }
3930 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3931 trash.len += 4;
3932 bo_inject(txn->rsp.chn, trash.str, trash.len);
3933 /* "eat" the request */
3934 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02003935 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003936 msg->sov = 0;
3937 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3938 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3939 txn->req.msg_state = HTTP_MSG_CLOSED;
3940 txn->rsp.msg_state = HTTP_MSG_DONE;
3941 } else {
3942 /* keep-alive not possible */
3943 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3944 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3945 trash.len += 29;
3946 } else {
3947 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3948 trash.len += 23;
3949 }
3950 stream_int_retnclose(txn->req.chn->prod, &trash);
3951 txn->req.chn->analysers = 0;
3952 }
3953
3954 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003955 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003956 if (!(s->flags & SN_FINST_MASK))
3957 s->flags |= SN_FINST_R;
3958
3959 return 1;
3960}
3961
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003962/* This stream analyser runs all HTTP request processing which is common to
3963 * frontends and backends, which means blocking ACLs, filters, connection-close,
3964 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003965 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003966 * either needs more data or wants to immediately abort the request (eg: deny,
3967 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003968 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003969int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003970{
Willy Tarreaud787e662009-07-07 10:14:51 +02003971 struct http_txn *txn = &s->txn;
3972 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003973 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003974 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003975 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02003976
Willy Tarreau655dce92009-11-08 13:10:58 +01003977 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003978 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003979 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003980 return 0;
3981 }
3982
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003983 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 +02003984 now_ms, __FUNCTION__,
3985 s,
3986 req,
3987 req->rex, req->wex,
3988 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003989 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003990 req->analysers);
3991
Willy Tarreau65410832014-04-28 21:25:43 +02003992 /* just in case we have some per-backend tracking */
3993 session_inc_be_http_req_ctr(s);
3994
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003995 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003996 if (!LIST_ISEMPTY(&px->http_req_rules)) {
3997 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003998
Willy Tarreau0b748332014-04-29 00:13:29 +02003999 switch (verdict) {
4000 case HTTP_RULE_RES_CONT:
4001 case HTTP_RULE_RES_STOP: /* nothing to do */
4002 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004003
Willy Tarreau0b748332014-04-29 00:13:29 +02004004 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4005 if (txn->flags & TX_CLTARPIT)
4006 goto tarpit;
4007 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004008
Willy Tarreau0b748332014-04-29 00:13:29 +02004009 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4010 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004011
Willy Tarreau0b748332014-04-29 00:13:29 +02004012 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004013 goto done;
4014
Willy Tarreau0b748332014-04-29 00:13:29 +02004015 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4016 goto return_bad_req;
4017 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004018 }
4019
Willy Tarreau52542592014-04-28 18:33:26 +02004020 /* OK at this stage, we know that the request was accepted according to
4021 * the http-request rules, we can check for the stats. Note that the
4022 * URI is detected *before* the req* rules in order not to be affected
4023 * by a possible reqrep, while they are processed *after* so that a
4024 * reqdeny can still block them. This clearly needs to change in 1.6!
4025 */
4026 if (stats_check_uri(s->rep->prod, txn, px)) {
4027 s->target = &http_stats_applet.obj_type;
4028 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
4029 txn->status = 500;
4030 s->logs.tv_request = now;
4031 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004032
Willy Tarreau52542592014-04-28 18:33:26 +02004033 if (!(s->flags & SN_ERR_MASK))
4034 s->flags |= SN_ERR_RESOURCE;
4035 goto return_prx_cond;
4036 }
4037
4038 /* parse the whole stats request and extract the relevant information */
4039 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004040 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4041 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004042
Willy Tarreau0b748332014-04-29 00:13:29 +02004043 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4044 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004045
Willy Tarreau0b748332014-04-29 00:13:29 +02004046 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4047 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004048 }
4049
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004050 /* evaluate the req* rules except reqadd */
4051 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004052 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004053 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004054
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004055 if (txn->flags & TX_CLDENY)
4056 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004057
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004058 if (txn->flags & TX_CLTARPIT)
4059 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004060 }
Willy Tarreau06619262006-12-17 08:37:22 +01004061
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004062 /* add request headers from the rule sets in the same order */
4063 list_for_each_entry(wl, &px->req_add, list) {
4064 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004065 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004066 ret = acl_pass(ret);
4067 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4068 ret = !ret;
4069 if (!ret)
4070 continue;
4071 }
4072
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004073 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004074 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004075 }
4076
Willy Tarreau52542592014-04-28 18:33:26 +02004077
4078 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004079 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004080 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004081 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
4082 s->fe->fe_counters.intercepted_req++;
4083
4084 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
4085 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
4086 if (!(s->flags & SN_FINST_MASK))
4087 s->flags |= SN_FINST_R;
4088
Willy Tarreau70730dd2014-04-24 18:06:27 +02004089 /* we may want to compress the stats page */
4090 if (s->fe->comp || s->be->comp)
4091 select_compression_request_header(s, req->buf);
4092
4093 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreaude8ca962014-11-20 22:23:10 +01004094 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004095 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004096 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004097
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004098 /* check whether we have some ACLs set to redirect this request */
4099 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004100 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004101 int ret;
4102
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004103 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004104 ret = acl_pass(ret);
4105 if (rule->cond->pol == ACL_COND_UNLESS)
4106 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004107 if (!ret)
4108 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004109 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004110 if (!http_apply_redirect_rule(rule, s, txn))
4111 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004112 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004113 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004114
Willy Tarreau2be39392010-01-03 17:24:51 +01004115 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4116 * If this happens, then the data will not come immediately, so we must
4117 * send all what we have without waiting. Note that due to the small gain
4118 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004119 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004120 * itself once used.
4121 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004122 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004123
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004124 done: /* done with this analyser, continue with next ones that the calling
4125 * points will have set, if any.
4126 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004127 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004128 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4129 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004130 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004131
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004132 tarpit:
4133 /* When a connection is tarpitted, we use the tarpit timeout,
4134 * which may be the same as the connect timeout if unspecified.
4135 * If unset, then set it to zero because we really want it to
4136 * eventually expire. We build the tarpit as an analyser.
4137 */
4138 channel_erase(s->req);
4139
4140 /* wipe the request out so that we can drop the connection early
4141 * if the client closes first.
4142 */
4143 channel_dont_connect(req);
4144 req->analysers = 0; /* remove switching rules etc... */
4145 req->analysers |= AN_REQ_HTTP_TARPIT;
4146 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4147 if (!req->analyse_exp)
4148 req->analyse_exp = tick_add(now_ms, 0);
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++;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004155 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004156
4157 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004158 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004159 txn->status = 403;
4160 s->logs.tv_request = now;
4161 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4162 session_inc_http_err_ctr(s);
4163 s->fe->fe_counters.denied_req++;
4164 if (s->fe != s->be)
4165 s->be->be_counters.denied_req++;
4166 if (s->listener->counters)
4167 s->listener->counters->denied_req++;
4168 goto return_prx_cond;
4169
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004170 return_bad_req:
4171 /* We centralize bad requests processing here */
4172 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4173 /* we detected a parsing error. We want to archive this request
4174 * in the dedicated proxy area for later troubleshooting.
4175 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004176 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004177 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004178
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004179 txn->req.msg_state = HTTP_MSG_ERROR;
4180 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004181 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004182
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004183 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004184 if (s->listener->counters)
4185 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004186
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004187 return_prx_cond:
4188 if (!(s->flags & SN_ERR_MASK))
4189 s->flags |= SN_ERR_PRXCOND;
4190 if (!(s->flags & SN_FINST_MASK))
4191 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004192
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004193 req->analysers = 0;
4194 req->analyse_exp = TICK_ETERNITY;
4195 return 0;
4196}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004197
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004198/* This function performs all the processing enabled for the current request.
4199 * It returns 1 if the processing can continue on next analysers, or zero if it
4200 * needs more data, encounters an error, or wants to immediately abort the
4201 * request. It relies on buffers flags, and updates s->req->analysers.
4202 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004203int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004204{
4205 struct http_txn *txn = &s->txn;
4206 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004207 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004208
Willy Tarreau655dce92009-11-08 13:10:58 +01004209 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004210 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004211 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004212 return 0;
4213 }
4214
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004215 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 +02004216 now_ms, __FUNCTION__,
4217 s,
4218 req,
4219 req->rex, req->wex,
4220 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004221 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004222 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004223
William Lallemand82fe75c2012-10-23 10:25:10 +02004224 if (s->fe->comp || s->be->comp)
4225 select_compression_request_header(s, req->buf);
4226
Willy Tarreau59234e92008-11-30 23:51:27 +01004227 /*
4228 * Right now, we know that we have processed the entire headers
4229 * and that unwanted requests have been filtered out. We can do
4230 * whatever we want with the remaining request. Also, now we
4231 * may have separate values for ->fe, ->be.
4232 */
Willy Tarreau06619262006-12-17 08:37:22 +01004233
Willy Tarreau59234e92008-11-30 23:51:27 +01004234 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004235 * If HTTP PROXY is set we simply get remote server address parsing
4236 * incoming request. Note that this requires that a connection is
4237 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004238 */
4239 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004240 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004241 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004242
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004243 /* Note that for now we don't reuse existing proxy connections */
4244 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004245 txn->req.msg_state = HTTP_MSG_ERROR;
4246 txn->status = 500;
4247 req->analysers = 0;
4248 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4249
4250 if (!(s->flags & SN_ERR_MASK))
4251 s->flags |= SN_ERR_RESOURCE;
4252 if (!(s->flags & SN_FINST_MASK))
4253 s->flags |= SN_FINST_R;
4254
4255 return 0;
4256 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004257
4258 path = http_get_path(txn);
4259 url2sa(req->buf->p + msg->sl.rq.u,
4260 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004261 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004262 /* if the path was found, we have to remove everything between
4263 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4264 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4265 * u_l characters by a single "/".
4266 */
4267 if (path) {
4268 char *cur_ptr = req->buf->p;
4269 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4270 int delta;
4271
4272 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4273 http_msg_move_end(&txn->req, delta);
4274 cur_end += delta;
4275 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4276 goto return_bad_req;
4277 }
4278 else {
4279 char *cur_ptr = req->buf->p;
4280 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4281 int delta;
4282
4283 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4284 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4285 http_msg_move_end(&txn->req, delta);
4286 cur_end += delta;
4287 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4288 goto return_bad_req;
4289 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004290 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004291
Willy Tarreau59234e92008-11-30 23:51:27 +01004292 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004293 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004294 * Note that doing so might move headers in the request, but
4295 * the fields will stay coherent and the URI will not move.
4296 * This should only be performed in the backend.
4297 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004298 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004299 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4300 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004301
Willy Tarreau59234e92008-11-30 23:51:27 +01004302 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004303 * 8: the appsession cookie was looked up very early in 1.2,
4304 * so let's do the same now.
4305 */
4306
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004307 /* It needs to look into the URI unless persistence must be ignored */
4308 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004309 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 +01004310 }
4311
William Lallemanda73203e2012-03-12 12:48:57 +01004312 /* add unique-id if "header-unique-id" is specified */
4313
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004314 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4315 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4316 goto return_bad_req;
4317 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004318 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004319 }
William Lallemanda73203e2012-03-12 12:48:57 +01004320
4321 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004322 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4323 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004324 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004325 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004326 goto return_bad_req;
4327 }
4328
Cyril Bontéb21570a2009-11-29 20:04:48 +01004329 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004330 * 9: add X-Forwarded-For if either the frontend or the backend
4331 * asks for it.
4332 */
4333 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004334 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004335 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004336 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4337 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004338 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004339 /* The header is set to be added only if none is present
4340 * and we found it, so don't do anything.
4341 */
4342 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004343 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004344 /* Add an X-Forwarded-For header unless the source IP is
4345 * in the 'except' network range.
4346 */
4347 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004348 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004349 != s->fe->except_net.s_addr) &&
4350 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004351 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004352 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004353 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004354 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004355 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004356
4357 /* Note: we rely on the backend to get the header name to be used for
4358 * x-forwarded-for, because the header is really meant for the backends.
4359 * However, if the backend did not specify any option, we have to rely
4360 * on the frontend's header name.
4361 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004362 if (s->be->fwdfor_hdr_len) {
4363 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004364 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004365 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004366 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004367 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004368 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004369 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 +01004370
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004371 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004372 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004373 }
4374 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004375 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004376 /* FIXME: for the sake of completeness, we should also support
4377 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004378 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004379 int len;
4380 char pn[INET6_ADDRSTRLEN];
4381 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004382 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004383 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004384
Willy Tarreau59234e92008-11-30 23:51:27 +01004385 /* Note: we rely on the backend to get the header name to be used for
4386 * x-forwarded-for, because the header is really meant for the backends.
4387 * However, if the backend did not specify any option, we have to rely
4388 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004389 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004390 if (s->be->fwdfor_hdr_len) {
4391 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004392 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004393 } else {
4394 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004395 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004396 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004397 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004398
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004399 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004400 goto return_bad_req;
4401 }
4402 }
4403
4404 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004405 * 10: add X-Original-To if either the frontend or the backend
4406 * asks for it.
4407 */
4408 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4409
4410 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004411 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004412 /* Add an X-Original-To header unless the destination IP is
4413 * in the 'except' network range.
4414 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004415 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004416
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004417 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004418 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004419 (((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 +02004420 != s->fe->except_to.s_addr) &&
4421 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004422 (((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 +02004423 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004424 int len;
4425 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004426 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004427
4428 /* Note: we rely on the backend to get the header name to be used for
4429 * x-original-to, because the header is really meant for the backends.
4430 * However, if the backend did not specify any option, we have to rely
4431 * on the frontend's header name.
4432 */
4433 if (s->be->orgto_hdr_len) {
4434 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004435 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004436 } else {
4437 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004438 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004439 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004440 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 +02004441
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004442 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004443 goto return_bad_req;
4444 }
4445 }
4446 }
4447
Willy Tarreau50fc7772012-11-11 22:19:57 +01004448 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4449 * If an "Upgrade" token is found, the header is left untouched in order not to have
4450 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4451 * "Upgrade" is present in the Connection header.
4452 */
4453 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4454 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004455 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4456 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004457 unsigned int want_flags = 0;
4458
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004459 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004460 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004461 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4462 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004463 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004464 want_flags |= TX_CON_CLO_SET;
4465 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004466 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004467 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4468 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004469 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004470 want_flags |= TX_CON_KAL_SET;
4471 }
4472
4473 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004474 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004475 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004476
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004477
Willy Tarreau522d6c02009-12-06 18:49:18 +01004478 /* If we have no server assigned yet and we're balancing on url_param
4479 * with a POST request, we may be interested in checking the body for
4480 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004481 */
4482 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4483 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004484 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004485 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004486 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004487 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004488
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004489 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004490 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004491#ifdef TCP_QUICKACK
4492 /* We expect some data from the client. Unless we know for sure
4493 * we already have a full request, we have to re-enable quick-ack
4494 * in case we previously disabled it, otherwise we might cause
4495 * the client to delay further data.
4496 */
4497 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004498 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004499 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004500 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004501 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004502#endif
4503 }
Willy Tarreau03945942009-12-22 16:50:27 +01004504
Willy Tarreau59234e92008-11-30 23:51:27 +01004505 /*************************************************************
4506 * OK, that's finished for the headers. We have done what we *
4507 * could. Let's switch to the DATA state. *
4508 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004509 req->analyse_exp = TICK_ETERNITY;
4510 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004511
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004512 /* if the server closes the connection, we want to immediately react
4513 * and close the socket to save packets and syscalls.
4514 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004515 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4516 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004517
Willy Tarreau59234e92008-11-30 23:51:27 +01004518 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004519 /* OK let's go on with the BODY now */
4520 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004521
Willy Tarreau59234e92008-11-30 23:51:27 +01004522 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004523 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004524 /* we detected a parsing error. We want to archive this request
4525 * in the dedicated proxy area for later troubleshooting.
4526 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004527 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004528 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004529
Willy Tarreau59234e92008-11-30 23:51:27 +01004530 txn->req.msg_state = HTTP_MSG_ERROR;
4531 txn->status = 400;
4532 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004533 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004534
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004535 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004536 if (s->listener->counters)
4537 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004538
Willy Tarreau59234e92008-11-30 23:51:27 +01004539 if (!(s->flags & SN_ERR_MASK))
4540 s->flags |= SN_ERR_PRXCOND;
4541 if (!(s->flags & SN_FINST_MASK))
4542 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004543 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004544}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004545
Willy Tarreau60b85b02008-11-30 23:28:40 +01004546/* This function is an analyser which processes the HTTP tarpit. It always
4547 * returns zero, at the beginning because it prevents any other processing
4548 * from occurring, and at the end because it terminates the request.
4549 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004550int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004551{
4552 struct http_txn *txn = &s->txn;
4553
4554 /* This connection is being tarpitted. The CLIENT side has
4555 * already set the connect expiration date to the right
4556 * timeout. We just have to check that the client is still
4557 * there and that the timeout has not expired.
4558 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004559 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004560 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004561 !tick_is_expired(req->analyse_exp, now_ms))
4562 return 0;
4563
4564 /* We will set the queue timer to the time spent, just for
4565 * logging purposes. We fake a 500 server error, so that the
4566 * attacker will not suspect his connection has been tarpitted.
4567 * It will not cause trouble to the logs because we can exclude
4568 * the tarpitted connections by filtering on the 'PT' status flags.
4569 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004570 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4571
4572 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004573 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004574 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004575
4576 req->analysers = 0;
4577 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004578
Willy Tarreau60b85b02008-11-30 23:28:40 +01004579 if (!(s->flags & SN_ERR_MASK))
4580 s->flags |= SN_ERR_PRXCOND;
4581 if (!(s->flags & SN_FINST_MASK))
4582 s->flags |= SN_FINST_T;
4583 return 0;
4584}
4585
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004586/* This function is an analyser which waits for the HTTP request body. It waits
4587 * for either the buffer to be full, or the full advertised contents to have
4588 * reached the buffer. It must only be called after the standard HTTP request
4589 * processing has occurred, because it expects the request to be parsed and will
4590 * look for the Expect header. It may send a 100-Continue interim response. It
4591 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4592 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4593 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004594 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004595int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004596{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004597 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004598 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004599
4600 /* We have to parse the HTTP request body to find any required data.
4601 * "balance url_param check_post" should have been the only way to get
4602 * into this. We were brought here after HTTP header analysis, so all
4603 * related structures are ready.
4604 */
4605
Willy Tarreau890988f2014-04-10 11:59:33 +02004606 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4607 /* This is the first call */
4608 if (msg->msg_state < HTTP_MSG_BODY)
4609 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004610
Willy Tarreau890988f2014-04-10 11:59:33 +02004611 if (msg->msg_state < HTTP_MSG_100_SENT) {
4612 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4613 * send an HTTP/1.1 100 Continue intermediate response.
4614 */
4615 if (msg->flags & HTTP_MSGF_VER_11) {
4616 struct hdr_ctx ctx;
4617 ctx.idx = 0;
4618 /* Expect is allowed in 1.1, look for it */
4619 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4620 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4621 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4622 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004623 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004624 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004625 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004626
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004627 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004628 * req->buf->p still points to the beginning of the message. We
4629 * must save the body in msg->next because it survives buffer
4630 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004631 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004632 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004633
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004634 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004635 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4636 else
4637 msg->msg_state = HTTP_MSG_DATA;
4638 }
4639
Willy Tarreau890988f2014-04-10 11:59:33 +02004640 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4641 /* We're in content-length mode, we just have to wait for enough data. */
4642 if (req->buf->i - msg->sov < msg->body_len)
4643 goto missing_data;
4644
4645 /* OK we have everything we need now */
4646 goto http_end;
4647 }
4648
4649 /* OK here we're parsing a chunked-encoded message */
4650
Willy Tarreau522d6c02009-12-06 18:49:18 +01004651 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004652 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004653 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004654 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004655 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004656 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004657
Willy Tarreau115acb92009-12-26 13:56:06 +01004658 if (!ret)
4659 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004660 else if (ret < 0) {
4661 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004662 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004663 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004664 }
4665
Willy Tarreaud98cf932009-12-27 22:54:55 +01004666 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004667 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004668 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004669 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004670 if (msg->msg_state == HTTP_MSG_TRAILERS)
4671 goto http_end;
4672
Willy Tarreau226071e2014-04-10 11:55:45 +02004673 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004674 goto http_end;
4675
4676 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004677 /* we get here if we need to wait for more data. If the buffer is full,
4678 * we have the maximum we can expect.
4679 */
4680 if (buffer_full(req->buf, global.tune.maxrewrite))
4681 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004682
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004683 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004684 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004685 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004686
4687 if (!(s->flags & SN_ERR_MASK))
4688 s->flags |= SN_ERR_CLITO;
4689 if (!(s->flags & SN_FINST_MASK))
4690 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004691 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004692 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004693
4694 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004695 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004696 /* Not enough data. We'll re-use the http-request
4697 * timeout here. Ideally, we should set the timeout
4698 * relative to the accept() date. We just set the
4699 * request timeout once at the beginning of the
4700 * request.
4701 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004702 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004703 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004704 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004705 return 0;
4706 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004707
4708 http_end:
4709 /* The situation will not evolve, so let's give up on the analysis. */
4710 s->logs.tv_request = now; /* update the request timer to reflect full request */
4711 req->analysers &= ~an_bit;
4712 req->analyse_exp = TICK_ETERNITY;
4713 return 1;
4714
4715 return_bad_req: /* let's centralize all bad requests */
4716 txn->req.msg_state = HTTP_MSG_ERROR;
4717 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004718 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004719
Willy Tarreau79ebac62010-06-07 13:47:49 +02004720 if (!(s->flags & SN_ERR_MASK))
4721 s->flags |= SN_ERR_PRXCOND;
4722 if (!(s->flags & SN_FINST_MASK))
4723 s->flags |= SN_FINST_R;
4724
Willy Tarreau522d6c02009-12-06 18:49:18 +01004725 return_err_msg:
4726 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004727 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004728 if (s->listener->counters)
4729 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004730 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004731}
4732
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004733/* send a server's name with an outgoing request over an established connection.
4734 * Note: this function is designed to be called once the request has been scheduled
4735 * for being forwarded. This is the reason why it rewinds the buffer before
4736 * proceeding.
4737 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004738int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004739
4740 struct hdr_ctx ctx;
4741
Mark Lamourinec2247f02012-01-04 13:02:01 -05004742 char *hdr_name = be->server_id_hdr_name;
4743 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004744 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004745 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004746 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004747
William Lallemandd9e90662012-01-30 17:27:17 +01004748 ctx.idx = 0;
4749
Willy Tarreau211cdec2014-04-17 20:18:08 +02004750 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004751 if (old_o) {
4752 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004753 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004754 txn->req.next += old_o;
4755 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004756 }
4757
Willy Tarreau9b28e032012-10-12 23:49:43 +02004758 old_i = chn->buf->i;
4759 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 -05004760 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004761 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004762 }
4763
4764 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004765 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004766 memcpy(hdr_val, hdr_name, hdr_name_len);
4767 hdr_val += hdr_name_len;
4768 *hdr_val++ = ':';
4769 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004770 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4771 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004772
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004773 if (old_o) {
4774 /* If this was a forwarded request, we must readjust the amount of
4775 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004776 * variations. Note that the current state is >= HTTP_MSG_BODY,
4777 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004778 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004779 old_o += chn->buf->i - old_i;
4780 b_adv(chn->buf, old_o);
4781 txn->req.next -= old_o;
4782 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004783 }
4784
Mark Lamourinec2247f02012-01-04 13:02:01 -05004785 return 0;
4786}
4787
Willy Tarreau610ecce2010-01-04 21:15:02 +01004788/* Terminate current transaction and prepare a new one. This is very tricky
4789 * right now but it works.
4790 */
4791void http_end_txn_clean_session(struct session *s)
4792{
Willy Tarreau068621e2013-12-23 15:11:25 +01004793 int prev_status = s->txn.status;
4794
Willy Tarreau610ecce2010-01-04 21:15:02 +01004795 /* FIXME: We need a more portable way of releasing a backend's and a
4796 * server's connections. We need a safer way to reinitialize buffer
4797 * flags. We also need a more accurate method for computing per-request
4798 * data.
4799 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004800
Willy Tarreau4213a112013-12-15 10:25:42 +01004801 /* unless we're doing keep-alive, we want to quickly close the connection
4802 * to the server.
4803 */
4804 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4805 !si_conn_ready(s->req->cons)) {
4806 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4807 si_shutr(s->req->cons);
4808 si_shutw(s->req->cons);
4809 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004810
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004811 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004812 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004813 if (unlikely(s->srv_conn))
4814 sess_change_server(s, NULL);
4815 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004816
4817 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4818 session_process_counters(s);
4819
4820 if (s->txn.status) {
4821 int n;
4822
4823 n = s->txn.status / 100;
4824 if (n < 1 || n > 5)
4825 n = 0;
4826
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004827 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004828 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004829 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004830 s->fe->fe_counters.p.http.comp_rsp++;
4831 }
Willy Tarreau24657792010-02-26 10:30:28 +01004832 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004833 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004834 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004835 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004836 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004837 s->be->be_counters.p.http.comp_rsp++;
4838 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004839 }
4840
4841 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004842 s->logs.bytes_in -= s->req->buf->i;
4843 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004844
4845 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004846 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004847 !(s->flags & SN_MONITOR) &&
4848 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4849 s->do_log(s);
4850 }
4851
Willy Tarreauc177ea72014-06-25 15:36:04 +02004852 /* stop tracking content-based counters */
4853 session_stop_content_counters(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004854 session_update_time_stats(s);
4855
Willy Tarreau610ecce2010-01-04 21:15:02 +01004856 s->logs.accept_date = date; /* user-visible date for logging */
4857 s->logs.tv_accept = now; /* corrected date for internal use */
4858 tv_zero(&s->logs.tv_request);
4859 s->logs.t_queue = -1;
4860 s->logs.t_connect = -1;
4861 s->logs.t_data = -1;
4862 s->logs.t_close = 0;
4863 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4864 s->logs.srv_queue_size = 0; /* we will get this number soon */
4865
Willy Tarreau9b28e032012-10-12 23:49:43 +02004866 s->logs.bytes_in = s->req->total = s->req->buf->i;
4867 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004868
4869 if (s->pend_pos)
4870 pendconn_free(s->pend_pos);
4871
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004872 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004873 if (s->flags & SN_CURR_SESS) {
4874 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004875 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004876 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004877 if (may_dequeue_tasks(objt_server(s->target), s->be))
4878 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004879 }
4880
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004881 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004882
Willy Tarreau4213a112013-12-15 10:25:42 +01004883 /* only release our endpoint if we don't intend to reuse the
4884 * connection.
4885 */
4886 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4887 !si_conn_ready(s->req->cons)) {
4888 si_release_endpoint(s->req->cons);
4889 }
4890
Willy Tarreau610ecce2010-01-04 21:15:02 +01004891 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004892 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004893 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004894 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004895 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreaub4d05092014-09-01 20:35:55 +02004896 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);
4897 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 +02004898 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 +01004899 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Cyril Bonté17f77072014-10-22 22:30:13 +02004900 s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004901
Willy Tarreau610ecce2010-01-04 21:15:02 +01004902 s->txn.meth = 0;
4903 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004904 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004905
4906 if (prev_status == 401 || prev_status == 407) {
4907 /* In HTTP keep-alive mode, if we receive a 401, we still have
4908 * a chance of being able to send the visitor again to the same
4909 * server over the same connection. This is required by some
4910 * broken protocols such as NTLM, and anyway whenever there is
4911 * an opportunity for sending the challenge to the proper place,
4912 * it's better to do it (at least it helps with debugging).
4913 */
4914 s->txn.flags |= TX_PREFER_LAST;
4915 }
4916
Willy Tarreauee55dc02010-06-01 10:56:34 +02004917 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004918 s->req->cons->flags |= SI_FL_INDEP_STR;
4919
Willy Tarreau96e31212011-05-30 18:10:30 +02004920 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004921 s->req->flags |= CF_NEVER_WAIT;
4922 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004923 }
4924
Willy Tarreau610ecce2010-01-04 21:15:02 +01004925 /* if the request buffer is not empty, it means we're
4926 * about to process another request, so send pending
4927 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004928 * Just don't do this if the buffer is close to be full,
4929 * because the request will wait for it to flush a little
4930 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004931 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004932 if (s->req->buf->i) {
4933 if (s->rep->buf->o &&
4934 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4935 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004936 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004937 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004938
4939 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004940 channel_auto_read(s->req);
4941 channel_auto_close(s->req);
4942 channel_auto_read(s->rep);
4943 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004944
Willy Tarreau27375622013-12-17 00:00:28 +01004945 /* we're in keep-alive with an idle connection, monitor it */
4946 si_idle_conn(s->req->cons);
4947
Willy Tarreau342b11c2010-11-24 16:22:09 +01004948 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004949 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004950}
4951
4952
4953/* This function updates the request state machine according to the response
4954 * state machine and buffer flags. It returns 1 if it changes anything (flag
4955 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4956 * it is only used to find when a request/response couple is complete. Both
4957 * this function and its equivalent should loop until both return zero. It
4958 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4959 */
4960int http_sync_req_state(struct session *s)
4961{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004962 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004963 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004964 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004965 unsigned int old_state = txn->req.msg_state;
4966
Willy Tarreau610ecce2010-01-04 21:15:02 +01004967 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4968 return 0;
4969
4970 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004971 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004972 * We can shut the read side unless we want to abort_on_close,
4973 * or we have a POST request. The issue with POST requests is
4974 * that some browsers still send a CRLF after the request, and
4975 * this CRLF must be read so that it does not remain in the kernel
4976 * buffers, otherwise a close could cause an RST on some systems
4977 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004978 * Note that if we're using keep-alive on the client side, we'd
4979 * rather poll now and keep the polling enabled for the whole
4980 * session's life than enabling/disabling it between each
4981 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004982 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004983 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4984 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4985 !(s->be->options & PR_O_ABRT_CLOSE) &&
4986 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004987 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004988
Willy Tarreau40f151a2012-12-20 12:10:09 +01004989 /* if the server closes the connection, we want to immediately react
4990 * and close the socket to save packets and syscalls.
4991 */
4992 chn->cons->flags |= SI_FL_NOHALF;
4993
Willy Tarreau610ecce2010-01-04 21:15:02 +01004994 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4995 goto wait_other_side;
4996
4997 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4998 /* The server has not finished to respond, so we
4999 * don't want to move in order not to upset it.
5000 */
5001 goto wait_other_side;
5002 }
5003
5004 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5005 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005006 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005007 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005008 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005009 goto wait_other_side;
5010 }
5011
5012 /* When we get here, it means that both the request and the
5013 * response have finished receiving. Depending on the connection
5014 * mode, we'll have to wait for the last bytes to leave in either
5015 * direction, and sometimes for a close to be effective.
5016 */
5017
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005018 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5019 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005020 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5021 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005022 }
5023 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5024 /* Option forceclose is set, or either side wants to close,
5025 * let's enforce it now that we're not expecting any new
5026 * data to come. The caller knows the session is complete
5027 * once both states are CLOSED.
5028 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005029 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5030 channel_shutr_now(chn);
5031 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005032 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005033 }
5034 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005035 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5036 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005037 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005038 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5039 channel_auto_read(chn);
5040 txn->req.msg_state = HTTP_MSG_TUNNEL;
5041 chn->flags |= CF_NEVER_WAIT;
5042 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005043 }
5044
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005045 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005046 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005047 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005048
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005049 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005050 txn->req.msg_state = HTTP_MSG_CLOSING;
5051 goto http_msg_closing;
5052 }
5053 else {
5054 txn->req.msg_state = HTTP_MSG_CLOSED;
5055 goto http_msg_closed;
5056 }
5057 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005058 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005059 }
5060
5061 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5062 http_msg_closing:
5063 /* nothing else to forward, just waiting for the output buffer
5064 * to be empty and for the shutw_now to take effect.
5065 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005066 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005067 txn->req.msg_state = HTTP_MSG_CLOSED;
5068 goto http_msg_closed;
5069 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005070 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005071 txn->req.msg_state = HTTP_MSG_ERROR;
5072 goto wait_other_side;
5073 }
5074 }
5075
5076 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5077 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005078 /* see above in MSG_DONE why we only do this in these states */
5079 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5080 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5081 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005082 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005083 goto wait_other_side;
5084 }
5085
5086 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005087 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005088}
5089
5090
5091/* This function updates the response state machine according to the request
5092 * state machine and buffer flags. It returns 1 if it changes anything (flag
5093 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5094 * it is only used to find when a request/response couple is complete. Both
5095 * this function and its equivalent should loop until both return zero. It
5096 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5097 */
5098int http_sync_res_state(struct session *s)
5099{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005100 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005101 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005102 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005103 unsigned int old_state = txn->rsp.msg_state;
5104
Willy Tarreau610ecce2010-01-04 21:15:02 +01005105 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5106 return 0;
5107
5108 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5109 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005110 * still monitor the server connection for a possible close
5111 * while the request is being uploaded, so we don't disable
5112 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005113 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005114 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005115
5116 if (txn->req.msg_state == HTTP_MSG_ERROR)
5117 goto wait_other_side;
5118
5119 if (txn->req.msg_state < HTTP_MSG_DONE) {
5120 /* The client seems to still be sending data, probably
5121 * because we got an error response during an upload.
5122 * We have the choice of either breaking the connection
5123 * or letting it pass through. Let's do the later.
5124 */
5125 goto wait_other_side;
5126 }
5127
5128 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5129 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005130 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005131 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005132 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005133 goto wait_other_side;
5134 }
5135
5136 /* When we get here, it means that both the request and the
5137 * response have finished receiving. Depending on the connection
5138 * mode, we'll have to wait for the last bytes to leave in either
5139 * direction, and sometimes for a close to be effective.
5140 */
5141
5142 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5143 /* Server-close mode : shut read and wait for the request
5144 * side to close its output buffer. The caller will detect
5145 * when we're in DONE and the other is in CLOSED and will
5146 * catch that for the final cleanup.
5147 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005148 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5149 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005150 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005151 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5152 /* Option forceclose is set, or either side wants to close,
5153 * let's enforce it now that we're not expecting any new
5154 * data to come. The caller knows the session is complete
5155 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005156 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005157 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5158 channel_shutr_now(chn);
5159 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005160 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005161 }
5162 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005163 /* The last possible modes are keep-alive and tunnel. Tunnel will
5164 * need to forward remaining data. Keep-alive will need to monitor
5165 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005166 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005167 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005168 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005169 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5170 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005171 }
5172
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005173 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005174 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005175 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005176 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5177 goto http_msg_closing;
5178 }
5179 else {
5180 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5181 goto http_msg_closed;
5182 }
5183 }
5184 goto wait_other_side;
5185 }
5186
5187 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5188 http_msg_closing:
5189 /* nothing else to forward, just waiting for the output buffer
5190 * to be empty and for the shutw_now to take effect.
5191 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005192 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005193 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5194 goto http_msg_closed;
5195 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005196 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005197 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005198 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005199 if (objt_server(s->target))
5200 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005201 goto wait_other_side;
5202 }
5203 }
5204
5205 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5206 http_msg_closed:
5207 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005208 bi_erase(chn);
5209 channel_auto_close(chn);
5210 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005211 goto wait_other_side;
5212 }
5213
5214 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005215 /* We force the response to leave immediately if we're waiting for the
5216 * other side, since there is no pending shutdown to push it out.
5217 */
5218 if (!channel_is_empty(chn))
5219 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005220 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005221}
5222
5223
5224/* Resync the request and response state machines. Return 1 if either state
5225 * changes.
5226 */
5227int http_resync_states(struct session *s)
5228{
5229 struct http_txn *txn = &s->txn;
5230 int old_req_state = txn->req.msg_state;
5231 int old_res_state = txn->rsp.msg_state;
5232
Willy Tarreau610ecce2010-01-04 21:15:02 +01005233 http_sync_req_state(s);
5234 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005235 if (!http_sync_res_state(s))
5236 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005237 if (!http_sync_req_state(s))
5238 break;
5239 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005240
Willy Tarreau610ecce2010-01-04 21:15:02 +01005241 /* OK, both state machines agree on a compatible state.
5242 * There are a few cases we're interested in :
5243 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5244 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5245 * directions, so let's simply disable both analysers.
5246 * - HTTP_MSG_CLOSED on the response only means we must abort the
5247 * request.
5248 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5249 * with server-close mode means we've completed one request and we
5250 * must re-initialize the server connection.
5251 */
5252
5253 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5254 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5255 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5256 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5257 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005258 channel_auto_close(s->req);
5259 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005260 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005261 channel_auto_close(s->rep);
5262 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005263 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005264 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5265 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005266 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005267 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005268 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005269 channel_auto_close(s->rep);
5270 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01005271 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005272 channel_abort(s->req);
5273 channel_auto_close(s->req);
5274 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005275 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005276 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005277 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5278 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005279 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005280 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5281 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5282 /* server-close/keep-alive: terminate this transaction,
5283 * possibly killing the server connection and reinitialize
5284 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005285 */
5286 http_end_txn_clean_session(s);
5287 }
5288
Willy Tarreau610ecce2010-01-04 21:15:02 +01005289 return txn->req.msg_state != old_req_state ||
5290 txn->rsp.msg_state != old_res_state;
5291}
5292
Willy Tarreaud98cf932009-12-27 22:54:55 +01005293/* This function is an analyser which forwards request body (including chunk
5294 * sizes if any). It is called as soon as we must forward, even if we forward
5295 * zero byte. The only situation where it must not be called is when we're in
5296 * tunnel mode and we want to forward till the close. It's used both to forward
5297 * remaining data and to resync after end of body. It expects the msg_state to
5298 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5299 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005300 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005301 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005302 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005303int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005304{
5305 struct http_txn *txn = &s->txn;
5306 struct http_msg *msg = &s->txn.req;
5307
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005308 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5309 return 0;
5310
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005311 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005312 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005313 /* Output closed while we were sending data. We must abort and
5314 * wake the other side up.
5315 */
5316 msg->msg_state = HTTP_MSG_ERROR;
5317 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005318 return 1;
5319 }
5320
Willy Tarreaud98cf932009-12-27 22:54:55 +01005321 /* Note that we don't have to send 100-continue back because we don't
5322 * need the data to complete our job, and it's up to the server to
5323 * decide whether to return 100, 417 or anything else in return of
5324 * an "Expect: 100-continue" header.
5325 */
5326
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005327 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005328 /* we have msg->sov which points to the first byte of message
5329 * body, and req->buf.p still points to the beginning of the
5330 * message. We forward the headers now, as we don't need them
5331 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005332 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005333 b_adv(req->buf, msg->sov);
5334 msg->next -= msg->sov;
5335 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005336
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005337 /* The previous analysers guarantee that the state is somewhere
5338 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5339 * msg->next are always correct.
5340 */
5341 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5342 if (msg->flags & HTTP_MSGF_TE_CHNK)
5343 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5344 else
5345 msg->msg_state = HTTP_MSG_DATA;
5346 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005347 }
5348
Willy Tarreau7ba23542014-04-17 21:50:00 +02005349 /* Some post-connect processing might want us to refrain from starting to
5350 * forward data. Currently, the only reason for this is "balance url_param"
5351 * whichs need to parse/process the request after we've enabled forwarding.
5352 */
5353 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5354 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5355 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005356 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005357 goto missing_data;
5358 }
5359 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5360 }
5361
Willy Tarreau80a92c02014-03-12 10:41:13 +01005362 /* in most states, we should abort in case of early close */
5363 channel_auto_close(req);
5364
Willy Tarreauefdf0942014-04-24 20:08:57 +02005365 if (req->to_forward) {
5366 /* We can't process the buffer's contents yet */
5367 req->flags |= CF_WAKE_WRITE;
5368 goto missing_data;
5369 }
5370
Willy Tarreaud98cf932009-12-27 22:54:55 +01005371 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005372 if (msg->msg_state == HTTP_MSG_DATA) {
5373 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005374 /* we may have some pending data starting at req->buf->p */
5375 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005376 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005377 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005378 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005379 msg->next += msg->chunk_len;
5380 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005381
5382 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005383 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005384 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005385 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005386 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005387 }
5388 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005389 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005390 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005391 * TRAILERS state.
5392 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005393 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005394
Willy Tarreau54d23df2012-10-25 19:04:45 +02005395 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005396 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005397 else if (ret < 0) {
5398 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005399 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005400 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005401 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005402 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005403 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005404 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005405 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005406 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005407 int ret = http_skip_chunk_crlf(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 Tarreau54d23df2012-10-25 19:04:45 +02005414 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, 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 MSG_CHUNK_SIZE now */
5418 }
5419 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005420 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005421
5422 if (ret == 0)
5423 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005424 else if (ret < 0) {
5425 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005426 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005427 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005428 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005429 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005430 /* we're in HTTP_MSG_DONE now */
5431 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005432 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005433 int old_state = msg->msg_state;
5434
Willy Tarreau610ecce2010-01-04 21:15:02 +01005435 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005436
5437 /* we may have some pending data starting at req->buf->p
5438 * such as last chunk of data or trailers.
5439 */
5440 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005441 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005442 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005443 msg->next = 0;
5444
Willy Tarreau4fe41902010-06-07 22:27:41 +02005445 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005446 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5447 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005448 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005449 if (http_resync_states(s)) {
5450 /* some state changes occurred, maybe the analyser
5451 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005452 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005453 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005454 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005455 /* request errors are most likely due to
5456 * the server aborting the transfer.
5457 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005458 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005459 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005460 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005461 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005462 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005463 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005464 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005465 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005466
5467 /* If "option abortonclose" is set on the backend, we
5468 * want to monitor the client's connection and forward
5469 * any shutdown notification to the server, which will
5470 * decide whether to close or to go on processing the
5471 * request.
5472 */
5473 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005474 channel_auto_read(req);
5475 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005476 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005477 else if (s->txn.meth == HTTP_METH_POST) {
5478 /* POST requests may require to read extra CRLF
5479 * sent by broken browsers and which could cause
5480 * an RST to be sent upon close on some systems
5481 * (eg: Linux).
5482 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005483 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005484 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005485
Willy Tarreau610ecce2010-01-04 21:15:02 +01005486 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005487 }
5488 }
5489
Willy Tarreaud98cf932009-12-27 22:54:55 +01005490 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005491 /* we may have some pending data starting at req->buf->p */
5492 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005493 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005494 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5495
Willy Tarreaubed410e2014-04-22 08:19:34 +02005496 msg->next = 0;
5497 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5498
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005499 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005500 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005501 if (!(s->flags & SN_ERR_MASK))
5502 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005503 if (!(s->flags & SN_FINST_MASK)) {
5504 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5505 s->flags |= SN_FINST_H;
5506 else
5507 s->flags |= SN_FINST_D;
5508 }
5509
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005510 s->fe->fe_counters.cli_aborts++;
5511 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005512 if (objt_server(s->target))
5513 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005514
5515 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005516 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005517
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005518 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005519 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005520 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005521
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005522 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005523 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005524 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005525 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005526 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005527
Willy Tarreau5c620922011-05-11 19:56:11 +02005528 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005529 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005530 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005531 * modes are already handled by the stream sock layer. We must not do
5532 * this in content-length mode because it could present the MSG_MORE
5533 * flag with the last block of forwarded data, which would cause an
5534 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005535 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005536 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005537 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005538
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005539 return 0;
5540
Willy Tarreaud98cf932009-12-27 22:54:55 +01005541 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005542 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005543 if (s->listener->counters)
5544 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005545
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005546 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005547 /* we may have some pending data starting at req->buf->p */
5548 b_adv(req->buf, msg->next);
5549 msg->next = 0;
5550
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005551 txn->req.msg_state = HTTP_MSG_ERROR;
5552 if (txn->status) {
5553 /* Note: we don't send any error if some data were already sent */
5554 stream_int_retnclose(req->prod, NULL);
5555 } else {
5556 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005557 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005558 }
5559 req->analysers = 0;
5560 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005561
5562 if (!(s->flags & SN_ERR_MASK))
5563 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005564 if (!(s->flags & SN_FINST_MASK)) {
5565 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5566 s->flags |= SN_FINST_H;
5567 else
5568 s->flags |= SN_FINST_D;
5569 }
5570 return 0;
5571
5572 aborted_xfer:
5573 txn->req.msg_state = HTTP_MSG_ERROR;
5574 if (txn->status) {
5575 /* Note: we don't send any error if some data were already sent */
5576 stream_int_retnclose(req->prod, NULL);
5577 } else {
5578 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005579 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005580 }
5581 req->analysers = 0;
5582 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5583
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005584 s->fe->fe_counters.srv_aborts++;
5585 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005586 if (objt_server(s->target))
5587 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005588
5589 if (!(s->flags & SN_ERR_MASK))
5590 s->flags |= SN_ERR_SRVCL;
5591 if (!(s->flags & SN_FINST_MASK)) {
5592 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5593 s->flags |= SN_FINST_H;
5594 else
5595 s->flags |= SN_FINST_D;
5596 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005597 return 0;
5598}
5599
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005600/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5601 * processing can continue on next analysers, or zero if it either needs more
5602 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5603 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5604 * when it has nothing left to do, and may remove any analyser when it wants to
5605 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005606 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005607int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005608{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005609 struct http_txn *txn = &s->txn;
5610 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005611 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005612 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005613 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005614 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005615
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005616 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 +02005617 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005618 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005619 rep,
5620 rep->rex, rep->wex,
5621 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005622 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005623 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005624
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005625 /*
5626 * Now parse the partial (or complete) lines.
5627 * We will check the response syntax, and also join multi-line
5628 * headers. An index of all the lines will be elaborated while
5629 * parsing.
5630 *
5631 * For the parsing, we use a 28 states FSM.
5632 *
5633 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005634 * rep->buf->p = beginning of response
5635 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5636 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005637 * msg->eol = end of current header or line (LF or CRLF)
5638 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005639 */
5640
Willy Tarreau628c40c2014-04-24 19:11:26 +02005641 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005642 /* There's a protected area at the end of the buffer for rewriting
5643 * purposes. We don't want to start to parse the request if the
5644 * protected area is affected, because we may have to move processed
5645 * data later, which is much more complicated.
5646 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005647 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005648 if (unlikely(!channel_reserved(rep))) {
5649 /* some data has still not left the buffer, wake us once that's done */
5650 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5651 goto abort_response;
5652 channel_dont_close(rep);
5653 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005654 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005655 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005656 }
5657
Willy Tarreau379357a2013-06-08 12:55:46 +02005658 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5659 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5660 buffer_slow_realign(rep->buf);
5661
Willy Tarreau9b28e032012-10-12 23:49:43 +02005662 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005663 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005664 }
5665
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005666 /* 1: we might have to print this header in debug mode */
5667 if (unlikely((global.mode & MODE_DEBUG) &&
5668 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau8767b132014-10-21 19:36:09 +02005669 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005670 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005671
Willy Tarreau9b28e032012-10-12 23:49:43 +02005672 sol = rep->buf->p;
5673 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005674 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005675
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005676 sol += hdr_idx_first_pos(&txn->hdr_idx);
5677 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005678
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005679 while (cur_idx) {
5680 eol = sol + txn->hdr_idx.v[cur_idx].len;
5681 debug_hdr("srvhdr", s, sol, eol);
5682 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5683 cur_idx = txn->hdr_idx.v[cur_idx].next;
5684 }
5685 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005686
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005687 /*
5688 * Now we quickly check if we have found a full valid response.
5689 * If not so, we check the FD and buffer states before leaving.
5690 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005691 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005692 * responses are checked first.
5693 *
5694 * Depending on whether the client is still there or not, we
5695 * may send an error response back or not. Note that normally
5696 * we should only check for HTTP status there, and check I/O
5697 * errors somewhere else.
5698 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005699
Willy Tarreau655dce92009-11-08 13:10:58 +01005700 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005701 /* Invalid response */
5702 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5703 /* we detected a parsing error. We want to archive this response
5704 * in the dedicated proxy area for later troubleshooting.
5705 */
5706 hdr_response_bad:
5707 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005708 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005709
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005710 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005711 if (objt_server(s->target)) {
5712 objt_server(s->target)->counters.failed_resp++;
5713 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005714 }
Willy Tarreau64648412010-03-05 10:41:54 +01005715 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005716 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005717 rep->analysers = 0;
5718 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005719 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005720 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005721 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005722
5723 if (!(s->flags & SN_ERR_MASK))
5724 s->flags |= SN_ERR_PRXCOND;
5725 if (!(s->flags & SN_FINST_MASK))
5726 s->flags |= SN_FINST_H;
5727
5728 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005729 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005730
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005731 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005732 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005733 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005734 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005735 goto hdr_response_bad;
5736 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005737
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005738 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005739 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005740 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005741 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005742 else if (txn->flags & TX_NOT_FIRST)
5743 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005744
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005745 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005746 if (objt_server(s->target)) {
5747 objt_server(s->target)->counters.failed_resp++;
5748 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005749 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005750
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005751 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005752 rep->analysers = 0;
5753 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005754 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005755 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005756 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005757
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005758 if (!(s->flags & SN_ERR_MASK))
5759 s->flags |= SN_ERR_SRVCL;
5760 if (!(s->flags & SN_FINST_MASK))
5761 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005762 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005763 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005764
Willy Tarreau2a4f5112014-06-23 15:22:31 +02005765 /* read timeout : return a 504 to the client. */
5766 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005767 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005768 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005769 else if (txn->flags & TX_NOT_FIRST)
5770 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005771
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005772 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005773 if (objt_server(s->target)) {
5774 objt_server(s->target)->counters.failed_resp++;
5775 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005776 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005777
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005778 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005779 rep->analysers = 0;
5780 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005781 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005782 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005783 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005784
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005785 if (!(s->flags & SN_ERR_MASK))
5786 s->flags |= SN_ERR_SRVTO;
5787 if (!(s->flags & SN_FINST_MASK))
5788 s->flags |= SN_FINST_H;
5789 return 0;
5790 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005791
Willy Tarreauf003d372012-11-26 13:35:37 +01005792 /* client abort with an abortonclose */
5793 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5794 s->fe->fe_counters.cli_aborts++;
5795 s->be->be_counters.cli_aborts++;
5796 if (objt_server(s->target))
5797 objt_server(s->target)->counters.cli_aborts++;
5798
5799 rep->analysers = 0;
5800 channel_auto_close(rep);
5801
5802 txn->status = 400;
5803 bi_erase(rep);
5804 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5805
5806 if (!(s->flags & SN_ERR_MASK))
5807 s->flags |= SN_ERR_CLICL;
5808 if (!(s->flags & SN_FINST_MASK))
5809 s->flags |= SN_FINST_H;
5810
5811 /* process_session() will take care of the error */
5812 return 0;
5813 }
5814
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005815 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005816 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005817 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005818 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005819 else if (txn->flags & TX_NOT_FIRST)
5820 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005821
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005822 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005823 if (objt_server(s->target)) {
5824 objt_server(s->target)->counters.failed_resp++;
5825 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005826 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005827
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005828 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005829 rep->analysers = 0;
5830 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005831 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005832 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005833 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005834
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005835 if (!(s->flags & SN_ERR_MASK))
5836 s->flags |= SN_ERR_SRVCL;
5837 if (!(s->flags & SN_FINST_MASK))
5838 s->flags |= SN_FINST_H;
5839 return 0;
5840 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005841
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005842 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005843 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005844 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005845 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005846 else if (txn->flags & TX_NOT_FIRST)
5847 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005848
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005849 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005850 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005851 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005852
5853 if (!(s->flags & SN_ERR_MASK))
5854 s->flags |= SN_ERR_CLICL;
5855 if (!(s->flags & SN_FINST_MASK))
5856 s->flags |= SN_FINST_H;
5857
5858 /* process_session() will take care of the error */
5859 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005860 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005861
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005862 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005863 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005864 return 0;
5865 }
5866
5867 /* More interesting part now : we know that we have a complete
5868 * response which at least looks like HTTP. We have an indicator
5869 * of each header's length, so we can parse them quickly.
5870 */
5871
5872 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005873 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005874
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005875 /*
5876 * 1: get the status code
5877 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005878 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005879 if (n < 1 || n > 5)
5880 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005881 /* when the client triggers a 4xx from the server, it's most often due
5882 * to a missing object or permission. These events should be tracked
5883 * because if they happen often, it may indicate a brute force or a
5884 * vulnerability scan.
5885 */
5886 if (n == 4)
5887 session_inc_http_err_ctr(s);
5888
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005889 if (objt_server(s->target))
5890 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005891
Willy Tarreau55645552015-05-01 13:26:00 +02005892 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5893 * exactly one digit "." one digit. This check may be disabled using
5894 * option accept-invalid-http-response.
5895 */
5896 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5897 if (msg->sl.st.v_l != 8) {
5898 msg->err_pos = 0;
5899 goto hdr_response_bad;
5900 }
5901
5902 if (rep->buf->p[4] != '/' ||
5903 !isdigit((unsigned char)rep->buf->p[5]) ||
5904 rep->buf->p[6] != '.' ||
5905 !isdigit((unsigned char)rep->buf->p[7])) {
5906 msg->err_pos = 4;
5907 goto hdr_response_bad;
5908 }
5909 }
5910
Willy Tarreau5b154472009-12-21 20:11:07 +01005911 /* check if the response is HTTP/1.1 or above */
5912 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005913 ((rep->buf->p[5] > '1') ||
5914 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005915 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005916
5917 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005918 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 +01005919
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005920 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005921 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005922
Willy Tarreau9b28e032012-10-12 23:49:43 +02005923 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005924
Willy Tarreau39650402010-03-15 19:44:39 +01005925 /* Adjust server's health based on status code. Note: status codes 501
5926 * and 505 are triggered on demand by client request, so we must not
5927 * count them as server failures.
5928 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005929 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005930 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005931 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005932 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005933 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005934 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005935
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005936 /*
5937 * 2: check for cacheability.
5938 */
5939
5940 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005941 case 100:
5942 /*
5943 * We may be facing a 100-continue response, in which case this
5944 * is not the right response, and we're waiting for the next one.
5945 * Let's allow this response to go to the client and wait for the
5946 * next one.
5947 */
5948 hdr_idx_init(&txn->hdr_idx);
5949 msg->next -= channel_forward(rep, msg->next);
5950 msg->msg_state = HTTP_MSG_RPBEFORE;
5951 txn->status = 0;
5952 s->logs.t_data = -1; /* was not a response yet */
5953 goto next_one;
5954
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005955 case 200:
5956 case 203:
5957 case 206:
5958 case 300:
5959 case 301:
5960 case 410:
5961 /* RFC2616 @13.4:
5962 * "A response received with a status code of
5963 * 200, 203, 206, 300, 301 or 410 MAY be stored
5964 * by a cache (...) unless a cache-control
5965 * directive prohibits caching."
5966 *
5967 * RFC2616 @9.5: POST method :
5968 * "Responses to this method are not cacheable,
5969 * unless the response includes appropriate
5970 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005971 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005972 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005973 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005974 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5975 break;
5976 default:
5977 break;
5978 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005979
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005980 /*
5981 * 3: we may need to capture headers
5982 */
5983 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005984 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005985 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005986 txn->rsp.cap, s->fe->rsp_cap);
5987
Willy Tarreau4db603d2015-05-01 10:05:17 +02005988 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
5989 * by RFC7230#3.3.3 :
5990 *
5991 * The length of a message body is determined by one of the following
5992 * (in order of precedence):
5993 *
5994 * 1. Any response to a HEAD request and any response with a 1xx
5995 * (Informational), 204 (No Content), or 304 (Not Modified) status
5996 * code is always terminated by the first empty line after the
5997 * header fields, regardless of the header fields present in the
5998 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005999 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006000 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6001 * the connection will become a tunnel immediately after the empty
6002 * line that concludes the header fields. A client MUST ignore any
6003 * Content-Length or Transfer-Encoding header fields received in
6004 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006005 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006006 * 3. If a Transfer-Encoding header field is present and the chunked
6007 * transfer coding (Section 4.1) is the final encoding, the message
6008 * body length is determined by reading and decoding the chunked
6009 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006010 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006011 * If a Transfer-Encoding header field is present in a response and
6012 * the chunked transfer coding is not the final encoding, the
6013 * message body length is determined by reading the connection until
6014 * it is closed by the server. If a Transfer-Encoding header field
6015 * is present in a request and the chunked transfer coding is not
6016 * the final encoding, the message body length cannot be determined
6017 * reliably; the server MUST respond with the 400 (Bad Request)
6018 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006019 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006020 * If a message is received with both a Transfer-Encoding and a
6021 * Content-Length header field, the Transfer-Encoding overrides the
6022 * Content-Length. Such a message might indicate an attempt to
6023 * perform request smuggling (Section 9.5) or response splitting
6024 * (Section 9.4) and ought to be handled as an error. A sender MUST
6025 * remove the received Content-Length field prior to forwarding such
6026 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006027 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006028 * 4. If a message is received without Transfer-Encoding and with
6029 * either multiple Content-Length header fields having differing
6030 * field-values or a single Content-Length header field having an
6031 * invalid value, then the message framing is invalid and the
6032 * recipient MUST treat it as an unrecoverable error. If this is a
6033 * request message, the server MUST respond with a 400 (Bad Request)
6034 * status code and then close the connection. If this is a response
6035 * message received by a proxy, the proxy MUST close the connection
6036 * to the server, discard the received response, and send a 502 (Bad
6037 * Gateway) response to the client. If this is a response message
6038 * received by a user agent, the user agent MUST close the
6039 * connection to the server and discard the received response.
6040 *
6041 * 5. If a valid Content-Length header field is present without
6042 * Transfer-Encoding, its decimal value defines the expected message
6043 * body length in octets. If the sender closes the connection or
6044 * the recipient times out before the indicated number of octets are
6045 * received, the recipient MUST consider the message to be
6046 * incomplete and close the connection.
6047 *
6048 * 6. If this is a request message and none of the above are true, then
6049 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006050 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006051 * 7. Otherwise, this is a response message without a declared message
6052 * body length, so the message body length is determined by the
6053 * number of octets received prior to the server closing the
6054 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006055 */
6056
6057 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006058 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006059 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006060 * FIXME: should we parse anyway and return an error on chunked encoding ?
6061 */
6062 if (txn->meth == HTTP_METH_HEAD ||
6063 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006064 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006065 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006066 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006067 goto skip_content_length;
6068 }
6069
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006070 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006071 ctx.idx = 0;
Willy Tarreaue77bc172015-05-01 10:06:30 +02006072 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006073 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006074 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6075 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006076 /* bad transfer-encoding (chunked followed by something else) */
6077 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006078 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006079 break;
6080 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006081 }
6082
Willy Tarreaudfa3d922015-04-30 10:57:51 +02006083 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006084 ctx.idx = 0;
Willy Tarreau660418d2015-05-01 10:25:45 +02006085 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreaudfa3d922015-04-30 10:57:51 +02006086 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6087 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6088 }
Willy Tarreau660418d2015-05-01 10:25:45 +02006089 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006090 signed long long cl;
6091
Willy Tarreauad14f752011-09-02 20:33:27 +02006092 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006093 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006094 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006095 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006096
Willy Tarreauad14f752011-09-02 20:33:27 +02006097 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006098 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006099 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006100 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006101
Willy Tarreauad14f752011-09-02 20:33:27 +02006102 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006103 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006104 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006105 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006106
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006107 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006108 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006109 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006110 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006111
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006112 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006113 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006114 }
6115
William Lallemand82fe75c2012-10-23 10:25:10 +02006116 if (s->fe->comp || s->be->comp)
6117 select_compression_response_header(s, rep->buf);
6118
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006119skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006120 /* Now we have to check if we need to modify the Connection header.
6121 * This is more difficult on the response than it is on the request,
6122 * because we can have two different HTTP versions and we don't know
6123 * how the client will interprete a response. For instance, let's say
6124 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6125 * HTTP/1.1 response without any header. Maybe it will bound itself to
6126 * HTTP/1.0 because it only knows about it, and will consider the lack
6127 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6128 * the lack of header as a keep-alive. Thus we will use two flags
6129 * indicating how a request MAY be understood by the client. In case
6130 * of multiple possibilities, we'll fix the header to be explicit. If
6131 * ambiguous cases such as both close and keepalive are seen, then we
6132 * will fall back to explicit close. Note that we won't take risks with
6133 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006134 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006135 */
6136
Willy Tarreaudc008c52010-02-01 16:20:08 +01006137 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6138 txn->status == 101)) {
6139 /* Either we've established an explicit tunnel, or we're
6140 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006141 * to understand the next protocols. We have to switch to tunnel
6142 * mode, so that we transfer the request and responses then let
6143 * this protocol pass unmodified. When we later implement specific
6144 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006145 * header which contains information about that protocol for
6146 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006147 */
6148 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6149 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006150 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6151 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006152 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6153 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006154 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006155
Willy Tarreau70dffda2014-01-30 03:07:23 +01006156 /* this situation happens when combining pretend-keepalive with httpclose. */
6157 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006158 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6159 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006160 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6161
Willy Tarreau60466522010-01-18 19:08:45 +01006162 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006163 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006164 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6165 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006166
Willy Tarreau60466522010-01-18 19:08:45 +01006167 /* now adjust header transformations depending on current state */
6168 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6169 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6170 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006171 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006172 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006173 }
Willy Tarreau60466522010-01-18 19:08:45 +01006174 else { /* SCL / KAL */
6175 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006176 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006177 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006178 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006179
Willy Tarreau60466522010-01-18 19:08:45 +01006180 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006181 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006182
Willy Tarreau60466522010-01-18 19:08:45 +01006183 /* Some keep-alive responses are converted to Server-close if
6184 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006185 */
Willy Tarreau60466522010-01-18 19:08:45 +01006186 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6187 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006188 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006189 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006190 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006191 }
6192
Willy Tarreau7959a552013-09-23 16:44:27 +02006193 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006194 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006195
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006196 /* end of job, return OK */
6197 rep->analysers &= ~an_bit;
6198 rep->analyse_exp = TICK_ETERNITY;
6199 channel_auto_close(rep);
6200 return 1;
6201
6202 abort_keep_alive:
6203 /* A keep-alive request to the server failed on a network error.
6204 * The client is required to retry. We need to close without returning
6205 * any other information so that the client retries.
6206 */
6207 txn->status = 0;
6208 rep->analysers = 0;
6209 s->req->analysers = 0;
6210 channel_auto_close(rep);
6211 s->logs.logwait = 0;
6212 s->logs.level = 0;
6213 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
6214 bi_erase(rep);
6215 stream_int_retnclose(rep->cons, NULL);
6216 return 0;
6217}
6218
6219/* This function performs all the processing enabled for the current response.
6220 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6221 * and updates s->rep->analysers. It might make sense to explode it into several
6222 * other functions. It works like process_request (see indications above).
6223 */
6224int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6225{
6226 struct http_txn *txn = &s->txn;
6227 struct http_msg *msg = &txn->rsp;
6228 struct proxy *cur_proxy;
6229 struct cond_wordlist *wl;
6230 struct http_res_rule *http_res_last_rule = NULL;
6231
6232 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6233 now_ms, __FUNCTION__,
6234 s,
6235 rep,
6236 rep->rex, rep->wex,
6237 rep->flags,
6238 rep->buf->i,
6239 rep->analysers);
6240
6241 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6242 return 0;
6243
6244 rep->analysers &= ~an_bit;
6245 rep->analyse_exp = TICK_ETERNITY;
6246
Willy Tarreau70730dd2014-04-24 18:06:27 +02006247 /* The stats applet needs to adjust the Connection header but we don't
6248 * apply any filter there.
6249 */
6250 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6251 goto skip_filters;
6252
Willy Tarreau58975672014-04-24 21:13:57 +02006253 /*
6254 * We will have to evaluate the filters.
6255 * As opposed to version 1.2, now they will be evaluated in the
6256 * filters order and not in the header order. This means that
6257 * each filter has to be validated among all headers.
6258 *
6259 * Filters are tried with ->be first, then with ->fe if it is
6260 * different from ->be.
6261 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006262
Willy Tarreau58975672014-04-24 21:13:57 +02006263 cur_proxy = s->be;
6264 while (1) {
6265 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006266
Willy Tarreau58975672014-04-24 21:13:57 +02006267 /* evaluate http-response rules */
6268 if (!http_res_last_rule)
6269 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 +02006270
Willy Tarreau58975672014-04-24 21:13:57 +02006271 /* try headers filters */
6272 if (rule_set->rsp_exp != NULL) {
6273 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6274 return_bad_resp:
6275 if (objt_server(s->target)) {
6276 objt_server(s->target)->counters.failed_resp++;
6277 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006278 }
Willy Tarreau58975672014-04-24 21:13:57 +02006279 s->be->be_counters.failed_resp++;
6280 return_srv_prx_502:
6281 rep->analysers = 0;
6282 txn->status = 502;
6283 s->logs.t_data = -1; /* was not a valid response */
6284 rep->prod->flags |= SI_FL_NOLINGER;
6285 bi_erase(rep);
6286 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6287 if (!(s->flags & SN_ERR_MASK))
6288 s->flags |= SN_ERR_PRXCOND;
6289 if (!(s->flags & SN_FINST_MASK))
6290 s->flags |= SN_FINST_H;
6291 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006292 }
Willy Tarreau58975672014-04-24 21:13:57 +02006293 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006294
Willy Tarreau58975672014-04-24 21:13:57 +02006295 /* has the response been denied ? */
6296 if (txn->flags & TX_SVDENY) {
6297 if (objt_server(s->target))
6298 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006299
Willy Tarreau58975672014-04-24 21:13:57 +02006300 s->be->be_counters.denied_resp++;
6301 s->fe->fe_counters.denied_resp++;
6302 if (s->listener->counters)
6303 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006304
Willy Tarreau58975672014-04-24 21:13:57 +02006305 goto return_srv_prx_502;
6306 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006307
Willy Tarreau58975672014-04-24 21:13:57 +02006308 /* add response headers from the rule sets in the same order */
6309 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006310 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006311 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006312 if (wl->cond) {
6313 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6314 ret = acl_pass(ret);
6315 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6316 ret = !ret;
6317 if (!ret)
6318 continue;
6319 }
6320 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6321 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006322 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006323
Willy Tarreau58975672014-04-24 21:13:57 +02006324 /* check whether we're already working on the frontend */
6325 if (cur_proxy == s->fe)
6326 break;
6327 cur_proxy = s->fe;
6328 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006329
Willy Tarreau58975672014-04-24 21:13:57 +02006330 /* OK that's all we can do for 1xx responses */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006331 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006332 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006333
Willy Tarreau58975672014-04-24 21:13:57 +02006334 /*
6335 * Now check for a server cookie.
6336 */
6337 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6338 (s->be->options & PR_O_CHK_CACHE))
6339 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006340
Willy Tarreau58975672014-04-24 21:13:57 +02006341 /*
6342 * Check for cache-control or pragma headers if required.
6343 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006344 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 +02006345 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006346
Willy Tarreau58975672014-04-24 21:13:57 +02006347 /*
6348 * Add server cookie in the response if needed
6349 */
6350 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6351 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6352 (!(s->flags & SN_DIRECT) ||
6353 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6354 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6355 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6356 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6357 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6358 !(s->flags & SN_IGNORE_PRST)) {
6359 /* the server is known, it's not the one the client requested, or the
6360 * cookie's last seen date needs to be refreshed. We have to
6361 * insert a set-cookie here, except if we want to insert only on POST
6362 * requests and this one isn't. Note that servers which don't have cookies
6363 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006364 */
Willy Tarreau58975672014-04-24 21:13:57 +02006365 if (!objt_server(s->target)->cookie) {
6366 chunk_printf(&trash,
6367 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6368 s->be->cookie_name);
6369 }
6370 else {
6371 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006372
Willy Tarreau58975672014-04-24 21:13:57 +02006373 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6374 /* emit last_date, which is mandatory */
6375 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6376 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6377 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006378
Willy Tarreau58975672014-04-24 21:13:57 +02006379 if (s->be->cookie_maxlife) {
6380 /* emit first_date, which is either the original one or
6381 * the current date.
6382 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006383 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006384 s30tob64(txn->cookie_first_date ?
6385 txn->cookie_first_date >> 2 :
6386 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006387 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006388 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006389 }
Willy Tarreau58975672014-04-24 21:13:57 +02006390 chunk_appendf(&trash, "; path=/");
6391 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006392
Willy Tarreau58975672014-04-24 21:13:57 +02006393 if (s->be->cookie_domain)
6394 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006395
Willy Tarreau58975672014-04-24 21:13:57 +02006396 if (s->be->ck_opts & PR_CK_HTTPONLY)
6397 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006398
Willy Tarreau58975672014-04-24 21:13:57 +02006399 if (s->be->ck_opts & PR_CK_SECURE)
6400 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006401
Willy Tarreau58975672014-04-24 21:13:57 +02006402 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6403 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006404
Willy Tarreau58975672014-04-24 21:13:57 +02006405 txn->flags &= ~TX_SCK_MASK;
6406 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6407 /* the server did not change, only the date was updated */
6408 txn->flags |= TX_SCK_UPDATED;
6409 else
6410 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006411
Willy Tarreau58975672014-04-24 21:13:57 +02006412 /* Here, we will tell an eventual cache on the client side that we don't
6413 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6414 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6415 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006416 */
Willy Tarreau58975672014-04-24 21:13:57 +02006417 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006418
Willy Tarreau58975672014-04-24 21:13:57 +02006419 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006420
Willy Tarreau58975672014-04-24 21:13:57 +02006421 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6422 "Cache-control: private", 22) < 0))
6423 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006424 }
Willy Tarreau58975672014-04-24 21:13:57 +02006425 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006426
Willy Tarreau58975672014-04-24 21:13:57 +02006427 /*
6428 * Check if result will be cacheable with a cookie.
6429 * We'll block the response if security checks have caught
6430 * nasty things such as a cacheable cookie.
6431 */
6432 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6433 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6434 (s->be->options & PR_O_CHK_CACHE)) {
6435 /* we're in presence of a cacheable response containing
6436 * a set-cookie header. We'll block it as requested by
6437 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006438 */
Willy Tarreau58975672014-04-24 21:13:57 +02006439 if (objt_server(s->target))
6440 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006441
Willy Tarreau58975672014-04-24 21:13:57 +02006442 s->be->be_counters.denied_resp++;
6443 s->fe->fe_counters.denied_resp++;
6444 if (s->listener->counters)
6445 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006446
Willy Tarreau58975672014-04-24 21:13:57 +02006447 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6448 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6449 send_log(s->be, LOG_ALERT,
6450 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6451 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6452 goto return_srv_prx_502;
6453 }
Willy Tarreau03945942009-12-22 16:50:27 +01006454
Willy Tarreau70730dd2014-04-24 18:06:27 +02006455 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006456 /*
6457 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6458 * If an "Upgrade" token is found, the header is left untouched in order
6459 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006460 * if anything but "Upgrade" is present in the Connection header. We don't
6461 * want to touch any 101 response either since it's switching to another
6462 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006463 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006464 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006465 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6466 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6467 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6468 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006469
Willy Tarreau58975672014-04-24 21:13:57 +02006470 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6471 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6472 /* we want a keep-alive response here. Keep-alive header
6473 * required if either side is not 1.1.
6474 */
6475 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6476 want_flags |= TX_CON_KAL_SET;
6477 }
6478 else {
6479 /* we want a close response here. Close header required if
6480 * the server is 1.1, regardless of the client.
6481 */
6482 if (msg->flags & HTTP_MSGF_VER_11)
6483 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006484 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006485
Willy Tarreau58975672014-04-24 21:13:57 +02006486 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6487 http_change_connection_header(txn, msg, want_flags);
6488 }
6489
6490 skip_header_mangling:
6491 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6492 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6493 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006494
Willy Tarreau58975672014-04-24 21:13:57 +02006495 /* if the user wants to log as soon as possible, without counting
6496 * bytes from the server, then this is the right moment. We have
6497 * to temporarily assign bytes_out to log what we currently have.
6498 */
6499 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6500 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6501 s->logs.bytes_out = txn->rsp.eoh;
6502 s->do_log(s);
6503 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006504 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006505 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006506}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006507
Willy Tarreaud98cf932009-12-27 22:54:55 +01006508/* This function is an analyser which forwards response body (including chunk
6509 * sizes if any). It is called as soon as we must forward, even if we forward
6510 * zero byte. The only situation where it must not be called is when we're in
6511 * tunnel mode and we want to forward till the close. It's used both to forward
6512 * remaining data and to resync after end of body. It expects the msg_state to
6513 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6514 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006515 *
6516 * It is capable of compressing response data both in content-length mode and
6517 * in chunked mode. The state machines follows different flows depending on
6518 * whether content-length and chunked modes are used, since there are no
6519 * trailers in content-length :
6520 *
6521 * chk-mode cl-mode
6522 * ,----- BODY -----.
6523 * / \
6524 * V size > 0 V chk-mode
6525 * .--> SIZE -------------> DATA -------------> CRLF
6526 * | | size == 0 | last byte |
6527 * | v final crlf v inspected |
6528 * | TRAILERS -----------> DONE |
6529 * | |
6530 * `----------------------------------------------'
6531 *
6532 * Compression only happens in the DATA state, and must be flushed in final
6533 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6534 * is performed at once on final states for all bytes parsed, or when leaving
6535 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006536 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006537int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006538{
6539 struct http_txn *txn = &s->txn;
6540 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006541 static struct buffer *tmpbuf = NULL;
6542 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006543 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006544
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006545 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6546 return 0;
6547
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006548 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006549 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006550 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006551 /* Output closed while we were sending data. We must abort and
6552 * wake the other side up.
6553 */
6554 msg->msg_state = HTTP_MSG_ERROR;
6555 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006556 return 1;
6557 }
6558
Willy Tarreau4fe41902010-06-07 22:27:41 +02006559 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006560 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006561
Willy Tarreau5bebcd02014-07-10 19:06:10 +02006562 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006563 /* we have msg->sov which points to the first byte of message
6564 * body, and res->buf.p still points to the beginning of the
6565 * message. We forward the headers now, as we don't need them
6566 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006567 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006568 b_adv(res->buf, msg->sov);
6569 msg->next -= msg->sov;
6570 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006571
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006572 /* The previous analysers guarantee that the state is somewhere
6573 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6574 * msg->next are always correct.
6575 */
6576 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6577 if (msg->flags & HTTP_MSGF_TE_CHNK)
6578 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6579 else
6580 msg->msg_state = HTTP_MSG_DATA;
6581 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006582 }
6583
Willy Tarreauefdf0942014-04-24 20:08:57 +02006584 if (res->to_forward) {
6585 /* We can't process the buffer's contents yet */
6586 res->flags |= CF_WAKE_WRITE;
6587 goto missing_data;
6588 }
6589
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006590 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6591 /* We need a compression buffer in the DATA state to put the
6592 * output of compressed data, and in CRLF state to let the
6593 * TRAILERS state finish the job of removing the trailing CRLF.
6594 */
6595 if (unlikely(tmpbuf == NULL)) {
6596 /* this is the first time we need the compression buffer */
6597 tmpbuf = pool_alloc2(pool2_buffer);
6598 if (tmpbuf == NULL)
6599 goto aborted_xfer; /* no memory */
6600 }
6601
6602 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006603 if (ret < 0) {
6604 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006605 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006606 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006607 compressing = 1;
6608 }
6609
Willy Tarreaud98cf932009-12-27 22:54:55 +01006610 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006611 switch (msg->msg_state - HTTP_MSG_DATA) {
6612 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006613 /* we may have some pending data starting at res->buf->p */
6614 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006615 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006616 if (ret < 0)
6617 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006618
Willy Tarreaud5a67832014-04-21 10:54:27 +02006619 if (msg->chunk_len) {
6620 /* input empty or output full */
6621 if (res->buf->i > msg->next)
6622 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006623 goto missing_data;
6624 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006625 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006626 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006627 if (msg->chunk_len > res->buf->i - msg->next) {
6628 /* output full */
6629 res->flags |= CF_WAKE_WRITE;
6630 goto missing_data;
6631 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006632 msg->next += msg->chunk_len;
6633 msg->chunk_len = 0;
6634 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006635
6636 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006637 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006638 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006639 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006640 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006641 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006642 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006643 /* fall through for HTTP_MSG_CHUNK_CRLF */
6644
6645 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6646 /* we want the CRLF after the data */
6647
6648 ret = http_skip_chunk_crlf(msg);
6649 if (ret == 0)
6650 goto missing_data;
6651 else if (ret < 0) {
6652 if (msg->err_pos >= 0)
6653 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6654 goto return_bad_res;
6655 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006656 /* we're in MSG_CHUNK_SIZE now, fall through */
6657
6658 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006659 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006660 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006661 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006662 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006663
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006664 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006665 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006666 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006667 else if (ret < 0) {
6668 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006669 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006670 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006671 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006672 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006673 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006674
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006675 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006676 if (unlikely(compressing)) {
6677 /* we need to flush output contents before syncing FSMs */
6678 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6679 compressing = 0;
6680 }
6681
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006682 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006683 if (ret == 0)
6684 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006685 else if (ret < 0) {
6686 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006687 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006688 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006689 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006690 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006691
6692 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006693 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006694 if (unlikely(compressing)) {
6695 /* we need to flush output contents before syncing FSMs */
6696 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6697 compressing = 0;
6698 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006699
Willy Tarreauc623c172014-04-18 09:53:50 +02006700 /* we may have some pending data starting at res->buf->p
6701 * such as a last chunk of data or trailers.
6702 */
6703 b_adv(res->buf, msg->next);
6704 msg->next = 0;
6705
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006706 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006707 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006708 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6709 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006710 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006711
Willy Tarreau610ecce2010-01-04 21:15:02 +01006712 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006713 /* some state changes occurred, maybe the analyser
6714 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006715 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006716 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006717 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006718 /* response errors are most likely due to
6719 * the client aborting the transfer.
6720 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006721 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006722 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006723 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006724 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006725 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006726 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006727 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006728 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006729 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006730 }
6731 }
6732
Willy Tarreaud98cf932009-12-27 22:54:55 +01006733 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006734 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006735 if (unlikely(compressing)) {
6736 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006737 compressing = 0;
6738 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006739
Willy Tarreauc623c172014-04-18 09:53:50 +02006740 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6741 b_adv(res->buf, msg->next);
6742 msg->next = 0;
6743 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6744 }
6745
Willy Tarreauf003d372012-11-26 13:35:37 +01006746 if (res->flags & CF_SHUTW)
6747 goto aborted_xfer;
6748
6749 /* stop waiting for data if the input is closed before the end. If the
6750 * client side was already closed, it means that the client has aborted,
6751 * so we don't want to count this as a server abort. Otherwise it's a
6752 * server abort.
6753 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006754 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006755 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006756 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006757 if (!(s->flags & SN_ERR_MASK))
6758 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006759 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006760 if (objt_server(s->target))
6761 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006762 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006763 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006764
Willy Tarreau40dba092010-03-04 18:14:51 +01006765 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006766 if (!s->req->analysers)
6767 goto return_bad_res;
6768
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006769 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006770 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006771 * Similarly, with keep-alive on the client side, we don't want to forward a
6772 * close.
6773 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006774 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006775 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6776 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006777 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006778
Willy Tarreau5c620922011-05-11 19:56:11 +02006779 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006780 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006781 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006782 * modes are already handled by the stream sock layer. We must not do
6783 * this in content-length mode because it could present the MSG_MORE
6784 * flag with the last block of forwarded data, which would cause an
6785 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006786 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006787 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006788 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006789
Willy Tarreaud98cf932009-12-27 22:54:55 +01006790 /* the session handler will take care of timeouts and errors */
6791 return 0;
6792
Willy Tarreau40dba092010-03-04 18:14:51 +01006793 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006794 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006795 if (objt_server(s->target))
6796 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006797
6798 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006799 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006800 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006801 compressing = 0;
6802 }
6803
Willy Tarreauc623c172014-04-18 09:53:50 +02006804 /* we may have some pending data starting at res->buf->p */
6805 if (s->comp_algo == NULL) {
6806 b_adv(res->buf, msg->next);
6807 msg->next = 0;
6808 }
6809
Willy Tarreaud98cf932009-12-27 22:54:55 +01006810 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006811 /* don't send any error message as we're in the body */
6812 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006813 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006814 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006815 if (objt_server(s->target))
6816 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006817
6818 if (!(s->flags & SN_ERR_MASK))
6819 s->flags |= SN_ERR_PRXCOND;
6820 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006821 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006822 return 0;
6823
6824 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006825 if (unlikely(compressing)) {
6826 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6827 compressing = 0;
6828 }
6829
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006830 txn->rsp.msg_state = HTTP_MSG_ERROR;
6831 /* don't send any error message as we're in the body */
6832 stream_int_retnclose(res->cons, NULL);
6833 res->analysers = 0;
6834 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6835
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006836 s->fe->fe_counters.cli_aborts++;
6837 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006838 if (objt_server(s->target))
6839 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006840
6841 if (!(s->flags & SN_ERR_MASK))
6842 s->flags |= SN_ERR_CLICL;
6843 if (!(s->flags & SN_FINST_MASK))
6844 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006845 return 0;
6846}
6847
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006848/* Iterate the same filter through all request headers.
6849 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006850 * Since it can manage the switch to another backend, it updates the per-proxy
6851 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006852 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006853int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006854{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006855 char *cur_ptr, *cur_end, *cur_next;
6856 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006857 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006858 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006859 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006860
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006861 last_hdr = 0;
6862
Willy Tarreau9b28e032012-10-12 23:49:43 +02006863 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006864 old_idx = 0;
6865
6866 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006867 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006868 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006869 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006870 (exp->action == ACT_ALLOW ||
6871 exp->action == ACT_DENY ||
6872 exp->action == ACT_TARPIT))
6873 return 0;
6874
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006875 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006876 if (!cur_idx)
6877 break;
6878
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006879 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006880 cur_ptr = cur_next;
6881 cur_end = cur_ptr + cur_hdr->len;
6882 cur_next = cur_end + cur_hdr->cr + 1;
6883
6884 /* Now we have one header between cur_ptr and cur_end,
6885 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006886 */
6887
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02006888 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006889 switch (exp->action) {
6890 case ACT_SETBE:
6891 /* It is not possible to jump a second time.
6892 * FIXME: should we return an HTTP/500 here so that
6893 * the admin knows there's a problem ?
6894 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006895 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006896 break;
6897
6898 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006899 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006900 last_hdr = 1;
6901 break;
6902
6903 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006904 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006905 last_hdr = 1;
6906 break;
6907
6908 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006909 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006910 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006911 break;
6912
6913 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006914 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006915 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006916 break;
6917
6918 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006919 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6920 if (trash.len < 0)
6921 return -1;
6922
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006923 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006924 /* FIXME: if the user adds a newline in the replacement, the
6925 * index will not be recalculated for now, and the new line
6926 * will not be counted as a new header.
6927 */
6928
6929 cur_end += delta;
6930 cur_next += delta;
6931 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006932 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006933 break;
6934
6935 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006936 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006937 cur_next += delta;
6938
Willy Tarreaufa355d42009-11-29 18:12:29 +01006939 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006940 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6941 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006942 cur_hdr->len = 0;
6943 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006944 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006945 break;
6946
6947 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006948 }
6949
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006950 /* keep the link from this header to next one in case of later
6951 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006952 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006953 old_idx = cur_idx;
6954 }
6955 return 0;
6956}
6957
6958
6959/* Apply the filter to the request line.
6960 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6961 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006962 * Since it can manage the switch to another backend, it updates the per-proxy
6963 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006964 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006965int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006966{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006967 char *cur_ptr, *cur_end;
6968 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006969 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006970 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006971
Willy Tarreau3d300592007-03-18 18:34:41 +01006972 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006973 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006974 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006975 (exp->action == ACT_ALLOW ||
6976 exp->action == ACT_DENY ||
6977 exp->action == ACT_TARPIT))
6978 return 0;
6979 else if (exp->action == ACT_REMOVE)
6980 return 0;
6981
6982 done = 0;
6983
Willy Tarreau9b28e032012-10-12 23:49:43 +02006984 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006985 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006986
6987 /* Now we have the request line between cur_ptr and cur_end */
6988
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02006989 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006990 switch (exp->action) {
6991 case ACT_SETBE:
6992 /* It is not possible to jump a second time.
6993 * FIXME: should we return an HTTP/500 here so that
6994 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006995 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006996 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006997 break;
6998
6999 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007000 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007001 done = 1;
7002 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007003
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007004 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007005 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007006 done = 1;
7007 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007008
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007009 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007010 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007011 done = 1;
7012 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007013
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007014 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007015 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007016 done = 1;
7017 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007018
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007019 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007020 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7021 if (trash.len < 0)
7022 return -1;
7023
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007024 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007025 /* FIXME: if the user adds a newline in the replacement, the
7026 * index will not be recalculated for now, and the new line
7027 * will not be counted as a new header.
7028 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007029
Willy Tarreaufa355d42009-11-29 18:12:29 +01007030 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007031 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007032 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007033 HTTP_MSG_RQMETH,
7034 cur_ptr, cur_end + 1,
7035 NULL, NULL);
7036 if (unlikely(!cur_end))
7037 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007038
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007039 /* we have a full request and we know that we have either a CR
7040 * or an LF at <ptr>.
7041 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007042 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7043 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007044 /* there is no point trying this regex on headers */
7045 return 1;
7046 }
7047 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007048 return done;
7049}
Willy Tarreau97de6242006-12-27 17:18:38 +01007050
Willy Tarreau58f10d72006-12-04 02:26:12 +01007051
Willy Tarreau58f10d72006-12-04 02:26:12 +01007052
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007053/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01007054 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007055 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007056 * unparsable request. Since it can manage the switch to another backend, it
7057 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007058 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007059int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007060{
Willy Tarreau6c123b12010-01-28 20:22:06 +01007061 struct http_txn *txn = &s->txn;
7062 struct hdr_exp *exp;
7063
7064 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007065 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007066
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007067 /*
7068 * The interleaving of transformations and verdicts
7069 * makes it difficult to decide to continue or stop
7070 * the evaluation.
7071 */
7072
Willy Tarreau6c123b12010-01-28 20:22:06 +01007073 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7074 break;
7075
Willy Tarreau3d300592007-03-18 18:34:41 +01007076 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007077 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007078 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007079 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007080
7081 /* if this filter had a condition, evaluate it now and skip to
7082 * next filter if the condition does not match.
7083 */
7084 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007085 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007086 ret = acl_pass(ret);
7087 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7088 ret = !ret;
7089
7090 if (!ret)
7091 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007092 }
7093
7094 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007095 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007096 if (unlikely(ret < 0))
7097 return -1;
7098
7099 if (likely(ret == 0)) {
7100 /* The filter did not match the request, it can be
7101 * iterated through all headers.
7102 */
Willy Tarreaua256fae2015-01-30 20:58:58 +01007103 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7104 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007105 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007106 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007107 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007108}
7109
7110
Willy Tarreaua15645d2007-03-18 16:22:39 +01007111
Willy Tarreau58f10d72006-12-04 02:26:12 +01007112/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007113 * Try to retrieve the server associated to the appsession.
7114 * If the server is found, it's assigned to the session.
7115 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007116void manage_client_side_appsession(struct session *s, const char *buf, int len) {
7117 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007118 appsess *asession = NULL;
7119 char *sessid_temp = NULL;
7120
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007121 if (len > s->be->appsession_len) {
7122 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007123 }
7124
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007125 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007126 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007127 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007128 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007129 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007130 }
7131
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007132 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007133 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007134 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007135 return;
7136 }
7137
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007138 memcpy(txn->sessid, buf, len);
7139 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007140 }
7141
7142 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7143 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007144 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007145 return;
7146 }
7147
Cyril Bontéb21570a2009-11-29 20:04:48 +01007148 memcpy(sessid_temp, buf, len);
7149 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007150
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007151 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007152 /* free previously allocated memory */
7153 pool_free2(apools.sessid, sessid_temp);
7154
7155 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007156 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7157 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007158 asession->request_count++;
7159
7160 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007161 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007162
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007163 while (srv) {
7164 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007165 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007166 (s->be->options & PR_O_PERSIST) ||
7167 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007168 /* we found the server and it's usable */
7169 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007170 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007171 s->flags |= SN_DIRECT | SN_ASSIGNED;
7172 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007173
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007174 break;
7175 } else {
7176 txn->flags &= ~TX_CK_MASK;
7177 txn->flags |= TX_CK_DOWN;
7178 }
7179 }
7180 srv = srv->next;
7181 }
7182 }
7183 }
7184}
7185
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007186/* Find the end of a cookie value contained between <s> and <e>. It works the
7187 * same way as with headers above except that the semi-colon also ends a token.
7188 * See RFC2965 for more information. Note that it requires a valid header to
7189 * return a valid result.
7190 */
7191char *find_cookie_value_end(char *s, const char *e)
7192{
7193 int quoted, qdpair;
7194
7195 quoted = qdpair = 0;
7196 for (; s < e; s++) {
7197 if (qdpair) qdpair = 0;
7198 else if (quoted) {
7199 if (*s == '\\') qdpair = 1;
7200 else if (*s == '"') quoted = 0;
7201 }
7202 else if (*s == '"') quoted = 1;
7203 else if (*s == ',' || *s == ';') return s;
7204 }
7205 return s;
7206}
7207
7208/* Delete a value in a header between delimiters <from> and <next> in buffer
7209 * <buf>. The number of characters displaced is returned, and the pointer to
7210 * the first delimiter is updated if required. The function tries as much as
7211 * possible to respect the following principles :
7212 * - replace <from> delimiter by the <next> one unless <from> points to a
7213 * colon, in which case <next> is simply removed
7214 * - set exactly one space character after the new first delimiter, unless
7215 * there are not enough characters in the block being moved to do so.
7216 * - remove unneeded spaces before the previous delimiter and after the new
7217 * one.
7218 *
7219 * It is the caller's responsibility to ensure that :
7220 * - <from> points to a valid delimiter or the colon ;
7221 * - <next> points to a valid delimiter or the final CR/LF ;
7222 * - there are non-space chars before <from> ;
7223 * - there is a CR/LF at or after <next>.
7224 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007225int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007226{
7227 char *prev = *from;
7228
7229 if (*prev == ':') {
7230 /* We're removing the first value, preserve the colon and add a
7231 * space if possible.
7232 */
7233 if (!http_is_crlf[(unsigned char)*next])
7234 next++;
7235 prev++;
7236 if (prev < next)
7237 *prev++ = ' ';
7238
7239 while (http_is_spht[(unsigned char)*next])
7240 next++;
7241 } else {
7242 /* Remove useless spaces before the old delimiter. */
7243 while (http_is_spht[(unsigned char)*(prev-1)])
7244 prev--;
7245 *from = prev;
7246
7247 /* copy the delimiter and if possible a space if we're
7248 * not at the end of the line.
7249 */
7250 if (!http_is_crlf[(unsigned char)*next]) {
7251 *prev++ = *next++;
7252 if (prev + 1 < next)
7253 *prev++ = ' ';
7254 while (http_is_spht[(unsigned char)*next])
7255 next++;
7256 }
7257 }
7258 return buffer_replace2(buf, prev, next, NULL, 0);
7259}
7260
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007261/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007262 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007263 * desirable to call it only when needed. This code is quite complex because
7264 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7265 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007266 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007267void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007268{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007269 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007270 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007271 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007272 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7273 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007274
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007275 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007276 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007277 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007278
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007279 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007280 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007281 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007282
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007283 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007284 hdr_beg = hdr_next;
7285 hdr_end = hdr_beg + cur_hdr->len;
7286 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007287
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007288 /* We have one full header between hdr_beg and hdr_end, and the
7289 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007290 * "Cookie:" headers.
7291 */
7292
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007293 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007294 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007295 old_idx = cur_idx;
7296 continue;
7297 }
7298
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007299 del_from = NULL; /* nothing to be deleted */
7300 preserve_hdr = 0; /* assume we may kill the whole header */
7301
Willy Tarreau58f10d72006-12-04 02:26:12 +01007302 /* Now look for cookies. Conforming to RFC2109, we have to support
7303 * attributes whose name begin with a '$', and associate them with
7304 * the right cookie, if we want to delete this cookie.
7305 * So there are 3 cases for each cookie read :
7306 * 1) it's a special attribute, beginning with a '$' : ignore it.
7307 * 2) it's a server id cookie that we *MAY* want to delete : save
7308 * some pointers on it (last semi-colon, beginning of cookie...)
7309 * 3) it's an application cookie : we *MAY* have to delete a previous
7310 * "special" cookie.
7311 * At the end of loop, if a "special" cookie remains, we may have to
7312 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007313 * *MUST* delete it.
7314 *
7315 * Note: RFC2965 is unclear about the processing of spaces around
7316 * the equal sign in the ATTR=VALUE form. A careful inspection of
7317 * the RFC explicitly allows spaces before it, and not within the
7318 * tokens (attrs or values). An inspection of RFC2109 allows that
7319 * too but section 10.1.3 lets one think that spaces may be allowed
7320 * after the equal sign too, resulting in some (rare) buggy
7321 * implementations trying to do that. So let's do what servers do.
7322 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7323 * allowed quoted strings in values, with any possible character
7324 * after a backslash, including control chars and delimitors, which
7325 * causes parsing to become ambiguous. Browsers also allow spaces
7326 * within values even without quotes.
7327 *
7328 * We have to keep multiple pointers in order to support cookie
7329 * removal at the beginning, middle or end of header without
7330 * corrupting the header. All of these headers are valid :
7331 *
7332 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7333 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7334 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7335 * | | | | | | | | |
7336 * | | | | | | | | hdr_end <--+
7337 * | | | | | | | +--> next
7338 * | | | | | | +----> val_end
7339 * | | | | | +-----------> val_beg
7340 * | | | | +--------------> equal
7341 * | | | +----------------> att_end
7342 * | | +---------------------> att_beg
7343 * | +--------------------------> prev
7344 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007345 */
7346
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007347 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7348 /* Iterate through all cookies on this line */
7349
7350 /* find att_beg */
7351 att_beg = prev + 1;
7352 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7353 att_beg++;
7354
7355 /* find att_end : this is the first character after the last non
7356 * space before the equal. It may be equal to hdr_end.
7357 */
7358 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007359
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007360 while (equal < hdr_end) {
7361 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007362 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007363 if (http_is_spht[(unsigned char)*equal++])
7364 continue;
7365 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007366 }
7367
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007368 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7369 * is between <att_beg> and <equal>, both may be identical.
7370 */
7371
7372 /* look for end of cookie if there is an equal sign */
7373 if (equal < hdr_end && *equal == '=') {
7374 /* look for the beginning of the value */
7375 val_beg = equal + 1;
7376 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7377 val_beg++;
7378
7379 /* find the end of the value, respecting quotes */
7380 next = find_cookie_value_end(val_beg, hdr_end);
7381
7382 /* make val_end point to the first white space or delimitor after the value */
7383 val_end = next;
7384 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7385 val_end--;
7386 } else {
7387 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007388 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007389
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007390 /* We have nothing to do with attributes beginning with '$'. However,
7391 * they will automatically be removed if a header before them is removed,
7392 * since they're supposed to be linked together.
7393 */
7394 if (*att_beg == '$')
7395 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007396
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007397 /* Ignore cookies with no equal sign */
7398 if (equal == next) {
7399 /* This is not our cookie, so we must preserve it. But if we already
7400 * scheduled another cookie for removal, we cannot remove the
7401 * complete header, but we can remove the previous block itself.
7402 */
7403 preserve_hdr = 1;
7404 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007405 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007406 val_end += delta;
7407 next += delta;
7408 hdr_end += delta;
7409 hdr_next += delta;
7410 cur_hdr->len += delta;
7411 http_msg_move_end(&txn->req, delta);
7412 prev = del_from;
7413 del_from = NULL;
7414 }
7415 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007416 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007417
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007418 /* if there are spaces around the equal sign, we need to
7419 * strip them otherwise we'll get trouble for cookie captures,
7420 * or even for rewrites. Since this happens extremely rarely,
7421 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007422 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007423 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7424 int stripped_before = 0;
7425 int stripped_after = 0;
7426
7427 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007428 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007429 equal += stripped_before;
7430 val_beg += stripped_before;
7431 }
7432
7433 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007434 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007435 val_beg += stripped_after;
7436 stripped_before += stripped_after;
7437 }
7438
7439 val_end += stripped_before;
7440 next += stripped_before;
7441 hdr_end += stripped_before;
7442 hdr_next += stripped_before;
7443 cur_hdr->len += stripped_before;
7444 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007445 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007446 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007447
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007448 /* First, let's see if we want to capture this cookie. We check
7449 * that we don't already have a client side cookie, because we
7450 * can only capture one. Also as an optimisation, we ignore
7451 * cookies shorter than the declared name.
7452 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007453 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7454 (val_end - att_beg >= s->fe->capture_namelen) &&
7455 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007456 int log_len = val_end - att_beg;
7457
7458 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7459 Alert("HTTP logging : out of memory.\n");
7460 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007461 if (log_len > s->fe->capture_len)
7462 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007463 memcpy(txn->cli_cookie, att_beg, log_len);
7464 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007465 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007466 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007467
Willy Tarreaubca99692010-10-06 19:25:55 +02007468 /* Persistence cookies in passive, rewrite or insert mode have the
7469 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007470 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007471 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007472 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007473 * For cookies in prefix mode, the form is :
7474 *
7475 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007476 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007477 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7478 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7479 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007480 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007481
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007482 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7483 * have the server ID between val_beg and delim, and the original cookie between
7484 * delim+1 and val_end. Otherwise, delim==val_end :
7485 *
7486 * Cookie: NAME=SRV; # in all but prefix modes
7487 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7488 * | || || | |+-> next
7489 * | || || | +--> val_end
7490 * | || || +---------> delim
7491 * | || |+------------> val_beg
7492 * | || +-------------> att_end = equal
7493 * | |+-----------------> att_beg
7494 * | +------------------> prev
7495 * +-------------------------> hdr_beg
7496 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007497
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007498 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007499 for (delim = val_beg; delim < val_end; delim++)
7500 if (*delim == COOKIE_DELIM)
7501 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007502 } else {
7503 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007504 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007505 /* Now check if the cookie contains a date field, which would
7506 * appear after a vertical bar ('|') just after the server name
7507 * and before the delimiter.
7508 */
7509 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7510 if (vbar1) {
7511 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007512 * right is the last seen date. It is a base64 encoded
7513 * 30-bit value representing the UNIX date since the
7514 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007515 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007516 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007517 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007518 if (val_end - vbar1 >= 5) {
7519 val = b64tos30(vbar1);
7520 if (val > 0)
7521 txn->cookie_last_date = val << 2;
7522 }
7523 /* look for a second vertical bar */
7524 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7525 if (vbar1 && (val_end - vbar1 > 5)) {
7526 val = b64tos30(vbar1 + 1);
7527 if (val > 0)
7528 txn->cookie_first_date = val << 2;
7529 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007530 }
7531 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007532
Willy Tarreauf64d1412010-10-07 20:06:11 +02007533 /* if the cookie has an expiration date and the proxy wants to check
7534 * it, then we do that now. We first check if the cookie is too old,
7535 * then only if it has expired. We detect strict overflow because the
7536 * time resolution here is not great (4 seconds). Cookies with dates
7537 * in the future are ignored if their offset is beyond one day. This
7538 * allows an admin to fix timezone issues without expiring everyone
7539 * and at the same time avoids keeping unwanted side effects for too
7540 * long.
7541 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007542 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7543 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007544 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007545 txn->flags &= ~TX_CK_MASK;
7546 txn->flags |= TX_CK_OLD;
7547 delim = val_beg; // let's pretend we have not found the cookie
7548 txn->cookie_first_date = 0;
7549 txn->cookie_last_date = 0;
7550 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007551 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7552 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007553 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007554 txn->flags &= ~TX_CK_MASK;
7555 txn->flags |= TX_CK_EXPIRED;
7556 delim = val_beg; // let's pretend we have not found the cookie
7557 txn->cookie_first_date = 0;
7558 txn->cookie_last_date = 0;
7559 }
7560
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007561 /* Here, we'll look for the first running server which supports the cookie.
7562 * This allows to share a same cookie between several servers, for example
7563 * to dedicate backup servers to specific servers only.
7564 * However, to prevent clients from sticking to cookie-less backup server
7565 * when they have incidentely learned an empty cookie, we simply ignore
7566 * empty cookies and mark them as invalid.
7567 * The same behaviour is applied when persistence must be ignored.
7568 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007569 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007570 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007571
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007572 while (srv) {
7573 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7574 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007575 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007576 (s->be->options & PR_O_PERSIST) ||
7577 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007578 /* we found the server and we can use it */
7579 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007580 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007581 s->flags |= SN_DIRECT | SN_ASSIGNED;
7582 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007583 break;
7584 } else {
7585 /* we found a server, but it's down,
7586 * mark it as such and go on in case
7587 * another one is available.
7588 */
7589 txn->flags &= ~TX_CK_MASK;
7590 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007591 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007592 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007593 srv = srv->next;
7594 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007595
Willy Tarreauf64d1412010-10-07 20:06:11 +02007596 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007597 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007598 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007599 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007600 txn->flags |= TX_CK_UNUSED;
7601 else
7602 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007603 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007604
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007605 /* depending on the cookie mode, we may have to either :
7606 * - delete the complete cookie if we're in insert+indirect mode, so that
7607 * the server never sees it ;
7608 * - remove the server id from the cookie value, and tag the cookie as an
7609 * application cookie so that it does not get accidentely removed later,
7610 * if we're in cookie prefix mode
7611 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007612 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007613 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007614
Willy Tarreau9b28e032012-10-12 23:49:43 +02007615 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007616 val_end += delta;
7617 next += delta;
7618 hdr_end += delta;
7619 hdr_next += delta;
7620 cur_hdr->len += delta;
7621 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007622
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007623 del_from = NULL;
7624 preserve_hdr = 1; /* we want to keep this cookie */
7625 }
7626 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007627 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007628 del_from = prev;
7629 }
7630 } else {
7631 /* This is not our cookie, so we must preserve it. But if we already
7632 * scheduled another cookie for removal, we cannot remove the
7633 * complete header, but we can remove the previous block itself.
7634 */
7635 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007636
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007637 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007638 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007639 if (att_beg >= del_from)
7640 att_beg += delta;
7641 if (att_end >= del_from)
7642 att_end += delta;
7643 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007644 val_end += delta;
7645 next += delta;
7646 hdr_end += delta;
7647 hdr_next += delta;
7648 cur_hdr->len += delta;
7649 http_msg_move_end(&txn->req, delta);
7650 prev = del_from;
7651 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007652 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007653 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007654
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007655 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007656 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007657 int cmp_len, value_len;
7658 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007659
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007660 if (s->be->options2 & PR_O2_AS_PFX) {
7661 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7662 value_begin = att_beg + s->be->appsession_name_len;
7663 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007664 } else {
7665 cmp_len = att_end - att_beg;
7666 value_begin = val_beg;
7667 value_len = val_end - val_beg;
7668 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007669
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007670 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007671 if (cmp_len == s->be->appsession_name_len &&
7672 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7673 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007674 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007675 }
7676
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007677 /* continue with next cookie on this header line */
7678 att_beg = next;
7679 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007680
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007681 /* There are no more cookies on this line.
7682 * We may still have one (or several) marked for deletion at the
7683 * end of the line. We must do this now in two ways :
7684 * - if some cookies must be preserved, we only delete from the
7685 * mark to the end of line ;
7686 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007687 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007688 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007689 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007690 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007691 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007692 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007693 cur_hdr->len += delta;
7694 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007695 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007696
7697 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007698 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7699 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007700 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007701 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007702 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007703 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007704 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007705 }
7706
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007707 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007708 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007709 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007710}
7711
7712
Willy Tarreaua15645d2007-03-18 16:22:39 +01007713/* Iterate the same filter through all response headers contained in <rtr>.
7714 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7715 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007716int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007717{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007718 char *cur_ptr, *cur_end, *cur_next;
7719 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007720 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007721 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007722 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007723
7724 last_hdr = 0;
7725
Willy Tarreau9b28e032012-10-12 23:49:43 +02007726 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007727 old_idx = 0;
7728
7729 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007730 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007731 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007732 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007733 (exp->action == ACT_ALLOW ||
7734 exp->action == ACT_DENY))
7735 return 0;
7736
7737 cur_idx = txn->hdr_idx.v[old_idx].next;
7738 if (!cur_idx)
7739 break;
7740
7741 cur_hdr = &txn->hdr_idx.v[cur_idx];
7742 cur_ptr = cur_next;
7743 cur_end = cur_ptr + cur_hdr->len;
7744 cur_next = cur_end + cur_hdr->cr + 1;
7745
7746 /* Now we have one header between cur_ptr and cur_end,
7747 * and the next header starts at cur_next.
7748 */
7749
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007750 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007751 switch (exp->action) {
7752 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007753 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007754 last_hdr = 1;
7755 break;
7756
7757 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007758 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007759 last_hdr = 1;
7760 break;
7761
7762 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007763 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7764 if (trash.len < 0)
7765 return -1;
7766
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007767 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007768 /* FIXME: if the user adds a newline in the replacement, the
7769 * index will not be recalculated for now, and the new line
7770 * will not be counted as a new header.
7771 */
7772
7773 cur_end += delta;
7774 cur_next += delta;
7775 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007776 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007777 break;
7778
7779 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007780 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007781 cur_next += delta;
7782
Willy Tarreaufa355d42009-11-29 18:12:29 +01007783 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007784 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7785 txn->hdr_idx.used--;
7786 cur_hdr->len = 0;
7787 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007788 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007789 break;
7790
7791 }
7792 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007793
7794 /* keep the link from this header to next one in case of later
7795 * removal of next header.
7796 */
7797 old_idx = cur_idx;
7798 }
7799 return 0;
7800}
7801
7802
7803/* Apply the filter to the status line in the response buffer <rtr>.
7804 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7805 * or -1 if a replacement resulted in an invalid status line.
7806 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007807int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007808{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007809 char *cur_ptr, *cur_end;
7810 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007811 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007812 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007813
7814
Willy Tarreau3d300592007-03-18 18:34:41 +01007815 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007816 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007817 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007818 (exp->action == ACT_ALLOW ||
7819 exp->action == ACT_DENY))
7820 return 0;
7821 else if (exp->action == ACT_REMOVE)
7822 return 0;
7823
7824 done = 0;
7825
Willy Tarreau9b28e032012-10-12 23:49:43 +02007826 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007827 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007828
7829 /* Now we have the status line between cur_ptr and cur_end */
7830
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007831 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007832 switch (exp->action) {
7833 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007834 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007835 done = 1;
7836 break;
7837
7838 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007839 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007840 done = 1;
7841 break;
7842
7843 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007844 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7845 if (trash.len < 0)
7846 return -1;
7847
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007848 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007849 /* FIXME: if the user adds a newline in the replacement, the
7850 * index will not be recalculated for now, and the new line
7851 * will not be counted as a new header.
7852 */
7853
Willy Tarreaufa355d42009-11-29 18:12:29 +01007854 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007855 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007856 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007857 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007858 cur_ptr, cur_end + 1,
7859 NULL, NULL);
7860 if (unlikely(!cur_end))
7861 return -1;
7862
7863 /* we have a full respnse and we know that we have either a CR
7864 * or an LF at <ptr>.
7865 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007866 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007867 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007868 /* there is no point trying this regex on headers */
7869 return 1;
7870 }
7871 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007872 return done;
7873}
7874
7875
7876
7877/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007878 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007879 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7880 * unparsable response.
7881 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007882int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007883{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007884 struct http_txn *txn = &s->txn;
7885 struct hdr_exp *exp;
7886
7887 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007888 int ret;
7889
7890 /*
7891 * The interleaving of transformations and verdicts
7892 * makes it difficult to decide to continue or stop
7893 * the evaluation.
7894 */
7895
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007896 if (txn->flags & TX_SVDENY)
7897 break;
7898
Willy Tarreau3d300592007-03-18 18:34:41 +01007899 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007900 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7901 exp->action == ACT_PASS)) {
7902 exp = exp->next;
7903 continue;
7904 }
7905
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007906 /* if this filter had a condition, evaluate it now and skip to
7907 * next filter if the condition does not match.
7908 */
7909 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007910 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007911 ret = acl_pass(ret);
7912 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7913 ret = !ret;
7914 if (!ret)
7915 continue;
7916 }
7917
Willy Tarreaua15645d2007-03-18 16:22:39 +01007918 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007919 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007920 if (unlikely(ret < 0))
7921 return -1;
7922
7923 if (likely(ret == 0)) {
7924 /* The filter did not match the response, it can be
7925 * iterated through all headers.
7926 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007927 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7928 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007929 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007930 }
7931 return 0;
7932}
7933
7934
Willy Tarreaua15645d2007-03-18 16:22:39 +01007935/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007936 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007937 * desirable to call it only when needed. This function is also used when we
7938 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007939 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007940void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007941{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007942 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007943 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007944 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007945 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007946 char *hdr_beg, *hdr_end, *hdr_next;
7947 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007948
Willy Tarreaua15645d2007-03-18 16:22:39 +01007949 /* Iterate through the headers.
7950 * we start with the start line.
7951 */
7952 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007953 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007954
7955 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7956 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007957 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007958
7959 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007960 hdr_beg = hdr_next;
7961 hdr_end = hdr_beg + cur_hdr->len;
7962 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007963
Willy Tarreau24581ba2010-08-31 22:39:35 +02007964 /* We have one full header between hdr_beg and hdr_end, and the
7965 * next header starts at hdr_next. We're only interested in
7966 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007967 */
7968
Willy Tarreau24581ba2010-08-31 22:39:35 +02007969 is_cookie2 = 0;
7970 prev = hdr_beg + 10;
7971 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007972 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007973 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7974 if (!val) {
7975 old_idx = cur_idx;
7976 continue;
7977 }
7978 is_cookie2 = 1;
7979 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007980 }
7981
Willy Tarreau24581ba2010-08-31 22:39:35 +02007982 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7983 * <prev> points to the colon.
7984 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007985 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007986
Willy Tarreau24581ba2010-08-31 22:39:35 +02007987 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7988 * check-cache is enabled) and we are not interested in checking
7989 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007990 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007991 if (s->be->cookie_name == NULL &&
7992 s->be->appsession_name == NULL &&
7993 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007994 return;
7995
Willy Tarreau24581ba2010-08-31 22:39:35 +02007996 /* OK so now we know we have to process this response cookie.
7997 * The format of the Set-Cookie header is slightly different
7998 * from the format of the Cookie header in that it does not
7999 * support the comma as a cookie delimiter (thus the header
8000 * cannot be folded) because the Expires attribute described in
8001 * the original Netscape's spec may contain an unquoted date
8002 * with a comma inside. We have to live with this because
8003 * many browsers don't support Max-Age and some browsers don't
8004 * support quoted strings. However the Set-Cookie2 header is
8005 * clean.
8006 *
8007 * We have to keep multiple pointers in order to support cookie
8008 * removal at the beginning, middle or end of header without
8009 * corrupting the header (in case of set-cookie2). A special
8010 * pointer, <scav> points to the beginning of the set-cookie-av
8011 * fields after the first semi-colon. The <next> pointer points
8012 * either to the end of line (set-cookie) or next unquoted comma
8013 * (set-cookie2). All of these headers are valid :
8014 *
8015 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8016 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8017 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8018 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8019 * | | | | | | | | | |
8020 * | | | | | | | | +-> next hdr_end <--+
8021 * | | | | | | | +------------> scav
8022 * | | | | | | +--------------> val_end
8023 * | | | | | +--------------------> val_beg
8024 * | | | | +----------------------> equal
8025 * | | | +------------------------> att_end
8026 * | | +----------------------------> att_beg
8027 * | +------------------------------> prev
8028 * +-----------------------------------------> hdr_beg
8029 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008030
Willy Tarreau24581ba2010-08-31 22:39:35 +02008031 for (; prev < hdr_end; prev = next) {
8032 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008033
Willy Tarreau24581ba2010-08-31 22:39:35 +02008034 /* find att_beg */
8035 att_beg = prev + 1;
8036 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8037 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008038
Willy Tarreau24581ba2010-08-31 22:39:35 +02008039 /* find att_end : this is the first character after the last non
8040 * space before the equal. It may be equal to hdr_end.
8041 */
8042 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008043
Willy Tarreau24581ba2010-08-31 22:39:35 +02008044 while (equal < hdr_end) {
8045 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8046 break;
8047 if (http_is_spht[(unsigned char)*equal++])
8048 continue;
8049 att_end = equal;
8050 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008051
Willy Tarreau24581ba2010-08-31 22:39:35 +02008052 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8053 * is between <att_beg> and <equal>, both may be identical.
8054 */
8055
8056 /* look for end of cookie if there is an equal sign */
8057 if (equal < hdr_end && *equal == '=') {
8058 /* look for the beginning of the value */
8059 val_beg = equal + 1;
8060 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8061 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008062
Willy Tarreau24581ba2010-08-31 22:39:35 +02008063 /* find the end of the value, respecting quotes */
8064 next = find_cookie_value_end(val_beg, hdr_end);
8065
8066 /* make val_end point to the first white space or delimitor after the value */
8067 val_end = next;
8068 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8069 val_end--;
8070 } else {
8071 /* <equal> points to next comma, semi-colon or EOL */
8072 val_beg = val_end = next = equal;
8073 }
8074
8075 if (next < hdr_end) {
8076 /* Set-Cookie2 supports multiple cookies, and <next> points to
8077 * a colon or semi-colon before the end. So skip all attr-value
8078 * pairs and look for the next comma. For Set-Cookie, since
8079 * commas are permitted in values, skip to the end.
8080 */
8081 if (is_cookie2)
8082 next = find_hdr_value_end(next, hdr_end);
8083 else
8084 next = hdr_end;
8085 }
8086
8087 /* Now everything is as on the diagram above */
8088
8089 /* Ignore cookies with no equal sign */
8090 if (equal == val_end)
8091 continue;
8092
8093 /* If there are spaces around the equal sign, we need to
8094 * strip them otherwise we'll get trouble for cookie captures,
8095 * or even for rewrites. Since this happens extremely rarely,
8096 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008097 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008098 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8099 int stripped_before = 0;
8100 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008101
Willy Tarreau24581ba2010-08-31 22:39:35 +02008102 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008103 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008104 equal += stripped_before;
8105 val_beg += stripped_before;
8106 }
8107
8108 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008109 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008110 val_beg += stripped_after;
8111 stripped_before += stripped_after;
8112 }
8113
8114 val_end += stripped_before;
8115 next += stripped_before;
8116 hdr_end += stripped_before;
8117 hdr_next += stripped_before;
8118 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008119 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008120 }
8121
8122 /* First, let's see if we want to capture this cookie. We check
8123 * that we don't already have a server side cookie, because we
8124 * can only capture one. Also as an optimisation, we ignore
8125 * cookies shorter than the declared name.
8126 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008127 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008128 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008129 (val_end - att_beg >= s->fe->capture_namelen) &&
8130 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008131 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008132 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008133 Alert("HTTP logging : out of memory.\n");
8134 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008135 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008136 if (log_len > s->fe->capture_len)
8137 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008138 memcpy(txn->srv_cookie, att_beg, log_len);
8139 txn->srv_cookie[log_len] = 0;
8140 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008141 }
8142
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008143 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008144 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008145 if (!(s->flags & SN_IGNORE_PRST) &&
8146 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8147 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008148 /* assume passive cookie by default */
8149 txn->flags &= ~TX_SCK_MASK;
8150 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008151
8152 /* If the cookie is in insert mode on a known server, we'll delete
8153 * this occurrence because we'll insert another one later.
8154 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008155 * a direct access.
8156 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008157 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008158 /* The "preserve" flag was set, we don't want to touch the
8159 * server's cookie.
8160 */
8161 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008162 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8163 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008164 /* this cookie must be deleted */
8165 if (*prev == ':' && next == hdr_end) {
8166 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008167 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008168 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8169 txn->hdr_idx.used--;
8170 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008171 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008172 hdr_next += delta;
8173 http_msg_move_end(&txn->rsp, delta);
8174 /* note: while both invalid now, <next> and <hdr_end>
8175 * are still equal, so the for() will stop as expected.
8176 */
8177 } else {
8178 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008179 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008180 next = prev;
8181 hdr_end += delta;
8182 hdr_next += delta;
8183 cur_hdr->len += delta;
8184 http_msg_move_end(&txn->rsp, delta);
8185 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008186 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008187 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008188 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008189 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008190 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008191 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008192 * with this server since we know it.
8193 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008194 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008195 next += delta;
8196 hdr_end += delta;
8197 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008198 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008199 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008200
Willy Tarreauf1348312010-10-07 15:54:11 +02008201 txn->flags &= ~TX_SCK_MASK;
8202 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008203 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008204 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008205 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008206 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008207 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008208 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008209 next += delta;
8210 hdr_end += delta;
8211 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008212 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008213 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008214
Willy Tarreau827aee92011-03-10 16:55:02 +01008215 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008216 txn->flags &= ~TX_SCK_MASK;
8217 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008218 }
8219 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008220 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008221 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008222 int cmp_len, value_len;
8223 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008224
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008225 if (s->be->options2 & PR_O2_AS_PFX) {
8226 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8227 value_begin = att_beg + s->be->appsession_name_len;
8228 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008229 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008230 cmp_len = att_end - att_beg;
8231 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008232 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008233 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008234
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008235 if ((cmp_len == s->be->appsession_name_len) &&
8236 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008237 /* free a possibly previously allocated memory */
8238 pool_free2(apools.sessid, txn->sessid);
8239
Cyril Bontéb21570a2009-11-29 20:04:48 +01008240 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008241 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008242 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008243 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008244 return;
8245 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008246 memcpy(txn->sessid, value_begin, value_len);
8247 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008248 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008249 }
8250 /* that's done for this cookie, check the next one on the same
8251 * line when next != hdr_end (only if is_cookie2).
8252 */
8253 }
8254 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008255 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008256 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008257
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008258 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008259 appsess *asession = NULL;
8260 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008261 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008262 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008263 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008264 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8265 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008266 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008267 return;
8268 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008269 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8270
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008271 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8272 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008273 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8274 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008275 return;
8276 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008277 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8278 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008279
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008280 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008281 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008282 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008283 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8284 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008285 return;
8286 }
8287 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008288 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008289
8290 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008291 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008292 }
8293
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008294 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008295 asession->request_count++;
8296 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008297}
8298
8299
Willy Tarreaua15645d2007-03-18 16:22:39 +01008300/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008301 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008302 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008303void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008304{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008305 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008306 char *p1, *p2;
8307
8308 char *cur_ptr, *cur_end, *cur_next;
8309 int cur_idx;
8310
Willy Tarreau5df51872007-11-25 16:20:08 +01008311 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008312 return;
8313
8314 /* Iterate through the headers.
8315 * we start with the start line.
8316 */
8317 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008318 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008319
8320 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8321 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008322 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008323
8324 cur_hdr = &txn->hdr_idx.v[cur_idx];
8325 cur_ptr = cur_next;
8326 cur_end = cur_ptr + cur_hdr->len;
8327 cur_next = cur_end + cur_hdr->cr + 1;
8328
8329 /* We have one full header between cur_ptr and cur_end, and the
8330 * next header starts at cur_next. We're only interested in
8331 * "Cookie:" headers.
8332 */
8333
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008334 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8335 if (val) {
8336 if ((cur_end - (cur_ptr + val) >= 8) &&
8337 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8338 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8339 return;
8340 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008341 }
8342
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008343 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8344 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008345 continue;
8346
8347 /* OK, right now we know we have a cache-control header at cur_ptr */
8348
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008349 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008350
8351 if (p1 >= cur_end) /* no more info */
8352 continue;
8353
8354 /* p1 is at the beginning of the value */
8355 p2 = p1;
8356
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008357 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008358 p2++;
8359
8360 /* we have a complete value between p1 and p2 */
8361 if (p2 < cur_end && *p2 == '=') {
8362 /* we have something of the form no-cache="set-cookie" */
8363 if ((cur_end - p1 >= 21) &&
8364 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8365 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008366 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008367 continue;
8368 }
8369
8370 /* OK, so we know that either p2 points to the end of string or to a comma */
8371 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008372 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008373 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8374 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8375 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008376 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008377 return;
8378 }
8379
8380 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008381 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008382 continue;
8383 }
8384 }
8385}
8386
8387
Willy Tarreau58f10d72006-12-04 02:26:12 +01008388/*
8389 * Try to retrieve a known appsession in the URI, then the associated server.
8390 * If the server is found, it's assigned to the session.
8391 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008392void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008393{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008394 char *end_params, *first_param, *cur_param, *next_param;
8395 char separator;
8396 int value_len;
8397
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008398 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008399
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008400 if (s->be->appsession_name == NULL ||
8401 (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 +01008402 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008403 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008404
Cyril Bontéb21570a2009-11-29 20:04:48 +01008405 first_param = NULL;
8406 switch (mode) {
8407 case PR_O2_AS_M_PP:
8408 first_param = memchr(begin, ';', len);
8409 break;
8410 case PR_O2_AS_M_QS:
8411 first_param = memchr(begin, '?', len);
8412 break;
8413 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008414
Cyril Bontéb21570a2009-11-29 20:04:48 +01008415 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008416 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008417 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008418
Cyril Bontéb21570a2009-11-29 20:04:48 +01008419 switch (mode) {
8420 case PR_O2_AS_M_PP:
8421 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8422 end_params = (char *) begin + len;
8423 }
8424 separator = ';';
8425 break;
8426 case PR_O2_AS_M_QS:
8427 end_params = (char *) begin + len;
8428 separator = '&';
8429 break;
8430 default:
8431 /* unknown mode, shouldn't happen */
8432 return;
8433 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008434
Cyril Bontéb21570a2009-11-29 20:04:48 +01008435 cur_param = next_param = end_params;
8436 while (cur_param > first_param) {
8437 cur_param--;
8438 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8439 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008440 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8441 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8442 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008443 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008444 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8445 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008446 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008447 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008448 }
8449 break;
8450 }
8451 next_param = cur_param;
8452 }
8453 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008454#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008455 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008456 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008457#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008458}
8459
Willy Tarreaub2513902006-12-17 14:52:38 +01008460/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008461 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008462 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008463 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008464 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008465 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008466 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008467 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008468 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008469int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008470{
8471 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008472 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008473 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008474
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008475 if (!uri_auth)
8476 return 0;
8477
Cyril Bonté70be45d2010-10-12 00:14:35 +02008478 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008479 return 0;
8480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008481 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008482 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008483 return 0;
8484
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008485 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008486 return 0;
8487
Willy Tarreaub2513902006-12-17 14:52:38 +01008488 return 1;
8489}
8490
Willy Tarreau4076a152009-04-02 15:18:36 +02008491/*
8492 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008493 * By default it tries to report the error position as msg->err_pos. However if
8494 * this one is not set, it will then report msg->next, which is the last known
8495 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008496 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008497 */
8498void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008499 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008500 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008501{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008502 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008503 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008504
Willy Tarreau9b28e032012-10-12 23:49:43 +02008505 es->len = MIN(chn->buf->i, sizeof(es->buf));
8506 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008507 len1 = MIN(len1, es->len);
8508 len2 = es->len - len1; /* remaining data if buffer wraps */
8509
Willy Tarreau9b28e032012-10-12 23:49:43 +02008510 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008511 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008512 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008513
Willy Tarreau4076a152009-04-02 15:18:36 +02008514 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008515 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008516 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008517 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008518
Willy Tarreau4076a152009-04-02 15:18:36 +02008519 es->when = date; // user-visible date
8520 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008521 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008522 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008523 if (objt_conn(s->req->prod->end))
8524 es->src = __objt_conn(s->req->prod->end)->addr.from;
8525 else
8526 memset(&es->src, 0, sizeof(es->src));
8527
Willy Tarreau078272e2010-12-12 12:46:33 +01008528 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008529 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008530 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008531 es->s_flags = s->flags;
8532 es->t_flags = s->txn.flags;
8533 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008534 es->b_out = chn->buf->o;
8535 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008536 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008537 es->m_clen = msg->chunk_len;
8538 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008539}
Willy Tarreaub2513902006-12-17 14:52:38 +01008540
Willy Tarreau294c4732011-12-16 21:35:50 +01008541/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8542 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8543 * performed over the whole headers. Otherwise it must contain a valid header
8544 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8545 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8546 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8547 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008548 * -1. The value fetch stops at commas, so this function is suited for use with
8549 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008550 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008551 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008552unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008553 struct hdr_idx *idx, int occ,
8554 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008555{
Willy Tarreau294c4732011-12-16 21:35:50 +01008556 struct hdr_ctx local_ctx;
8557 char *ptr_hist[MAX_HDR_HISTORY];
8558 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008559 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008560 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008561
Willy Tarreau294c4732011-12-16 21:35:50 +01008562 if (!ctx) {
8563 local_ctx.idx = 0;
8564 ctx = &local_ctx;
8565 }
8566
Willy Tarreaubce70882009-09-07 11:51:47 +02008567 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008568 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008569 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008570 occ--;
8571 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008572 *vptr = ctx->line + ctx->val;
8573 *vlen = ctx->vlen;
8574 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008575 }
8576 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008577 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008578 }
8579
8580 /* negative occurrence, we scan all the list then walk back */
8581 if (-occ > MAX_HDR_HISTORY)
8582 return 0;
8583
Willy Tarreau294c4732011-12-16 21:35:50 +01008584 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008585 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008586 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8587 len_hist[hist_ptr] = ctx->vlen;
8588 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008589 hist_ptr = 0;
8590 found++;
8591 }
8592 if (-occ > found)
8593 return 0;
8594 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008595 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8596 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8597 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008598 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008599 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008600 if (hist_ptr >= MAX_HDR_HISTORY)
8601 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008602 *vptr = ptr_hist[hist_ptr];
8603 *vlen = len_hist[hist_ptr];
8604 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008605}
8606
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008607/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8608 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8609 * performed over the whole headers. Otherwise it must contain a valid header
8610 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8611 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8612 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8613 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8614 * -1. This function differs from http_get_hdr() in that it only returns full
8615 * line header values and does not stop at commas.
8616 * The return value is 0 if nothing was found, or non-zero otherwise.
8617 */
8618unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8619 struct hdr_idx *idx, int occ,
8620 struct hdr_ctx *ctx, char **vptr, int *vlen)
8621{
8622 struct hdr_ctx local_ctx;
8623 char *ptr_hist[MAX_HDR_HISTORY];
8624 int len_hist[MAX_HDR_HISTORY];
8625 unsigned int hist_ptr;
8626 int found;
8627
8628 if (!ctx) {
8629 local_ctx.idx = 0;
8630 ctx = &local_ctx;
8631 }
8632
8633 if (occ >= 0) {
8634 /* search from the beginning */
8635 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8636 occ--;
8637 if (occ <= 0) {
8638 *vptr = ctx->line + ctx->val;
8639 *vlen = ctx->vlen;
8640 return 1;
8641 }
8642 }
8643 return 0;
8644 }
8645
8646 /* negative occurrence, we scan all the list then walk back */
8647 if (-occ > MAX_HDR_HISTORY)
8648 return 0;
8649
8650 found = hist_ptr = 0;
8651 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8652 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8653 len_hist[hist_ptr] = ctx->vlen;
8654 if (++hist_ptr >= MAX_HDR_HISTORY)
8655 hist_ptr = 0;
8656 found++;
8657 }
8658 if (-occ > found)
8659 return 0;
8660 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8661 * find occurrence -occ, so we have to check [hist_ptr+occ].
8662 */
8663 hist_ptr += occ;
8664 if (hist_ptr >= MAX_HDR_HISTORY)
8665 hist_ptr -= MAX_HDR_HISTORY;
8666 *vptr = ptr_hist[hist_ptr];
8667 *vlen = len_hist[hist_ptr];
8668 return 1;
8669}
8670
Willy Tarreaubaaee002006-06-26 02:48:02 +02008671/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008672 * Print a debug line with a header. Always stop at the first CR or LF char,
8673 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8674 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008675 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008676void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008677{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008678 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008679 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008680 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008681 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8682 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 +02008683
8684 for (max = 0; start + max < end; max++)
8685 if (start[max] == '\r' || start[max] == '\n')
8686 break;
8687
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008688 UBOUND(max, trash.size - trash.len - 3);
8689 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8690 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008691 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008692}
8693
Willy Tarreau0937bc42009-12-22 15:03:09 +01008694/*
8695 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8696 * the required fields are properly allocated and that we only need to (re)init
8697 * them. This should be used before processing any new request.
8698 */
8699void http_init_txn(struct session *s)
8700{
8701 struct http_txn *txn = &s->txn;
8702 struct proxy *fe = s->fe;
8703
8704 txn->flags = 0;
8705 txn->status = -1;
8706
Willy Tarreauf64d1412010-10-07 20:06:11 +02008707 txn->cookie_first_date = 0;
8708 txn->cookie_last_date = 0;
8709
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008710 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008711 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008712 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008713 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008714 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008715 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008716 txn->req.chunk_len = 0LL;
8717 txn->req.body_len = 0LL;
8718 txn->rsp.chunk_len = 0LL;
8719 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008720 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8721 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008722 txn->req.chn = s->req;
8723 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008724
8725 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008726
8727 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8728 if (fe->options2 & PR_O2_REQBUG_OK)
8729 txn->req.err_pos = -1; /* let buggy requests pass */
8730
Willy Tarreau46023632010-01-07 22:51:47 +01008731 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008732 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8733
Willy Tarreau46023632010-01-07 22:51:47 +01008734 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008735 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8736
8737 if (txn->hdr_idx.v)
8738 hdr_idx_init(&txn->hdr_idx);
8739}
8740
8741/* to be used at the end of a transaction */
8742void http_end_txn(struct session *s)
8743{
8744 struct http_txn *txn = &s->txn;
8745
Willy Tarreau75195602014-03-11 15:48:55 +01008746 /* release any possible compression context */
8747 if (s->flags & SN_COMP_READY)
8748 s->comp_algo->end(&s->comp_ctx);
8749 s->comp_algo = NULL;
8750 s->flags &= ~SN_COMP_READY;
8751
Willy Tarreau0937bc42009-12-22 15:03:09 +01008752 /* these ones will have been dynamically allocated */
8753 pool_free2(pool2_requri, txn->uri);
8754 pool_free2(pool2_capture, txn->cli_cookie);
8755 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008756 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008757 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008758
William Lallemanda73203e2012-03-12 12:48:57 +01008759 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008760 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008761 txn->uri = NULL;
8762 txn->srv_cookie = NULL;
8763 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008764
8765 if (txn->req.cap) {
8766 struct cap_hdr *h;
8767 for (h = s->fe->req_cap; h; h = h->next)
8768 pool_free2(h->pool, txn->req.cap[h->index]);
8769 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8770 }
8771
8772 if (txn->rsp.cap) {
8773 struct cap_hdr *h;
8774 for (h = s->fe->rsp_cap; h; h = h->next)
8775 pool_free2(h->pool, txn->rsp.cap[h->index]);
8776 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8777 }
8778
Willy Tarreau0937bc42009-12-22 15:03:09 +01008779}
8780
8781/* to be used at the end of a transaction to prepare a new one */
8782void http_reset_txn(struct session *s)
8783{
8784 http_end_txn(s);
8785 http_init_txn(s);
8786
8787 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008788 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008789 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008790 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008791 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008792 /* re-init store persistence */
8793 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008794 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008795
Willy Tarreau0937bc42009-12-22 15:03:09 +01008796 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008797
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008798 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008799
Willy Tarreau739cfba2010-01-25 23:11:14 +01008800 /* We must trim any excess data from the response buffer, because we
8801 * may have blocked an invalid response from a server that we don't
8802 * want to accidentely forward once we disable the analysers, nor do
8803 * we want those data to come along with next response. A typical
8804 * example of such data would be from a buggy server responding to
8805 * a HEAD with some data, or sending more than the advertised
8806 * content-length.
8807 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008808 if (unlikely(s->rep->buf->i))
8809 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008810
Willy Tarreau0937bc42009-12-22 15:03:09 +01008811 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008812 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008813
Willy Tarreaud04e8582010-05-31 12:31:35 +02008814 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008815 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008816
8817 s->req->rex = TICK_ETERNITY;
8818 s->req->wex = TICK_ETERNITY;
8819 s->req->analyse_exp = TICK_ETERNITY;
8820 s->rep->rex = TICK_ETERNITY;
8821 s->rep->wex = TICK_ETERNITY;
8822 s->rep->analyse_exp = TICK_ETERNITY;
8823}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008824
Sasha Pachev218f0642014-06-16 12:05:59 -06008825void free_http_res_rules(struct list *r)
8826{
8827 struct http_res_rule *tr, *pr;
8828
8829 list_for_each_entry_safe(pr, tr, r, list) {
8830 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008831 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008832 free(pr);
8833 }
8834}
8835
8836void free_http_req_rules(struct list *r)
8837{
Willy Tarreauff011f22011-01-06 17:51:27 +01008838 struct http_req_rule *tr, *pr;
8839
8840 list_for_each_entry_safe(pr, tr, r, list) {
8841 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008842 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008843 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008844
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008845 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008846 free(pr);
8847 }
8848}
8849
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008850/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008851struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8852{
8853 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008854 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008855 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008856 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008857
8858 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8859 if (!rule) {
8860 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008861 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008862 }
8863
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008864 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008865 rule->action = HTTP_REQ_ACT_ALLOW;
8866 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008867 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008868 rule->action = HTTP_REQ_ACT_DENY;
8869 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008870 } else if (!strcmp(args[0], "tarpit")) {
8871 rule->action = HTTP_REQ_ACT_TARPIT;
8872 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008873 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008874 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008875 cur_arg = 1;
8876
8877 while(*args[cur_arg]) {
8878 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008879 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008880 cur_arg+=2;
8881 continue;
8882 } else
8883 break;
8884 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008885 } else if (!strcmp(args[0], "set-nice")) {
8886 rule->action = HTTP_REQ_ACT_SET_NICE;
8887 cur_arg = 1;
8888
8889 if (!*args[cur_arg] ||
8890 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8891 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8892 file, linenum, args[0]);
8893 goto out_err;
8894 }
8895 rule->arg.nice = atoi(args[cur_arg]);
8896 if (rule->arg.nice < -1024)
8897 rule->arg.nice = -1024;
8898 else if (rule->arg.nice > 1024)
8899 rule->arg.nice = 1024;
8900 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008901 } else if (!strcmp(args[0], "set-tos")) {
8902#ifdef IP_TOS
8903 char *err;
8904 rule->action = HTTP_REQ_ACT_SET_TOS;
8905 cur_arg = 1;
8906
8907 if (!*args[cur_arg] ||
8908 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8909 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8910 file, linenum, args[0]);
8911 goto out_err;
8912 }
8913
8914 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8915 if (err && *err != '\0') {
8916 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8917 file, linenum, err, args[0]);
8918 goto out_err;
8919 }
8920 cur_arg++;
8921#else
8922 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8923 goto out_err;
8924#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008925 } else if (!strcmp(args[0], "set-mark")) {
8926#ifdef SO_MARK
8927 char *err;
8928 rule->action = HTTP_REQ_ACT_SET_MARK;
8929 cur_arg = 1;
8930
8931 if (!*args[cur_arg] ||
8932 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8933 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8934 file, linenum, args[0]);
8935 goto out_err;
8936 }
8937
8938 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8939 if (err && *err != '\0') {
8940 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8941 file, linenum, err, args[0]);
8942 goto out_err;
8943 }
8944 cur_arg++;
8945 global.last_checks |= LSTCHK_NETADM;
8946#else
8947 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8948 goto out_err;
8949#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008950 } else if (!strcmp(args[0], "set-log-level")) {
8951 rule->action = HTTP_REQ_ACT_SET_LOGL;
8952 cur_arg = 1;
8953
8954 if (!*args[cur_arg] ||
8955 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8956 bad_log_level:
8957 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8958 file, linenum, args[0]);
8959 goto out_err;
8960 }
8961 if (strcmp(args[cur_arg], "silent") == 0)
8962 rule->arg.loglevel = -1;
8963 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8964 goto bad_log_level;
8965 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008966 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8967 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8968 cur_arg = 1;
8969
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008970 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8971 (*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 +01008972 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8973 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008974 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008975 }
8976
8977 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8978 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8979 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008980
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008981 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008982 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008983 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8984 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008985 free(proxy->conf.lfs_file);
8986 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8987 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008988 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008989 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
8990 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008991 cur_arg = 1;
8992
8993 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann8f249f42014-06-24 11:10:00 +02008994 (*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 -06008995 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8996 file, linenum, args[0]);
8997 goto out_err;
8998 }
8999
9000 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9001 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9002 LIST_INIT(&rule->arg.hdr_add.fmt);
9003
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009004 error = NULL;
9005 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9006 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9007 args[cur_arg + 1], error);
9008 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009009 goto out_err;
9010 }
9011
9012 proxy->conf.args.ctx = ARGC_HRQ;
9013 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9014 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9015 file, linenum);
9016
9017 free(proxy->conf.lfs_file);
9018 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9019 proxy->conf.lfs_line = proxy->conf.args.line;
9020 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009021 } else if (strcmp(args[0], "del-header") == 0) {
9022 rule->action = HTTP_REQ_ACT_DEL_HDR;
9023 cur_arg = 1;
9024
9025 if (!*args[cur_arg] ||
9026 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9027 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9028 file, linenum, args[0]);
9029 goto out_err;
9030 }
9031
9032 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9033 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9034
9035 proxy->conf.args.ctx = ARGC_HRQ;
9036 free(proxy->conf.lfs_file);
9037 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9038 proxy->conf.lfs_line = proxy->conf.args.line;
9039 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009040 } else if (strcmp(args[0], "redirect") == 0) {
9041 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009042 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009043
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009044 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009045 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9046 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9047 goto out_err;
9048 }
9049
9050 /* this redirect rule might already contain a parsed condition which
9051 * we'll pass to the http-request rule.
9052 */
9053 rule->action = HTTP_REQ_ACT_REDIR;
9054 rule->arg.redir = redir;
9055 rule->cond = redir->cond;
9056 redir->cond = NULL;
9057 cur_arg = 2;
9058 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009059 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9060 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9061 rule->action = HTTP_REQ_ACT_ADD_ACL;
9062 /*
9063 * '+ 8' for 'add-acl('
9064 * '- 9' for 'add-acl(' + trailing ')'
9065 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009066 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009067
9068 cur_arg = 1;
9069
9070 if (!*args[cur_arg] ||
9071 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9072 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9073 file, linenum, args[0]);
9074 goto out_err;
9075 }
9076
9077 LIST_INIT(&rule->arg.map.key);
9078 proxy->conf.args.ctx = ARGC_HRQ;
9079 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9080 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9081 file, linenum);
9082 free(proxy->conf.lfs_file);
9083 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9084 proxy->conf.lfs_line = proxy->conf.args.line;
9085 cur_arg += 1;
9086 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9087 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9088 rule->action = HTTP_REQ_ACT_DEL_ACL;
9089 /*
9090 * '+ 8' for 'del-acl('
9091 * '- 9' for 'del-acl(' + trailing ')'
9092 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009093 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009094
9095 cur_arg = 1;
9096
9097 if (!*args[cur_arg] ||
9098 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9099 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9100 file, linenum, args[0]);
9101 goto out_err;
9102 }
9103
9104 LIST_INIT(&rule->arg.map.key);
9105 proxy->conf.args.ctx = ARGC_HRQ;
9106 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9107 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9108 file, linenum);
9109 free(proxy->conf.lfs_file);
9110 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9111 proxy->conf.lfs_line = proxy->conf.args.line;
9112 cur_arg += 1;
9113 } else if (strncmp(args[0], "del-map", 7) == 0) {
9114 /* http-request del-map(<reference (map name)>) <key pattern> */
9115 rule->action = HTTP_REQ_ACT_DEL_MAP;
9116 /*
9117 * '+ 8' for 'del-map('
9118 * '- 9' for 'del-map(' + trailing ')'
9119 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009120 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009121
9122 cur_arg = 1;
9123
9124 if (!*args[cur_arg] ||
9125 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9126 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9127 file, linenum, args[0]);
9128 goto out_err;
9129 }
9130
9131 LIST_INIT(&rule->arg.map.key);
9132 proxy->conf.args.ctx = ARGC_HRQ;
9133 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9134 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9135 file, linenum);
9136 free(proxy->conf.lfs_file);
9137 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9138 proxy->conf.lfs_line = proxy->conf.args.line;
9139 cur_arg += 1;
9140 } else if (strncmp(args[0], "set-map", 7) == 0) {
9141 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9142 rule->action = HTTP_REQ_ACT_SET_MAP;
9143 /*
9144 * '+ 8' for 'set-map('
9145 * '- 9' for 'set-map(' + trailing ')'
9146 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009147 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009148
9149 cur_arg = 1;
9150
9151 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9152 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9153 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9154 file, linenum, args[0]);
9155 goto out_err;
9156 }
9157
9158 LIST_INIT(&rule->arg.map.key);
9159 LIST_INIT(&rule->arg.map.value);
9160 proxy->conf.args.ctx = ARGC_HRQ;
9161
9162 /* key pattern */
9163 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9164 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9165 file, linenum);
9166
9167 /* value pattern */
9168 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9169 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9170 file, linenum);
9171 free(proxy->conf.lfs_file);
9172 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9173 proxy->conf.lfs_line = proxy->conf.args.line;
9174
9175 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009176 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9177 char *errmsg = NULL;
9178 cur_arg = 1;
9179 /* try in the module list */
9180 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9181 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9182 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9183 free(errmsg);
9184 goto out_err;
9185 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009186 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009187 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 +01009188 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009189 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009190 }
9191
9192 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9193 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009194 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009195
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009196 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9197 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9198 file, linenum, args[0], errmsg);
9199 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009200 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009201 }
9202 rule->cond = cond;
9203 }
9204 else if (*args[cur_arg]) {
9205 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9206 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9207 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009208 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009209 }
9210
9211 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009212 out_err:
9213 free(rule);
9214 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009215}
9216
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009217/* parse an "http-respose" rule */
9218struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9219{
9220 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009221 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009222 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009223 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009224
9225 rule = calloc(1, sizeof(*rule));
9226 if (!rule) {
9227 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9228 goto out_err;
9229 }
9230
9231 if (!strcmp(args[0], "allow")) {
9232 rule->action = HTTP_RES_ACT_ALLOW;
9233 cur_arg = 1;
9234 } else if (!strcmp(args[0], "deny")) {
9235 rule->action = HTTP_RES_ACT_DENY;
9236 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009237 } else if (!strcmp(args[0], "set-nice")) {
9238 rule->action = HTTP_RES_ACT_SET_NICE;
9239 cur_arg = 1;
9240
9241 if (!*args[cur_arg] ||
9242 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9243 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9244 file, linenum, args[0]);
9245 goto out_err;
9246 }
9247 rule->arg.nice = atoi(args[cur_arg]);
9248 if (rule->arg.nice < -1024)
9249 rule->arg.nice = -1024;
9250 else if (rule->arg.nice > 1024)
9251 rule->arg.nice = 1024;
9252 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009253 } else if (!strcmp(args[0], "set-tos")) {
9254#ifdef IP_TOS
9255 char *err;
9256 rule->action = HTTP_RES_ACT_SET_TOS;
9257 cur_arg = 1;
9258
9259 if (!*args[cur_arg] ||
9260 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9261 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9262 file, linenum, args[0]);
9263 goto out_err;
9264 }
9265
9266 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9267 if (err && *err != '\0') {
9268 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9269 file, linenum, err, args[0]);
9270 goto out_err;
9271 }
9272 cur_arg++;
9273#else
9274 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9275 goto out_err;
9276#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009277 } else if (!strcmp(args[0], "set-mark")) {
9278#ifdef SO_MARK
9279 char *err;
9280 rule->action = HTTP_RES_ACT_SET_MARK;
9281 cur_arg = 1;
9282
9283 if (!*args[cur_arg] ||
9284 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9285 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9286 file, linenum, args[0]);
9287 goto out_err;
9288 }
9289
9290 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9291 if (err && *err != '\0') {
9292 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9293 file, linenum, err, args[0]);
9294 goto out_err;
9295 }
9296 cur_arg++;
9297 global.last_checks |= LSTCHK_NETADM;
9298#else
9299 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9300 goto out_err;
9301#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009302 } else if (!strcmp(args[0], "set-log-level")) {
9303 rule->action = HTTP_RES_ACT_SET_LOGL;
9304 cur_arg = 1;
9305
9306 if (!*args[cur_arg] ||
9307 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9308 bad_log_level:
9309 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9310 file, linenum, args[0]);
9311 goto out_err;
9312 }
9313 if (strcmp(args[cur_arg], "silent") == 0)
9314 rule->arg.loglevel = -1;
9315 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9316 goto bad_log_level;
9317 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009318 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9319 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9320 cur_arg = 1;
9321
9322 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9323 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9324 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9325 file, linenum, args[0]);
9326 goto out_err;
9327 }
9328
9329 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9330 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9331 LIST_INIT(&rule->arg.hdr_add.fmt);
9332
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009333 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009334 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009335 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9336 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009337 free(proxy->conf.lfs_file);
9338 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9339 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009340 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009341 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009342 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009343 cur_arg = 1;
9344
9345 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmanna772b942014-08-08 17:29:06 +02009346 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9347 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009348 file, linenum, args[0]);
9349 goto out_err;
9350 }
9351
9352 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9353 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9354 LIST_INIT(&rule->arg.hdr_add.fmt);
9355
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009356 error = NULL;
9357 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9358 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9359 args[cur_arg + 1], error);
9360 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009361 goto out_err;
9362 }
9363
9364 proxy->conf.args.ctx = ARGC_HRQ;
9365 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9366 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9367 file, linenum);
9368
9369 free(proxy->conf.lfs_file);
9370 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9371 proxy->conf.lfs_line = proxy->conf.args.line;
9372 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009373 } else if (strcmp(args[0], "del-header") == 0) {
9374 rule->action = HTTP_RES_ACT_DEL_HDR;
9375 cur_arg = 1;
9376
9377 if (!*args[cur_arg] ||
9378 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9379 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9380 file, linenum, args[0]);
9381 goto out_err;
9382 }
9383
9384 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9385 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9386
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009387 proxy->conf.args.ctx = ARGC_HRS;
9388 free(proxy->conf.lfs_file);
9389 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9390 proxy->conf.lfs_line = proxy->conf.args.line;
9391 cur_arg += 1;
9392 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9393 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9394 rule->action = HTTP_RES_ACT_ADD_ACL;
9395 /*
9396 * '+ 8' for 'add-acl('
9397 * '- 9' for 'add-acl(' + trailing ')'
9398 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009399 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009400
9401 cur_arg = 1;
9402
9403 if (!*args[cur_arg] ||
9404 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9405 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9406 file, linenum, args[0]);
9407 goto out_err;
9408 }
9409
9410 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009411 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009412 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9413 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9414 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009415 free(proxy->conf.lfs_file);
9416 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9417 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009418
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009419 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009420 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9421 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9422 rule->action = HTTP_RES_ACT_DEL_ACL;
9423 /*
9424 * '+ 8' for 'del-acl('
9425 * '- 9' for 'del-acl(' + trailing ')'
9426 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009427 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009428
9429 cur_arg = 1;
9430
9431 if (!*args[cur_arg] ||
9432 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9433 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9434 file, linenum, args[0]);
9435 goto out_err;
9436 }
9437
9438 LIST_INIT(&rule->arg.map.key);
9439 proxy->conf.args.ctx = ARGC_HRS;
9440 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9441 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9442 file, linenum);
9443 free(proxy->conf.lfs_file);
9444 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9445 proxy->conf.lfs_line = proxy->conf.args.line;
9446 cur_arg += 1;
9447 } else if (strncmp(args[0], "del-map", 7) == 0) {
9448 /* http-response del-map(<reference (map name)>) <key pattern> */
9449 rule->action = HTTP_RES_ACT_DEL_MAP;
9450 /*
9451 * '+ 8' for 'del-map('
9452 * '- 9' for 'del-map(' + trailing ')'
9453 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009454 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009455
9456 cur_arg = 1;
9457
9458 if (!*args[cur_arg] ||
9459 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9460 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9461 file, linenum, args[0]);
9462 goto out_err;
9463 }
9464
9465 LIST_INIT(&rule->arg.map.key);
9466 proxy->conf.args.ctx = ARGC_HRS;
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 free(proxy->conf.lfs_file);
9471 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9472 proxy->conf.lfs_line = proxy->conf.args.line;
9473 cur_arg += 1;
9474 } else if (strncmp(args[0], "set-map", 7) == 0) {
9475 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9476 rule->action = HTTP_RES_ACT_SET_MAP;
9477 /*
9478 * '+ 8' for 'set-map('
9479 * '- 9' for 'set-map(' + trailing ')'
9480 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009481 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009482
9483 cur_arg = 1;
9484
9485 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9486 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9487 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9488 file, linenum, args[0]);
9489 goto out_err;
9490 }
9491
9492 LIST_INIT(&rule->arg.map.key);
9493 LIST_INIT(&rule->arg.map.value);
9494
9495 proxy->conf.args.ctx = ARGC_HRS;
9496
9497 /* key pattern */
9498 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9499 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9500 file, linenum);
9501
9502 /* value pattern */
9503 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9504 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9505 file, linenum);
9506
9507 free(proxy->conf.lfs_file);
9508 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9509 proxy->conf.lfs_line = proxy->conf.args.line;
9510
9511 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009512 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9513 char *errmsg = NULL;
9514 cur_arg = 1;
9515 /* try in the module list */
9516 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9517 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9518 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9519 free(errmsg);
9520 goto out_err;
9521 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009522 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009523 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 +02009524 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9525 goto out_err;
9526 }
9527
9528 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9529 struct acl_cond *cond;
9530 char *errmsg = NULL;
9531
9532 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9533 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9534 file, linenum, args[0], errmsg);
9535 free(errmsg);
9536 goto out_err;
9537 }
9538 rule->cond = cond;
9539 }
9540 else if (*args[cur_arg]) {
9541 Alert("parsing [%s:%d]: 'http-response %s' expects"
9542 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9543 file, linenum, args[0], args[cur_arg]);
9544 goto out_err;
9545 }
9546
9547 return rule;
9548 out_err:
9549 free(rule);
9550 return NULL;
9551}
9552
Willy Tarreau4baae242012-12-27 12:00:31 +01009553/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009554 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9555 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009556 */
9557struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009558 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009559{
9560 struct redirect_rule *rule;
9561 int cur_arg;
9562 int type = REDIRECT_TYPE_NONE;
9563 int code = 302;
9564 const char *destination = NULL;
9565 const char *cookie = NULL;
9566 int cookie_set = 0;
9567 unsigned int flags = REDIRECT_FLAG_NONE;
9568 struct acl_cond *cond = NULL;
9569
9570 cur_arg = 0;
9571 while (*(args[cur_arg])) {
9572 if (strcmp(args[cur_arg], "location") == 0) {
9573 if (!*args[cur_arg + 1])
9574 goto missing_arg;
9575
9576 type = REDIRECT_TYPE_LOCATION;
9577 cur_arg++;
9578 destination = args[cur_arg];
9579 }
9580 else if (strcmp(args[cur_arg], "prefix") == 0) {
9581 if (!*args[cur_arg + 1])
9582 goto missing_arg;
9583
9584 type = REDIRECT_TYPE_PREFIX;
9585 cur_arg++;
9586 destination = args[cur_arg];
9587 }
9588 else if (strcmp(args[cur_arg], "scheme") == 0) {
9589 if (!*args[cur_arg + 1])
9590 goto missing_arg;
9591
9592 type = REDIRECT_TYPE_SCHEME;
9593 cur_arg++;
9594 destination = args[cur_arg];
9595 }
9596 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9597 if (!*args[cur_arg + 1])
9598 goto missing_arg;
9599
9600 cur_arg++;
9601 cookie = args[cur_arg];
9602 cookie_set = 1;
9603 }
9604 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9605 if (!*args[cur_arg + 1])
9606 goto missing_arg;
9607
9608 cur_arg++;
9609 cookie = args[cur_arg];
9610 cookie_set = 0;
9611 }
9612 else if (strcmp(args[cur_arg], "code") == 0) {
9613 if (!*args[cur_arg + 1])
9614 goto missing_arg;
9615
9616 cur_arg++;
9617 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009618 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009619 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009620 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009621 args[cur_arg - 1], args[cur_arg]);
9622 return NULL;
9623 }
9624 }
9625 else if (!strcmp(args[cur_arg],"drop-query")) {
9626 flags |= REDIRECT_FLAG_DROP_QS;
9627 }
9628 else if (!strcmp(args[cur_arg],"append-slash")) {
9629 flags |= REDIRECT_FLAG_APPEND_SLASH;
9630 }
9631 else if (strcmp(args[cur_arg], "if") == 0 ||
9632 strcmp(args[cur_arg], "unless") == 0) {
9633 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9634 if (!cond) {
9635 memprintf(errmsg, "error in condition: %s", *errmsg);
9636 return NULL;
9637 }
9638 break;
9639 }
9640 else {
9641 memprintf(errmsg,
9642 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9643 args[cur_arg]);
9644 return NULL;
9645 }
9646 cur_arg++;
9647 }
9648
9649 if (type == REDIRECT_TYPE_NONE) {
9650 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9651 return NULL;
9652 }
9653
9654 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9655 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009656 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009657
9658 if (!use_fmt) {
9659 /* old-style static redirect rule */
9660 rule->rdr_str = strdup(destination);
9661 rule->rdr_len = strlen(destination);
9662 }
9663 else {
9664 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009665
9666 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9667 * if prefix == "/", we don't want to add anything, otherwise it
9668 * makes it hard for the user to configure a self-redirection.
9669 */
Godbach543b9782014-12-18 15:44:58 +08009670 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009671 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009672 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009673 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9674 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009675 free(curproxy->conf.lfs_file);
9676 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9677 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009678 }
9679 }
9680
Willy Tarreau4baae242012-12-27 12:00:31 +01009681 if (cookie) {
9682 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9683 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9684 */
9685 rule->cookie_len = strlen(cookie);
9686 if (cookie_set) {
9687 rule->cookie_str = malloc(rule->cookie_len + 10);
9688 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9689 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9690 rule->cookie_len += 9;
9691 } else {
9692 rule->cookie_str = malloc(rule->cookie_len + 21);
9693 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9694 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9695 rule->cookie_len += 20;
9696 }
9697 }
9698 rule->type = type;
9699 rule->code = code;
9700 rule->flags = flags;
9701 LIST_INIT(&rule->list);
9702 return rule;
9703
9704 missing_arg:
9705 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9706 return NULL;
9707}
9708
Willy Tarreau8797c062007-05-07 00:55:35 +02009709/************************************************************************/
9710/* The code below is dedicated to ACL parsing and matching */
9711/************************************************************************/
9712
9713
Willy Tarreau14174bc2012-04-16 14:34:04 +02009714/* This function ensures that the prerequisites for an L7 fetch are ready,
9715 * which means that a request or response is ready. If some data is missing,
9716 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009717 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9718 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009719 *
9720 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009721 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9722 * decide whether or not an HTTP message is present ;
9723 * 0 if the requested data cannot be fetched or if it is certain that
9724 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009725 * 1 if an HTTP message is ready
9726 */
9727static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009728smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009729 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009730{
9731 struct http_txn *txn = l7;
9732 struct http_msg *msg = &txn->req;
9733
9734 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9735 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9736 */
9737
9738 if (unlikely(!s || !txn))
9739 return 0;
9740
9741 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009742 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009743
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009744 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009745 if (unlikely(!s->req))
9746 return 0;
9747
Willy Tarreauaae75e32013-03-29 12:31:49 +01009748 /* If the buffer does not leave enough free space at the end,
9749 * we must first realign it.
9750 */
9751 if (s->req->buf->p > s->req->buf->data &&
9752 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9753 buffer_slow_realign(s->req->buf);
9754
Willy Tarreau14174bc2012-04-16 14:34:04 +02009755 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009756 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009757 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009758
9759 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009760 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009761 http_msg_analyzer(msg, &txn->hdr_idx);
9762
9763 /* Still no valid request ? */
9764 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009765 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009766 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009767 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009768 }
9769 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009770 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009771 return 0;
9772 }
9773
9774 /* OK we just got a valid HTTP request. We have some minor
9775 * preparation to perform so that further checks can rely
9776 * on HTTP tests.
9777 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009778
9779 /* If the request was parsed but was too large, we must absolutely
9780 * return an error so that it is not processed. At the moment this
9781 * cannot happen, but if the parsers are to change in the future,
9782 * we want this check to be maintained.
9783 */
9784 if (unlikely(s->req->buf->i + s->req->buf->p >
9785 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9786 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009787 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009788 return 1;
9789 }
9790
Willy Tarreau9b28e032012-10-12 23:49:43 +02009791 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009792 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9793 s->flags |= SN_REDIRECTABLE;
9794
Willy Tarreau506d0502013-07-06 13:29:24 +02009795 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9796 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009797 }
9798
Willy Tarreau506d0502013-07-06 13:29:24 +02009799 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009800 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009801 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009802
9803 /* otherwise everything's ready for the request */
9804 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009805 else {
9806 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009807 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9808 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009809 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009810 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009811 }
9812
9813 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009814 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009815 return 1;
9816}
Willy Tarreau8797c062007-05-07 00:55:35 +02009817
Willy Tarreaua4ba9db2014-06-25 16:56:41 +02009818/* Note: these functinos *do* modify the sample. Even in case of success, at
9819 * least the type and uint value are modified.
9820 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009821#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009822 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 +02009823
Willy Tarreau24e32d82012-04-23 23:55:44 +02009824#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009825 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 +02009826
Willy Tarreau8797c062007-05-07 00:55:35 +02009827
9828/* 1. Check on METHOD
9829 * We use the pre-parsed method if it is known, and store its number as an
9830 * integer. If it is unknown, we use the pointer and the length.
9831 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009832static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009833{
9834 int len, meth;
9835
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009836 len = strlen(text);
9837 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009838
9839 pattern->val.i = meth;
9840 if (meth == HTTP_METH_OTHER) {
Willy Tarreau71196f32014-08-29 15:15:50 +02009841 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009842 pattern->len = len;
9843 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009844 else {
9845 pattern->ptr.str = NULL;
9846 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009847 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009848 return 1;
9849}
9850
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009851/* This function fetches the method of current HTTP request and stores
9852 * it in the global pattern struct as a chunk. There are two possibilities :
9853 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9854 * in <len> and <ptr> is NULL ;
9855 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9856 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009857 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009858 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009859static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009860smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009861 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009862{
9863 int meth;
9864 struct http_txn *txn = l7;
9865
Willy Tarreau24e32d82012-04-23 23:55:44 +02009866 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009867
Willy Tarreau8797c062007-05-07 00:55:35 +02009868 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009869 smp->type = SMP_T_METH;
9870 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009871 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009872 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9873 /* ensure the indexes are not affected */
9874 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009875 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009876 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9877 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009878 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009879 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009880 return 1;
9881}
9882
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009883/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009884static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009885{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009886 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009887 struct pattern_list *lst;
9888 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009889
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009890 list_for_each_entry(lst, &expr->patterns, list) {
9891 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009892
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009893 /* well-known method */
9894 if (pattern->val.i != HTTP_METH_OTHER) {
9895 if (smp->data.meth.meth == pattern->val.i)
9896 return pattern;
9897 else
9898 continue;
9899 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009900
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009901 /* Other method, we must compare the strings */
9902 if (pattern->len != smp->data.meth.str.len)
9903 continue;
9904
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009905 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreauc62a0c62014-08-28 20:42:57 +02009906 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
9907 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009908 return pattern;
9909 }
9910 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009911}
9912
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009913static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009914smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009915 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009916{
9917 struct http_txn *txn = l7;
9918 char *ptr;
9919 int len;
9920
Willy Tarreauc0239e02012-04-16 14:42:55 +02009921 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009922
Willy Tarreau8797c062007-05-07 00:55:35 +02009923 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009924 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009925
9926 while ((len-- > 0) && (*ptr++ != '/'));
9927 if (len <= 0)
9928 return 0;
9929
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009930 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009931 smp->data.str.str = ptr;
9932 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009933
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009934 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009935 return 1;
9936}
9937
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009938static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009939smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009940 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009941{
9942 struct http_txn *txn = l7;
9943 char *ptr;
9944 int len;
9945
Willy Tarreauc0239e02012-04-16 14:42:55 +02009946 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009947
Willy Tarreauf26b2522012-12-14 08:33:14 +01009948 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9949 return 0;
9950
Willy Tarreau8797c062007-05-07 00:55:35 +02009951 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009952 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009953
9954 while ((len-- > 0) && (*ptr++ != '/'));
9955 if (len <= 0)
9956 return 0;
9957
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009958 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009959 smp->data.str.str = ptr;
9960 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009961
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009962 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009963 return 1;
9964}
9965
9966/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009967static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009968smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009969 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009970{
9971 struct http_txn *txn = l7;
9972 char *ptr;
9973 int len;
9974
Willy Tarreauc0239e02012-04-16 14:42:55 +02009975 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009976
Willy Tarreauf26b2522012-12-14 08:33:14 +01009977 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9978 return 0;
9979
Willy Tarreau8797c062007-05-07 00:55:35 +02009980 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009981 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009982
Willy Tarreauf853c462012-04-23 18:53:56 +02009983 smp->type = SMP_T_UINT;
9984 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009985 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009986 return 1;
9987}
9988
9989/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009990static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009991smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009992 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009993{
9994 struct http_txn *txn = l7;
9995
Willy Tarreauc0239e02012-04-16 14:42:55 +02009996 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009997
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009998 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009999 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010000 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010001 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010002 return 1;
10003}
10004
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010005static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010006smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010007 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010008{
10009 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010010 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010011
Willy Tarreauc0239e02012-04-16 14:42:55 +020010012 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010013
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010014 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 +020010015 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010016 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010017
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010018 smp->type = SMP_T_IPV4;
10019 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010020 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010021 return 1;
10022}
10023
10024static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010025smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010026 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010027{
10028 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010029 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010030
Willy Tarreauc0239e02012-04-16 14:42:55 +020010031 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010032
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010033 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 +020010034 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10035 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010036
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010037 smp->type = SMP_T_UINT;
10038 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010039 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010040 return 1;
10041}
10042
Willy Tarreau185b5c42012-04-26 15:11:51 +020010043/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10044 * Accepts an optional argument of type string containing the header field name,
10045 * and an optional argument of type signed or unsigned integer to request an
10046 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010047 * headers are considered from the first one. It does not stop on commas and
10048 * returns full lines instead (useful for User-Agent or Date for example).
10049 */
10050static int
10051smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010052 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010053{
10054 struct http_txn *txn = l7;
10055 struct hdr_idx *idx = &txn->hdr_idx;
10056 struct hdr_ctx *ctx = smp->ctx.a[0];
10057 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10058 int occ = 0;
10059 const char *name_str = NULL;
10060 int name_len = 0;
10061
10062 if (!ctx) {
10063 /* first call */
10064 ctx = &static_hdr_ctx;
10065 ctx->idx = 0;
10066 smp->ctx.a[0] = ctx;
10067 }
10068
10069 if (args) {
10070 if (args[0].type != ARGT_STR)
10071 return 0;
10072 name_str = args[0].data.str.str;
10073 name_len = args[0].data.str.len;
10074
10075 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10076 occ = args[1].data.uint;
10077 }
10078
10079 CHECK_HTTP_MESSAGE_FIRST();
10080
10081 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10082 /* search for header from the beginning */
10083 ctx->idx = 0;
10084
10085 if (!occ && !(opt & SMP_OPT_ITERATE))
10086 /* no explicit occurrence and single fetch => last header by default */
10087 occ = -1;
10088
10089 if (!occ)
10090 /* prepare to report multiple occurrences for ACL fetches */
10091 smp->flags |= SMP_F_NOT_LAST;
10092
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010093 smp->type = SMP_T_STR;
10094 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010095 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10096 return 1;
10097
10098 smp->flags &= ~SMP_F_NOT_LAST;
10099 return 0;
10100}
10101
10102/* 6. Check on HTTP header count. The number of occurrences is returned.
10103 * Accepts exactly 1 argument of type string. It does not stop on commas and
10104 * returns full lines instead (useful for User-Agent or Date for example).
10105 */
10106static int
10107smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010108 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010109{
10110 struct http_txn *txn = l7;
10111 struct hdr_idx *idx = &txn->hdr_idx;
10112 struct hdr_ctx ctx;
10113 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10114 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010115 const char *name = NULL;
10116 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010117
Willy Tarreau29437342015-04-01 19:16:09 +020010118 if (args && args->type == ARGT_STR) {
10119 name = args->data.str.str;
10120 len = args->data.str.len;
10121 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010122
10123 CHECK_HTTP_MESSAGE_FIRST();
10124
10125 ctx.idx = 0;
10126 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010127 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010128 cnt++;
10129
10130 smp->type = SMP_T_UINT;
10131 smp->data.uint = cnt;
10132 smp->flags = SMP_F_VOL_HDR;
10133 return 1;
10134}
10135
10136/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10137 * Accepts an optional argument of type string containing the header field name,
10138 * and an optional argument of type signed or unsigned integer to request an
10139 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010140 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010141 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010142static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010143smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010144 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010145{
10146 struct http_txn *txn = l7;
10147 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010148 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010149 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010150 int occ = 0;
10151 const char *name_str = NULL;
10152 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010153
Willy Tarreaua890d072013-04-02 12:01:06 +020010154 if (!ctx) {
10155 /* first call */
10156 ctx = &static_hdr_ctx;
10157 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010158 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010159 }
10160
Willy Tarreau185b5c42012-04-26 15:11:51 +020010161 if (args) {
10162 if (args[0].type != ARGT_STR)
10163 return 0;
10164 name_str = args[0].data.str.str;
10165 name_len = args[0].data.str.len;
10166
10167 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10168 occ = args[1].data.uint;
10169 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010170
Willy Tarreaue333ec92012-04-16 16:26:40 +020010171 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010172
Willy Tarreau185b5c42012-04-26 15:11:51 +020010173 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010174 /* search for header from the beginning */
10175 ctx->idx = 0;
10176
Willy Tarreau185b5c42012-04-26 15:11:51 +020010177 if (!occ && !(opt & SMP_OPT_ITERATE))
10178 /* no explicit occurrence and single fetch => last header by default */
10179 occ = -1;
10180
10181 if (!occ)
10182 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010183 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010184
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010185 smp->type = SMP_T_STR;
10186 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010187 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 +020010188 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010189
Willy Tarreau37406352012-04-23 16:16:37 +020010190 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010191 return 0;
10192}
10193
Willy Tarreauc11416f2007-06-17 16:58:38 +020010194/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010195 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010196 */
10197static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010198smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010199 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010200{
10201 struct http_txn *txn = l7;
10202 struct hdr_idx *idx = &txn->hdr_idx;
10203 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010204 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010205 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010206 const char *name = NULL;
10207 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010208
Willy Tarreau29437342015-04-01 19:16:09 +020010209 if (args && args->type == ARGT_STR) {
10210 name = args->data.str.str;
10211 len = args->data.str.len;
10212 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010213
Willy Tarreaue333ec92012-04-16 16:26:40 +020010214 CHECK_HTTP_MESSAGE_FIRST();
10215
Willy Tarreau33a7e692007-06-10 19:45:56 +020010216 ctx.idx = 0;
10217 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010218 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010219 cnt++;
10220
Willy Tarreauf853c462012-04-23 18:53:56 +020010221 smp->type = SMP_T_UINT;
10222 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010223 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010224 return 1;
10225}
10226
Willy Tarreau185b5c42012-04-26 15:11:51 +020010227/* Fetch an HTTP header's integer value. The integer value is returned. It
10228 * takes a mandatory argument of type string and an optional one of type int
10229 * to designate a specific occurrence. It returns an unsigned integer, which
10230 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010231 */
10232static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010233smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010234 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010235{
Willy Tarreauef38c392013-07-22 16:29:32 +020010236 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010237
Willy Tarreauf853c462012-04-23 18:53:56 +020010238 if (ret > 0) {
10239 smp->type = SMP_T_UINT;
10240 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10241 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010242
Willy Tarreaud53e2422012-04-16 17:21:11 +020010243 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010244}
10245
Cyril Bonté69fa9922012-10-25 00:01:06 +020010246/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10247 * and an optional one of type int to designate a specific occurrence.
10248 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010249 */
10250static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010251smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010252 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +020010253{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010254 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010255
Willy Tarreauef38c392013-07-22 16:29:32 +020010256 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010257 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10258 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010259 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010260 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010261 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010262 if (smp->data.str.len < temp->size - 1) {
10263 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10264 temp->str[smp->data.str.len] = '\0';
10265 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10266 smp->type = SMP_T_IPV6;
10267 break;
10268 }
10269 }
10270 }
10271
Willy Tarreaud53e2422012-04-16 17:21:11 +020010272 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010273 if (!(smp->flags & SMP_F_NOT_LAST))
10274 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010275 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010276 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010277}
10278
Willy Tarreau737b0c12007-06-10 21:28:46 +020010279/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10280 * the first '/' after the possible hostname, and ends before the possible '?'.
10281 */
10282static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010283smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010284 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010285{
10286 struct http_txn *txn = l7;
10287 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010288
Willy Tarreauc0239e02012-04-16 14:42:55 +020010289 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010290
Willy Tarreau9b28e032012-10-12 23:49:43 +020010291 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010292 ptr = http_get_path(txn);
10293 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010294 return 0;
10295
10296 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010297 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010298 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010299
10300 while (ptr < end && *ptr != '?')
10301 ptr++;
10302
Willy Tarreauf853c462012-04-23 18:53:56 +020010303 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010304 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010305 return 1;
10306}
10307
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010308/* This produces a concatenation of the first occurrence of the Host header
10309 * followed by the path component if it begins with a slash ('/'). This means
10310 * that '*' will not be added, resulting in exactly the first Host entry.
10311 * If no Host header is found, then the path is returned as-is. The returned
10312 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010313 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010314 */
10315static int
10316smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010317 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010318{
10319 struct http_txn *txn = l7;
10320 char *ptr, *end, *beg;
10321 struct hdr_ctx ctx;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010322 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010323
10324 CHECK_HTTP_MESSAGE_FIRST();
10325
10326 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010327 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +020010328 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010329
10330 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010331 temp = get_trash_chunk();
10332 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010333 smp->type = SMP_T_STR;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010334 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010335 smp->data.str.len = ctx.vlen;
10336
10337 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010338 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010339 beg = http_get_path(txn);
10340 if (!beg)
10341 beg = end;
10342
10343 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10344
10345 if (beg < ptr && *beg == '/') {
10346 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10347 smp->data.str.len += ptr - beg;
10348 }
10349
10350 smp->flags = SMP_F_VOL_1ST;
10351 return 1;
10352}
10353
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010354/* This produces a 32-bit hash of the concatenation of the first occurrence of
10355 * the Host header followed by the path component if it begins with a slash ('/').
10356 * This means that '*' will not be added, resulting in exactly the first Host
10357 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010358 * is hashed using the path hash followed by a full avalanche hash and provides a
10359 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010360 * high-traffic sites without having to store whole paths.
10361 */
10362static int
10363smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010364 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010365{
10366 struct http_txn *txn = l7;
10367 struct hdr_ctx ctx;
10368 unsigned int hash = 0;
10369 char *ptr, *beg, *end;
10370 int len;
10371
10372 CHECK_HTTP_MESSAGE_FIRST();
10373
10374 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010375 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010376 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10377 ptr = ctx.line + ctx.val;
10378 len = ctx.vlen;
10379 while (len--)
10380 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10381 }
10382
10383 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010384 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010385 beg = http_get_path(txn);
10386 if (!beg)
10387 beg = end;
10388
10389 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10390
10391 if (beg < ptr && *beg == '/') {
10392 while (beg < ptr)
10393 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10394 }
10395 hash = full_hash(hash);
10396
10397 smp->type = SMP_T_UINT;
10398 smp->data.uint = hash;
10399 smp->flags = SMP_F_VOL_1ST;
10400 return 1;
10401}
10402
Willy Tarreau4a550602012-12-09 14:53:32 +010010403/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010404 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10405 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10406 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010407 * that in environments where IPv6 is insignificant, truncating the output to
10408 * 8 bytes would still work.
10409 */
10410static int
10411smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010412 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +010010413{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010414 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010415 struct connection *cli_conn = objt_conn(l4->si[0].end);
10416
10417 if (!cli_conn)
10418 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010419
Willy Tarreauef38c392013-07-22 16:29:32 +020010420 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +010010421 return 0;
10422
Willy Tarreau47ca5452012-12-23 20:22:19 +010010423 temp = get_trash_chunk();
Willy Tarreau0dff81c2014-07-15 21:34:06 +020010424 *(unsigned int *)temp->str = htonl(smp->data.uint);
10425 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010426
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010427 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010428 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010429 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010430 temp->len += 4;
10431 break;
10432 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010433 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010434 temp->len += 16;
10435 break;
10436 default:
10437 return 0;
10438 }
10439
10440 smp->data.str = *temp;
10441 smp->type = SMP_T_BIN;
10442 return 1;
10443}
10444
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010445static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010446smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010447 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010448{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010449 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10450 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10451 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010452
Willy Tarreau24e32d82012-04-23 23:55:44 +020010453 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010454
Willy Tarreauf853c462012-04-23 18:53:56 +020010455 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010456 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010457 return 1;
10458}
10459
Willy Tarreau7f18e522010-10-22 20:04:13 +020010460/* return a valid test if the current request is the first one on the connection */
10461static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010462smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010463 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010464{
10465 if (!s)
10466 return 0;
10467
Willy Tarreauf853c462012-04-23 18:53:56 +020010468 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010469 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010470 return 1;
10471}
10472
Willy Tarreau34db1082012-04-19 17:16:54 +020010473/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010474static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010475smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010476 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010477{
10478
Willy Tarreau24e32d82012-04-23 23:55:44 +020010479 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010480 return 0;
10481
Willy Tarreauc0239e02012-04-16 14:42:55 +020010482 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010483
Willy Tarreauc0239e02012-04-16 14:42:55 +020010484 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010485 return 0;
10486
Willy Tarreauf853c462012-04-23 18:53:56 +020010487 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010488 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010489 return 1;
10490}
Willy Tarreau8797c062007-05-07 00:55:35 +020010491
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010492/* Accepts exactly 1 argument of type userlist */
10493static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010494smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010495 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010496{
10497
10498 if (!args || args->type != ARGT_USR)
10499 return 0;
10500
10501 CHECK_HTTP_MESSAGE_FIRST();
10502
10503 if (!get_http_auth(l4))
10504 return 0;
10505
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010506 /* if the user does not belong to the userlist or has a wrong password,
10507 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010508 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010509 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010510 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10511 return 0;
10512
10513 /* pat_match_auth() will need the user list */
10514 smp->ctx.a[0] = args->data.usr;
10515
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010516 smp->type = SMP_T_STR;
10517 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010518 smp->data.str.str = l4->txn.auth.user;
10519 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010520
10521 return 1;
10522}
10523
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010524/* Try to find the next occurrence of a cookie name in a cookie header value.
10525 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10526 * the cookie value is returned into *value and *value_l, and the function
10527 * returns a pointer to the next pointer to search from if the value was found.
10528 * Otherwise if the cookie was not found, NULL is returned and neither value
10529 * nor value_l are touched. The input <hdr> string should first point to the
10530 * header's value, and the <hdr_end> pointer must point to the first character
10531 * not part of the value. <list> must be non-zero if value may represent a list
10532 * of values (cookie headers). This makes it faster to abort parsing when no
10533 * list is expected.
10534 */
10535static char *
10536extract_cookie_value(char *hdr, const char *hdr_end,
10537 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010538 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010539{
10540 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10541 char *next;
10542
10543 /* we search at least a cookie name followed by an equal, and more
10544 * generally something like this :
10545 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10546 */
10547 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10548 /* Iterate through all cookies on this line */
10549
10550 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10551 att_beg++;
10552
10553 /* find att_end : this is the first character after the last non
10554 * space before the equal. It may be equal to hdr_end.
10555 */
10556 equal = att_end = att_beg;
10557
10558 while (equal < hdr_end) {
10559 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10560 break;
10561 if (http_is_spht[(unsigned char)*equal++])
10562 continue;
10563 att_end = equal;
10564 }
10565
10566 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10567 * is between <att_beg> and <equal>, both may be identical.
10568 */
10569
10570 /* look for end of cookie if there is an equal sign */
10571 if (equal < hdr_end && *equal == '=') {
10572 /* look for the beginning of the value */
10573 val_beg = equal + 1;
10574 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10575 val_beg++;
10576
10577 /* find the end of the value, respecting quotes */
10578 next = find_cookie_value_end(val_beg, hdr_end);
10579
10580 /* make val_end point to the first white space or delimitor after the value */
10581 val_end = next;
10582 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10583 val_end--;
10584 } else {
10585 val_beg = val_end = next = equal;
10586 }
10587
10588 /* We have nothing to do with attributes beginning with '$'. However,
10589 * they will automatically be removed if a header before them is removed,
10590 * since they're supposed to be linked together.
10591 */
10592 if (*att_beg == '$')
10593 continue;
10594
10595 /* Ignore cookies with no equal sign */
10596 if (equal == next)
10597 continue;
10598
10599 /* Now we have the cookie name between att_beg and att_end, and
10600 * its value between val_beg and val_end.
10601 */
10602
10603 if (att_end - att_beg == cookie_name_l &&
10604 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10605 /* let's return this value and indicate where to go on from */
10606 *value = val_beg;
10607 *value_l = val_end - val_beg;
10608 return next + 1;
10609 }
10610
10611 /* Set-Cookie headers only have the name in the first attr=value part */
10612 if (!list)
10613 break;
10614 }
10615
10616 return NULL;
10617}
10618
William Lallemanda43ba4e2014-01-28 18:14:25 +010010619/* Fetch a captured HTTP request header. The index is the position of
10620 * the "capture" option in the configuration file
10621 */
10622static int
10623smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10624 const struct arg *args, struct sample *smp, const char *kw)
10625{
10626 struct proxy *fe = l4->fe;
10627 struct http_txn *txn = l7;
10628 int idx;
10629
10630 if (!args || args->type != ARGT_UINT)
10631 return 0;
10632
10633 idx = args->data.uint;
10634
10635 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10636 return 0;
10637
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010638 smp->type = SMP_T_STR;
10639 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010640 smp->data.str.str = txn->req.cap[idx];
10641 smp->data.str.len = strlen(txn->req.cap[idx]);
10642
10643 return 1;
10644}
10645
10646/* Fetch a captured HTTP response header. The index is the position of
10647 * the "capture" option in the configuration file
10648 */
10649static int
10650smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10651 const struct arg *args, struct sample *smp, const char *kw)
10652{
10653 struct proxy *fe = l4->fe;
10654 struct http_txn *txn = l7;
10655 int idx;
10656
10657 if (!args || args->type != ARGT_UINT)
10658 return 0;
10659
10660 idx = args->data.uint;
10661
10662 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10663 return 0;
10664
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010665 smp->type = SMP_T_STR;
10666 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010667 smp->data.str.str = txn->rsp.cap[idx];
10668 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10669
10670 return 1;
10671}
10672
William Lallemand65ad6e12014-01-31 15:08:02 +010010673/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10674static int
10675smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10676 const struct arg *args, struct sample *smp, const char *kw)
10677{
10678 struct chunk *temp;
10679 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010680 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010681
10682 if (!txn->uri)
10683 return 0;
10684
William Lallemand96a77852014-02-05 00:30:02 +010010685 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010686
William Lallemand96a77852014-02-05 00:30:02 +010010687 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10688 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010689
William Lallemand96a77852014-02-05 00:30:02 +010010690 temp = get_trash_chunk();
10691 temp->str = txn->uri;
10692 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010693 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010694 smp->type = SMP_T_STR;
10695 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010696
10697 return 1;
10698
10699}
10700
10701/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10702static int
10703smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10704 const struct arg *args, struct sample *smp, const char *kw)
10705{
10706 struct chunk *temp;
10707 struct http_txn *txn = l7;
10708 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010709
10710 if (!txn->uri)
10711 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010712
William Lallemand65ad6e12014-01-31 15:08:02 +010010713 ptr = txn->uri;
10714
10715 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10716 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010717
William Lallemand65ad6e12014-01-31 15:08:02 +010010718 if (!*ptr)
10719 return 0;
10720
10721 ptr++; /* skip the space */
10722
10723 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010724 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010725 if (!ptr)
10726 return 0;
10727 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10728 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010729
10730 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010731 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010732 smp->type = SMP_T_STR;
10733 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010734
10735 return 1;
10736}
10737
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010738/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10739 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10740 */
10741static int
10742smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10743 const struct arg *args, struct sample *smp, const char *kw)
10744{
10745 struct http_txn *txn = l7;
10746
10747 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10748 return 0;
10749
10750 if (txn->req.flags & HTTP_MSGF_VER_11)
10751 smp->data.str.str = "HTTP/1.1";
10752 else
10753 smp->data.str.str = "HTTP/1.0";
10754
10755 smp->data.str.len = 8;
10756 smp->type = SMP_T_STR;
10757 smp->flags = SMP_F_CONST;
10758 return 1;
10759
10760}
10761
10762/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10763 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10764 */
10765static int
10766smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10767 const struct arg *args, struct sample *smp, const char *kw)
10768{
10769 struct http_txn *txn = l7;
10770
10771 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10772 return 0;
10773
10774 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10775 smp->data.str.str = "HTTP/1.1";
10776 else
10777 smp->data.str.str = "HTTP/1.0";
10778
10779 smp->data.str.len = 8;
10780 smp->type = SMP_T_STR;
10781 smp->flags = SMP_F_CONST;
10782 return 1;
10783
10784}
10785
William Lallemand65ad6e12014-01-31 15:08:02 +010010786
Willy Tarreaue333ec92012-04-16 16:26:40 +020010787/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010788 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010789 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010790 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010791 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010792 * Accepts exactly 1 argument of type string. If the input options indicate
10793 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010794 * The returned sample is of type CSTR. Can be used to parse cookies in other
10795 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010796 */
William Lallemand07c8b242014-05-02 17:11:07 +020010797int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010798 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010799{
10800 struct http_txn *txn = l7;
10801 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010802 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010803 const struct http_msg *msg;
10804 const char *hdr_name;
10805 int hdr_name_len;
10806 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010807 int occ = 0;
10808 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010809
Willy Tarreau24e32d82012-04-23 23:55:44 +020010810 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010811 return 0;
10812
Willy Tarreaua890d072013-04-02 12:01:06 +020010813 if (!ctx) {
10814 /* first call */
10815 ctx = &static_hdr_ctx;
10816 ctx->idx = 0;
10817 smp->ctx.a[2] = ctx;
10818 }
10819
Willy Tarreaue333ec92012-04-16 16:26:40 +020010820 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010821
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010822 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010823 msg = &txn->req;
10824 hdr_name = "Cookie";
10825 hdr_name_len = 6;
10826 } else {
10827 msg = &txn->rsp;
10828 hdr_name = "Set-Cookie";
10829 hdr_name_len = 10;
10830 }
10831
Willy Tarreau28376d62012-04-26 21:26:10 +020010832 if (!occ && !(opt & SMP_OPT_ITERATE))
10833 /* no explicit occurrence and single fetch => last cookie by default */
10834 occ = -1;
10835
10836 /* OK so basically here, either we want only one value and it's the
10837 * last one, or we want to iterate over all of them and we fetch the
10838 * next one.
10839 */
10840
Willy Tarreau9b28e032012-10-12 23:49:43 +020010841 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010842 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010843 /* search for the header from the beginning, we must first initialize
10844 * the search parameters.
10845 */
Willy Tarreau37406352012-04-23 16:16:37 +020010846 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010847 ctx->idx = 0;
10848 }
10849
Willy Tarreau28376d62012-04-26 21:26:10 +020010850 smp->flags |= SMP_F_VOL_HDR;
10851
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010852 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010853 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10854 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010855 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10856 goto out;
10857
Willy Tarreau24e32d82012-04-23 23:55:44 +020010858 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010859 continue;
10860
Willy Tarreau37406352012-04-23 16:16:37 +020010861 smp->ctx.a[0] = ctx->line + ctx->val;
10862 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010863 }
10864
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010865 smp->type = SMP_T_STR;
10866 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010867 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010868 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010869 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010870 &smp->data.str.str,
10871 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010872 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010873 found = 1;
10874 if (occ >= 0) {
10875 /* one value was returned into smp->data.str.{str,len} */
10876 smp->flags |= SMP_F_NOT_LAST;
10877 return 1;
10878 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010879 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010880 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010881 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010882 /* all cookie headers and values were scanned. If we're looking for the
10883 * last occurrence, we may return it now.
10884 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010885 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010886 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010887 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010888}
10889
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010890/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010891 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010892 * multiple cookies may be parsed on the same line. The returned sample is of
10893 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010894 */
10895static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010896smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010897 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010898{
10899 struct http_txn *txn = l7;
10900 struct hdr_idx *idx = &txn->hdr_idx;
10901 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010902 const struct http_msg *msg;
10903 const char *hdr_name;
10904 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010905 int cnt;
10906 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010907 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010908
Willy Tarreau24e32d82012-04-23 23:55:44 +020010909 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010910 return 0;
10911
Willy Tarreaue333ec92012-04-16 16:26:40 +020010912 CHECK_HTTP_MESSAGE_FIRST();
10913
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010914 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010915 msg = &txn->req;
10916 hdr_name = "Cookie";
10917 hdr_name_len = 6;
10918 } else {
10919 msg = &txn->rsp;
10920 hdr_name = "Set-Cookie";
10921 hdr_name_len = 10;
10922 }
10923
Willy Tarreau9b28e032012-10-12 23:49:43 +020010924 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010925 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010926 ctx.idx = 0;
10927 cnt = 0;
10928
10929 while (1) {
10930 /* Note: val_beg == NULL every time we need to fetch a new header */
10931 if (!val_beg) {
10932 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10933 break;
10934
Willy Tarreau24e32d82012-04-23 23:55:44 +020010935 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010936 continue;
10937
10938 val_beg = ctx.line + ctx.val;
10939 val_end = val_beg + ctx.vlen;
10940 }
10941
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010942 smp->type = SMP_T_STR;
10943 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010944 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010945 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010946 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010947 &smp->data.str.str,
10948 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010949 cnt++;
10950 }
10951 }
10952
Willy Tarreaub169eba2013-12-16 15:14:43 +010010953 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010954 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010955 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010956 return 1;
10957}
10958
Willy Tarreau51539362012-05-08 12:46:28 +020010959/* Fetch an cookie's integer value. The integer value is returned. It
10960 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10961 */
10962static int
10963smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010964 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010965{
Willy Tarreauef38c392013-07-22 16:29:32 +020010966 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010967
10968 if (ret > 0) {
10969 smp->type = SMP_T_UINT;
10970 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10971 }
10972
10973 return ret;
10974}
10975
Willy Tarreau8797c062007-05-07 00:55:35 +020010976/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010977/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010978/************************************************************************/
10979
David Cournapeau16023ee2010-12-23 20:55:41 +090010980/*
10981 * Given a path string and its length, find the position of beginning of the
10982 * query string. Returns NULL if no query string is found in the path.
10983 *
10984 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10985 *
10986 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10987 */
bedis4c75cca2012-10-05 08:38:24 +020010988static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010989{
10990 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010991
bedis4c75cca2012-10-05 08:38:24 +020010992 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010993 return p ? p + 1 : NULL;
10994}
10995
bedis4c75cca2012-10-05 08:38:24 +020010996static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010997{
bedis4c75cca2012-10-05 08:38:24 +020010998 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010999}
11000
11001/*
11002 * Given a url parameter, find the starting position of the first occurence,
11003 * or NULL if the parameter is not found.
11004 *
11005 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11006 * the function will return query_string+8.
11007 */
11008static char*
11009find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011010 char* url_param_name, size_t url_param_name_l,
11011 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011012{
11013 char *pos, *last;
11014
11015 pos = query_string;
11016 last = query_string + query_string_l - url_param_name_l - 1;
11017
11018 while (pos <= last) {
11019 if (pos[url_param_name_l] == '=') {
11020 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11021 return pos;
11022 pos += url_param_name_l + 1;
11023 }
bedis4c75cca2012-10-05 08:38:24 +020011024 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011025 pos++;
11026 pos++;
11027 }
11028 return NULL;
11029}
11030
11031/*
11032 * Given a url parameter name, returns its value and size into *value and
11033 * *value_l respectively, and returns non-zero. If the parameter is not found,
11034 * zero is returned and value/value_l are not touched.
11035 */
11036static int
11037find_url_param_value(char* path, size_t path_l,
11038 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011039 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011040{
11041 char *query_string, *qs_end;
11042 char *arg_start;
11043 char *value_start, *value_end;
11044
bedis4c75cca2012-10-05 08:38:24 +020011045 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011046 if (!query_string)
11047 return 0;
11048
11049 qs_end = path + path_l;
11050 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011051 url_param_name, url_param_name_l,
11052 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011053 if (!arg_start)
11054 return 0;
11055
11056 value_start = arg_start + url_param_name_l + 1;
11057 value_end = value_start;
11058
bedis4c75cca2012-10-05 08:38:24 +020011059 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011060 value_end++;
11061
11062 *value = value_start;
11063 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011064 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011065}
11066
11067static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011068smp_fetch_url_param(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)
David Cournapeau16023ee2010-12-23 20:55:41 +090011070{
bedis4c75cca2012-10-05 08:38:24 +020011071 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090011072 struct http_txn *txn = l7;
11073 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011074
bedis4c75cca2012-10-05 08:38:24 +020011075 if (!args || args[0].type != ARGT_STR ||
11076 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011077 return 0;
11078
11079 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011080
bedis4c75cca2012-10-05 08:38:24 +020011081 if (args[1].type)
11082 delim = *args[1].data.str.str;
11083
Willy Tarreau9b28e032012-10-12 23:49:43 +020011084 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011085 args->data.str.str, args->data.str.len,
11086 &smp->data.str.str, &smp->data.str.len,
11087 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011088 return 0;
11089
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011090 smp->type = SMP_T_STR;
11091 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011092 return 1;
11093}
11094
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011095/* Return the signed integer value for the specified url parameter (see url_param
11096 * above).
11097 */
11098static int
11099smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011100 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011101{
Willy Tarreauef38c392013-07-22 16:29:32 +020011102 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011103
11104 if (ret > 0) {
11105 smp->type = SMP_T_UINT;
11106 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11107 }
11108
11109 return ret;
11110}
11111
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011112/* This produces a 32-bit hash of the concatenation of the first occurrence of
11113 * the Host header followed by the path component if it begins with a slash ('/').
11114 * This means that '*' will not be added, resulting in exactly the first Host
11115 * entry. If no Host header is found, then the path is used. The resulting value
11116 * is hashed using the url hash followed by a full avalanche hash and provides a
11117 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11118 * high-traffic sites without having to store whole paths.
11119 * this differs from the base32 functions in that it includes the url parameters
11120 * as well as the path
11121 */
11122static int
11123smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011124 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011125{
11126 struct http_txn *txn = l7;
11127 struct hdr_ctx ctx;
11128 unsigned int hash = 0;
11129 char *ptr, *beg, *end;
11130 int len;
11131
11132 CHECK_HTTP_MESSAGE_FIRST();
11133
11134 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011135 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011136 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11137 ptr = ctx.line + ctx.val;
11138 len = ctx.vlen;
11139 while (len--)
11140 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11141 }
11142
11143 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011144 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 +000011145 beg = http_get_path(txn);
11146 if (!beg)
11147 beg = end;
11148
11149 for (ptr = beg; ptr < end ; ptr++);
11150
11151 if (beg < ptr && *beg == '/') {
11152 while (beg < ptr)
11153 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11154 }
11155 hash = full_hash(hash);
11156
11157 smp->type = SMP_T_UINT;
11158 smp->data.uint = hash;
11159 smp->flags = SMP_F_VOL_1ST;
11160 return 1;
11161}
11162
11163/* This concatenates the source address with the 32-bit hash of the Host and
11164 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11165 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11166 * on the source address length. The URL hash is stored before the address so
11167 * that in environments where IPv6 is insignificant, truncating the output to
11168 * 8 bytes would still work.
11169 */
11170static int
11171smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011172 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011173{
11174 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011175 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011176
Willy Tarreaue155ec22013-11-18 18:33:22 +010011177 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011178 return 0;
11179
11180 temp = get_trash_chunk();
11181 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11182 temp->len += sizeof(smp->data.uint);
11183
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011184 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011185 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011186 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011187 temp->len += 4;
11188 break;
11189 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011190 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011191 temp->len += 16;
11192 break;
11193 default:
11194 return 0;
11195 }
11196
11197 smp->data.str = *temp;
11198 smp->type = SMP_T_BIN;
11199 return 1;
11200}
11201
Willy Tarreau185b5c42012-04-26 15:11:51 +020011202/* This function is used to validate the arguments passed to any "hdr" fetch
11203 * keyword. These keywords support an optional positive or negative occurrence
11204 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11205 * is assumed that the types are already the correct ones. Returns 0 on error,
11206 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11207 * error message in case of error, that the caller is responsible for freeing.
11208 * The initial location must either be freeable or NULL.
11209 */
11210static int val_hdr(struct arg *arg, char **err_msg)
11211{
11212 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011213 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011214 return 0;
11215 }
11216 return 1;
11217}
11218
Willy Tarreau276fae92013-07-25 14:36:01 +020011219/* takes an UINT value on input supposed to represent the time since EPOCH,
11220 * adds an optional offset found in args[0] and emits a string representing
11221 * the date in RFC-1123/5322 format.
11222 */
11223static int sample_conv_http_date(const struct arg *args, struct sample *smp)
11224{
11225 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11226 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11227 struct chunk *temp;
11228 struct tm *tm;
11229 time_t curr_date = smp->data.uint;
11230
11231 /* add offset */
11232 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11233 curr_date += args[0].data.sint;
11234
11235 tm = gmtime(&curr_date);
11236
11237 temp = get_trash_chunk();
11238 temp->len = snprintf(temp->str, temp->size - temp->len,
11239 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11240 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11241 tm->tm_hour, tm->tm_min, tm->tm_sec);
11242
11243 smp->data.str = *temp;
11244 smp->type = SMP_T_STR;
11245 return 1;
11246}
11247
Thierry FOURNIERad903512014-04-11 17:51:01 +020011248/* Match language range with language tag. RFC2616 14.4:
11249 *
11250 * A language-range matches a language-tag if it exactly equals
11251 * the tag, or if it exactly equals a prefix of the tag such
11252 * that the first tag character following the prefix is "-".
11253 *
11254 * Return 1 if the strings match, else return 0.
11255 */
11256static inline int language_range_match(const char *range, int range_len,
11257 const char *tag, int tag_len)
11258{
11259 const char *end = range + range_len;
11260 const char *tend = tag + tag_len;
11261 while (range < end) {
11262 if (*range == '-' && tag == tend)
11263 return 1;
11264 if (*range != *tag || tag == tend)
11265 return 0;
11266 range++;
11267 tag++;
11268 }
11269 /* Return true only if the last char of the tag is matched. */
11270 return tag == tend;
11271}
11272
11273/* Arguments: The list of expected value, the number of parts returned and the separator */
11274static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
11275{
11276 const char *al = smp->data.str.str;
11277 const char *end = al + smp->data.str.len;
11278 const char *token;
11279 int toklen;
11280 int qvalue;
11281 const char *str;
11282 const char *w;
11283 int best_q = 0;
11284
11285 /* Set the constant to the sample, because the output of the
11286 * function will be peek in the constant configuration string.
11287 */
11288 smp->flags |= SMP_F_CONST;
11289 smp->data.str.size = 0;
11290 smp->data.str.str = "";
11291 smp->data.str.len = 0;
11292
11293 /* Parse the accept language */
11294 while (1) {
11295
11296 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011297 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011298 al++;
11299 if (al >= end)
11300 break;
11301
11302 /* Start of the fisrt word. */
11303 token = al;
11304
11305 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011306 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011307 al++;
11308 if (al == token)
11309 goto expect_comma;
11310
11311 /* Length of the token. */
11312 toklen = al - token;
11313 qvalue = 1000;
11314
11315 /* Check if the token exists in the list. If the token not exists,
11316 * jump to the next token.
11317 */
11318 str = args[0].data.str.str;
11319 w = str;
11320 while (1) {
11321 if (*str == ';' || *str == '\0') {
11322 if (language_range_match(token, toklen, w, str-w))
11323 goto look_for_q;
11324 if (*str == '\0')
11325 goto expect_comma;
11326 w = str + 1;
11327 }
11328 str++;
11329 }
11330 goto expect_comma;
11331
11332look_for_q:
11333
11334 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011335 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011336 al++;
11337 if (al >= end)
11338 goto process_value;
11339
11340 /* If ',' is found, process the result */
11341 if (*al == ',')
11342 goto process_value;
11343
11344 /* If the character is different from ';', look
11345 * for the end of the header part in best effort.
11346 */
11347 if (*al != ';')
11348 goto expect_comma;
11349
11350 /* Assumes that the char is ';', now expect "q=". */
11351 al++;
11352
11353 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011354 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011355 al++;
11356 if (al >= end)
11357 goto process_value;
11358
11359 /* Expect 'q'. If no 'q', continue in best effort */
11360 if (*al != 'q')
11361 goto process_value;
11362 al++;
11363
11364 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011365 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011366 al++;
11367 if (al >= end)
11368 goto process_value;
11369
11370 /* Expect '='. If no '=', continue in best effort */
11371 if (*al != '=')
11372 goto process_value;
11373 al++;
11374
11375 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011376 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011377 al++;
11378 if (al >= end)
11379 goto process_value;
11380
11381 /* Parse the q value. */
11382 qvalue = parse_qvalue(al, &al);
11383
11384process_value:
11385
11386 /* If the new q value is the best q value, then store the associated
11387 * language in the response. If qvalue is the biggest value (1000),
11388 * break the process.
11389 */
11390 if (qvalue > best_q) {
11391 smp->data.str.str = (char *)w;
11392 smp->data.str.len = str - w;
11393 if (qvalue >= 1000)
11394 break;
11395 best_q = qvalue;
11396 }
11397
11398expect_comma:
11399
11400 /* Expect comma or end. If the end is detected, quit the loop. */
11401 while (al < end && *al != ',')
11402 al++;
11403 if (al >= end)
11404 break;
11405
11406 /* Comma is found, jump it and restart the analyzer. */
11407 al++;
11408 }
11409
11410 /* Set default value if required. */
11411 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11412 smp->data.str.str = args[1].data.str.str;
11413 smp->data.str.len = args[1].data.str.len;
11414 }
11415
11416 /* Return true only if a matching language was found. */
11417 return smp->data.str.len != 0;
11418}
11419
William Lallemand73025dd2014-04-24 14:38:37 +020011420/*
11421 * Return the struct http_req_action_kw associated to a keyword.
11422 */
11423struct http_req_action_kw *action_http_req_custom(const char *kw)
11424{
11425 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11426 struct http_req_action_kw_list *kw_list;
11427 int i;
11428
11429 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11430 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11431 if (!strcmp(kw, kw_list->kw[i].kw))
11432 return &kw_list->kw[i];
11433 }
11434 }
11435 }
11436 return NULL;
11437}
11438
11439/*
11440 * Return the struct http_res_action_kw associated to a keyword.
11441 */
11442struct http_res_action_kw *action_http_res_custom(const char *kw)
11443{
11444 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11445 struct http_res_action_kw_list *kw_list;
11446 int i;
11447
11448 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11449 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11450 if (!strcmp(kw, kw_list->kw[i].kw))
11451 return &kw_list->kw[i];
11452 }
11453 }
11454 }
11455 return NULL;
11456}
11457
Willy Tarreau4a568972010-05-12 08:08:50 +020011458/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011459/* All supported ACL keywords must be declared here. */
11460/************************************************************************/
11461
11462/* Note: must not be declared <const> as its list will be overwritten.
11463 * Please take care of keeping this list alphabetically sorted.
11464 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011465static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011466 { "base", "base", PAT_MATCH_STR },
11467 { "base_beg", "base", PAT_MATCH_BEG },
11468 { "base_dir", "base", PAT_MATCH_DIR },
11469 { "base_dom", "base", PAT_MATCH_DOM },
11470 { "base_end", "base", PAT_MATCH_END },
11471 { "base_len", "base", PAT_MATCH_LEN },
11472 { "base_reg", "base", PAT_MATCH_REG },
11473 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011474
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011475 { "cook", "req.cook", PAT_MATCH_STR },
11476 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11477 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11478 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11479 { "cook_end", "req.cook", PAT_MATCH_END },
11480 { "cook_len", "req.cook", PAT_MATCH_LEN },
11481 { "cook_reg", "req.cook", PAT_MATCH_REG },
11482 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011483
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011484 { "hdr", "req.hdr", PAT_MATCH_STR },
11485 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11486 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11487 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11488 { "hdr_end", "req.hdr", PAT_MATCH_END },
11489 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11490 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11491 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011492
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011493 /* these two declarations uses strings with list storage (in place
11494 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11495 * and delete functions are relative to the list management. The parse
11496 * and match method are related to the corresponding fetch methods. This
11497 * is very particular ACL declaration mode.
11498 */
11499 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11500 { "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 +020011501
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011502 { "path", "path", PAT_MATCH_STR },
11503 { "path_beg", "path", PAT_MATCH_BEG },
11504 { "path_dir", "path", PAT_MATCH_DIR },
11505 { "path_dom", "path", PAT_MATCH_DOM },
11506 { "path_end", "path", PAT_MATCH_END },
11507 { "path_len", "path", PAT_MATCH_LEN },
11508 { "path_reg", "path", PAT_MATCH_REG },
11509 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011510
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011511 { "req_ver", "req.ver", PAT_MATCH_STR },
11512 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011513
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011514 { "scook", "res.cook", PAT_MATCH_STR },
11515 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11516 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11517 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11518 { "scook_end", "res.cook", PAT_MATCH_END },
11519 { "scook_len", "res.cook", PAT_MATCH_LEN },
11520 { "scook_reg", "res.cook", PAT_MATCH_REG },
11521 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011522
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011523 { "shdr", "res.hdr", PAT_MATCH_STR },
11524 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11525 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11526 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11527 { "shdr_end", "res.hdr", PAT_MATCH_END },
11528 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11529 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11530 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011531
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011532 { "url", "url", PAT_MATCH_STR },
11533 { "url_beg", "url", PAT_MATCH_BEG },
11534 { "url_dir", "url", PAT_MATCH_DIR },
11535 { "url_dom", "url", PAT_MATCH_DOM },
11536 { "url_end", "url", PAT_MATCH_END },
11537 { "url_len", "url", PAT_MATCH_LEN },
11538 { "url_reg", "url", PAT_MATCH_REG },
11539 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011540
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011541 { "urlp", "urlp", PAT_MATCH_STR },
11542 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11543 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11544 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11545 { "urlp_end", "urlp", PAT_MATCH_END },
11546 { "urlp_len", "urlp", PAT_MATCH_LEN },
11547 { "urlp_reg", "urlp", PAT_MATCH_REG },
11548 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011549
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011550 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011551}};
11552
11553/************************************************************************/
11554/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011555/************************************************************************/
11556/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011557static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011558 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011559 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11560 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11561
William Lallemanda43ba4e2014-01-28 18:14:25 +010011562 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011563 { "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 +020011564
11565 /* retrieve these captures from the HTTP logs */
11566 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11567 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11568 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11569
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011570 { "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 +020011571 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011572
Willy Tarreau409bcde2013-01-08 00:31:00 +010011573 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11574 * are only here to match the ACL's name, are request-only and are used
11575 * for ACL compatibility only.
11576 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011577 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11578 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011579 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11580 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11581
11582 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11583 * only here to match the ACL's name, are request-only and are used for
11584 * ACL compatibility only.
11585 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011586 { "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 +010011587 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11588 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11589 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11590
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011591 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011592 { "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 +010011593 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011594 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011595 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011596
11597 /* HTTP protocol on the request path */
11598 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011599 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011600
11601 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011602 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11603 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011604
11605 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011606 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11607 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011608
Willy Tarreau18ed2562013-01-14 15:56:36 +010011609 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011610 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011611 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11612 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11613
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011614 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011615 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011616 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011617 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11618 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11619 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11620
11621 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011622 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011623 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11624 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11625
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011626 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011627 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011628 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011629 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11630 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11631 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11632
Willy Tarreau409bcde2013-01-08 00:31:00 +010011633 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011634 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011635 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11636 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011637 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011638
11639 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011640 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011641 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11642 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11643 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11644
11645 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011646 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011647 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11648 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011649 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11650 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011651 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11652 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011653 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11654 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011655}};
11656
Willy Tarreau8797c062007-05-07 00:55:35 +020011657
Willy Tarreau276fae92013-07-25 14:36:01 +020011658/* Note: must not be declared <const> as its list will be overwritten */
11659static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011660 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11661 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011662 { NULL, NULL, 0, 0, 0 },
11663}};
11664
Willy Tarreau8797c062007-05-07 00:55:35 +020011665__attribute__((constructor))
11666static void __http_protocol_init(void)
11667{
11668 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011669 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011670 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020011671}
11672
11673
Willy Tarreau58f10d72006-12-04 02:26:12 +010011674/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011675 * Local variables:
11676 * c-indent-level: 8
11677 * c-basic-offset: 8
11678 * End:
11679 */