blob: 082a62b3537102a7c8a280355284179a56f2e70c [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreau522d6c02009-12-06 18:49:18 +010069const char HTTP_100[] =
70 "HTTP/1.1 100 Continue\r\n\r\n";
71
72const struct chunk http_100_chunk = {
73 .str = (char *)&HTTP_100,
74 .len = sizeof(HTTP_100)-1
75};
76
Willy Tarreaua9679ac2010-01-03 17:32:57 +010077/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020081 "Location: "; /* not terminated since it will be concatenated with the URL */
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
89/* same as 302 except that the browser MUST retry with the GET method */
90const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Location: "; /* not terminated since it will be concatenated with the URL */
95
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040096
97/* same as 302 except that the browser MUST retry with the same method */
98const char *HTTP_307 =
99 "HTTP/1.1 307 Temporary Redirect\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Content-length: 0\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
104/* same as 301 except that the browser MUST retry with the same method */
105const char *HTTP_308 =
106 "HTTP/1.1 308 Permanent Redirect\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
111const char *HTTP_401_fmt =
112 "HTTP/1.0 401 Unauthorized\r\n"
113 "Cache-Control: no-cache\r\n"
114 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200115 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
117 "\r\n"
118 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
119
Willy Tarreau844a7e72010-01-31 21:46:18 +0100120const char *HTTP_407_fmt =
121 "HTTP/1.0 407 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
124 "Content-Type: text/html\r\n"
125 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
Godbachb3016542014-12-17 16:32:05 +0800127 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100128
Willy Tarreau0f772532006-12-23 20:51:41 +0100129
130const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200131 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100132 [HTTP_ERR_400] = 400,
133 [HTTP_ERR_403] = 403,
134 [HTTP_ERR_408] = 408,
135 [HTTP_ERR_500] = 500,
136 [HTTP_ERR_502] = 502,
137 [HTTP_ERR_503] = 503,
138 [HTTP_ERR_504] = 504,
139};
140
Willy Tarreau80587432006-12-24 17:47:20 +0100141static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200142 [HTTP_ERR_200] =
143 "HTTP/1.0 200 OK\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
149
Willy Tarreau0f772532006-12-23 20:51:41 +0100150 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100151 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
157
158 [HTTP_ERR_403] =
159 "HTTP/1.0 403 Forbidden\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
165
166 [HTTP_ERR_408] =
167 "HTTP/1.0 408 Request Time-out\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
173
174 [HTTP_ERR_500] =
175 "HTTP/1.0 500 Server Error\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
181
182 [HTTP_ERR_502] =
183 "HTTP/1.0 502 Bad Gateway\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
189
190 [HTTP_ERR_503] =
191 "HTTP/1.0 503 Service Unavailable\r\n"
192 "Cache-Control: no-cache\r\n"
193 "Connection: close\r\n"
194 "Content-Type: text/html\r\n"
195 "\r\n"
196 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
197
198 [HTTP_ERR_504] =
199 "HTTP/1.0 504 Gateway Time-out\r\n"
200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
204 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
205
206};
207
Cyril Bonté19979e12012-04-04 12:57:21 +0200208/* status codes available for the stats admin page (strictly 4 chars length) */
209const char *stat_status_codes[STAT_STATUS_SIZE] = {
210 [STAT_STATUS_DENY] = "DENY",
211 [STAT_STATUS_DONE] = "DONE",
212 [STAT_STATUS_ERRP] = "ERRP",
213 [STAT_STATUS_EXCD] = "EXCD",
214 [STAT_STATUS_NONE] = "NONE",
215 [STAT_STATUS_PART] = "PART",
216 [STAT_STATUS_UNKN] = "UNKN",
217};
218
219
William Lallemand73025dd2014-04-24 14:38:37 +0200220/* List head of all known action keywords for "http-request" */
221struct http_req_action_kw_list http_req_keywords = {
222 .list = LIST_HEAD_INIT(http_req_keywords.list)
223};
224
225/* List head of all known action keywords for "http-response" */
226struct http_res_action_kw_list http_res_keywords = {
227 .list = LIST_HEAD_INIT(http_res_keywords.list)
228};
229
Willy Tarreau80587432006-12-24 17:47:20 +0100230/* We must put the messages here since GCC cannot initialize consts depending
231 * on strlen().
232 */
233struct chunk http_err_chunks[HTTP_ERR_SIZE];
234
Willy Tarreaua890d072013-04-02 12:01:06 +0200235/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
236static struct hdr_ctx static_hdr_ctx;
237
Willy Tarreau42250582007-04-01 01:30:43 +0200238#define FD_SETS_ARE_BITFIELDS
239#ifdef FD_SETS_ARE_BITFIELDS
240/*
241 * This map is used with all the FD_* macros to check whether a particular bit
242 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
243 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
244 * byte should be encoded. Be careful to always pass bytes from 0 to 255
245 * exclusively to the macros.
246 */
247fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
248fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100249fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200250
251#else
252#error "Check if your OS uses bitfields for fd_sets"
253#endif
254
Willy Tarreau0b748332014-04-29 00:13:29 +0200255static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn);
256
Willy Tarreau80587432006-12-24 17:47:20 +0100257void init_proto_http()
258{
Willy Tarreau42250582007-04-01 01:30:43 +0200259 int i;
260 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100261 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200262
Willy Tarreau80587432006-12-24 17:47:20 +0100263 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
264 if (!http_err_msgs[msg]) {
265 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
266 abort();
267 }
268
269 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
270 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
271 }
Willy Tarreau42250582007-04-01 01:30:43 +0200272
273 /* initialize the log header encoding map : '{|}"#' should be encoded with
274 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
275 * URL encoding only requires '"', '#' to be encoded as well as non-
276 * printable characters above.
277 */
278 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
279 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100280 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200281 for (i = 0; i < 32; i++) {
282 FD_SET(i, hdr_encode_map);
283 FD_SET(i, url_encode_map);
284 }
285 for (i = 127; i < 256; i++) {
286 FD_SET(i, hdr_encode_map);
287 FD_SET(i, url_encode_map);
288 }
289
290 tmp = "\"#{|}";
291 while (*tmp) {
292 FD_SET(*tmp, hdr_encode_map);
293 tmp++;
294 }
295
296 tmp = "\"#";
297 while (*tmp) {
298 FD_SET(*tmp, url_encode_map);
299 tmp++;
300 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200301
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100302 /* initialize the http header encoding map. The draft httpbis define the
303 * header content as:
304 *
305 * HTTP-message = start-line
306 * *( header-field CRLF )
307 * CRLF
308 * [ message-body ]
309 * header-field = field-name ":" OWS field-value OWS
310 * field-value = *( field-content / obs-fold )
311 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
312 * obs-fold = CRLF 1*( SP / HTAB )
313 * field-vchar = VCHAR / obs-text
314 * VCHAR = %x21-7E
315 * obs-text = %x80-FF
316 *
317 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
318 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
319 * "obs-fold" is volontary forgotten because haproxy remove this.
320 */
321 memset(http_encode_map, 0, sizeof(http_encode_map));
322 for (i = 0x00; i <= 0x08; i++)
323 FD_SET(i, http_encode_map);
324 for (i = 0x0a; i <= 0x1f; i++)
325 FD_SET(i, http_encode_map);
326 FD_SET(0x7f, http_encode_map);
327
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200328 /* memory allocations */
329 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100330 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100331}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332
Willy Tarreau53b6c742006-12-17 13:37:46 +0100333/*
334 * We have 26 list of methods (1 per first letter), each of which can have
335 * up to 3 entries (2 valid, 1 null).
336 */
337struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100338 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100339 int len;
340 const char text[8];
341};
342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100343const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100344 ['C' - 'A'] = {
345 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
346 },
347 ['D' - 'A'] = {
348 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
349 },
350 ['G' - 'A'] = {
351 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
352 },
353 ['H' - 'A'] = {
354 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
355 },
356 ['P' - 'A'] = {
357 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
358 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
359 },
360 ['T' - 'A'] = {
361 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
362 },
363 /* rest is empty like this :
364 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
365 */
366};
367
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100368const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
369 [HTTP_METH_NONE] = { "", 0 },
370 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
371 [HTTP_METH_GET] = { "GET", 3 },
372 [HTTP_METH_HEAD] = { "HEAD", 4 },
373 [HTTP_METH_POST] = { "POST", 4 },
374 [HTTP_METH_PUT] = { "PUT", 3 },
375 [HTTP_METH_DELETE] = { "DELETE", 6 },
376 [HTTP_METH_TRACE] = { "TRACE", 5 },
377 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
378};
379
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100380/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200381 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100382 * RFC2616 for those chars.
383 */
384
385const char http_is_spht[256] = {
386 [' '] = 1, ['\t'] = 1,
387};
388
389const char http_is_crlf[256] = {
390 ['\r'] = 1, ['\n'] = 1,
391};
392
393const char http_is_lws[256] = {
394 [' '] = 1, ['\t'] = 1,
395 ['\r'] = 1, ['\n'] = 1,
396};
397
398const char http_is_sep[256] = {
399 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
400 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
401 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
402 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
403 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
404};
405
406const char http_is_ctl[256] = {
407 [0 ... 31] = 1,
408 [127] = 1,
409};
410
411/*
412 * A token is any ASCII char that is neither a separator nor a CTL char.
413 * Do not overwrite values in assignment since gcc-2.95 will not handle
414 * them correctly. Instead, define every non-CTL char's status.
415 */
416const char http_is_token[256] = {
417 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
418 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
419 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
420 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
421 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
422 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
423 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
424 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
425 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
426 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
427 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
428 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
429 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
430 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
431 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
432 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
433 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
434 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
435 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
436 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
437 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
438 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
439 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
440 ['|'] = 1, ['}'] = 0, ['~'] = 1,
441};
442
443
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100444/*
445 * An http ver_token is any ASCII which can be found in an HTTP version,
446 * which includes 'H', 'T', 'P', '/', '.' and any digit.
447 */
448const char http_is_ver_token[256] = {
449 ['.'] = 1, ['/'] = 1,
450 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
451 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
452 ['H'] = 1, ['P'] = 1, ['T'] = 1,
453};
454
455
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100456/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100457 * Adds a header and its CRLF at the tail of the message's buffer, just before
458 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100459 * The header is also automatically added to the index <hdr_idx>, and the end
460 * of headers is automatically adjusted. The number of bytes added is returned
461 * on success, otherwise <0 is returned indicating an error.
462 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100463int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100464{
465 int bytes, len;
466
467 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200468 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100469 if (!bytes)
470 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100471 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100472 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
473}
474
475/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100476 * Adds a header and its CRLF at the tail of the message's buffer, just before
477 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100478 * the buffer is only opened and the space reserved, but nothing is copied.
479 * The header is also automatically added to the index <hdr_idx>, and the end
480 * of headers is automatically adjusted. The number of bytes added is returned
481 * on success, otherwise <0 is returned indicating an error.
482 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100483int http_header_add_tail2(struct http_msg *msg,
484 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100485{
486 int bytes;
487
Willy Tarreau9b28e032012-10-12 23:49:43 +0200488 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100489 if (!bytes)
490 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100491 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100492 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
493}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200494
495/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100496 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
497 * If so, returns the position of the first non-space character relative to
498 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
499 * to return a pointer to the place after the first space. Returns 0 if the
500 * header name does not match. Checks are case-insensitive.
501 */
502int http_header_match2(const char *hdr, const char *end,
503 const char *name, int len)
504{
505 const char *val;
506
507 if (hdr + len >= end)
508 return 0;
509 if (hdr[len] != ':')
510 return 0;
511 if (strncasecmp(hdr, name, len) != 0)
512 return 0;
513 val = hdr + len + 1;
514 while (val < end && HTTP_IS_SPHT(*val))
515 val++;
516 if ((val >= end) && (len + 2 <= end - hdr))
517 return len + 2; /* we may replace starting from second space */
518 return val - hdr;
519}
520
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200521/* Find the first or next occurrence of header <name> in message buffer <sol>
522 * using headers index <idx>, and return it in the <ctx> structure. This
523 * structure holds everything necessary to use the header and find next
524 * occurrence. If its <idx> member is 0, the header is searched from the
525 * beginning. Otherwise, the next occurrence is returned. The function returns
526 * 1 when it finds a value, and 0 when there is no more. It is very similar to
527 * http_find_header2() except that it is designed to work with full-line headers
528 * whose comma is not a delimiter but is part of the syntax. As a special case,
529 * if ctx->val is NULL when searching for a new values of a header, the current
530 * header is rescanned. This allows rescanning after a header deletion.
531 */
532int http_find_full_header2(const char *name, int len,
533 char *sol, struct hdr_idx *idx,
534 struct hdr_ctx *ctx)
535{
536 char *eol, *sov;
537 int cur_idx, old_idx;
538
539 cur_idx = ctx->idx;
540 if (cur_idx) {
541 /* We have previously returned a header, let's search another one */
542 sol = ctx->line;
543 eol = sol + idx->v[cur_idx].len;
544 goto next_hdr;
545 }
546
547 /* first request for this header */
548 sol += hdr_idx_first_pos(idx);
549 old_idx = 0;
550 cur_idx = hdr_idx_first_idx(idx);
551 while (cur_idx) {
552 eol = sol + idx->v[cur_idx].len;
553
554 if (len == 0) {
555 /* No argument was passed, we want any header.
556 * To achieve this, we simply build a fake request. */
557 while (sol + len < eol && sol[len] != ':')
558 len++;
559 name = sol;
560 }
561
562 if ((len < eol - sol) &&
563 (sol[len] == ':') &&
564 (strncasecmp(sol, name, len) == 0)) {
565 ctx->del = len;
566 sov = sol + len + 1;
567 while (sov < eol && http_is_lws[(unsigned char)*sov])
568 sov++;
569
570 ctx->line = sol;
571 ctx->prev = old_idx;
572 ctx->idx = cur_idx;
573 ctx->val = sov - sol;
574 ctx->tws = 0;
575 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
576 eol--;
577 ctx->tws++;
578 }
579 ctx->vlen = eol - sov;
580 return 1;
581 }
582 next_hdr:
583 sol = eol + idx->v[cur_idx].cr + 1;
584 old_idx = cur_idx;
585 cur_idx = idx->v[cur_idx].next;
586 }
587 return 0;
588}
589
Willy Tarreau68085d82010-01-18 14:54:04 +0100590/* Find the end of the header value contained between <s> and <e>. See RFC2616,
591 * par 2.2 for more information. Note that it requires a valid header to return
592 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200593 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100594char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200595{
596 int quoted, qdpair;
597
598 quoted = qdpair = 0;
599 for (; s < e; s++) {
600 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200601 else if (quoted) {
602 if (*s == '\\') qdpair = 1;
603 else if (*s == '"') quoted = 0;
604 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200605 else if (*s == '"') quoted = 1;
606 else if (*s == ',') return s;
607 }
608 return s;
609}
610
611/* Find the first or next occurrence of header <name> in message buffer <sol>
612 * using headers index <idx>, and return it in the <ctx> structure. This
613 * structure holds everything necessary to use the header and find next
614 * occurrence. If its <idx> member is 0, the header is searched from the
615 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100616 * 1 when it finds a value, and 0 when there is no more. It is designed to work
617 * with headers defined as comma-separated lists. As a special case, if ctx->val
618 * is NULL when searching for a new values of a header, the current header is
619 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200620 */
621int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100622 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200623 struct hdr_ctx *ctx)
624{
Willy Tarreau68085d82010-01-18 14:54:04 +0100625 char *eol, *sov;
626 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200627
Willy Tarreau68085d82010-01-18 14:54:04 +0100628 cur_idx = ctx->idx;
629 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200630 /* We have previously returned a value, let's search
631 * another one on the same line.
632 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200633 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200634 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100635 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200636 eol = sol + idx->v[cur_idx].len;
637
638 if (sov >= eol)
639 /* no more values in this header */
640 goto next_hdr;
641
Willy Tarreau68085d82010-01-18 14:54:04 +0100642 /* values remaining for this header, skip the comma but save it
643 * for later use (eg: for header deletion).
644 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200645 sov++;
646 while (sov < eol && http_is_lws[(unsigned char)*sov])
647 sov++;
648
649 goto return_hdr;
650 }
651
652 /* first request for this header */
653 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100654 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200655 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200656 while (cur_idx) {
657 eol = sol + idx->v[cur_idx].len;
658
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200659 if (len == 0) {
660 /* No argument was passed, we want any header.
661 * To achieve this, we simply build a fake request. */
662 while (sol + len < eol && sol[len] != ':')
663 len++;
664 name = sol;
665 }
666
Willy Tarreau33a7e692007-06-10 19:45:56 +0200667 if ((len < eol - sol) &&
668 (sol[len] == ':') &&
669 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100670 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200671 sov = sol + len + 1;
672 while (sov < eol && http_is_lws[(unsigned char)*sov])
673 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100674
Willy Tarreau33a7e692007-06-10 19:45:56 +0200675 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100676 ctx->prev = old_idx;
677 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200678 ctx->idx = cur_idx;
679 ctx->val = sov - sol;
680
681 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200682 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200683 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200684 eol--;
685 ctx->tws++;
686 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200687 ctx->vlen = eol - sov;
688 return 1;
689 }
690 next_hdr:
691 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100692 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200693 cur_idx = idx->v[cur_idx].next;
694 }
695 return 0;
696}
697
698int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100699 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200700 struct hdr_ctx *ctx)
701{
702 return http_find_header2(name, strlen(name), sol, idx, ctx);
703}
704
Willy Tarreau68085d82010-01-18 14:54:04 +0100705/* Remove one value of a header. This only works on a <ctx> returned by one of
706 * the http_find_header functions. The value is removed, as well as surrounding
707 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100708 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100709 * message <msg>. The new index is returned. If it is zero, it means there is
710 * no more header, so any processing may stop. The ctx is always left in a form
711 * that can be handled by http_find_header2() to find next occurrence.
712 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100713int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100714{
715 int cur_idx = ctx->idx;
716 char *sol = ctx->line;
717 struct hdr_idx_elem *hdr;
718 int delta, skip_comma;
719
720 if (!cur_idx)
721 return 0;
722
723 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200724 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100725 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200726 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100727 http_msg_move_end(msg, delta);
728 idx->used--;
729 hdr->len = 0; /* unused entry */
730 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100731 if (idx->tail == ctx->idx)
732 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100733 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau14acc652015-01-07 17:23:50 +0100734 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100735 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200736 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100737 return ctx->idx;
738 }
739
740 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200741 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
742 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100743 */
744
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200745 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200746 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200747 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100748 NULL, 0);
749 hdr->len += delta;
750 http_msg_move_end(msg, delta);
751 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200752 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100753 return ctx->idx;
754}
755
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100756/* This function handles a server error at the stream interface level. The
757 * stream interface is assumed to be already in a closed state. An optional
758 * message is copied into the input buffer, and an HTTP status code stored.
759 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100760 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200761 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200762static void http_server_error(struct session *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100763 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200765 channel_auto_read(si->ob);
766 channel_abort(si->ob);
767 channel_auto_close(si->ob);
768 channel_erase(si->ob);
769 channel_auto_close(si->ib);
770 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100771 if (status > 0 && msg) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200772 s->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200773 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200774 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200775 if (!(s->flags & SN_ERR_MASK))
776 s->flags |= err;
777 if (!(s->flags & SN_FINST_MASK))
778 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200779}
780
Willy Tarreau80587432006-12-24 17:47:20 +0100781/* This function returns the appropriate error location for the given session
782 * and message.
783 */
784
Willy Tarreau783f2582012-09-04 12:19:04 +0200785struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100786{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200787 if (s->be->errmsg[msgnum].str)
788 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100789 else if (s->fe->errmsg[msgnum].str)
790 return &s->fe->errmsg[msgnum];
791 else
792 return &http_err_chunks[msgnum];
793}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200794
Willy Tarreau53b6c742006-12-17 13:37:46 +0100795/*
796 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
797 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
798 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100799enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100800{
801 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100802 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100803
804 m = ((unsigned)*str - 'A');
805
806 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100807 for (h = http_methods[m]; h->len > 0; h++) {
808 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100809 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100810 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100811 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100812 };
813 return HTTP_METH_OTHER;
814 }
815 return HTTP_METH_NONE;
816
817}
818
Willy Tarreau21d2af32008-02-14 20:25:24 +0100819/* Parse the URI from the given transaction (which is assumed to be in request
820 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
821 * It is returned otherwise.
822 */
823static char *
824http_get_path(struct http_txn *txn)
825{
826 char *ptr, *end;
827
Willy Tarreau9b28e032012-10-12 23:49:43 +0200828 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100829 end = ptr + txn->req.sl.rq.u_l;
830
831 if (ptr >= end)
832 return NULL;
833
834 /* RFC2616, par. 5.1.2 :
835 * Request-URI = "*" | absuri | abspath | authority
836 */
837
838 if (*ptr == '*')
839 return NULL;
840
841 if (isalpha((unsigned char)*ptr)) {
842 /* this is a scheme as described by RFC3986, par. 3.1 */
843 ptr++;
844 while (ptr < end &&
845 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
846 ptr++;
847 /* skip '://' */
848 if (ptr == end || *ptr++ != ':')
849 return NULL;
850 if (ptr == end || *ptr++ != '/')
851 return NULL;
852 if (ptr == end || *ptr++ != '/')
853 return NULL;
854 }
855 /* skip [user[:passwd]@]host[:[port]] */
856
857 while (ptr < end && *ptr != '/')
858 ptr++;
859
860 if (ptr == end)
861 return NULL;
862
863 /* OK, we got the '/' ! */
864 return ptr;
865}
866
William Lallemand65ad6e12014-01-31 15:08:02 +0100867/* Parse the URI from the given string and look for the "/" beginning the PATH.
868 * If not found, return NULL. It is returned otherwise.
869 */
870static char *
871http_get_path_from_string(char *str)
872{
873 char *ptr = str;
874
875 /* RFC2616, par. 5.1.2 :
876 * Request-URI = "*" | absuri | abspath | authority
877 */
878
879 if (*ptr == '*')
880 return NULL;
881
882 if (isalpha((unsigned char)*ptr)) {
883 /* this is a scheme as described by RFC3986, par. 3.1 */
884 ptr++;
885 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
886 ptr++;
887 /* skip '://' */
888 if (*ptr == '\0' || *ptr++ != ':')
889 return NULL;
890 if (*ptr == '\0' || *ptr++ != '/')
891 return NULL;
892 if (*ptr == '\0' || *ptr++ != '/')
893 return NULL;
894 }
895 /* skip [user[:passwd]@]host[:[port]] */
896
897 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
898 ptr++;
899
900 if (*ptr == '\0' || *ptr == ' ')
901 return NULL;
902
903 /* OK, we got the '/' ! */
904 return ptr;
905}
906
Willy Tarreau71241ab2012-12-27 11:30:54 +0100907/* Returns a 302 for a redirectable request that reaches a server working in
908 * in redirect mode. This may only be called just after the stream interface
909 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
910 * follow normal proxy processing. NOTE: this function is designed to support
911 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100912 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100913void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100914{
915 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100916 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100917 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200918 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100919
920 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100921 trash.len = strlen(HTTP_302);
922 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100923
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100924 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100925
Willy Tarreauefb453c2008-10-26 20:49:47 +0100926 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100927 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100928 return;
929
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100930 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100931 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100932 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
933 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100934 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100935
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200936 /* 3: add the request URI. Since it was already forwarded, we need
937 * to temporarily rewind the buffer.
938 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100939 txn = &s->txn;
Willy Tarreau211cdec2014-04-17 20:18:08 +0200940 b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200941
Willy Tarreauefb453c2008-10-26 20:49:47 +0100942 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200943 len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200944
Willy Tarreau9b28e032012-10-12 23:49:43 +0200945 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200946
Willy Tarreauefb453c2008-10-26 20:49:47 +0100947 if (!path)
948 return;
949
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100950 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100951 return;
952
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100953 memcpy(trash.str + trash.len, path, len);
954 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100955
956 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100957 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
958 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100959 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100960 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
961 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100962 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100963
964 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200965 si_shutr(si);
966 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100967 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100968 si->state = SI_ST_CLO;
969
970 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200971 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100972
973 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100974 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500975 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100976}
977
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100978/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100979 * that the server side is closed. Note that err_type is actually a
980 * bitmask, where almost only aborts may be cumulated with other
981 * values. We consider that aborted operations are more important
982 * than timeouts or errors due to the fact that nobody else in the
983 * logs might explain incomplete retries. All others should avoid
984 * being cumulated. It should normally not be possible to have multiple
985 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100986 * Note that connection errors appearing on the second request of a keep-alive
987 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100988 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100989void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100991 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100992
993 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100994 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200995 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100996 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100997 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100998 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
999 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001000 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001001 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001002 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001003 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001004 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001005 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001006 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001007 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001008 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1009 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001010 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001011 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001012 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001013 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001014 else if (err_type & SI_ET_CONN_RES)
1015 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001016 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1017 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001018 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001019 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001020 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001021}
1022
Willy Tarreau42250582007-04-01 01:30:43 +02001023extern const char sess_term_cond[8];
1024extern const char sess_fin_state[8];
1025extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001026struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001027struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001028struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001029
Willy Tarreau117f59e2007-03-04 18:17:17 +01001030/*
1031 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001032 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001033 */
1034void capture_headers(char *som, struct hdr_idx *idx,
1035 char **cap, struct cap_hdr *cap_hdr)
1036{
1037 char *eol, *sol, *col, *sov;
1038 int cur_idx;
1039 struct cap_hdr *h;
1040 int len;
1041
1042 sol = som + hdr_idx_first_pos(idx);
1043 cur_idx = hdr_idx_first_idx(idx);
1044
1045 while (cur_idx) {
1046 eol = sol + idx->v[cur_idx].len;
1047
1048 col = sol;
1049 while (col < eol && *col != ':')
1050 col++;
1051
1052 sov = col + 1;
1053 while (sov < eol && http_is_lws[(unsigned char)*sov])
1054 sov++;
1055
1056 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001057 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001058 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1059 if (cap[h->index] == NULL)
1060 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001061 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001062
1063 if (cap[h->index] == NULL) {
1064 Alert("HTTP capture : out of memory.\n");
1065 continue;
1066 }
1067
1068 len = eol - sov;
1069 if (len > h->len)
1070 len = h->len;
1071
1072 memcpy(cap[h->index], sov, len);
1073 cap[h->index][len]=0;
1074 }
1075 }
1076 sol = eol + idx->v[cur_idx].cr + 1;
1077 cur_idx = idx->v[cur_idx].next;
1078 }
1079}
1080
1081
Willy Tarreau42250582007-04-01 01:30:43 +02001082/* either we find an LF at <ptr> or we jump to <bad>.
1083 */
1084#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1085
1086/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1087 * otherwise to <http_msg_ood> with <state> set to <st>.
1088 */
1089#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1090 ptr++; \
1091 if (likely(ptr < end)) \
1092 goto good; \
1093 else { \
1094 state = (st); \
1095 goto http_msg_ood; \
1096 } \
1097 } while (0)
1098
1099
Willy Tarreaubaaee002006-06-26 02:48:02 +02001100/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001101 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001102 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1103 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1104 * will give undefined results.
1105 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1106 * and that msg->sol points to the beginning of the response.
1107 * If a complete line is found (which implies that at least one CR or LF is
1108 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1109 * returned indicating an incomplete line (which does not mean that parts have
1110 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1111 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1112 * upon next call.
1113 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001114 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001115 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1116 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001117 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001118 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001119const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001120 enum ht_state state, const char *ptr, const char *end,
1121 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001122{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001123 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001124
Willy Tarreau8973c702007-01-21 23:58:29 +01001125 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001126 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001127 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001128 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001129 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1130
1131 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001132 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001133 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1134 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001135 state = HTTP_MSG_ERROR;
1136 break;
1137
Willy Tarreau8973c702007-01-21 23:58:29 +01001138 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001139 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001140 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001141 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001142 goto http_msg_rpcode;
1143 }
1144 if (likely(HTTP_IS_SPHT(*ptr)))
1145 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1146 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001147 state = HTTP_MSG_ERROR;
1148 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001149
Willy Tarreau8973c702007-01-21 23:58:29 +01001150 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001151 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001152 if (likely(!HTTP_IS_LWS(*ptr)))
1153 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1154
1155 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001156 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001157 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1158 }
1159
1160 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001161 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001162 http_msg_rsp_reason:
1163 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001164 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001165 msg->sl.st.r_l = 0;
1166 goto http_msg_rpline_eol;
1167
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001169 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001170 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001171 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001172 goto http_msg_rpreason;
1173 }
1174 if (likely(HTTP_IS_SPHT(*ptr)))
1175 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1176 /* so it's a CR/LF, so there is no reason phrase */
1177 goto http_msg_rsp_reason;
1178
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001180 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001181 if (likely(!HTTP_IS_CRLF(*ptr)))
1182 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001183 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001184 http_msg_rpline_eol:
1185 /* We have seen the end of line. Note that we do not
1186 * necessarily have the \n yet, but at least we know that we
1187 * have EITHER \r OR \n, otherwise the response would not be
1188 * complete. We can then record the response length and return
1189 * to the caller which will be able to register it.
1190 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001191 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001192 return ptr;
1193
Willy Tarreau8973c702007-01-21 23:58:29 +01001194 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001195#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001196 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1197 exit(1);
1198#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001199 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001200 }
1201
1202 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001203 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001204 if (ret_state)
1205 *ret_state = state;
1206 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001207 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001208 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001209}
1210
Willy Tarreau8973c702007-01-21 23:58:29 +01001211/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001212 * This function parses a request line between <ptr> and <end>, starting with
1213 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1214 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1215 * will give undefined results.
1216 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1217 * and that msg->sol points to the beginning of the request.
1218 * If a complete line is found (which implies that at least one CR or LF is
1219 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1220 * returned indicating an incomplete line (which does not mean that parts have
1221 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1222 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1223 * upon next call.
1224 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001225 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1227 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001228 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001229 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001230const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001231 enum ht_state state, const char *ptr, const char *end,
1232 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001233{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001234 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001235
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001238 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239 if (likely(HTTP_IS_TOKEN(*ptr)))
1240 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001241
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001242 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001243 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001244 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1245 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001246
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001247 if (likely(HTTP_IS_CRLF(*ptr))) {
1248 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001249 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001250 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001251 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001252 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001253 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001254 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001255 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001256 msg->sl.rq.v_l = 0;
1257 goto http_msg_rqline_eol;
1258 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001259 state = HTTP_MSG_ERROR;
1260 break;
1261
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001262 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001263 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001265 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001266 goto http_msg_rquri;
1267 }
1268 if (likely(HTTP_IS_SPHT(*ptr)))
1269 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1270 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1271 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001272
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001274 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001275 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001277
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001279 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001280 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1281 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001282
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001283 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001284 /* non-ASCII chars are forbidden unless option
1285 * accept-invalid-http-request is enabled in the frontend.
1286 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001287 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001288 if (msg->err_pos < -1)
1289 goto invalid_char;
1290 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001291 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001292 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1293 }
1294
1295 if (likely(HTTP_IS_CRLF(*ptr))) {
1296 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1297 goto http_msg_req09_uri_e;
1298 }
1299
1300 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001301 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001302 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001303 state = HTTP_MSG_ERROR;
1304 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001307 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001309 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 goto http_msg_rqver;
1311 }
1312 if (likely(HTTP_IS_SPHT(*ptr)))
1313 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1314 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1315 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001318 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001319 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001321
1322 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001323 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001324 http_msg_rqline_eol:
1325 /* We have seen the end of line. Note that we do not
1326 * necessarily have the \n yet, but at least we know that we
1327 * have EITHER \r OR \n, otherwise the request would not be
1328 * complete. We can then record the request length and return
1329 * to the caller which will be able to register it.
1330 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001331 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001332 return ptr;
1333 }
1334
1335 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001336 state = HTTP_MSG_ERROR;
1337 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001338
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001339 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001340#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001341 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1342 exit(1);
1343#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001344 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001345 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001348 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001349 if (ret_state)
1350 *ret_state = state;
1351 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001352 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001353 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001355
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001356/*
1357 * Returns the data from Authorization header. Function may be called more
1358 * than once so data is stored in txn->auth_data. When no header is found
1359 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001360 * searching again for something we are unable to find anyway. However, if
1361 * the result if valid, the cache is not reused because we would risk to
1362 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001363 */
1364
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001365/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1366 * set according to global.tune.bufsize.
1367 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001368char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001369
1370int
1371get_http_auth(struct session *s)
1372{
1373
1374 struct http_txn *txn = &s->txn;
1375 struct chunk auth_method;
1376 struct hdr_ctx ctx;
1377 char *h, *p;
1378 int len;
1379
1380#ifdef DEBUG_AUTH
1381 printf("Auth for session %p: %d\n", s, txn->auth.method);
1382#endif
1383
1384 if (txn->auth.method == HTTP_AUTH_WRONG)
1385 return 0;
1386
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001387 txn->auth.method = HTTP_AUTH_WRONG;
1388
1389 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001390
1391 if (txn->flags & TX_USE_PX_CONN) {
1392 h = "Proxy-Authorization";
1393 len = strlen(h);
1394 } else {
1395 h = "Authorization";
1396 len = strlen(h);
1397 }
1398
Willy Tarreau9b28e032012-10-12 23:49:43 +02001399 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001400 return 0;
1401
1402 h = ctx.line + ctx.val;
1403
1404 p = memchr(h, ' ', ctx.vlen);
1405 if (!p || p == h)
1406 return 0;
1407
1408 chunk_initlen(&auth_method, h, 0, p-h);
1409 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1410
1411 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1412
1413 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001414 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001415
1416 if (len < 0)
1417 return 0;
1418
1419
1420 get_http_auth_buff[len] = '\0';
1421
1422 p = strchr(get_http_auth_buff, ':');
1423
1424 if (!p)
1425 return 0;
1426
1427 txn->auth.user = get_http_auth_buff;
1428 *p = '\0';
1429 txn->auth.pass = p+1;
1430
1431 txn->auth.method = HTTP_AUTH_BASIC;
1432 return 1;
1433 }
1434
1435 return 0;
1436}
1437
Willy Tarreau58f10d72006-12-04 02:26:12 +01001438
Willy Tarreau8973c702007-01-21 23:58:29 +01001439/*
1440 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001441 * depending on the initial msg->msg_state. The caller is responsible for
1442 * ensuring that the message does not wrap. The function can be preempted
1443 * everywhere when data are missing and recalled at the exact same location
1444 * with no information loss. The message may even be realigned between two
1445 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001446 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001447 * fields. Note that msg->sol will be initialized after completing the first
1448 * state, so that none of the msg pointers has to be initialized prior to the
1449 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001451void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452{
Willy Tarreau3770f232013-12-07 00:01:53 +01001453 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001455 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001456
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001457 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001458 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001459 ptr = buf->p + msg->next;
1460 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001461
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 if (unlikely(ptr >= end))
1463 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001466 /*
1467 * First, states that are specific to the response only.
1468 * We check them first so that request and headers are
1469 * closer to each other (accessed more often).
1470 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001471 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001472 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001473 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001474 /* we have a start of message, but we have to check
1475 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001476 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001477 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001478 if (unlikely(ptr != buf->p)) {
1479 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001480 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001481 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001482 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001483 }
Willy Tarreau26927362012-05-18 23:22:52 +02001484 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001485 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001486 hdr_idx_init(idx);
1487 state = HTTP_MSG_RPVER;
1488 goto http_msg_rpver;
1489 }
1490
1491 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1492 goto http_msg_invalid;
1493
1494 if (unlikely(*ptr == '\n'))
1495 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1496 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1497 /* stop here */
1498
Willy Tarreau8973c702007-01-21 23:58:29 +01001499 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001500 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001501 EXPECT_LF_HERE(ptr, http_msg_invalid);
1502 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1503 /* stop here */
1504
Willy Tarreau8973c702007-01-21 23:58:29 +01001505 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001506 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001507 case HTTP_MSG_RPVER_SP:
1508 case HTTP_MSG_RPCODE:
1509 case HTTP_MSG_RPCODE_SP:
1510 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001511 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001512 state, ptr, end,
1513 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001514 if (unlikely(!ptr))
1515 return;
1516
1517 /* we have a full response and we know that we have either a CR
1518 * or an LF at <ptr>.
1519 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001520 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1521
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001522 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001523 if (likely(*ptr == '\r'))
1524 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1525 goto http_msg_rpline_end;
1526
Willy Tarreau8973c702007-01-21 23:58:29 +01001527 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001528 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001529 /* msg->sol must point to the first of CR or LF. */
1530 EXPECT_LF_HERE(ptr, http_msg_invalid);
1531 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1532 /* stop here */
1533
1534 /*
1535 * Second, states that are specific to the request only
1536 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001538 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001540 /* we have a start of message, but we have to check
1541 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001542 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001543 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001544 if (likely(ptr != buf->p)) {
1545 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001546 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001547 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001548 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 }
Willy Tarreau26927362012-05-18 23:22:52 +02001550 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001551 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001552 state = HTTP_MSG_RQMETH;
1553 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001555
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1557 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 if (unlikely(*ptr == '\n'))
1560 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1561 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001562 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001565 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 EXPECT_LF_HERE(ptr, http_msg_invalid);
1567 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001568 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001571 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 case HTTP_MSG_RQMETH_SP:
1573 case HTTP_MSG_RQURI:
1574 case HTTP_MSG_RQURI_SP:
1575 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001576 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001577 state, ptr, end,
1578 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 if (unlikely(!ptr))
1580 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001581
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 /* we have a full request and we know that we have either a CR
1583 * or an LF at <ptr>.
1584 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001586
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001587 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 if (likely(*ptr == '\r'))
1589 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001591
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001593 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 /* check for HTTP/0.9 request : no version information available.
1595 * msg->sol must point to the first of CR or LF.
1596 */
1597 if (unlikely(msg->sl.rq.v_l == 0))
1598 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 EXPECT_LF_HERE(ptr, http_msg_invalid);
1601 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001602 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001603
Willy Tarreau8973c702007-01-21 23:58:29 +01001604 /*
1605 * Common states below
1606 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001608 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001609 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 if (likely(!HTTP_IS_CRLF(*ptr))) {
1611 goto http_msg_hdr_name;
1612 }
1613
1614 if (likely(*ptr == '\r'))
1615 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1616 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001617
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001619 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 /* assumes msg->sol points to the first char */
1621 if (likely(HTTP_IS_TOKEN(*ptr)))
1622 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001623
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001624 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001626
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001627 if (likely(msg->err_pos < -1) || *ptr == '\n')
1628 goto http_msg_invalid;
1629
1630 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001631 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001632
1633 /* and we still accept this non-token character */
1634 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001635
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001637 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001638 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 if (likely(HTTP_IS_SPHT(*ptr)))
1640 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001643 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001644
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 if (likely(!HTTP_IS_CRLF(*ptr))) {
1646 goto http_msg_hdr_val;
1647 }
1648
1649 if (likely(*ptr == '\r'))
1650 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1651 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001654 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001655 EXPECT_LF_HERE(ptr, http_msg_invalid);
1656 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001657
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001659 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660 if (likely(HTTP_IS_SPHT(*ptr))) {
1661 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001662 for (; buf->p + msg->sov < ptr; msg->sov++)
1663 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 goto http_msg_hdr_l1_sp;
1665 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001666 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001667 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 goto http_msg_complete_header;
1669
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001670 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001671 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001672 /* assumes msg->sol points to the first char, and msg->sov
1673 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 */
1675 if (likely(!HTTP_IS_CRLF(*ptr)))
1676 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001677
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001678 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 /* Note: we could also copy eol into ->eoh so that we have the
1680 * real header end in case it ends with lots of LWS, but is this
1681 * really needed ?
1682 */
1683 if (likely(*ptr == '\r'))
1684 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1685 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001686
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001687 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001688 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 EXPECT_LF_HERE(ptr, http_msg_invalid);
1690 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001691
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001693 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1695 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001696 for (; buf->p + msg->eol < ptr; msg->eol++)
1697 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 goto http_msg_hdr_val;
1699 }
1700 http_msg_complete_header:
1701 /*
1702 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001703 * Assumes msg->sol points to the first char, msg->sov points
1704 * to the first character of the value and msg->eol to the
1705 * first CR or LF so we know how the line ends. We insert last
1706 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001707 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001708 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 idx, idx->tail) < 0))
1710 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001711
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001712 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001713 if (likely(!HTTP_IS_CRLF(*ptr))) {
1714 goto http_msg_hdr_name;
1715 }
1716
1717 if (likely(*ptr == '\r'))
1718 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1719 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001720
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001722 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001723 /* Assumes msg->sol points to the first of either CR or LF.
1724 * Sets ->sov and ->next to the total header length, ->eoh to
1725 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1726 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001727 EXPECT_LF_HERE(ptr, http_msg_invalid);
1728 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001729 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001730 msg->eoh = msg->sol;
1731 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001732 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001733 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001735
1736 case HTTP_MSG_ERROR:
1737 /* this may only happen if we call http_msg_analyser() twice with an error */
1738 break;
1739
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001741#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1743 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001744#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001745 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 }
1747 http_msg_ood:
1748 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001749 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001750 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001752
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 http_msg_invalid:
1754 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001755 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001756 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 return;
1758}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001759
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001760/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1761 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1762 * nothing is done and 1 is returned.
1763 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001764static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001765{
1766 int delta;
1767 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001768 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001769
1770 if (msg->sl.rq.v_l != 0)
1771 return 1;
1772
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001773 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1774 if (txn->meth != HTTP_METH_GET)
1775 return 0;
1776
Willy Tarreau9b28e032012-10-12 23:49:43 +02001777 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001778 delta = 0;
1779
1780 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001781 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1782 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001783 }
1784 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001785 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001786 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001787 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001788 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001789 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001790 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001791 NULL, NULL);
1792 if (unlikely(!cur_end))
1793 return 0;
1794
1795 /* we have a full HTTP/1.0 request now and we know that
1796 * we have either a CR or an LF at <ptr>.
1797 */
1798 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1799 return 1;
1800}
1801
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001802/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001803 * and "keep-alive" values. If we already know that some headers may safely
1804 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001805 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1806 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001807 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001808 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1809 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1810 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001811 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001812 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001813void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001814{
Willy Tarreau5b154472009-12-21 20:11:07 +01001815 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001816 const char *hdr_val = "Connection";
1817 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001818
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001819 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001820 return;
1821
Willy Tarreau88d349d2010-01-25 12:15:43 +01001822 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1823 hdr_val = "Proxy-Connection";
1824 hdr_len = 16;
1825 }
1826
Willy Tarreau5b154472009-12-21 20:11:07 +01001827 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001828 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001829 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001830 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1831 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001832 if (to_del & 2)
1833 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001834 else
1835 txn->flags |= TX_CON_KAL_SET;
1836 }
1837 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1838 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001839 if (to_del & 1)
1840 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001841 else
1842 txn->flags |= TX_CON_CLO_SET;
1843 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001844 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1845 txn->flags |= TX_HDR_CONN_UPG;
1846 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001847 }
1848
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001849 txn->flags |= TX_HDR_CONN_PRS;
1850 return;
1851}
Willy Tarreau5b154472009-12-21 20:11:07 +01001852
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001853/* Apply desired changes on the Connection: header. Values may be removed and/or
1854 * added depending on the <wanted> flags, which are exclusively composed of
1855 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1856 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1857 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001858void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001859{
1860 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001861 const char *hdr_val = "Connection";
1862 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001863
1864 ctx.idx = 0;
1865
Willy Tarreau88d349d2010-01-25 12:15:43 +01001866
1867 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1868 hdr_val = "Proxy-Connection";
1869 hdr_len = 16;
1870 }
1871
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001872 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001873 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001874 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1875 if (wanted & TX_CON_KAL_SET)
1876 txn->flags |= TX_CON_KAL_SET;
1877 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001878 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001879 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001880 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1881 if (wanted & TX_CON_CLO_SET)
1882 txn->flags |= TX_CON_CLO_SET;
1883 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001884 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001885 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001886 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001887
1888 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1889 return;
1890
1891 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1892 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001893 hdr_val = "Connection: close";
1894 hdr_len = 17;
1895 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1896 hdr_val = "Proxy-Connection: close";
1897 hdr_len = 23;
1898 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001899 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001900 }
1901
1902 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1903 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001904 hdr_val = "Connection: keep-alive";
1905 hdr_len = 22;
1906 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1907 hdr_val = "Proxy-Connection: keep-alive";
1908 hdr_len = 28;
1909 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001910 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001911 }
1912 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001913}
1914
Willy Tarreauc24715e2014-04-17 15:21:20 +02001915/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1916 * the first byte of data after the chunk size, so that we know we can forward
1917 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1918 * the chunk size. That way it is always possible to differentiate between the
1919 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001920 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001922 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001923static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001924{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001925 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001926 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001927 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001928 const char *end = buf->data + buf->size;
1929 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001930 unsigned int chunk = 0;
1931
1932 /* The chunk size is in the following form, though we are only
1933 * interested in the size and CRLF :
1934 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1935 */
1936 while (1) {
1937 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001938 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001939 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001941 if (c < 0) /* not a hex digit anymore */
1942 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001943 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001944 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001945 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001946 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001947 chunk = (chunk << 4) + c;
1948 }
1949
Willy Tarreaud98cf932009-12-27 22:54:55 +01001950 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001951 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001952 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953
1954 while (http_is_spht[(unsigned char)*ptr]) {
1955 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001956 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001957 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001958 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001959 }
1960
Willy Tarreaud98cf932009-12-27 22:54:55 +01001961 /* Up to there, we know that at least one byte is present at *ptr. Check
1962 * for the end of chunk size.
1963 */
1964 while (1) {
1965 if (likely(HTTP_IS_CRLF(*ptr))) {
1966 /* we now have a CR or an LF at ptr */
1967 if (likely(*ptr == '\r')) {
1968 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001969 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001970 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001971 return 0;
1972 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001973
Willy Tarreaud98cf932009-12-27 22:54:55 +01001974 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001975 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001976 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001977 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001978 /* done */
1979 break;
1980 }
1981 else if (*ptr == ';') {
1982 /* chunk extension, ends at next CRLF */
1983 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001984 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001985 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001986 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001987
1988 while (!HTTP_IS_CRLF(*ptr)) {
1989 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001990 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001991 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001992 return 0;
1993 }
1994 /* we have a CRLF now, loop above */
1995 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001996 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001997 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001998 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001999 }
2000
Willy Tarreaud98cf932009-12-27 22:54:55 +01002001 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002002 * which may or may not be present. We save that into ->next,
2003 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002004 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002005 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002006 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002007 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002008 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002009 msg->chunk_len = chunk;
2010 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002011 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002012 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002013 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002014 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002015 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002016}
2017
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002018/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002019 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002020 * the trailers is found, it is automatically scheduled to be forwarded,
2021 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2022 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002023 * except maybe msg->next if it could parse some lines, and returns zero.
2024 * If a parse error is encountered, the function returns < 0 and does not
2025 * change anything except maybe msg->next. Note that the message must
2026 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002027 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002028 * forwarding, and that msg->next exactly matches the length of trailers
2029 * already parsed and not forwarded. It is also important to note that this
2030 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002031 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002032static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002033{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002034 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002035
Willy Tarreaua458b672012-03-05 11:17:50 +01002036 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002037 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002038 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002039 const char *ptr = b_ptr(buf, msg->next);
2040 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002041 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002042
2043 /* scan current line and stop at LF or CRLF */
2044 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002045 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002046 return 0;
2047
2048 if (*ptr == '\n') {
2049 if (!p1)
2050 p1 = ptr;
2051 p2 = ptr;
2052 break;
2053 }
2054
2055 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002056 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002057 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002058 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002059 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002060 p1 = ptr;
2061 }
2062
2063 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002064 if (ptr >= buf->data + buf->size)
2065 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002066 }
2067
2068 /* after LF; point to beginning of next line */
2069 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002070 if (p2 >= buf->data + buf->size)
2071 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002072
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002073 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002074 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002075 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002076
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002077 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002078 /* LF/CRLF at beginning of line => end of trailers at p2.
2079 * Everything was scheduled for forwarding, there's nothing
2080 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002081 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002082 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002083 msg->msg_state = HTTP_MSG_DONE;
2084 return 1;
2085 }
2086 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002087 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002088 }
2089}
2090
Willy Tarreauc24715e2014-04-17 15:21:20 +02002091/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2092 * or a possible LF alone at the end of a chunk. It automatically adjusts
2093 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002094 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2096 * not enough data are available, the function does not change anything and
2097 * returns zero. If a parse error is encountered, the function returns < 0 and
2098 * does not change anything. Note: this function is designed to parse wrapped
2099 * CRLF at the end of the buffer.
2100 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002101static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002102{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002103 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002104 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002105 int bytes;
2106
2107 /* NB: we'll check data availabilty at the end. It's not a
2108 * problem because whatever we match first will be checked
2109 * against the correct length.
2110 */
2111 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002112 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002113 if (*ptr == '\r') {
2114 bytes++;
2115 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002116 if (ptr >= buf->data + buf->size)
2117 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002118 }
2119
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002120 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002121 return 0;
2122
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002123 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002124 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002125 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002126 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002127
2128 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002129 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002130 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002131 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002132 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002133 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2134 return 1;
2135}
Willy Tarreau5b154472009-12-21 20:11:07 +01002136
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002137/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2138 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2139 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2140 * Unparsable qvalues return 1000 as "q=1.000".
2141 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002142int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002143{
2144 int q = 1000;
2145
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002146 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002147 goto out;
2148 q = (*qvalue++ - '0') * 1000;
2149
2150 if (*qvalue++ != '.')
2151 goto out;
2152
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002153 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002154 goto out;
2155 q += (*qvalue++ - '0') * 100;
2156
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002157 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002158 goto out;
2159 q += (*qvalue++ - '0') * 10;
2160
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002161 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002162 goto out;
2163 q += (*qvalue++ - '0') * 1;
2164 out:
2165 if (q > 1000)
2166 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002167 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002168 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002169 return q;
2170}
William Lallemand82fe75c2012-10-23 10:25:10 +02002171
2172/*
2173 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002174 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002175int select_compression_request_header(struct session *s, struct buffer *req)
2176{
2177 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002178 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002179 struct hdr_ctx ctx;
2180 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002181 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002182
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002183 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2184 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002185 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2186 */
2187 ctx.idx = 0;
2188 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2189 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002190 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2191 (ctx.vlen < 31 ||
2192 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2193 ctx.line[ctx.val + 30] < '6' ||
2194 (ctx.line[ctx.val + 30] == '6' &&
2195 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2196 s->comp_algo = NULL;
2197 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002198 }
2199
William Lallemand82fe75c2012-10-23 10:25:10 +02002200 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002201 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002202 int best_q = 0;
2203
William Lallemand82fe75c2012-10-23 10:25:10 +02002204 ctx.idx = 0;
2205 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002206 const char *qval;
2207 int q;
2208 int toklen;
2209
2210 /* try to isolate the token from the optional q-value */
2211 toklen = 0;
2212 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2213 toklen++;
2214
2215 qval = ctx.line + ctx.val + toklen;
2216 while (1) {
2217 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2218 qval++;
2219
2220 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2221 qval = NULL;
2222 break;
2223 }
2224 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002225
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002226 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2227 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002228
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002229 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2230 qval = NULL;
2231 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002232 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002233 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2234 break;
2235
2236 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2237 qval++;
2238 }
2239
2240 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002241 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002242
2243 if (q <= best_q)
2244 continue;
2245
2246 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2247 if (*(ctx.line + ctx.val) == '*' ||
2248 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2249 s->comp_algo = comp_algo;
2250 best_q = q;
2251 break;
2252 }
2253 }
2254 }
2255 }
2256
2257 /* remove all occurrences of the header when "compression offload" is set */
2258 if (s->comp_algo) {
2259 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2260 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2261 ctx.idx = 0;
2262 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2263 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002264 }
2265 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002266 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002267 }
2268
2269 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002270 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2271 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002272 if (comp_algo->add_data == identity_add_data) {
2273 s->comp_algo = comp_algo;
2274 return 1;
2275 }
2276 }
2277 }
2278
2279 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002280 return 0;
2281}
2282
2283/*
2284 * Selects a comression algorithm depending of the server response.
2285 */
2286int select_compression_response_header(struct session *s, struct buffer *res)
2287{
2288 struct http_txn *txn = &s->txn;
2289 struct http_msg *msg = &txn->rsp;
2290 struct hdr_ctx ctx;
2291 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002292
2293 /* no common compression algorithm was found in request header */
2294 if (s->comp_algo == NULL)
2295 goto fail;
2296
2297 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002298 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002299 goto fail;
2300
William Lallemandd3002612012-11-26 14:34:47 +01002301 /* 200 only */
2302 if (txn->status != 200)
2303 goto fail;
2304
William Lallemand82fe75c2012-10-23 10:25:10 +02002305 /* Content-Length is null */
2306 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2307 goto fail;
2308
2309 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002310 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002311 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2312 goto fail;
2313
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002314 /* no compression when Cache-Control: no-transform is present in the message */
2315 ctx.idx = 0;
2316 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2317 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2318 goto fail;
2319 }
2320
William Lallemand82fe75c2012-10-23 10:25:10 +02002321 comp_type = NULL;
2322
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002323 /* we don't want to compress multipart content-types, nor content-types that are
2324 * not listed in the "compression type" directive if any. If no content-type was
2325 * found but configuration requires one, we don't compress either. Backend has
2326 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002327 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002328 ctx.idx = 0;
2329 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2330 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2331 goto fail;
2332
2333 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2334 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002335 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002336 if (ctx.vlen >= comp_type->name_len &&
2337 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002338 /* this Content-Type should be compressed */
2339 break;
2340 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002341 /* this Content-Type should not be compressed */
2342 if (comp_type == NULL)
2343 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002344 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002345 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002346 else { /* no content-type header */
2347 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2348 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002349 }
2350
William Lallemandd85f9172012-11-09 17:05:39 +01002351 /* limit compression rate */
2352 if (global.comp_rate_lim > 0)
2353 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2354 goto fail;
2355
William Lallemand072a2bf2012-11-20 17:01:01 +01002356 /* limit cpu usage */
2357 if (idle_pct < compress_min_idle)
2358 goto fail;
2359
William Lallemand4c49fae2012-11-07 15:00:23 +01002360 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002361 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002362 goto fail;
2363
William Lallemandec3e3892012-11-12 17:02:18 +01002364 s->flags |= SN_COMP_READY;
2365
William Lallemand82fe75c2012-10-23 10:25:10 +02002366 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002367 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002368 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2369 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2370
2371 /* add Transfer-Encoding header */
2372 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2373 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2374
2375 /*
2376 * Add Content-Encoding header when it's not identity encoding.
2377 * RFC 2616 : Identity encoding: This content-coding is used only in the
2378 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2379 * header.
2380 */
2381 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002382 trash.len = 18;
2383 memcpy(trash.str, "Content-Encoding: ", trash.len);
2384 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2385 trash.len += s->comp_algo->name_len;
2386 trash.str[trash.len] = '\0';
2387 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002388 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002389 return 1;
2390
2391fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002392 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002393 return 0;
2394}
2395
Willy Tarreau2e47a3a2014-09-30 18:44:22 +02002396void http_adjust_conn_mode(struct session *s, struct http_txn *txn, struct http_msg *msg)
2397{
2398 int tmp = TX_CON_WANT_KAL;
2399
2400 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2401 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
2402 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2403 tmp = TX_CON_WANT_TUN;
2404
2405 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2406 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2407 tmp = TX_CON_WANT_TUN;
2408 }
2409
2410 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
2411 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2412 /* option httpclose + server_close => forceclose */
2413 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2414 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2415 tmp = TX_CON_WANT_CLO;
2416 else
2417 tmp = TX_CON_WANT_SCL;
2418 }
2419
2420 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
2421 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2422 tmp = TX_CON_WANT_CLO;
2423
2424 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2425 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2426
2427 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2428 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2429 /* parse the Connection header and possibly clean it */
2430 int to_del = 0;
2431 if ((msg->flags & HTTP_MSGF_VER_11) ||
2432 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2433 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
2434 to_del |= 2; /* remove "keep-alive" */
2435 if (!(msg->flags & HTTP_MSGF_VER_11))
2436 to_del |= 1; /* remove "close" */
2437 http_parse_connection_header(txn, msg, to_del);
2438 }
2439
2440 /* check if client or config asks for explicit close in KAL/SCL */
2441 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2442 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2443 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2444 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2445 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
2446 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
2447 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2448}
William Lallemand82fe75c2012-10-23 10:25:10 +02002449
Willy Tarreaud787e662009-07-07 10:14:51 +02002450/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2451 * processing can continue on next analysers, or zero if it either needs more
2452 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2453 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2454 * when it has nothing left to do, and may remove any analyser when it wants to
2455 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002456 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002457int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002458{
Willy Tarreau59234e92008-11-30 23:51:27 +01002459 /*
2460 * We will parse the partial (or complete) lines.
2461 * We will check the request syntax, and also join multi-line
2462 * headers. An index of all the lines will be elaborated while
2463 * parsing.
2464 *
2465 * For the parsing, we use a 28 states FSM.
2466 *
2467 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002468 * req->buf->p = beginning of request
2469 * req->buf->p + msg->eoh = end of processed headers / start of current one
2470 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002471 * msg->eol = end of current header or line (LF or CRLF)
2472 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002473 *
2474 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002475 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002476 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2477 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002478 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002479
Willy Tarreau59234e92008-11-30 23:51:27 +01002480 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002481 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002482 struct http_txn *txn = &s->txn;
2483 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002484 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002485
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002486 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002487 now_ms, __FUNCTION__,
2488 s,
2489 req,
2490 req->rex, req->wex,
2491 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002492 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002493 req->analysers);
2494
Willy Tarreau52a0c602009-08-16 22:45:38 +02002495 /* we're speaking HTTP here, so let's speak HTTP to the client */
2496 s->srv_error = http_return_srv_error;
2497
Willy Tarreau83e3af02009-12-28 17:39:57 +01002498 /* There's a protected area at the end of the buffer for rewriting
2499 * purposes. We don't want to start to parse the request if the
2500 * protected area is affected, because we may have to move processed
2501 * data later, which is much more complicated.
2502 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002503 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002504 if (txn->flags & TX_NOT_FIRST) {
2505 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002506 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002507 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002508 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002509 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002510 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002511 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002512 return 0;
2513 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002514 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2515 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2516 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002517 }
2518
Willy Tarreau065e8332010-01-08 00:30:20 +01002519 /* Note that we have the same problem with the response ; we
2520 * may want to send a redirect, error or anything which requires
2521 * some spare space. So we'll ensure that we have at least
2522 * maxrewrite bytes available in the response buffer before
2523 * processing that one. This will only affect pipelined
2524 * keep-alive requests.
2525 */
2526 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002527 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002528 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2529 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2530 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002531 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002532 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002533 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002534 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002535 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002536 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002537 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002538 return 0;
2539 }
2540 }
2541
Willy Tarreau9b28e032012-10-12 23:49:43 +02002542 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002543 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002544 }
2545
Willy Tarreau59234e92008-11-30 23:51:27 +01002546 /* 1: we might have to print this header in debug mode */
2547 if (unlikely((global.mode & MODE_DEBUG) &&
2548 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau8767b132014-10-21 19:36:09 +02002549 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002551
Willy Tarreau9b28e032012-10-12 23:49:43 +02002552 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002553 /* this is a bit complex : in case of error on the request line,
2554 * we know that rq.l is still zero, so we display only the part
2555 * up to the end of the line (truncated by debug_hdr).
2556 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002557 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002558 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002559
Willy Tarreau59234e92008-11-30 23:51:27 +01002560 sol += hdr_idx_first_pos(&txn->hdr_idx);
2561 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002562
Willy Tarreau59234e92008-11-30 23:51:27 +01002563 while (cur_idx) {
2564 eol = sol + txn->hdr_idx.v[cur_idx].len;
2565 debug_hdr("clihdr", s, sol, eol);
2566 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2567 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002568 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002569 }
2570
Willy Tarreau58f10d72006-12-04 02:26:12 +01002571
Willy Tarreau59234e92008-11-30 23:51:27 +01002572 /*
2573 * Now we quickly check if we have found a full valid request.
2574 * If not so, we check the FD and buffer states before leaving.
2575 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002576 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002577 * requests are checked first. When waiting for a second request
2578 * on a keep-alive session, if we encounter and error, close, t/o,
2579 * we note the error in the session flags but don't set any state.
2580 * Since the error will be noted there, it will not be counted by
2581 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002582 * Last, we may increase some tracked counters' http request errors on
2583 * the cases that are deliberately the client's fault. For instance,
2584 * a timeout or connection reset is not counted as an error. However
2585 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002587
Willy Tarreau655dce92009-11-08 13:10:58 +01002588 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002589 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002590 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002591 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002592 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002593 session_inc_http_req_ctr(s);
2594 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002595 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002596 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002597 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002598
Willy Tarreau59234e92008-11-30 23:51:27 +01002599 /* 1: Since we are in header mode, if there's no space
2600 * left for headers, we won't be able to free more
2601 * later, so the session will never terminate. We
2602 * must terminate it now.
2603 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002604 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002605 /* FIXME: check if URI is set and return Status
2606 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002607 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002608 session_inc_http_req_ctr(s);
2609 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002610 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002611 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002612 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002613 goto return_bad_req;
2614 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002615
Willy Tarreau59234e92008-11-30 23:51:27 +01002616 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002617 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002618 if (!(s->flags & SN_ERR_MASK))
2619 s->flags |= SN_ERR_CLICL;
2620
Willy Tarreaufcffa692010-01-10 14:21:19 +01002621 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002622 goto failed_keep_alive;
2623
Willy Tarreau59234e92008-11-30 23:51:27 +01002624 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002625 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002626 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002627 session_inc_http_err_ctr(s);
2628 }
2629
Willy Tarreaudc979f22012-12-04 10:39:01 +01002630 txn->status = 400;
2631 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 msg->msg_state = HTTP_MSG_ERROR;
2633 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002634
Willy Tarreauda7ff642010-06-23 11:44:09 +02002635 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002636 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002637 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002638 if (s->listener->counters)
2639 s->listener->counters->failed_req++;
2640
Willy Tarreau59234e92008-11-30 23:51:27 +01002641 if (!(s->flags & SN_FINST_MASK))
2642 s->flags |= SN_FINST_R;
2643 return 0;
2644 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002645
Willy Tarreau59234e92008-11-30 23:51:27 +01002646 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002647 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002648 if (!(s->flags & SN_ERR_MASK))
2649 s->flags |= SN_ERR_CLITO;
2650
Willy Tarreaufcffa692010-01-10 14:21:19 +01002651 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002652 goto failed_keep_alive;
2653
Willy Tarreau59234e92008-11-30 23:51:27 +01002654 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002655 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002656 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002657 session_inc_http_err_ctr(s);
2658 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002659 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002660 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002661 msg->msg_state = HTTP_MSG_ERROR;
2662 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002663
Willy Tarreauda7ff642010-06-23 11:44:09 +02002664 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002665 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002666 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002667 if (s->listener->counters)
2668 s->listener->counters->failed_req++;
2669
Willy Tarreau59234e92008-11-30 23:51:27 +01002670 if (!(s->flags & SN_FINST_MASK))
2671 s->flags |= SN_FINST_R;
2672 return 0;
2673 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002674
Willy Tarreau59234e92008-11-30 23:51:27 +01002675 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002676 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002677 if (!(s->flags & SN_ERR_MASK))
2678 s->flags |= SN_ERR_CLICL;
2679
Willy Tarreaufcffa692010-01-10 14:21:19 +01002680 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002681 goto failed_keep_alive;
2682
Willy Tarreau4076a152009-04-02 15:18:36 +02002683 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002684 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002685 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002686 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002687 msg->msg_state = HTTP_MSG_ERROR;
2688 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002689
Willy Tarreauda7ff642010-06-23 11:44:09 +02002690 session_inc_http_err_ctr(s);
2691 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002692 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002693 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002694 if (s->listener->counters)
2695 s->listener->counters->failed_req++;
2696
Willy Tarreau59234e92008-11-30 23:51:27 +01002697 if (!(s->flags & SN_FINST_MASK))
2698 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002699 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002700 }
2701
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002702 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002703 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2704 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002705#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002706 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && conn_ctrl_ready(__objt_conn(s->req->prod->end))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002707 /* We need more data, we have to re-enable quick-ack in case we
2708 * previously disabled it, otherwise we might cause the client
2709 * to delay next data.
2710 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002711 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002712 }
2713#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002714
Willy Tarreaufcffa692010-01-10 14:21:19 +01002715 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2716 /* If the client starts to talk, let's fall back to
2717 * request timeout processing.
2718 */
2719 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002720 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002721 }
2722
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002724 if (!tick_isset(req->analyse_exp)) {
2725 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2726 (txn->flags & TX_WAIT_NEXT_RQ) &&
2727 tick_isset(s->be->timeout.httpka))
2728 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2729 else
2730 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2731 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002732
Willy Tarreau59234e92008-11-30 23:51:27 +01002733 /* we're not ready yet */
2734 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002735
2736 failed_keep_alive:
2737 /* Here we process low-level errors for keep-alive requests. In
2738 * short, if the request is not the first one and it experiences
2739 * a timeout, read error or shutdown, we just silently close so
2740 * that the client can try again.
2741 */
2742 txn->status = 0;
2743 msg->msg_state = HTTP_MSG_RQBEFORE;
2744 req->analysers = 0;
2745 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002746 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002747 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002748 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002749 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002750 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002751
Willy Tarreaud787e662009-07-07 10:14:51 +02002752 /* OK now we have a complete HTTP request with indexed headers. Let's
2753 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002754 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002755 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002756 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002757 * byte after the last LF. msg->sov points to the first byte of data.
2758 * msg->eol cannot be trusted because it may have been left uninitialized
2759 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002760 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002761
Willy Tarreauda7ff642010-06-23 11:44:09 +02002762 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002763 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2764
Willy Tarreaub16a5742010-01-10 14:46:16 +01002765 if (txn->flags & TX_WAIT_NEXT_RQ) {
2766 /* kill the pending keep-alive timeout */
2767 txn->flags &= ~TX_WAIT_NEXT_RQ;
2768 req->analyse_exp = TICK_ETERNITY;
2769 }
2770
2771
Willy Tarreaud787e662009-07-07 10:14:51 +02002772 /* Maybe we found in invalid header name while we were configured not
2773 * to block on that, so we have to capture it now.
2774 */
2775 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002776 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002777
Willy Tarreau59234e92008-11-30 23:51:27 +01002778 /*
2779 * 1: identify the method
2780 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002781 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002782
2783 /* we can make use of server redirect on GET and HEAD */
2784 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2785 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002786
Willy Tarreau59234e92008-11-30 23:51:27 +01002787 /*
2788 * 2: check if the URI matches the monitor_uri.
2789 * We have to do this for every request which gets in, because
2790 * the monitor-uri is defined by the frontend.
2791 */
2792 if (unlikely((s->fe->monitor_uri_len != 0) &&
2793 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002794 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002795 s->fe->monitor_uri,
2796 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002797 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002798 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002799 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002800 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002801
Willy Tarreau59234e92008-11-30 23:51:27 +01002802 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002803 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002804
Willy Tarreau59234e92008-11-30 23:51:27 +01002805 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002806 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002807 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002808
Willy Tarreau59234e92008-11-30 23:51:27 +01002809 ret = acl_pass(ret);
2810 if (cond->pol == ACL_COND_UNLESS)
2811 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002812
Willy Tarreau59234e92008-11-30 23:51:27 +01002813 if (ret) {
2814 /* we fail this request, let's return 503 service unavail */
2815 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002816 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002817 if (!(s->flags & SN_ERR_MASK))
2818 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002819 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002820 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002821 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002822
Willy Tarreau59234e92008-11-30 23:51:27 +01002823 /* nothing to fail, let's reply normaly */
2824 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002825 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002826 if (!(s->flags & SN_ERR_MASK))
2827 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002828 goto return_prx_cond;
2829 }
2830
2831 /*
2832 * 3: Maybe we have to copy the original REQURI for the logs ?
2833 * Note: we cannot log anymore if the request has been
2834 * classified as invalid.
2835 */
2836 if (unlikely(s->logs.logwait & LW_REQ)) {
2837 /* we have a complete HTTP request that we must log */
2838 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2839 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002840
Willy Tarreau59234e92008-11-30 23:51:27 +01002841 if (urilen >= REQURI_LEN)
2842 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002843 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002844 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002845
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002846 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002847 s->do_log(s);
2848 } else {
2849 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002850 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002851 }
Willy Tarreau06619262006-12-17 08:37:22 +01002852
Willy Tarreau59234e92008-11-30 23:51:27 +01002853 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002854 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002855 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002856
Willy Tarreau5b154472009-12-21 20:11:07 +01002857 /* ... and check if the request is HTTP/1.1 or above */
2858 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002859 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2860 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2861 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002862 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002863
2864 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002865 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002866
Willy Tarreau88d349d2010-01-25 12:15:43 +01002867 /* if the frontend has "option http-use-proxy-header", we'll check if
2868 * we have what looks like a proxied connection instead of a connection,
2869 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2870 * Note that this is *not* RFC-compliant, however browsers and proxies
2871 * happen to do that despite being non-standard :-(
2872 * We consider that a request not beginning with either '/' or '*' is
2873 * a proxied connection, which covers both "scheme://location" and
2874 * CONNECT ip:port.
2875 */
2876 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002877 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002878 txn->flags |= TX_USE_PX_CONN;
2879
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002880 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002881 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002882
Willy Tarreau59234e92008-11-30 23:51:27 +01002883 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002884 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002885 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002886 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002887
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002888 /* 6: determine the transfer-length.
2889 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2890 * the presence of a message-body in a REQUEST and its transfer length
2891 * must be determined that way (in order of precedence) :
2892 * 1. The presence of a message-body in a request is signaled by the
2893 * inclusion of a Content-Length or Transfer-Encoding header field
2894 * in the request's header fields. When a request message contains
2895 * both a message-body of non-zero length and a method that does
2896 * not define any semantics for that request message-body, then an
2897 * origin server SHOULD either ignore the message-body or respond
2898 * with an appropriate error message (e.g., 413). A proxy or
2899 * gateway, when presented the same request, SHOULD either forward
2900 * the request inbound with the message- body or ignore the
2901 * message-body when determining a response.
2902 *
2903 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2904 * and the "chunked" transfer-coding (Section 6.2) is used, the
2905 * transfer-length is defined by the use of this transfer-coding.
2906 * If a Transfer-Encoding header field is present and the "chunked"
2907 * transfer-coding is not present, the transfer-length is defined
2908 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002909 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002910 * 3. If a Content-Length header field is present, its decimal value in
2911 * OCTETs represents both the entity-length and the transfer-length.
2912 * If a message is received with both a Transfer-Encoding header
2913 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002914 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002915 * 4. By the server closing the connection. (Closing the connection
2916 * cannot be used to indicate the end of a request body, since that
2917 * would leave no possibility for the server to send back a response.)
2918 *
2919 * Whenever a transfer-coding is applied to a message-body, the set of
2920 * transfer-codings MUST include "chunked", unless the message indicates
2921 * it is terminated by closing the connection. When the "chunked"
2922 * transfer-coding is used, it MUST be the last transfer-coding applied
2923 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002924 */
2925
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002926 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002927 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002928 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002929 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002930 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002931 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002932 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2933 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002934 /* bad transfer-encoding (chunked followed by something else) */
2935 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002936 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002937 break;
2938 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002939 }
2940
Willy Tarreaudfa3d922015-04-30 10:57:51 +02002941 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002942 ctx.idx = 0;
Willy Tarreaudfa3d922015-04-30 10:57:51 +02002943 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2944 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
2945 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2946 }
2947 else while (!use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002948 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002949 signed long long cl;
2950
Willy Tarreauad14f752011-09-02 20:33:27 +02002951 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002952 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002953 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002954 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002955
Willy Tarreauad14f752011-09-02 20:33:27 +02002956 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002957 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002958 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002959 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002960
Willy Tarreauad14f752011-09-02 20:33:27 +02002961 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002962 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002963 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002964 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002965
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002966 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002967 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002968 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002969 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002970
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002971 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002972 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002973 }
2974
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002975 /* bodyless requests have a known length */
2976 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002977 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002978
Willy Tarreau179085c2014-04-28 16:48:56 +02002979 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2980 * only change if both the request and the config reference something else.
2981 * Option httpclose by itself sets tunnel mode where headers are mangled.
2982 * However, if another mode is set, it will affect it (eg: server-close/
2983 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2984 * if FE and BE have the same settings (common). The method consists in
2985 * checking if options changed between the two calls (implying that either
2986 * one is non-null, or one of them is non-null and we are there for the first
2987 * time.
2988 */
2989 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau2e47a3a2014-09-30 18:44:22 +02002990 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
2991 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002992
Willy Tarreaud787e662009-07-07 10:14:51 +02002993 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002994 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002995 req->analyse_exp = TICK_ETERNITY;
2996 return 1;
2997
2998 return_bad_req:
2999 /* We centralize bad requests processing here */
3000 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3001 /* we detected a parsing error. We want to archive this request
3002 * in the dedicated proxy area for later troubleshooting.
3003 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003004 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003005 }
3006
3007 txn->req.msg_state = HTTP_MSG_ERROR;
3008 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003009 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003010
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003011 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003012 if (s->listener->counters)
3013 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003014
3015 return_prx_cond:
3016 if (!(s->flags & SN_ERR_MASK))
3017 s->flags |= SN_ERR_PRXCOND;
3018 if (!(s->flags & SN_FINST_MASK))
3019 s->flags |= SN_FINST_R;
3020
3021 req->analysers = 0;
3022 req->analyse_exp = TICK_ETERNITY;
3023 return 0;
3024}
3025
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003026
Willy Tarreau347a35d2013-11-22 17:51:09 +01003027/* This function prepares an applet to handle the stats. It can deal with the
3028 * "100-continue" expectation, check that admin rules are met for POST requests,
3029 * and program a response message if something was unexpected. It cannot fail
3030 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003031 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003032 * s->target which is supposed to already point to the stats applet. The caller
3033 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003034 */
3035int http_handle_stats(struct session *s, struct channel *req)
3036{
3037 struct stats_admin_rule *stats_admin_rule;
3038 struct stream_interface *si = s->rep->prod;
3039 struct http_txn *txn = &s->txn;
3040 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003041 struct uri_auth *uri_auth = s->be->uri_auth;
3042 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003043 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003044
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003045 appctx = si_appctx(si);
3046 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3047 appctx->st1 = appctx->st2 = 0;
3048 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3049 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003050 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3051 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003052
3053 uri = msg->chn->buf->p + msg->sl.rq.u;
3054 lookup = uri + uri_auth->uri_len;
3055
3056 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3057 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003058 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003059 break;
3060 }
3061 }
3062
3063 if (uri_auth->refresh) {
3064 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3065 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003066 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003067 break;
3068 }
3069 }
3070 }
3071
3072 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3073 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003074 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003075 break;
3076 }
3077 }
3078
3079 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3080 if (memcmp(h, ";st=", 4) == 0) {
3081 int i;
3082 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003083 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003084 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3085 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003086 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003087 break;
3088 }
3089 }
3090 break;
3091 }
3092 }
3093
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003094 appctx->ctx.stats.scope_str = 0;
3095 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003096 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3097 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3098 int itx = 0;
3099 const char *h2;
3100 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3101 const char *err;
3102
3103 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3104 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003105 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003106 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3107 itx++;
3108 h++;
3109 }
3110
3111 if (itx > STAT_SCOPE_TXT_MAXLEN)
3112 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003113 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003114
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003115 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003116 memcpy(scope_txt, h2, itx);
3117 scope_txt[itx] = '\0';
3118 err = invalid_char(scope_txt);
3119 if (err) {
3120 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003121 appctx->ctx.stats.scope_str = 0;
3122 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003123 }
3124 break;
3125 }
3126 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003127
3128 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003129 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003130 int ret = 1;
3131
3132 if (stats_admin_rule->cond) {
3133 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3134 ret = acl_pass(ret);
3135 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3136 ret = !ret;
3137 }
3138
3139 if (ret) {
3140 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003141 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003142 break;
3143 }
3144 }
3145
3146 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003147 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003148 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003149 /* we'll need the request body, possibly after sending 100-continue */
3150 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003151 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003152 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003153 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003154 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3155 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003156 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003157 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003158 else {
3159 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003160 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003161 }
3162
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003163 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003164 return 1;
3165}
3166
Lukas Tribus67db8df2013-06-23 17:37:13 +02003167/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3168 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3169 */
3170static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3171{
3172#ifdef IP_TOS
3173 if (from.ss_family == AF_INET)
3174 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3175#endif
3176#ifdef IPV6_TCLASS
3177 if (from.ss_family == AF_INET6) {
3178 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3179 /* v4-mapped addresses need IP_TOS */
3180 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3181 else
3182 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3183 }
3184#endif
3185}
3186
Sasha Pachev218f0642014-06-16 12:05:59 -06003187static int http_transform_header(struct session* s, struct http_msg *msg, const char* name, uint name_len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003188 char* buf, struct hdr_idx* idx, struct list *fmt, struct my_regex *re,
Sasha Pachev218f0642014-06-16 12:05:59 -06003189 struct hdr_ctx* ctx, int action)
3190{
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003191 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3192 struct hdr_idx *idx, struct hdr_ctx *ctx);
3193 struct chunk *replace = get_trash_chunk();
3194 struct chunk *output = get_trash_chunk();
3195
3196 replace->len = build_logline(s, replace->str, replace->size, fmt);
3197 if (replace->len >= replace->size - 1)
3198 return -1;
3199
Sasha Pachev218f0642014-06-16 12:05:59 -06003200 ctx->idx = 0;
3201
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003202 /* Choose the header browsing function. */
3203 switch (action) {
3204 case HTTP_REQ_ACT_REPLACE_VAL:
3205 case HTTP_RES_ACT_REPLACE_VAL:
3206 http_find_hdr_func = http_find_header2;
3207 break;
3208 case HTTP_REQ_ACT_REPLACE_HDR:
3209 case HTTP_RES_ACT_REPLACE_HDR:
3210 http_find_hdr_func = http_find_full_header2;
3211 break;
3212 default: /* impossible */
3213 return -1;
3214 }
3215
3216 while (http_find_hdr_func(name, name_len, buf, idx, ctx)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06003217 struct hdr_idx_elem *hdr = idx->v + ctx->idx;
3218 int delta;
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003219 char *val = ctx->line + ctx->val;
3220 char* val_end = val + ctx->vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003221
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003222 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch))
3223 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003224
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003225 output->len = exp_replace(output->str, output->size, val, replace->str, pmatch);
3226 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003227 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003228
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003229 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003230
3231 hdr->len += delta;
3232 http_msg_move_end(msg, delta);
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003233
3234 /* Adjust the length of the current value of the index. */
3235 ctx->vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003236 }
3237
3238 return 0;
3239}
3240
Willy Tarreau20b0de52012-12-24 15:45:22 +01003241/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003242 * transaction <txn>. Returns the verdict of the first rule that prevents
3243 * further processing of the request (auth, deny, ...), and defaults to
3244 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3245 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3246 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003247 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003248enum rule_result
Willy Tarreau96257ec2012-12-27 10:46:37 +01003249http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003250{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003251 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003252 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003253 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003254 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003255
Willy Tarreauff011f22011-01-06 17:51:27 +01003256 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003257 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003258 continue;
3259
Willy Tarreau96257ec2012-12-27 10:46:37 +01003260 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003261 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003262 int ret;
3263
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003264 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003265 ret = acl_pass(ret);
3266
Willy Tarreauff011f22011-01-06 17:51:27 +01003267 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003268 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003269
3270 if (!ret) /* condition not matched */
3271 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003272 }
3273
Willy Tarreau20b0de52012-12-24 15:45:22 +01003274
Willy Tarreau96257ec2012-12-27 10:46:37 +01003275 switch (rule->action) {
3276 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003277 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003278
3279 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003280 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003281
Willy Tarreauccbcc372012-12-27 12:37:57 +01003282 case HTTP_REQ_ACT_TARPIT:
3283 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003284 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003285
Willy Tarreau96257ec2012-12-27 10:46:37 +01003286 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003287 /* Auth might be performed on regular http-req rules as well as on stats */
3288 auth_realm = rule->arg.auth.realm;
3289 if (!auth_realm) {
3290 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3291 auth_realm = STATS_DEFAULT_REALM;
3292 else
3293 auth_realm = px->id;
3294 }
3295 /* send 401/407 depending on whether we use a proxy or not. We still
3296 * count one error, because normal browsing won't significantly
3297 * increase the counter but brute force attempts will.
3298 */
3299 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3300 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3301 stream_int_retnclose(&s->si[0], &trash);
3302 session_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003303 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003304
Willy Tarreau81499eb2012-12-27 12:19:02 +01003305 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003306 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3307 return HTTP_RULE_RES_BADREQ;
3308 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003309
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003310 case HTTP_REQ_ACT_SET_NICE:
3311 s->task->nice = rule->arg.nice;
3312 break;
3313
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003314 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003315 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003316 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003317 break;
3318
Willy Tarreau51347ed2013-06-11 19:34:13 +02003319 case HTTP_REQ_ACT_SET_MARK:
3320#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003321 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003322 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003323#endif
3324 break;
3325
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003326 case HTTP_REQ_ACT_SET_LOGL:
3327 s->logs.level = rule->arg.loglevel;
3328 break;
3329
Sasha Pachev218f0642014-06-16 12:05:59 -06003330 case HTTP_REQ_ACT_REPLACE_HDR:
3331 case HTTP_REQ_ACT_REPLACE_VAL:
3332 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3333 txn->req.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003334 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003335 return HTTP_RULE_RES_BADREQ;
3336 break;
3337
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003338 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003339 ctx.idx = 0;
3340 /* remove all occurrences of the header */
3341 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3342 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3343 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003344 }
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003345 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003346
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003347 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003348 case HTTP_REQ_ACT_ADD_HDR:
3349 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3350 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3351 trash.len = rule->arg.hdr_add.name_len;
3352 trash.str[trash.len++] = ':';
3353 trash.str[trash.len++] = ' ';
3354 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 +01003355
3356 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3357 /* remove all occurrences of the header */
3358 ctx.idx = 0;
3359 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3360 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3361 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3362 }
3363 }
3364
Willy Tarreau96257ec2012-12-27 10:46:37 +01003365 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3366 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003367
3368 case HTTP_REQ_ACT_DEL_ACL:
3369 case HTTP_REQ_ACT_DEL_MAP: {
3370 struct pat_ref *ref;
3371 char *key;
3372 int len;
3373
3374 /* collect reference */
3375 ref = pat_ref_lookup(rule->arg.map.ref);
3376 if (!ref)
3377 continue;
3378
3379 /* collect key */
3380 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3381 key = trash.str;
3382 key[len] = '\0';
3383
3384 /* perform update */
3385 /* returned code: 1=ok, 0=ko */
3386 pat_ref_delete(ref, key);
3387
3388 break;
3389 }
3390
3391 case HTTP_REQ_ACT_ADD_ACL: {
3392 struct pat_ref *ref;
3393 char *key;
3394 struct chunk *trash_key;
3395 int len;
3396
3397 trash_key = get_trash_chunk();
3398
3399 /* collect reference */
3400 ref = pat_ref_lookup(rule->arg.map.ref);
3401 if (!ref)
3402 continue;
3403
3404 /* collect key */
3405 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3406 key = trash_key->str;
3407 key[len] = '\0';
3408
3409 /* perform update */
3410 /* add entry only if it does not already exist */
3411 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003412 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003413
3414 break;
3415 }
3416
3417 case HTTP_REQ_ACT_SET_MAP: {
3418 struct pat_ref *ref;
3419 char *key, *value;
3420 struct chunk *trash_key, *trash_value;
3421 int len;
3422
3423 trash_key = get_trash_chunk();
3424 trash_value = get_trash_chunk();
3425
3426 /* collect reference */
3427 ref = pat_ref_lookup(rule->arg.map.ref);
3428 if (!ref)
3429 continue;
3430
3431 /* collect key */
3432 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3433 key = trash_key->str;
3434 key[len] = '\0';
3435
3436 /* collect value */
3437 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3438 value = trash_value->str;
3439 value[len] = '\0';
3440
3441 /* perform update */
3442 if (pat_ref_find_elt(ref, key) != NULL)
3443 /* update entry if it exists */
3444 pat_ref_set(ref, key, value, NULL);
3445 else
3446 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003447 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003448
3449 break;
3450 }
William Lallemand73025dd2014-04-24 14:38:37 +02003451
3452 case HTTP_REQ_ACT_CUSTOM_CONT:
3453 rule->action_ptr(rule, px, s, txn);
3454 break;
3455
3456 case HTTP_REQ_ACT_CUSTOM_STOP:
3457 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003458 return HTTP_RULE_RES_DONE;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003459 }
3460 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003461
3462 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003463 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003464}
3465
Willy Tarreau71241ab2012-12-27 11:30:54 +01003466
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003467/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3468 * transaction <txn>. Returns the first rule that prevents further processing
3469 * of the response (deny, ...) or NULL if it executed all rules or stopped
3470 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3471 * rule.
3472 */
3473static struct http_res_rule *
3474http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3475{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003476 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003477 struct http_res_rule *rule;
3478 struct hdr_ctx ctx;
3479
3480 list_for_each_entry(rule, rules, list) {
3481 if (rule->action >= HTTP_RES_ACT_MAX)
3482 continue;
3483
3484 /* check optional condition */
3485 if (rule->cond) {
3486 int ret;
3487
3488 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3489 ret = acl_pass(ret);
3490
3491 if (rule->cond->pol == ACL_COND_UNLESS)
3492 ret = !ret;
3493
3494 if (!ret) /* condition not matched */
3495 continue;
3496 }
3497
3498
3499 switch (rule->action) {
3500 case HTTP_RES_ACT_ALLOW:
3501 return NULL; /* "allow" rules are OK */
3502
3503 case HTTP_RES_ACT_DENY:
3504 txn->flags |= TX_SVDENY;
3505 return rule;
3506
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003507 case HTTP_RES_ACT_SET_NICE:
3508 s->task->nice = rule->arg.nice;
3509 break;
3510
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003511 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003512 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003513 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003514 break;
3515
Willy Tarreau51347ed2013-06-11 19:34:13 +02003516 case HTTP_RES_ACT_SET_MARK:
3517#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003518 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003519 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003520#endif
3521 break;
3522
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003523 case HTTP_RES_ACT_SET_LOGL:
3524 s->logs.level = rule->arg.loglevel;
3525 break;
3526
Sasha Pachev218f0642014-06-16 12:05:59 -06003527 case HTTP_RES_ACT_REPLACE_HDR:
3528 case HTTP_RES_ACT_REPLACE_VAL:
3529 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3530 txn->rsp.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003531 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003532 return NULL; /* note: we should report an error here */
3533 break;
3534
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003535 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003536 ctx.idx = 0;
3537 /* remove all occurrences of the header */
3538 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3539 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3540 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3541 }
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003542 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003543
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003544 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003545 case HTTP_RES_ACT_ADD_HDR:
3546 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3547 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3548 trash.len = rule->arg.hdr_add.name_len;
3549 trash.str[trash.len++] = ':';
3550 trash.str[trash.len++] = ' ';
3551 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 +01003552
3553 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3554 /* remove all occurrences of the header */
3555 ctx.idx = 0;
3556 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3557 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3558 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3559 }
3560 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003561 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3562 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003563
3564 case HTTP_RES_ACT_DEL_ACL:
3565 case HTTP_RES_ACT_DEL_MAP: {
3566 struct pat_ref *ref;
3567 char *key;
3568 int len;
3569
3570 /* collect reference */
3571 ref = pat_ref_lookup(rule->arg.map.ref);
3572 if (!ref)
3573 continue;
3574
3575 /* collect key */
3576 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3577 key = trash.str;
3578 key[len] = '\0';
3579
3580 /* perform update */
3581 /* returned code: 1=ok, 0=ko */
3582 pat_ref_delete(ref, key);
3583
3584 break;
3585 }
3586
3587 case HTTP_RES_ACT_ADD_ACL: {
3588 struct pat_ref *ref;
3589 char *key;
3590 struct chunk *trash_key;
3591 int len;
3592
3593 trash_key = get_trash_chunk();
3594
3595 /* collect reference */
3596 ref = pat_ref_lookup(rule->arg.map.ref);
3597 if (!ref)
3598 continue;
3599
3600 /* collect key */
3601 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3602 key = trash_key->str;
3603 key[len] = '\0';
3604
3605 /* perform update */
3606 /* check if the entry already exists */
3607 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003608 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003609
3610 break;
3611 }
3612
3613 case HTTP_RES_ACT_SET_MAP: {
3614 struct pat_ref *ref;
3615 char *key, *value;
3616 struct chunk *trash_key, *trash_value;
3617 int len;
3618
3619 trash_key = get_trash_chunk();
3620 trash_value = get_trash_chunk();
3621
3622 /* collect reference */
3623 ref = pat_ref_lookup(rule->arg.map.ref);
3624 if (!ref)
3625 continue;
3626
3627 /* collect key */
3628 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3629 key = trash_key->str;
3630 key[len] = '\0';
3631
3632 /* collect value */
3633 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3634 value = trash_value->str;
3635 value[len] = '\0';
3636
3637 /* perform update */
3638 if (pat_ref_find_elt(ref, key) != NULL)
3639 /* update entry if it exists */
3640 pat_ref_set(ref, key, value, NULL);
3641 else
3642 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003643 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003644
3645 break;
3646 }
William Lallemand73025dd2014-04-24 14:38:37 +02003647
3648 case HTTP_RES_ACT_CUSTOM_CONT:
3649 rule->action_ptr(rule, px, s, txn);
3650 break;
3651
3652 case HTTP_RES_ACT_CUSTOM_STOP:
3653 rule->action_ptr(rule, px, s, txn);
3654 return rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003655 }
3656 }
3657
3658 /* we reached the end of the rules, nothing to report */
3659 return NULL;
3660}
3661
3662
Willy Tarreau71241ab2012-12-27 11:30:54 +01003663/* Perform an HTTP redirect based on the information in <rule>. The function
3664 * returns non-zero on success, or zero in case of a, irrecoverable error such
3665 * as too large a request to build a valid response.
3666 */
3667static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3668{
3669 struct http_msg *msg = &txn->req;
3670 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003671 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003672
3673 /* build redirect message */
3674 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003675 case 308:
3676 msg_fmt = HTTP_308;
3677 break;
3678 case 307:
3679 msg_fmt = HTTP_307;
3680 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003681 case 303:
3682 msg_fmt = HTTP_303;
3683 break;
3684 case 301:
3685 msg_fmt = HTTP_301;
3686 break;
3687 case 302:
3688 default:
3689 msg_fmt = HTTP_302;
3690 break;
3691 }
3692
3693 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3694 return 0;
3695
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003696 location = trash.str + trash.len;
3697
Willy Tarreau71241ab2012-12-27 11:30:54 +01003698 switch(rule->type) {
3699 case REDIRECT_TYPE_SCHEME: {
3700 const char *path;
3701 const char *host;
3702 struct hdr_ctx ctx;
3703 int pathlen;
3704 int hostlen;
3705
3706 host = "";
3707 hostlen = 0;
3708 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003709 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003710 host = ctx.line + ctx.val;
3711 hostlen = ctx.vlen;
3712 }
3713
3714 path = http_get_path(txn);
3715 /* build message using path */
3716 if (path) {
3717 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3718 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3719 int qs = 0;
3720 while (qs < pathlen) {
3721 if (path[qs] == '?') {
3722 pathlen = qs;
3723 break;
3724 }
3725 qs++;
3726 }
3727 }
3728 } else {
3729 path = "/";
3730 pathlen = 1;
3731 }
3732
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003733 if (rule->rdr_str) { /* this is an old "redirect" rule */
3734 /* check if we can add scheme + "://" + host + path */
3735 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3736 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003737
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003738 /* add scheme */
3739 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3740 trash.len += rule->rdr_len;
3741 }
3742 else {
3743 /* add scheme with executing log format */
3744 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003745
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003746 /* check if we can add scheme + "://" + host + path */
3747 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3748 return 0;
3749 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003750 /* add "://" */
3751 memcpy(trash.str + trash.len, "://", 3);
3752 trash.len += 3;
3753
3754 /* add host */
3755 memcpy(trash.str + trash.len, host, hostlen);
3756 trash.len += hostlen;
3757
3758 /* add path */
3759 memcpy(trash.str + trash.len, path, pathlen);
3760 trash.len += pathlen;
3761
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003762 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003763 if (trash.len && trash.str[trash.len - 1] != '/' &&
3764 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3765 if (trash.len > trash.size - 5)
3766 return 0;
3767 trash.str[trash.len] = '/';
3768 trash.len++;
3769 }
3770
3771 break;
3772 }
3773 case REDIRECT_TYPE_PREFIX: {
3774 const char *path;
3775 int pathlen;
3776
3777 path = http_get_path(txn);
3778 /* build message using path */
3779 if (path) {
3780 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3781 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3782 int qs = 0;
3783 while (qs < pathlen) {
3784 if (path[qs] == '?') {
3785 pathlen = qs;
3786 break;
3787 }
3788 qs++;
3789 }
3790 }
3791 } else {
3792 path = "/";
3793 pathlen = 1;
3794 }
3795
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003796 if (rule->rdr_str) { /* this is an old "redirect" rule */
3797 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3798 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003799
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003800 /* add prefix. Note that if prefix == "/", we don't want to
3801 * add anything, otherwise it makes it hard for the user to
3802 * configure a self-redirection.
3803 */
3804 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3805 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3806 trash.len += rule->rdr_len;
3807 }
3808 }
3809 else {
3810 /* add prefix with executing log format */
3811 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3812
3813 /* Check length */
3814 if (trash.len + pathlen > trash.size - 4)
3815 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003816 }
3817
3818 /* add path */
3819 memcpy(trash.str + trash.len, path, pathlen);
3820 trash.len += pathlen;
3821
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003822 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003823 if (trash.len && trash.str[trash.len - 1] != '/' &&
3824 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3825 if (trash.len > trash.size - 5)
3826 return 0;
3827 trash.str[trash.len] = '/';
3828 trash.len++;
3829 }
3830
3831 break;
3832 }
3833 case REDIRECT_TYPE_LOCATION:
3834 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003835 if (rule->rdr_str) { /* this is an old "redirect" rule */
3836 if (trash.len + rule->rdr_len > trash.size - 4)
3837 return 0;
3838
3839 /* add location */
3840 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3841 trash.len += rule->rdr_len;
3842 }
3843 else {
3844 /* add location with executing log format */
3845 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003846
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003847 /* Check left length */
3848 if (trash.len > trash.size - 4)
3849 return 0;
3850 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003851 break;
3852 }
3853
3854 if (rule->cookie_len) {
3855 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3856 trash.len += 14;
3857 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3858 trash.len += rule->cookie_len;
3859 memcpy(trash.str + trash.len, "\r\n", 2);
3860 trash.len += 2;
3861 }
3862
3863 /* add end of headers and the keep-alive/close status.
3864 * We may choose to set keep-alive if the Location begins
3865 * with a slash, because the client will come back to the
3866 * same server.
3867 */
3868 txn->status = rule->code;
3869 /* let's log the request time */
3870 s->logs.tv_request = now;
3871
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003872 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003873 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3874 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3875 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3876 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3877 /* keep-alive possible */
3878 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3879 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3880 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3881 trash.len += 30;
3882 } else {
3883 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3884 trash.len += 24;
3885 }
3886 }
3887 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3888 trash.len += 4;
3889 bo_inject(txn->rsp.chn, trash.str, trash.len);
3890 /* "eat" the request */
3891 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02003892 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003893 msg->sov = 0;
3894 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3895 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3896 txn->req.msg_state = HTTP_MSG_CLOSED;
3897 txn->rsp.msg_state = HTTP_MSG_DONE;
3898 } else {
3899 /* keep-alive not possible */
3900 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3901 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3902 trash.len += 29;
3903 } else {
3904 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3905 trash.len += 23;
3906 }
3907 stream_int_retnclose(txn->req.chn->prod, &trash);
3908 txn->req.chn->analysers = 0;
3909 }
3910
3911 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003912 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003913 if (!(s->flags & SN_FINST_MASK))
3914 s->flags |= SN_FINST_R;
3915
3916 return 1;
3917}
3918
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003919/* This stream analyser runs all HTTP request processing which is common to
3920 * frontends and backends, which means blocking ACLs, filters, connection-close,
3921 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003922 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003923 * either needs more data or wants to immediately abort the request (eg: deny,
3924 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003925 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003926int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003927{
Willy Tarreaud787e662009-07-07 10:14:51 +02003928 struct http_txn *txn = &s->txn;
3929 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003930 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003931 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003932 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02003933
Willy Tarreau655dce92009-11-08 13:10:58 +01003934 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003935 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003936 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003937 return 0;
3938 }
3939
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003940 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 +02003941 now_ms, __FUNCTION__,
3942 s,
3943 req,
3944 req->rex, req->wex,
3945 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003946 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003947 req->analysers);
3948
Willy Tarreau65410832014-04-28 21:25:43 +02003949 /* just in case we have some per-backend tracking */
3950 session_inc_be_http_req_ctr(s);
3951
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003952 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02003953 if (!LIST_ISEMPTY(&px->http_req_rules)) {
3954 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003955
Willy Tarreau0b748332014-04-29 00:13:29 +02003956 switch (verdict) {
3957 case HTTP_RULE_RES_CONT:
3958 case HTTP_RULE_RES_STOP: /* nothing to do */
3959 break;
Willy Tarreau52542592014-04-28 18:33:26 +02003960
Willy Tarreau0b748332014-04-29 00:13:29 +02003961 case HTTP_RULE_RES_DENY: /* deny or tarpit */
3962 if (txn->flags & TX_CLTARPIT)
3963 goto tarpit;
3964 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02003965
Willy Tarreau0b748332014-04-29 00:13:29 +02003966 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
3967 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02003968
Willy Tarreau0b748332014-04-29 00:13:29 +02003969 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02003970 goto done;
3971
Willy Tarreau0b748332014-04-29 00:13:29 +02003972 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
3973 goto return_bad_req;
3974 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003975 }
3976
Willy Tarreau52542592014-04-28 18:33:26 +02003977 /* OK at this stage, we know that the request was accepted according to
3978 * the http-request rules, we can check for the stats. Note that the
3979 * URI is detected *before* the req* rules in order not to be affected
3980 * by a possible reqrep, while they are processed *after* so that a
3981 * reqdeny can still block them. This clearly needs to change in 1.6!
3982 */
3983 if (stats_check_uri(s->rep->prod, txn, px)) {
3984 s->target = &http_stats_applet.obj_type;
3985 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3986 txn->status = 500;
3987 s->logs.tv_request = now;
3988 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003989
Willy Tarreau52542592014-04-28 18:33:26 +02003990 if (!(s->flags & SN_ERR_MASK))
3991 s->flags |= SN_ERR_RESOURCE;
3992 goto return_prx_cond;
3993 }
3994
3995 /* parse the whole stats request and extract the relevant information */
3996 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02003997 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
3998 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003999
Willy Tarreau0b748332014-04-29 00:13:29 +02004000 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4001 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004002
Willy Tarreau0b748332014-04-29 00:13:29 +02004003 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4004 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004005 }
4006
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004007 /* evaluate the req* rules except reqadd */
4008 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004009 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004010 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004011
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004012 if (txn->flags & TX_CLDENY)
4013 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004014
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004015 if (txn->flags & TX_CLTARPIT)
4016 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004017 }
Willy Tarreau06619262006-12-17 08:37:22 +01004018
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004019 /* add request headers from the rule sets in the same order */
4020 list_for_each_entry(wl, &px->req_add, list) {
4021 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004022 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004023 ret = acl_pass(ret);
4024 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4025 ret = !ret;
4026 if (!ret)
4027 continue;
4028 }
4029
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004030 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004031 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004032 }
4033
Willy Tarreau52542592014-04-28 18:33:26 +02004034
4035 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004036 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004037 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004038 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
4039 s->fe->fe_counters.intercepted_req++;
4040
4041 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
4042 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
4043 if (!(s->flags & SN_FINST_MASK))
4044 s->flags |= SN_FINST_R;
4045
Willy Tarreau70730dd2014-04-24 18:06:27 +02004046 /* we may want to compress the stats page */
4047 if (s->fe->comp || s->be->comp)
4048 select_compression_request_header(s, req->buf);
4049
4050 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreaude8ca962014-11-20 22:23:10 +01004051 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004052 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004053 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004054
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004055 /* check whether we have some ACLs set to redirect this request */
4056 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004057 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004058 int ret;
4059
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004060 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004061 ret = acl_pass(ret);
4062 if (rule->cond->pol == ACL_COND_UNLESS)
4063 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004064 if (!ret)
4065 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004066 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004067 if (!http_apply_redirect_rule(rule, s, txn))
4068 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004069 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004070 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004071
Willy Tarreau2be39392010-01-03 17:24:51 +01004072 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4073 * If this happens, then the data will not come immediately, so we must
4074 * send all what we have without waiting. Note that due to the small gain
4075 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004076 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004077 * itself once used.
4078 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004079 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004080
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004081 done: /* done with this analyser, continue with next ones that the calling
4082 * points will have set, if any.
4083 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004084 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004085 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4086 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004087 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004088
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004089 tarpit:
4090 /* When a connection is tarpitted, we use the tarpit timeout,
4091 * which may be the same as the connect timeout if unspecified.
4092 * If unset, then set it to zero because we really want it to
4093 * eventually expire. We build the tarpit as an analyser.
4094 */
4095 channel_erase(s->req);
4096
4097 /* wipe the request out so that we can drop the connection early
4098 * if the client closes first.
4099 */
4100 channel_dont_connect(req);
4101 req->analysers = 0; /* remove switching rules etc... */
4102 req->analysers |= AN_REQ_HTTP_TARPIT;
4103 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4104 if (!req->analyse_exp)
4105 req->analyse_exp = tick_add(now_ms, 0);
4106 session_inc_http_err_ctr(s);
4107 s->fe->fe_counters.denied_req++;
4108 if (s->fe != s->be)
4109 s->be->be_counters.denied_req++;
4110 if (s->listener->counters)
4111 s->listener->counters->denied_req++;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004112 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004113
4114 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004115 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004116 txn->status = 403;
4117 s->logs.tv_request = now;
4118 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4119 session_inc_http_err_ctr(s);
4120 s->fe->fe_counters.denied_req++;
4121 if (s->fe != s->be)
4122 s->be->be_counters.denied_req++;
4123 if (s->listener->counters)
4124 s->listener->counters->denied_req++;
4125 goto return_prx_cond;
4126
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004127 return_bad_req:
4128 /* We centralize bad requests processing here */
4129 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4130 /* we detected a parsing error. We want to archive this request
4131 * in the dedicated proxy area for later troubleshooting.
4132 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004133 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004134 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004135
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004136 txn->req.msg_state = HTTP_MSG_ERROR;
4137 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004138 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004139
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004140 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004141 if (s->listener->counters)
4142 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004143
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004144 return_prx_cond:
4145 if (!(s->flags & SN_ERR_MASK))
4146 s->flags |= SN_ERR_PRXCOND;
4147 if (!(s->flags & SN_FINST_MASK))
4148 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004149
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004150 req->analysers = 0;
4151 req->analyse_exp = TICK_ETERNITY;
4152 return 0;
4153}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004154
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004155/* This function performs all the processing enabled for the current request.
4156 * It returns 1 if the processing can continue on next analysers, or zero if it
4157 * needs more data, encounters an error, or wants to immediately abort the
4158 * request. It relies on buffers flags, and updates s->req->analysers.
4159 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004160int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004161{
4162 struct http_txn *txn = &s->txn;
4163 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004164 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004165
Willy Tarreau655dce92009-11-08 13:10:58 +01004166 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004167 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004168 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004169 return 0;
4170 }
4171
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004172 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 +02004173 now_ms, __FUNCTION__,
4174 s,
4175 req,
4176 req->rex, req->wex,
4177 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004178 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004179 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004180
William Lallemand82fe75c2012-10-23 10:25:10 +02004181 if (s->fe->comp || s->be->comp)
4182 select_compression_request_header(s, req->buf);
4183
Willy Tarreau59234e92008-11-30 23:51:27 +01004184 /*
4185 * Right now, we know that we have processed the entire headers
4186 * and that unwanted requests have been filtered out. We can do
4187 * whatever we want with the remaining request. Also, now we
4188 * may have separate values for ->fe, ->be.
4189 */
Willy Tarreau06619262006-12-17 08:37:22 +01004190
Willy Tarreau59234e92008-11-30 23:51:27 +01004191 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004192 * If HTTP PROXY is set we simply get remote server address parsing
4193 * incoming request. Note that this requires that a connection is
4194 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004195 */
4196 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004197 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004198 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004199
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004200 /* Note that for now we don't reuse existing proxy connections */
4201 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004202 txn->req.msg_state = HTTP_MSG_ERROR;
4203 txn->status = 500;
4204 req->analysers = 0;
4205 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4206
4207 if (!(s->flags & SN_ERR_MASK))
4208 s->flags |= SN_ERR_RESOURCE;
4209 if (!(s->flags & SN_FINST_MASK))
4210 s->flags |= SN_FINST_R;
4211
4212 return 0;
4213 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004214
4215 path = http_get_path(txn);
4216 url2sa(req->buf->p + msg->sl.rq.u,
4217 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004218 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004219 /* if the path was found, we have to remove everything between
4220 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4221 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4222 * u_l characters by a single "/".
4223 */
4224 if (path) {
4225 char *cur_ptr = req->buf->p;
4226 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4227 int delta;
4228
4229 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4230 http_msg_move_end(&txn->req, delta);
4231 cur_end += delta;
4232 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4233 goto return_bad_req;
4234 }
4235 else {
4236 char *cur_ptr = req->buf->p;
4237 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4238 int delta;
4239
4240 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4241 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4242 http_msg_move_end(&txn->req, delta);
4243 cur_end += delta;
4244 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4245 goto return_bad_req;
4246 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004247 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004248
Willy Tarreau59234e92008-11-30 23:51:27 +01004249 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004250 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004251 * Note that doing so might move headers in the request, but
4252 * the fields will stay coherent and the URI will not move.
4253 * This should only be performed in the backend.
4254 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004255 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004256 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4257 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004258
Willy Tarreau59234e92008-11-30 23:51:27 +01004259 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004260 * 8: the appsession cookie was looked up very early in 1.2,
4261 * so let's do the same now.
4262 */
4263
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004264 /* It needs to look into the URI unless persistence must be ignored */
4265 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004266 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 +01004267 }
4268
William Lallemanda73203e2012-03-12 12:48:57 +01004269 /* add unique-id if "header-unique-id" is specified */
4270
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004271 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4272 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4273 goto return_bad_req;
4274 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004275 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004276 }
William Lallemanda73203e2012-03-12 12:48:57 +01004277
4278 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004279 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4280 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004281 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004282 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004283 goto return_bad_req;
4284 }
4285
Cyril Bontéb21570a2009-11-29 20:04:48 +01004286 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004287 * 9: add X-Forwarded-For if either the frontend or the backend
4288 * asks for it.
4289 */
4290 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004291 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004292 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004293 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4294 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004295 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004296 /* The header is set to be added only if none is present
4297 * and we found it, so don't do anything.
4298 */
4299 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004300 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004301 /* Add an X-Forwarded-For header unless the source IP is
4302 * in the 'except' network range.
4303 */
4304 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004305 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004306 != s->fe->except_net.s_addr) &&
4307 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004308 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004309 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004310 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004311 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004312 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004313
4314 /* Note: we rely on the backend to get the header name to be used for
4315 * x-forwarded-for, because the header is really meant for the backends.
4316 * However, if the backend did not specify any option, we have to rely
4317 * on the frontend's header name.
4318 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004319 if (s->be->fwdfor_hdr_len) {
4320 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004321 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004322 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004323 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004324 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004325 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004326 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 +01004327
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004328 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004329 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004330 }
4331 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004332 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004333 /* FIXME: for the sake of completeness, we should also support
4334 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004335 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004336 int len;
4337 char pn[INET6_ADDRSTRLEN];
4338 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004339 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004340 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004341
Willy Tarreau59234e92008-11-30 23:51:27 +01004342 /* Note: we rely on the backend to get the header name to be used for
4343 * x-forwarded-for, because the header is really meant for the backends.
4344 * However, if the backend did not specify any option, we have to rely
4345 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004346 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004347 if (s->be->fwdfor_hdr_len) {
4348 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004349 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004350 } else {
4351 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004352 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004353 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004354 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004355
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004356 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004357 goto return_bad_req;
4358 }
4359 }
4360
4361 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004362 * 10: add X-Original-To if either the frontend or the backend
4363 * asks for it.
4364 */
4365 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4366
4367 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004368 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004369 /* Add an X-Original-To header unless the destination IP is
4370 * in the 'except' network range.
4371 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004372 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004373
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004374 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004375 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004376 (((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 +02004377 != s->fe->except_to.s_addr) &&
4378 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004379 (((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 +02004380 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004381 int len;
4382 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004383 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004384
4385 /* Note: we rely on the backend to get the header name to be used for
4386 * x-original-to, 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.
4389 */
4390 if (s->be->orgto_hdr_len) {
4391 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004392 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004393 } else {
4394 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004395 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004396 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004397 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 +02004398
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004399 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004400 goto return_bad_req;
4401 }
4402 }
4403 }
4404
Willy Tarreau50fc7772012-11-11 22:19:57 +01004405 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4406 * If an "Upgrade" token is found, the header is left untouched in order not to have
4407 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4408 * "Upgrade" is present in the Connection header.
4409 */
4410 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4411 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004412 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4413 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004414 unsigned int want_flags = 0;
4415
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004416 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004417 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004418 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4419 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004420 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004421 want_flags |= TX_CON_CLO_SET;
4422 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004423 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004424 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4425 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004426 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004427 want_flags |= TX_CON_KAL_SET;
4428 }
4429
4430 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004431 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004432 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004433
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004434
Willy Tarreau522d6c02009-12-06 18:49:18 +01004435 /* If we have no server assigned yet and we're balancing on url_param
4436 * with a POST request, we may be interested in checking the body for
4437 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004438 */
4439 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4440 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004441 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004442 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004443 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004444 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004445
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004446 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004447 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004448#ifdef TCP_QUICKACK
4449 /* We expect some data from the client. Unless we know for sure
4450 * we already have a full request, we have to re-enable quick-ack
4451 * in case we previously disabled it, otherwise we might cause
4452 * the client to delay further data.
4453 */
4454 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004455 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004456 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004457 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004458 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004459#endif
4460 }
Willy Tarreau03945942009-12-22 16:50:27 +01004461
Willy Tarreau59234e92008-11-30 23:51:27 +01004462 /*************************************************************
4463 * OK, that's finished for the headers. We have done what we *
4464 * could. Let's switch to the DATA state. *
4465 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004466 req->analyse_exp = TICK_ETERNITY;
4467 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004468
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004469 /* if the server closes the connection, we want to immediately react
4470 * and close the socket to save packets and syscalls.
4471 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004472 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4473 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004474
Willy Tarreau59234e92008-11-30 23:51:27 +01004475 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004476 /* OK let's go on with the BODY now */
4477 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004478
Willy Tarreau59234e92008-11-30 23:51:27 +01004479 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004480 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004481 /* we detected a parsing error. We want to archive this request
4482 * in the dedicated proxy area for later troubleshooting.
4483 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004484 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004485 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004486
Willy Tarreau59234e92008-11-30 23:51:27 +01004487 txn->req.msg_state = HTTP_MSG_ERROR;
4488 txn->status = 400;
4489 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004490 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004491
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004492 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004493 if (s->listener->counters)
4494 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004495
Willy Tarreau59234e92008-11-30 23:51:27 +01004496 if (!(s->flags & SN_ERR_MASK))
4497 s->flags |= SN_ERR_PRXCOND;
4498 if (!(s->flags & SN_FINST_MASK))
4499 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004500 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004501}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004502
Willy Tarreau60b85b02008-11-30 23:28:40 +01004503/* This function is an analyser which processes the HTTP tarpit. It always
4504 * returns zero, at the beginning because it prevents any other processing
4505 * from occurring, and at the end because it terminates the request.
4506 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004507int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004508{
4509 struct http_txn *txn = &s->txn;
4510
4511 /* This connection is being tarpitted. The CLIENT side has
4512 * already set the connect expiration date to the right
4513 * timeout. We just have to check that the client is still
4514 * there and that the timeout has not expired.
4515 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004516 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004517 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004518 !tick_is_expired(req->analyse_exp, now_ms))
4519 return 0;
4520
4521 /* We will set the queue timer to the time spent, just for
4522 * logging purposes. We fake a 500 server error, so that the
4523 * attacker will not suspect his connection has been tarpitted.
4524 * It will not cause trouble to the logs because we can exclude
4525 * the tarpitted connections by filtering on the 'PT' status flags.
4526 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004527 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4528
4529 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004530 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004531 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004532
4533 req->analysers = 0;
4534 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004535
Willy Tarreau60b85b02008-11-30 23:28:40 +01004536 if (!(s->flags & SN_ERR_MASK))
4537 s->flags |= SN_ERR_PRXCOND;
4538 if (!(s->flags & SN_FINST_MASK))
4539 s->flags |= SN_FINST_T;
4540 return 0;
4541}
4542
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004543/* This function is an analyser which waits for the HTTP request body. It waits
4544 * for either the buffer to be full, or the full advertised contents to have
4545 * reached the buffer. It must only be called after the standard HTTP request
4546 * processing has occurred, because it expects the request to be parsed and will
4547 * look for the Expect header. It may send a 100-Continue interim response. It
4548 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4549 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4550 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004551 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004552int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004553{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004554 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004555 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004556
4557 /* We have to parse the HTTP request body to find any required data.
4558 * "balance url_param check_post" should have been the only way to get
4559 * into this. We were brought here after HTTP header analysis, so all
4560 * related structures are ready.
4561 */
4562
Willy Tarreau890988f2014-04-10 11:59:33 +02004563 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4564 /* This is the first call */
4565 if (msg->msg_state < HTTP_MSG_BODY)
4566 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004567
Willy Tarreau890988f2014-04-10 11:59:33 +02004568 if (msg->msg_state < HTTP_MSG_100_SENT) {
4569 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4570 * send an HTTP/1.1 100 Continue intermediate response.
4571 */
4572 if (msg->flags & HTTP_MSGF_VER_11) {
4573 struct hdr_ctx ctx;
4574 ctx.idx = 0;
4575 /* Expect is allowed in 1.1, look for it */
4576 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4577 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4578 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4579 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004580 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004581 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004582 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004583
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004584 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004585 * req->buf->p still points to the beginning of the message. We
4586 * must save the body in msg->next because it survives buffer
4587 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004588 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004589 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004590
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004591 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004592 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4593 else
4594 msg->msg_state = HTTP_MSG_DATA;
4595 }
4596
Willy Tarreau890988f2014-04-10 11:59:33 +02004597 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4598 /* We're in content-length mode, we just have to wait for enough data. */
4599 if (req->buf->i - msg->sov < msg->body_len)
4600 goto missing_data;
4601
4602 /* OK we have everything we need now */
4603 goto http_end;
4604 }
4605
4606 /* OK here we're parsing a chunked-encoded message */
4607
Willy Tarreau522d6c02009-12-06 18:49:18 +01004608 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004609 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004610 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004611 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004612 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004613 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004614
Willy Tarreau115acb92009-12-26 13:56:06 +01004615 if (!ret)
4616 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004617 else if (ret < 0) {
4618 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004619 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004620 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004621 }
4622
Willy Tarreaud98cf932009-12-27 22:54:55 +01004623 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004624 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004625 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004626 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004627 if (msg->msg_state == HTTP_MSG_TRAILERS)
4628 goto http_end;
4629
Willy Tarreau226071e2014-04-10 11:55:45 +02004630 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004631 goto http_end;
4632
4633 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004634 /* we get here if we need to wait for more data. If the buffer is full,
4635 * we have the maximum we can expect.
4636 */
4637 if (buffer_full(req->buf, global.tune.maxrewrite))
4638 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004639
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004640 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004641 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004642 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004643
4644 if (!(s->flags & SN_ERR_MASK))
4645 s->flags |= SN_ERR_CLITO;
4646 if (!(s->flags & SN_FINST_MASK))
4647 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004648 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004649 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004650
4651 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004652 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004653 /* Not enough data. We'll re-use the http-request
4654 * timeout here. Ideally, we should set the timeout
4655 * relative to the accept() date. We just set the
4656 * request timeout once at the beginning of the
4657 * request.
4658 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004659 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004660 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004661 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004662 return 0;
4663 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004664
4665 http_end:
4666 /* The situation will not evolve, so let's give up on the analysis. */
4667 s->logs.tv_request = now; /* update the request timer to reflect full request */
4668 req->analysers &= ~an_bit;
4669 req->analyse_exp = TICK_ETERNITY;
4670 return 1;
4671
4672 return_bad_req: /* let's centralize all bad requests */
4673 txn->req.msg_state = HTTP_MSG_ERROR;
4674 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004675 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004676
Willy Tarreau79ebac62010-06-07 13:47:49 +02004677 if (!(s->flags & SN_ERR_MASK))
4678 s->flags |= SN_ERR_PRXCOND;
4679 if (!(s->flags & SN_FINST_MASK))
4680 s->flags |= SN_FINST_R;
4681
Willy Tarreau522d6c02009-12-06 18:49:18 +01004682 return_err_msg:
4683 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004684 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004685 if (s->listener->counters)
4686 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004687 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004688}
4689
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004690/* send a server's name with an outgoing request over an established connection.
4691 * Note: this function is designed to be called once the request has been scheduled
4692 * for being forwarded. This is the reason why it rewinds the buffer before
4693 * proceeding.
4694 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004695int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004696
4697 struct hdr_ctx ctx;
4698
Mark Lamourinec2247f02012-01-04 13:02:01 -05004699 char *hdr_name = be->server_id_hdr_name;
4700 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004701 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004702 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004703 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004704
William Lallemandd9e90662012-01-30 17:27:17 +01004705 ctx.idx = 0;
4706
Willy Tarreau211cdec2014-04-17 20:18:08 +02004707 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004708 if (old_o) {
4709 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004710 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004711 txn->req.next += old_o;
4712 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004713 }
4714
Willy Tarreau9b28e032012-10-12 23:49:43 +02004715 old_i = chn->buf->i;
4716 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 -05004717 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004718 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004719 }
4720
4721 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004722 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004723 memcpy(hdr_val, hdr_name, hdr_name_len);
4724 hdr_val += hdr_name_len;
4725 *hdr_val++ = ':';
4726 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004727 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4728 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004729
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004730 if (old_o) {
4731 /* If this was a forwarded request, we must readjust the amount of
4732 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004733 * variations. Note that the current state is >= HTTP_MSG_BODY,
4734 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004735 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004736 old_o += chn->buf->i - old_i;
4737 b_adv(chn->buf, old_o);
4738 txn->req.next -= old_o;
4739 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004740 }
4741
Mark Lamourinec2247f02012-01-04 13:02:01 -05004742 return 0;
4743}
4744
Willy Tarreau610ecce2010-01-04 21:15:02 +01004745/* Terminate current transaction and prepare a new one. This is very tricky
4746 * right now but it works.
4747 */
4748void http_end_txn_clean_session(struct session *s)
4749{
Willy Tarreau068621e2013-12-23 15:11:25 +01004750 int prev_status = s->txn.status;
4751
Willy Tarreau610ecce2010-01-04 21:15:02 +01004752 /* FIXME: We need a more portable way of releasing a backend's and a
4753 * server's connections. We need a safer way to reinitialize buffer
4754 * flags. We also need a more accurate method for computing per-request
4755 * data.
4756 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004757
Willy Tarreau4213a112013-12-15 10:25:42 +01004758 /* unless we're doing keep-alive, we want to quickly close the connection
4759 * to the server.
4760 */
4761 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4762 !si_conn_ready(s->req->cons)) {
4763 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4764 si_shutr(s->req->cons);
4765 si_shutw(s->req->cons);
4766 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004767
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004768 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004769 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004770 if (unlikely(s->srv_conn))
4771 sess_change_server(s, NULL);
4772 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004773
4774 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4775 session_process_counters(s);
4776
4777 if (s->txn.status) {
4778 int n;
4779
4780 n = s->txn.status / 100;
4781 if (n < 1 || n > 5)
4782 n = 0;
4783
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004784 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004785 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004786 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004787 s->fe->fe_counters.p.http.comp_rsp++;
4788 }
Willy Tarreau24657792010-02-26 10:30:28 +01004789 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004790 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004791 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004792 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004793 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004794 s->be->be_counters.p.http.comp_rsp++;
4795 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004796 }
4797
4798 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004799 s->logs.bytes_in -= s->req->buf->i;
4800 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004801
4802 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004803 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004804 !(s->flags & SN_MONITOR) &&
4805 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4806 s->do_log(s);
4807 }
4808
Willy Tarreauc177ea72014-06-25 15:36:04 +02004809 /* stop tracking content-based counters */
4810 session_stop_content_counters(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004811 session_update_time_stats(s);
4812
Willy Tarreau610ecce2010-01-04 21:15:02 +01004813 s->logs.accept_date = date; /* user-visible date for logging */
4814 s->logs.tv_accept = now; /* corrected date for internal use */
4815 tv_zero(&s->logs.tv_request);
4816 s->logs.t_queue = -1;
4817 s->logs.t_connect = -1;
4818 s->logs.t_data = -1;
4819 s->logs.t_close = 0;
4820 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4821 s->logs.srv_queue_size = 0; /* we will get this number soon */
4822
Willy Tarreau9b28e032012-10-12 23:49:43 +02004823 s->logs.bytes_in = s->req->total = s->req->buf->i;
4824 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004825
4826 if (s->pend_pos)
4827 pendconn_free(s->pend_pos);
4828
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004829 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004830 if (s->flags & SN_CURR_SESS) {
4831 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004832 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004833 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004834 if (may_dequeue_tasks(objt_server(s->target), s->be))
4835 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004836 }
4837
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004838 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004839
Willy Tarreau4213a112013-12-15 10:25:42 +01004840 /* only release our endpoint if we don't intend to reuse the
4841 * connection.
4842 */
4843 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4844 !si_conn_ready(s->req->cons)) {
4845 si_release_endpoint(s->req->cons);
4846 }
4847
Willy Tarreau610ecce2010-01-04 21:15:02 +01004848 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004849 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004850 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004851 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004852 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreaub4d05092014-09-01 20:35:55 +02004853 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);
4854 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 +02004855 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 +01004856 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Cyril Bonté17f77072014-10-22 22:30:13 +02004857 s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004858
Willy Tarreau610ecce2010-01-04 21:15:02 +01004859 s->txn.meth = 0;
4860 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004861 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004862
4863 if (prev_status == 401 || prev_status == 407) {
4864 /* In HTTP keep-alive mode, if we receive a 401, we still have
4865 * a chance of being able to send the visitor again to the same
4866 * server over the same connection. This is required by some
4867 * broken protocols such as NTLM, and anyway whenever there is
4868 * an opportunity for sending the challenge to the proper place,
4869 * it's better to do it (at least it helps with debugging).
4870 */
4871 s->txn.flags |= TX_PREFER_LAST;
4872 }
4873
Willy Tarreauee55dc02010-06-01 10:56:34 +02004874 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004875 s->req->cons->flags |= SI_FL_INDEP_STR;
4876
Willy Tarreau96e31212011-05-30 18:10:30 +02004877 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004878 s->req->flags |= CF_NEVER_WAIT;
4879 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004880 }
4881
Willy Tarreau610ecce2010-01-04 21:15:02 +01004882 /* if the request buffer is not empty, it means we're
4883 * about to process another request, so send pending
4884 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004885 * Just don't do this if the buffer is close to be full,
4886 * because the request will wait for it to flush a little
4887 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004888 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004889 if (s->req->buf->i) {
4890 if (s->rep->buf->o &&
4891 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4892 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004893 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004894 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004895
4896 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004897 channel_auto_read(s->req);
4898 channel_auto_close(s->req);
4899 channel_auto_read(s->rep);
4900 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004901
Willy Tarreau27375622013-12-17 00:00:28 +01004902 /* we're in keep-alive with an idle connection, monitor it */
4903 si_idle_conn(s->req->cons);
4904
Willy Tarreau342b11c2010-11-24 16:22:09 +01004905 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004906 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004907}
4908
4909
4910/* This function updates the request state machine according to the response
4911 * state machine and buffer flags. It returns 1 if it changes anything (flag
4912 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4913 * it is only used to find when a request/response couple is complete. Both
4914 * this function and its equivalent should loop until both return zero. It
4915 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4916 */
4917int http_sync_req_state(struct session *s)
4918{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004919 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004920 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004921 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004922 unsigned int old_state = txn->req.msg_state;
4923
Willy Tarreau610ecce2010-01-04 21:15:02 +01004924 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4925 return 0;
4926
4927 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004928 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004929 * We can shut the read side unless we want to abort_on_close,
4930 * or we have a POST request. The issue with POST requests is
4931 * that some browsers still send a CRLF after the request, and
4932 * this CRLF must be read so that it does not remain in the kernel
4933 * buffers, otherwise a close could cause an RST on some systems
4934 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004935 * Note that if we're using keep-alive on the client side, we'd
4936 * rather poll now and keep the polling enabled for the whole
4937 * session's life than enabling/disabling it between each
4938 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004939 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004940 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4941 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4942 !(s->be->options & PR_O_ABRT_CLOSE) &&
4943 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004944 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004945
Willy Tarreau40f151a2012-12-20 12:10:09 +01004946 /* if the server closes the connection, we want to immediately react
4947 * and close the socket to save packets and syscalls.
4948 */
4949 chn->cons->flags |= SI_FL_NOHALF;
4950
Willy Tarreau610ecce2010-01-04 21:15:02 +01004951 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4952 goto wait_other_side;
4953
4954 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4955 /* The server has not finished to respond, so we
4956 * don't want to move in order not to upset it.
4957 */
4958 goto wait_other_side;
4959 }
4960
4961 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4962 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004963 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004964 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004965 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004966 goto wait_other_side;
4967 }
4968
4969 /* When we get here, it means that both the request and the
4970 * response have finished receiving. Depending on the connection
4971 * mode, we'll have to wait for the last bytes to leave in either
4972 * direction, and sometimes for a close to be effective.
4973 */
4974
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004975 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4976 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004977 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4978 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004979 }
4980 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4981 /* Option forceclose is set, or either side wants to close,
4982 * let's enforce it now that we're not expecting any new
4983 * data to come. The caller knows the session is complete
4984 * once both states are CLOSED.
4985 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004986 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4987 channel_shutr_now(chn);
4988 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004989 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004990 }
4991 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004992 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4993 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004994 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004995 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4996 channel_auto_read(chn);
4997 txn->req.msg_state = HTTP_MSG_TUNNEL;
4998 chn->flags |= CF_NEVER_WAIT;
4999 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005000 }
5001
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005002 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005003 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005004 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005005
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005006 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005007 txn->req.msg_state = HTTP_MSG_CLOSING;
5008 goto http_msg_closing;
5009 }
5010 else {
5011 txn->req.msg_state = HTTP_MSG_CLOSED;
5012 goto http_msg_closed;
5013 }
5014 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005015 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005016 }
5017
5018 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5019 http_msg_closing:
5020 /* nothing else to forward, just waiting for the output buffer
5021 * to be empty and for the shutw_now to take effect.
5022 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005023 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005024 txn->req.msg_state = HTTP_MSG_CLOSED;
5025 goto http_msg_closed;
5026 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005027 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005028 txn->req.msg_state = HTTP_MSG_ERROR;
5029 goto wait_other_side;
5030 }
5031 }
5032
5033 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5034 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005035 /* see above in MSG_DONE why we only do this in these states */
5036 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5037 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5038 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005039 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005040 goto wait_other_side;
5041 }
5042
5043 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005044 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005045}
5046
5047
5048/* This function updates the response state machine according to the request
5049 * state machine and buffer flags. It returns 1 if it changes anything (flag
5050 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5051 * it is only used to find when a request/response couple is complete. Both
5052 * this function and its equivalent should loop until both return zero. It
5053 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5054 */
5055int http_sync_res_state(struct session *s)
5056{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005057 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005058 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005059 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005060 unsigned int old_state = txn->rsp.msg_state;
5061
Willy Tarreau610ecce2010-01-04 21:15:02 +01005062 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5063 return 0;
5064
5065 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5066 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005067 * still monitor the server connection for a possible close
5068 * while the request is being uploaded, so we don't disable
5069 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005070 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005071 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005072
5073 if (txn->req.msg_state == HTTP_MSG_ERROR)
5074 goto wait_other_side;
5075
5076 if (txn->req.msg_state < HTTP_MSG_DONE) {
5077 /* The client seems to still be sending data, probably
5078 * because we got an error response during an upload.
5079 * We have the choice of either breaking the connection
5080 * or letting it pass through. Let's do the later.
5081 */
5082 goto wait_other_side;
5083 }
5084
5085 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5086 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005087 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005088 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005089 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005090 goto wait_other_side;
5091 }
5092
5093 /* When we get here, it means that both the request and the
5094 * response have finished receiving. Depending on the connection
5095 * mode, we'll have to wait for the last bytes to leave in either
5096 * direction, and sometimes for a close to be effective.
5097 */
5098
5099 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5100 /* Server-close mode : shut read and wait for the request
5101 * side to close its output buffer. The caller will detect
5102 * when we're in DONE and the other is in CLOSED and will
5103 * catch that for the final cleanup.
5104 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005105 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5106 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005107 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005108 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5109 /* Option forceclose is set, or either side wants to close,
5110 * let's enforce it now that we're not expecting any new
5111 * data to come. The caller knows the session is complete
5112 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005113 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005114 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5115 channel_shutr_now(chn);
5116 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005117 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005118 }
5119 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005120 /* The last possible modes are keep-alive and tunnel. Tunnel will
5121 * need to forward remaining data. Keep-alive will need to monitor
5122 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005123 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005124 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005125 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005126 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5127 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005128 }
5129
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005130 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005131 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005132 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005133 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5134 goto http_msg_closing;
5135 }
5136 else {
5137 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5138 goto http_msg_closed;
5139 }
5140 }
5141 goto wait_other_side;
5142 }
5143
5144 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5145 http_msg_closing:
5146 /* nothing else to forward, just waiting for the output buffer
5147 * to be empty and for the shutw_now to take effect.
5148 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005149 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005150 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5151 goto http_msg_closed;
5152 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005153 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005154 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005155 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005156 if (objt_server(s->target))
5157 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005158 goto wait_other_side;
5159 }
5160 }
5161
5162 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5163 http_msg_closed:
5164 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005165 bi_erase(chn);
5166 channel_auto_close(chn);
5167 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005168 goto wait_other_side;
5169 }
5170
5171 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005172 /* We force the response to leave immediately if we're waiting for the
5173 * other side, since there is no pending shutdown to push it out.
5174 */
5175 if (!channel_is_empty(chn))
5176 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005177 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005178}
5179
5180
5181/* Resync the request and response state machines. Return 1 if either state
5182 * changes.
5183 */
5184int http_resync_states(struct session *s)
5185{
5186 struct http_txn *txn = &s->txn;
5187 int old_req_state = txn->req.msg_state;
5188 int old_res_state = txn->rsp.msg_state;
5189
Willy Tarreau610ecce2010-01-04 21:15:02 +01005190 http_sync_req_state(s);
5191 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005192 if (!http_sync_res_state(s))
5193 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005194 if (!http_sync_req_state(s))
5195 break;
5196 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005197
Willy Tarreau610ecce2010-01-04 21:15:02 +01005198 /* OK, both state machines agree on a compatible state.
5199 * There are a few cases we're interested in :
5200 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5201 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5202 * directions, so let's simply disable both analysers.
5203 * - HTTP_MSG_CLOSED on the response only means we must abort the
5204 * request.
5205 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5206 * with server-close mode means we've completed one request and we
5207 * must re-initialize the server connection.
5208 */
5209
5210 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5211 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5212 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5213 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5214 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005215 channel_auto_close(s->req);
5216 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005217 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005218 channel_auto_close(s->rep);
5219 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005220 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005221 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5222 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005223 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005224 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005225 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005226 channel_auto_close(s->rep);
5227 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01005228 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005229 channel_abort(s->req);
5230 channel_auto_close(s->req);
5231 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005232 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005233 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005234 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5235 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005236 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005237 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5238 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5239 /* server-close/keep-alive: terminate this transaction,
5240 * possibly killing the server connection and reinitialize
5241 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005242 */
5243 http_end_txn_clean_session(s);
5244 }
5245
Willy Tarreau610ecce2010-01-04 21:15:02 +01005246 return txn->req.msg_state != old_req_state ||
5247 txn->rsp.msg_state != old_res_state;
5248}
5249
Willy Tarreaud98cf932009-12-27 22:54:55 +01005250/* This function is an analyser which forwards request body (including chunk
5251 * sizes if any). It is called as soon as we must forward, even if we forward
5252 * zero byte. The only situation where it must not be called is when we're in
5253 * tunnel mode and we want to forward till the close. It's used both to forward
5254 * remaining data and to resync after end of body. It expects the msg_state to
5255 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5256 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005257 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005258 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005259 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005260int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005261{
5262 struct http_txn *txn = &s->txn;
5263 struct http_msg *msg = &s->txn.req;
5264
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005265 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5266 return 0;
5267
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005268 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005269 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005270 /* Output closed while we were sending data. We must abort and
5271 * wake the other side up.
5272 */
5273 msg->msg_state = HTTP_MSG_ERROR;
5274 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005275 return 1;
5276 }
5277
Willy Tarreaud98cf932009-12-27 22:54:55 +01005278 /* Note that we don't have to send 100-continue back because we don't
5279 * need the data to complete our job, and it's up to the server to
5280 * decide whether to return 100, 417 or anything else in return of
5281 * an "Expect: 100-continue" header.
5282 */
5283
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005284 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005285 /* we have msg->sov which points to the first byte of message
5286 * body, and req->buf.p still points to the beginning of the
5287 * message. We forward the headers now, as we don't need them
5288 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005289 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005290 b_adv(req->buf, msg->sov);
5291 msg->next -= msg->sov;
5292 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005293
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005294 /* The previous analysers guarantee that the state is somewhere
5295 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5296 * msg->next are always correct.
5297 */
5298 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5299 if (msg->flags & HTTP_MSGF_TE_CHNK)
5300 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5301 else
5302 msg->msg_state = HTTP_MSG_DATA;
5303 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005304 }
5305
Willy Tarreau7ba23542014-04-17 21:50:00 +02005306 /* Some post-connect processing might want us to refrain from starting to
5307 * forward data. Currently, the only reason for this is "balance url_param"
5308 * whichs need to parse/process the request after we've enabled forwarding.
5309 */
5310 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5311 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5312 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005313 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005314 goto missing_data;
5315 }
5316 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5317 }
5318
Willy Tarreau80a92c02014-03-12 10:41:13 +01005319 /* in most states, we should abort in case of early close */
5320 channel_auto_close(req);
5321
Willy Tarreauefdf0942014-04-24 20:08:57 +02005322 if (req->to_forward) {
5323 /* We can't process the buffer's contents yet */
5324 req->flags |= CF_WAKE_WRITE;
5325 goto missing_data;
5326 }
5327
Willy Tarreaud98cf932009-12-27 22:54:55 +01005328 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005329 if (msg->msg_state == HTTP_MSG_DATA) {
5330 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005331 /* we may have some pending data starting at req->buf->p */
5332 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005333 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005334 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005335 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005336 msg->next += msg->chunk_len;
5337 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005338
5339 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005340 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005341 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005342 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005343 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005344 }
5345 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005346 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005347 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005348 * TRAILERS state.
5349 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005350 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005351
Willy Tarreau54d23df2012-10-25 19:04:45 +02005352 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005353 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005354 else if (ret < 0) {
5355 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005356 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005357 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005358 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005359 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005360 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005361 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005362 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005363 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005364 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005365
5366 if (ret == 0)
5367 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005368 else if (ret < 0) {
5369 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005370 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005371 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005372 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005373 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005374 /* we're in MSG_CHUNK_SIZE now */
5375 }
5376 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005377 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005378
5379 if (ret == 0)
5380 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005381 else if (ret < 0) {
5382 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005383 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005384 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005385 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005386 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005387 /* we're in HTTP_MSG_DONE now */
5388 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005389 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005390 int old_state = msg->msg_state;
5391
Willy Tarreau610ecce2010-01-04 21:15:02 +01005392 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005393
5394 /* we may have some pending data starting at req->buf->p
5395 * such as last chunk of data or trailers.
5396 */
5397 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005398 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005399 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005400 msg->next = 0;
5401
Willy Tarreau4fe41902010-06-07 22:27:41 +02005402 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005403 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5404 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005405 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005406 if (http_resync_states(s)) {
5407 /* some state changes occurred, maybe the analyser
5408 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005409 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005410 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005411 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005412 /* request errors are most likely due to
5413 * the server aborting the transfer.
5414 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005415 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005416 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005417 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005418 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005419 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005420 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005421 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005422 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005423
5424 /* If "option abortonclose" is set on the backend, we
5425 * want to monitor the client's connection and forward
5426 * any shutdown notification to the server, which will
5427 * decide whether to close or to go on processing the
5428 * request.
5429 */
5430 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005431 channel_auto_read(req);
5432 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005433 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005434 else if (s->txn.meth == HTTP_METH_POST) {
5435 /* POST requests may require to read extra CRLF
5436 * sent by broken browsers and which could cause
5437 * an RST to be sent upon close on some systems
5438 * (eg: Linux).
5439 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005440 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005441 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005442
Willy Tarreau610ecce2010-01-04 21:15:02 +01005443 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005444 }
5445 }
5446
Willy Tarreaud98cf932009-12-27 22:54:55 +01005447 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005448 /* we may have some pending data starting at req->buf->p */
5449 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005450 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005451 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5452
Willy Tarreaubed410e2014-04-22 08:19:34 +02005453 msg->next = 0;
5454 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5455
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005456 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005457 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005458 if (!(s->flags & SN_ERR_MASK))
5459 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005460 if (!(s->flags & SN_FINST_MASK)) {
5461 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5462 s->flags |= SN_FINST_H;
5463 else
5464 s->flags |= SN_FINST_D;
5465 }
5466
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005467 s->fe->fe_counters.cli_aborts++;
5468 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005469 if (objt_server(s->target))
5470 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005471
5472 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005473 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005474
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005475 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005476 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005477 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005478
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005479 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005480 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005481 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005482 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005483 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005484
Willy Tarreau5c620922011-05-11 19:56:11 +02005485 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005486 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005487 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005488 * modes are already handled by the stream sock layer. We must not do
5489 * this in content-length mode because it could present the MSG_MORE
5490 * flag with the last block of forwarded data, which would cause an
5491 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005492 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005493 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005494 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005495
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005496 return 0;
5497
Willy Tarreaud98cf932009-12-27 22:54:55 +01005498 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005499 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005500 if (s->listener->counters)
5501 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005502
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005503 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005504 /* we may have some pending data starting at req->buf->p */
5505 b_adv(req->buf, msg->next);
5506 msg->next = 0;
5507
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005508 txn->req.msg_state = HTTP_MSG_ERROR;
5509 if (txn->status) {
5510 /* Note: we don't send any error if some data were already sent */
5511 stream_int_retnclose(req->prod, NULL);
5512 } else {
5513 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005514 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005515 }
5516 req->analysers = 0;
5517 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005518
5519 if (!(s->flags & SN_ERR_MASK))
5520 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005521 if (!(s->flags & SN_FINST_MASK)) {
5522 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5523 s->flags |= SN_FINST_H;
5524 else
5525 s->flags |= SN_FINST_D;
5526 }
5527 return 0;
5528
5529 aborted_xfer:
5530 txn->req.msg_state = HTTP_MSG_ERROR;
5531 if (txn->status) {
5532 /* Note: we don't send any error if some data were already sent */
5533 stream_int_retnclose(req->prod, NULL);
5534 } else {
5535 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005536 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005537 }
5538 req->analysers = 0;
5539 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5540
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005541 s->fe->fe_counters.srv_aborts++;
5542 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005543 if (objt_server(s->target))
5544 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005545
5546 if (!(s->flags & SN_ERR_MASK))
5547 s->flags |= SN_ERR_SRVCL;
5548 if (!(s->flags & SN_FINST_MASK)) {
5549 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5550 s->flags |= SN_FINST_H;
5551 else
5552 s->flags |= SN_FINST_D;
5553 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005554 return 0;
5555}
5556
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005557/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5558 * processing can continue on next analysers, or zero if it either needs more
5559 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5560 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5561 * when it has nothing left to do, and may remove any analyser when it wants to
5562 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005563 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005564int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005565{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005566 struct http_txn *txn = &s->txn;
5567 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005568 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005569 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005570 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005571 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005572
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005573 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 +02005574 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005575 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005576 rep,
5577 rep->rex, rep->wex,
5578 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005579 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005580 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005581
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005582 /*
5583 * Now parse the partial (or complete) lines.
5584 * We will check the response syntax, and also join multi-line
5585 * headers. An index of all the lines will be elaborated while
5586 * parsing.
5587 *
5588 * For the parsing, we use a 28 states FSM.
5589 *
5590 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005591 * rep->buf->p = beginning of response
5592 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5593 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005594 * msg->eol = end of current header or line (LF or CRLF)
5595 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005596 */
5597
Willy Tarreau628c40c2014-04-24 19:11:26 +02005598 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005599 /* There's a protected area at the end of the buffer for rewriting
5600 * purposes. We don't want to start to parse the request if the
5601 * protected area is affected, because we may have to move processed
5602 * data later, which is much more complicated.
5603 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005604 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005605 if (unlikely(!channel_reserved(rep))) {
5606 /* some data has still not left the buffer, wake us once that's done */
5607 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5608 goto abort_response;
5609 channel_dont_close(rep);
5610 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005611 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005612 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005613 }
5614
Willy Tarreau379357a2013-06-08 12:55:46 +02005615 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5616 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5617 buffer_slow_realign(rep->buf);
5618
Willy Tarreau9b28e032012-10-12 23:49:43 +02005619 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005620 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005621 }
5622
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005623 /* 1: we might have to print this header in debug mode */
5624 if (unlikely((global.mode & MODE_DEBUG) &&
5625 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau8767b132014-10-21 19:36:09 +02005626 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005627 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005628
Willy Tarreau9b28e032012-10-12 23:49:43 +02005629 sol = rep->buf->p;
5630 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005631 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005632
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005633 sol += hdr_idx_first_pos(&txn->hdr_idx);
5634 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005635
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005636 while (cur_idx) {
5637 eol = sol + txn->hdr_idx.v[cur_idx].len;
5638 debug_hdr("srvhdr", s, sol, eol);
5639 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5640 cur_idx = txn->hdr_idx.v[cur_idx].next;
5641 }
5642 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005643
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005644 /*
5645 * Now we quickly check if we have found a full valid response.
5646 * If not so, we check the FD and buffer states before leaving.
5647 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005648 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005649 * responses are checked first.
5650 *
5651 * Depending on whether the client is still there or not, we
5652 * may send an error response back or not. Note that normally
5653 * we should only check for HTTP status there, and check I/O
5654 * errors somewhere else.
5655 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005656
Willy Tarreau655dce92009-11-08 13:10:58 +01005657 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005658 /* Invalid response */
5659 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5660 /* we detected a parsing error. We want to archive this response
5661 * in the dedicated proxy area for later troubleshooting.
5662 */
5663 hdr_response_bad:
5664 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005665 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005666
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005667 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005668 if (objt_server(s->target)) {
5669 objt_server(s->target)->counters.failed_resp++;
5670 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005671 }
Willy Tarreau64648412010-03-05 10:41:54 +01005672 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005673 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005674 rep->analysers = 0;
5675 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005676 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005677 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005678 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005679
5680 if (!(s->flags & SN_ERR_MASK))
5681 s->flags |= SN_ERR_PRXCOND;
5682 if (!(s->flags & SN_FINST_MASK))
5683 s->flags |= SN_FINST_H;
5684
5685 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005686 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005687
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005688 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005689 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005690 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005691 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005692 goto hdr_response_bad;
5693 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005694
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005695 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005696 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005697 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005698 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005699 else if (txn->flags & TX_NOT_FIRST)
5700 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005701
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005702 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005703 if (objt_server(s->target)) {
5704 objt_server(s->target)->counters.failed_resp++;
5705 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005706 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005707
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005708 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005709 rep->analysers = 0;
5710 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005711 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005712 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005713 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005714
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005715 if (!(s->flags & SN_ERR_MASK))
5716 s->flags |= SN_ERR_SRVCL;
5717 if (!(s->flags & SN_FINST_MASK))
5718 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005719 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005720 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005721
Willy Tarreau2a4f5112014-06-23 15:22:31 +02005722 /* read timeout : return a 504 to the client. */
5723 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005724 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005725 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005726 else if (txn->flags & TX_NOT_FIRST)
5727 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005728
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005729 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005730 if (objt_server(s->target)) {
5731 objt_server(s->target)->counters.failed_resp++;
5732 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005733 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005734
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005735 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005736 rep->analysers = 0;
5737 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005738 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005739 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005740 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005741
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005742 if (!(s->flags & SN_ERR_MASK))
5743 s->flags |= SN_ERR_SRVTO;
5744 if (!(s->flags & SN_FINST_MASK))
5745 s->flags |= SN_FINST_H;
5746 return 0;
5747 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005748
Willy Tarreauf003d372012-11-26 13:35:37 +01005749 /* client abort with an abortonclose */
5750 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5751 s->fe->fe_counters.cli_aborts++;
5752 s->be->be_counters.cli_aborts++;
5753 if (objt_server(s->target))
5754 objt_server(s->target)->counters.cli_aborts++;
5755
5756 rep->analysers = 0;
5757 channel_auto_close(rep);
5758
5759 txn->status = 400;
5760 bi_erase(rep);
5761 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5762
5763 if (!(s->flags & SN_ERR_MASK))
5764 s->flags |= SN_ERR_CLICL;
5765 if (!(s->flags & SN_FINST_MASK))
5766 s->flags |= SN_FINST_H;
5767
5768 /* process_session() will take care of the error */
5769 return 0;
5770 }
5771
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005772 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005773 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005774 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005775 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005776 else if (txn->flags & TX_NOT_FIRST)
5777 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005778
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005779 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005780 if (objt_server(s->target)) {
5781 objt_server(s->target)->counters.failed_resp++;
5782 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005783 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005784
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005785 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005786 rep->analysers = 0;
5787 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005788 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005789 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005790 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005791
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005792 if (!(s->flags & SN_ERR_MASK))
5793 s->flags |= SN_ERR_SRVCL;
5794 if (!(s->flags & SN_FINST_MASK))
5795 s->flags |= SN_FINST_H;
5796 return 0;
5797 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005798
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005799 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005800 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005801 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005802 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005803 else if (txn->flags & TX_NOT_FIRST)
5804 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005805
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005806 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005807 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005808 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005809
5810 if (!(s->flags & SN_ERR_MASK))
5811 s->flags |= SN_ERR_CLICL;
5812 if (!(s->flags & SN_FINST_MASK))
5813 s->flags |= SN_FINST_H;
5814
5815 /* process_session() will take care of the error */
5816 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005817 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005818
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005819 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005820 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005821 return 0;
5822 }
5823
5824 /* More interesting part now : we know that we have a complete
5825 * response which at least looks like HTTP. We have an indicator
5826 * of each header's length, so we can parse them quickly.
5827 */
5828
5829 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005830 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005831
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005832 /*
5833 * 1: get the status code
5834 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005835 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005836 if (n < 1 || n > 5)
5837 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005838 /* when the client triggers a 4xx from the server, it's most often due
5839 * to a missing object or permission. These events should be tracked
5840 * because if they happen often, it may indicate a brute force or a
5841 * vulnerability scan.
5842 */
5843 if (n == 4)
5844 session_inc_http_err_ctr(s);
5845
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005846 if (objt_server(s->target))
5847 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005848
Willy Tarreau5b154472009-12-21 20:11:07 +01005849 /* check if the response is HTTP/1.1 or above */
5850 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005851 ((rep->buf->p[5] > '1') ||
5852 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005853 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005854
5855 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005856 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 +01005857
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005858 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005859 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005860
Willy Tarreau9b28e032012-10-12 23:49:43 +02005861 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005862
Willy Tarreau39650402010-03-15 19:44:39 +01005863 /* Adjust server's health based on status code. Note: status codes 501
5864 * and 505 are triggered on demand by client request, so we must not
5865 * count them as server failures.
5866 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005867 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005868 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005869 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005870 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005871 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005872 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005873
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005874 /*
5875 * 2: check for cacheability.
5876 */
5877
5878 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005879 case 100:
5880 /*
5881 * We may be facing a 100-continue response, in which case this
5882 * is not the right response, and we're waiting for the next one.
5883 * Let's allow this response to go to the client and wait for the
5884 * next one.
5885 */
5886 hdr_idx_init(&txn->hdr_idx);
5887 msg->next -= channel_forward(rep, msg->next);
5888 msg->msg_state = HTTP_MSG_RPBEFORE;
5889 txn->status = 0;
5890 s->logs.t_data = -1; /* was not a response yet */
5891 goto next_one;
5892
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005893 case 200:
5894 case 203:
5895 case 206:
5896 case 300:
5897 case 301:
5898 case 410:
5899 /* RFC2616 @13.4:
5900 * "A response received with a status code of
5901 * 200, 203, 206, 300, 301 or 410 MAY be stored
5902 * by a cache (...) unless a cache-control
5903 * directive prohibits caching."
5904 *
5905 * RFC2616 @9.5: POST method :
5906 * "Responses to this method are not cacheable,
5907 * unless the response includes appropriate
5908 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005909 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005910 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005911 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005912 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5913 break;
5914 default:
5915 break;
5916 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005917
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005918 /*
5919 * 3: we may need to capture headers
5920 */
5921 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005922 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005923 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005924 txn->rsp.cap, s->fe->rsp_cap);
5925
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005926 /* 4: determine the transfer-length.
5927 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5928 * the presence of a message-body in a RESPONSE and its transfer length
5929 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005930 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005931 * All responses to the HEAD request method MUST NOT include a
5932 * message-body, even though the presence of entity-header fields
5933 * might lead one to believe they do. All 1xx (informational), 204
5934 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5935 * message-body. All other responses do include a message-body,
5936 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005937 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005938 * 1. Any response which "MUST NOT" include a message-body (such as the
5939 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5940 * always terminated by the first empty line after the header fields,
5941 * regardless of the entity-header fields present in the message.
5942 *
5943 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5944 * the "chunked" transfer-coding (Section 6.2) is used, the
5945 * transfer-length is defined by the use of this transfer-coding.
5946 * If a Transfer-Encoding header field is present and the "chunked"
5947 * transfer-coding is not present, the transfer-length is defined by
5948 * the sender closing the connection.
5949 *
5950 * 3. If a Content-Length header field is present, its decimal value in
5951 * OCTETs represents both the entity-length and the transfer-length.
5952 * If a message is received with both a Transfer-Encoding header
5953 * field and a Content-Length header field, the latter MUST be ignored.
5954 *
5955 * 4. If the message uses the media type "multipart/byteranges", and
5956 * the transfer-length is not otherwise specified, then this self-
5957 * delimiting media type defines the transfer-length. This media
5958 * type MUST NOT be used unless the sender knows that the recipient
5959 * can parse it; the presence in a request of a Range header with
5960 * multiple byte-range specifiers from a 1.1 client implies that the
5961 * client can parse multipart/byteranges responses.
5962 *
5963 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005964 */
5965
5966 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005967 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005968 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005969 * FIXME: should we parse anyway and return an error on chunked encoding ?
5970 */
5971 if (txn->meth == HTTP_METH_HEAD ||
5972 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005973 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005974 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005975 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005976 goto skip_content_length;
5977 }
5978
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005979 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005980 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005981 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005982 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005983 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005984 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5985 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005986 /* bad transfer-encoding (chunked followed by something else) */
5987 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005988 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005989 break;
5990 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005991 }
5992
Willy Tarreaudfa3d922015-04-30 10:57:51 +02005993 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005994 ctx.idx = 0;
Willy Tarreaudfa3d922015-04-30 10:57:51 +02005995 if (msg->flags & HTTP_MSGF_TE_CHNK) {
5996 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
5997 http_remove_header2(msg, &txn->hdr_idx, &ctx);
5998 }
5999 else while (!use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006000 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006001 signed long long cl;
6002
Willy Tarreauad14f752011-09-02 20:33:27 +02006003 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006004 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006005 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006006 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006007
Willy Tarreauad14f752011-09-02 20:33:27 +02006008 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006009 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006010 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006011 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006012
Willy Tarreauad14f752011-09-02 20:33:27 +02006013 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006014 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006015 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006016 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006017
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006018 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006019 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006020 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006021 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006022
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006023 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006024 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006025 }
6026
William Lallemand82fe75c2012-10-23 10:25:10 +02006027 if (s->fe->comp || s->be->comp)
6028 select_compression_response_header(s, rep->buf);
6029
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006030skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006031 /* Now we have to check if we need to modify the Connection header.
6032 * This is more difficult on the response than it is on the request,
6033 * because we can have two different HTTP versions and we don't know
6034 * how the client will interprete a response. For instance, let's say
6035 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6036 * HTTP/1.1 response without any header. Maybe it will bound itself to
6037 * HTTP/1.0 because it only knows about it, and will consider the lack
6038 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6039 * the lack of header as a keep-alive. Thus we will use two flags
6040 * indicating how a request MAY be understood by the client. In case
6041 * of multiple possibilities, we'll fix the header to be explicit. If
6042 * ambiguous cases such as both close and keepalive are seen, then we
6043 * will fall back to explicit close. Note that we won't take risks with
6044 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006045 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006046 */
6047
Willy Tarreaudc008c52010-02-01 16:20:08 +01006048 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6049 txn->status == 101)) {
6050 /* Either we've established an explicit tunnel, or we're
6051 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006052 * to understand the next protocols. We have to switch to tunnel
6053 * mode, so that we transfer the request and responses then let
6054 * this protocol pass unmodified. When we later implement specific
6055 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006056 * header which contains information about that protocol for
6057 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006058 */
6059 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6060 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006061 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6062 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006063 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6064 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006065 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006066
Willy Tarreau70dffda2014-01-30 03:07:23 +01006067 /* this situation happens when combining pretend-keepalive with httpclose. */
6068 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006069 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6070 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006071 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6072
Willy Tarreau60466522010-01-18 19:08:45 +01006073 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006074 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006075 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6076 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006077
Willy Tarreau60466522010-01-18 19:08:45 +01006078 /* now adjust header transformations depending on current state */
6079 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6080 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6081 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006082 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006083 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006084 }
Willy Tarreau60466522010-01-18 19:08:45 +01006085 else { /* SCL / KAL */
6086 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006087 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006088 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006089 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006090
Willy Tarreau60466522010-01-18 19:08:45 +01006091 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006092 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006093
Willy Tarreau60466522010-01-18 19:08:45 +01006094 /* Some keep-alive responses are converted to Server-close if
6095 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006096 */
Willy Tarreau60466522010-01-18 19:08:45 +01006097 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6098 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006099 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006100 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006101 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006102 }
6103
Willy Tarreau7959a552013-09-23 16:44:27 +02006104 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006105 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006106
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006107 /* end of job, return OK */
6108 rep->analysers &= ~an_bit;
6109 rep->analyse_exp = TICK_ETERNITY;
6110 channel_auto_close(rep);
6111 return 1;
6112
6113 abort_keep_alive:
6114 /* A keep-alive request to the server failed on a network error.
6115 * The client is required to retry. We need to close without returning
6116 * any other information so that the client retries.
6117 */
6118 txn->status = 0;
6119 rep->analysers = 0;
6120 s->req->analysers = 0;
6121 channel_auto_close(rep);
6122 s->logs.logwait = 0;
6123 s->logs.level = 0;
6124 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
6125 bi_erase(rep);
6126 stream_int_retnclose(rep->cons, NULL);
6127 return 0;
6128}
6129
6130/* This function performs all the processing enabled for the current response.
6131 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6132 * and updates s->rep->analysers. It might make sense to explode it into several
6133 * other functions. It works like process_request (see indications above).
6134 */
6135int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6136{
6137 struct http_txn *txn = &s->txn;
6138 struct http_msg *msg = &txn->rsp;
6139 struct proxy *cur_proxy;
6140 struct cond_wordlist *wl;
6141 struct http_res_rule *http_res_last_rule = NULL;
6142
6143 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6144 now_ms, __FUNCTION__,
6145 s,
6146 rep,
6147 rep->rex, rep->wex,
6148 rep->flags,
6149 rep->buf->i,
6150 rep->analysers);
6151
6152 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6153 return 0;
6154
6155 rep->analysers &= ~an_bit;
6156 rep->analyse_exp = TICK_ETERNITY;
6157
Willy Tarreau70730dd2014-04-24 18:06:27 +02006158 /* The stats applet needs to adjust the Connection header but we don't
6159 * apply any filter there.
6160 */
6161 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6162 goto skip_filters;
6163
Willy Tarreau58975672014-04-24 21:13:57 +02006164 /*
6165 * We will have to evaluate the filters.
6166 * As opposed to version 1.2, now they will be evaluated in the
6167 * filters order and not in the header order. This means that
6168 * each filter has to be validated among all headers.
6169 *
6170 * Filters are tried with ->be first, then with ->fe if it is
6171 * different from ->be.
6172 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006173
Willy Tarreau58975672014-04-24 21:13:57 +02006174 cur_proxy = s->be;
6175 while (1) {
6176 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006177
Willy Tarreau58975672014-04-24 21:13:57 +02006178 /* evaluate http-response rules */
6179 if (!http_res_last_rule)
6180 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 +02006181
Willy Tarreau58975672014-04-24 21:13:57 +02006182 /* try headers filters */
6183 if (rule_set->rsp_exp != NULL) {
6184 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6185 return_bad_resp:
6186 if (objt_server(s->target)) {
6187 objt_server(s->target)->counters.failed_resp++;
6188 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006189 }
Willy Tarreau58975672014-04-24 21:13:57 +02006190 s->be->be_counters.failed_resp++;
6191 return_srv_prx_502:
6192 rep->analysers = 0;
6193 txn->status = 502;
6194 s->logs.t_data = -1; /* was not a valid response */
6195 rep->prod->flags |= SI_FL_NOLINGER;
6196 bi_erase(rep);
6197 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6198 if (!(s->flags & SN_ERR_MASK))
6199 s->flags |= SN_ERR_PRXCOND;
6200 if (!(s->flags & SN_FINST_MASK))
6201 s->flags |= SN_FINST_H;
6202 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006203 }
Willy Tarreau58975672014-04-24 21:13:57 +02006204 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006205
Willy Tarreau58975672014-04-24 21:13:57 +02006206 /* has the response been denied ? */
6207 if (txn->flags & TX_SVDENY) {
6208 if (objt_server(s->target))
6209 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006210
Willy Tarreau58975672014-04-24 21:13:57 +02006211 s->be->be_counters.denied_resp++;
6212 s->fe->fe_counters.denied_resp++;
6213 if (s->listener->counters)
6214 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006215
Willy Tarreau58975672014-04-24 21:13:57 +02006216 goto return_srv_prx_502;
6217 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006218
Willy Tarreau58975672014-04-24 21:13:57 +02006219 /* add response headers from the rule sets in the same order */
6220 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006221 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006222 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006223 if (wl->cond) {
6224 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6225 ret = acl_pass(ret);
6226 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6227 ret = !ret;
6228 if (!ret)
6229 continue;
6230 }
6231 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6232 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006233 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006234
Willy Tarreau58975672014-04-24 21:13:57 +02006235 /* check whether we're already working on the frontend */
6236 if (cur_proxy == s->fe)
6237 break;
6238 cur_proxy = s->fe;
6239 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006240
Willy Tarreau58975672014-04-24 21:13:57 +02006241 /* OK that's all we can do for 1xx responses */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006242 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006243 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006244
Willy Tarreau58975672014-04-24 21:13:57 +02006245 /*
6246 * Now check for a server cookie.
6247 */
6248 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6249 (s->be->options & PR_O_CHK_CACHE))
6250 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006251
Willy Tarreau58975672014-04-24 21:13:57 +02006252 /*
6253 * Check for cache-control or pragma headers if required.
6254 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006255 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 +02006256 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006257
Willy Tarreau58975672014-04-24 21:13:57 +02006258 /*
6259 * Add server cookie in the response if needed
6260 */
6261 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6262 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6263 (!(s->flags & SN_DIRECT) ||
6264 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6265 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6266 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6267 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6268 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6269 !(s->flags & SN_IGNORE_PRST)) {
6270 /* the server is known, it's not the one the client requested, or the
6271 * cookie's last seen date needs to be refreshed. We have to
6272 * insert a set-cookie here, except if we want to insert only on POST
6273 * requests and this one isn't. Note that servers which don't have cookies
6274 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006275 */
Willy Tarreau58975672014-04-24 21:13:57 +02006276 if (!objt_server(s->target)->cookie) {
6277 chunk_printf(&trash,
6278 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6279 s->be->cookie_name);
6280 }
6281 else {
6282 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006283
Willy Tarreau58975672014-04-24 21:13:57 +02006284 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6285 /* emit last_date, which is mandatory */
6286 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6287 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6288 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006289
Willy Tarreau58975672014-04-24 21:13:57 +02006290 if (s->be->cookie_maxlife) {
6291 /* emit first_date, which is either the original one or
6292 * the current date.
6293 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006294 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006295 s30tob64(txn->cookie_first_date ?
6296 txn->cookie_first_date >> 2 :
6297 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006298 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006299 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006300 }
Willy Tarreau58975672014-04-24 21:13:57 +02006301 chunk_appendf(&trash, "; path=/");
6302 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006303
Willy Tarreau58975672014-04-24 21:13:57 +02006304 if (s->be->cookie_domain)
6305 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006306
Willy Tarreau58975672014-04-24 21:13:57 +02006307 if (s->be->ck_opts & PR_CK_HTTPONLY)
6308 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006309
Willy Tarreau58975672014-04-24 21:13:57 +02006310 if (s->be->ck_opts & PR_CK_SECURE)
6311 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006312
Willy Tarreau58975672014-04-24 21:13:57 +02006313 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6314 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006315
Willy Tarreau58975672014-04-24 21:13:57 +02006316 txn->flags &= ~TX_SCK_MASK;
6317 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6318 /* the server did not change, only the date was updated */
6319 txn->flags |= TX_SCK_UPDATED;
6320 else
6321 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006322
Willy Tarreau58975672014-04-24 21:13:57 +02006323 /* Here, we will tell an eventual cache on the client side that we don't
6324 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6325 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6326 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006327 */
Willy Tarreau58975672014-04-24 21:13:57 +02006328 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006329
Willy Tarreau58975672014-04-24 21:13:57 +02006330 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006331
Willy Tarreau58975672014-04-24 21:13:57 +02006332 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6333 "Cache-control: private", 22) < 0))
6334 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006335 }
Willy Tarreau58975672014-04-24 21:13:57 +02006336 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006337
Willy Tarreau58975672014-04-24 21:13:57 +02006338 /*
6339 * Check if result will be cacheable with a cookie.
6340 * We'll block the response if security checks have caught
6341 * nasty things such as a cacheable cookie.
6342 */
6343 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6344 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6345 (s->be->options & PR_O_CHK_CACHE)) {
6346 /* we're in presence of a cacheable response containing
6347 * a set-cookie header. We'll block it as requested by
6348 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006349 */
Willy Tarreau58975672014-04-24 21:13:57 +02006350 if (objt_server(s->target))
6351 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006352
Willy Tarreau58975672014-04-24 21:13:57 +02006353 s->be->be_counters.denied_resp++;
6354 s->fe->fe_counters.denied_resp++;
6355 if (s->listener->counters)
6356 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006357
Willy Tarreau58975672014-04-24 21:13:57 +02006358 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6359 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6360 send_log(s->be, LOG_ALERT,
6361 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6362 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6363 goto return_srv_prx_502;
6364 }
Willy Tarreau03945942009-12-22 16:50:27 +01006365
Willy Tarreau70730dd2014-04-24 18:06:27 +02006366 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006367 /*
6368 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6369 * If an "Upgrade" token is found, the header is left untouched in order
6370 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006371 * if anything but "Upgrade" is present in the Connection header. We don't
6372 * want to touch any 101 response either since it's switching to another
6373 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006374 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006375 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006376 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6377 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6378 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6379 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006380
Willy Tarreau58975672014-04-24 21:13:57 +02006381 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6382 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6383 /* we want a keep-alive response here. Keep-alive header
6384 * required if either side is not 1.1.
6385 */
6386 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6387 want_flags |= TX_CON_KAL_SET;
6388 }
6389 else {
6390 /* we want a close response here. Close header required if
6391 * the server is 1.1, regardless of the client.
6392 */
6393 if (msg->flags & HTTP_MSGF_VER_11)
6394 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006395 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006396
Willy Tarreau58975672014-04-24 21:13:57 +02006397 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6398 http_change_connection_header(txn, msg, want_flags);
6399 }
6400
6401 skip_header_mangling:
6402 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6403 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6404 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006405
Willy Tarreau58975672014-04-24 21:13:57 +02006406 /* if the user wants to log as soon as possible, without counting
6407 * bytes from the server, then this is the right moment. We have
6408 * to temporarily assign bytes_out to log what we currently have.
6409 */
6410 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6411 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6412 s->logs.bytes_out = txn->rsp.eoh;
6413 s->do_log(s);
6414 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006415 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006416 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006417}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006418
Willy Tarreaud98cf932009-12-27 22:54:55 +01006419/* This function is an analyser which forwards response body (including chunk
6420 * sizes if any). It is called as soon as we must forward, even if we forward
6421 * zero byte. The only situation where it must not be called is when we're in
6422 * tunnel mode and we want to forward till the close. It's used both to forward
6423 * remaining data and to resync after end of body. It expects the msg_state to
6424 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6425 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006426 *
6427 * It is capable of compressing response data both in content-length mode and
6428 * in chunked mode. The state machines follows different flows depending on
6429 * whether content-length and chunked modes are used, since there are no
6430 * trailers in content-length :
6431 *
6432 * chk-mode cl-mode
6433 * ,----- BODY -----.
6434 * / \
6435 * V size > 0 V chk-mode
6436 * .--> SIZE -------------> DATA -------------> CRLF
6437 * | | size == 0 | last byte |
6438 * | v final crlf v inspected |
6439 * | TRAILERS -----------> DONE |
6440 * | |
6441 * `----------------------------------------------'
6442 *
6443 * Compression only happens in the DATA state, and must be flushed in final
6444 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6445 * is performed at once on final states for all bytes parsed, or when leaving
6446 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006447 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006448int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006449{
6450 struct http_txn *txn = &s->txn;
6451 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006452 static struct buffer *tmpbuf = NULL;
6453 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006454 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006455
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006456 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6457 return 0;
6458
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006459 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006460 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006461 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006462 /* Output closed while we were sending data. We must abort and
6463 * wake the other side up.
6464 */
6465 msg->msg_state = HTTP_MSG_ERROR;
6466 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006467 return 1;
6468 }
6469
Willy Tarreau4fe41902010-06-07 22:27:41 +02006470 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006471 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006472
Willy Tarreau5bebcd02014-07-10 19:06:10 +02006473 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006474 /* we have msg->sov which points to the first byte of message
6475 * body, and res->buf.p still points to the beginning of the
6476 * message. We forward the headers now, as we don't need them
6477 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006478 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006479 b_adv(res->buf, msg->sov);
6480 msg->next -= msg->sov;
6481 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006482
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006483 /* The previous analysers guarantee that the state is somewhere
6484 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6485 * msg->next are always correct.
6486 */
6487 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6488 if (msg->flags & HTTP_MSGF_TE_CHNK)
6489 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6490 else
6491 msg->msg_state = HTTP_MSG_DATA;
6492 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006493 }
6494
Willy Tarreauefdf0942014-04-24 20:08:57 +02006495 if (res->to_forward) {
6496 /* We can't process the buffer's contents yet */
6497 res->flags |= CF_WAKE_WRITE;
6498 goto missing_data;
6499 }
6500
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006501 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6502 /* We need a compression buffer in the DATA state to put the
6503 * output of compressed data, and in CRLF state to let the
6504 * TRAILERS state finish the job of removing the trailing CRLF.
6505 */
6506 if (unlikely(tmpbuf == NULL)) {
6507 /* this is the first time we need the compression buffer */
6508 tmpbuf = pool_alloc2(pool2_buffer);
6509 if (tmpbuf == NULL)
6510 goto aborted_xfer; /* no memory */
6511 }
6512
6513 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006514 if (ret < 0) {
6515 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006516 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006517 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006518 compressing = 1;
6519 }
6520
Willy Tarreaud98cf932009-12-27 22:54:55 +01006521 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006522 switch (msg->msg_state - HTTP_MSG_DATA) {
6523 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006524 /* we may have some pending data starting at res->buf->p */
6525 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006526 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006527 if (ret < 0)
6528 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006529
Willy Tarreaud5a67832014-04-21 10:54:27 +02006530 if (msg->chunk_len) {
6531 /* input empty or output full */
6532 if (res->buf->i > msg->next)
6533 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006534 goto missing_data;
6535 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006536 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006537 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006538 if (msg->chunk_len > res->buf->i - msg->next) {
6539 /* output full */
6540 res->flags |= CF_WAKE_WRITE;
6541 goto missing_data;
6542 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006543 msg->next += msg->chunk_len;
6544 msg->chunk_len = 0;
6545 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006546
6547 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006548 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006549 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006550 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006551 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006552 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006553 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006554 /* fall through for HTTP_MSG_CHUNK_CRLF */
6555
6556 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6557 /* we want the CRLF after the data */
6558
6559 ret = http_skip_chunk_crlf(msg);
6560 if (ret == 0)
6561 goto missing_data;
6562 else if (ret < 0) {
6563 if (msg->err_pos >= 0)
6564 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6565 goto return_bad_res;
6566 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006567 /* we're in MSG_CHUNK_SIZE now, fall through */
6568
6569 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006570 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006571 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006572 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006573 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006574
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006575 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006576 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006577 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006578 else if (ret < 0) {
6579 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006580 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006581 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006582 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006583 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006584 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006585
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006586 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006587 if (unlikely(compressing)) {
6588 /* we need to flush output contents before syncing FSMs */
6589 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6590 compressing = 0;
6591 }
6592
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006593 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006594 if (ret == 0)
6595 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006596 else if (ret < 0) {
6597 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006598 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006599 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006600 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006601 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006602
6603 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006604 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006605 if (unlikely(compressing)) {
6606 /* we need to flush output contents before syncing FSMs */
6607 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6608 compressing = 0;
6609 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006610
Willy Tarreauc623c172014-04-18 09:53:50 +02006611 /* we may have some pending data starting at res->buf->p
6612 * such as a last chunk of data or trailers.
6613 */
6614 b_adv(res->buf, msg->next);
6615 msg->next = 0;
6616
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006617 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006618 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006619 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6620 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006621 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006622
Willy Tarreau610ecce2010-01-04 21:15:02 +01006623 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006624 /* some state changes occurred, maybe the analyser
6625 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006626 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006627 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006628 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006629 /* response errors are most likely due to
6630 * the client aborting the transfer.
6631 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006632 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006633 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006634 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006635 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006636 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006637 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006638 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006639 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006640 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006641 }
6642 }
6643
Willy Tarreaud98cf932009-12-27 22:54:55 +01006644 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006645 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006646 if (unlikely(compressing)) {
6647 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006648 compressing = 0;
6649 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006650
Willy Tarreauc623c172014-04-18 09:53:50 +02006651 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6652 b_adv(res->buf, msg->next);
6653 msg->next = 0;
6654 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6655 }
6656
Willy Tarreauf003d372012-11-26 13:35:37 +01006657 if (res->flags & CF_SHUTW)
6658 goto aborted_xfer;
6659
6660 /* stop waiting for data if the input is closed before the end. If the
6661 * client side was already closed, it means that the client has aborted,
6662 * so we don't want to count this as a server abort. Otherwise it's a
6663 * server abort.
6664 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006665 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006666 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006667 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006668 if (!(s->flags & SN_ERR_MASK))
6669 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006670 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006671 if (objt_server(s->target))
6672 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006673 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006674 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006675
Willy Tarreau40dba092010-03-04 18:14:51 +01006676 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006677 if (!s->req->analysers)
6678 goto return_bad_res;
6679
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006680 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006681 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006682 * Similarly, with keep-alive on the client side, we don't want to forward a
6683 * close.
6684 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006685 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006686 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6687 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006688 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006689
Willy Tarreau5c620922011-05-11 19:56:11 +02006690 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006691 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006692 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006693 * modes are already handled by the stream sock layer. We must not do
6694 * this in content-length mode because it could present the MSG_MORE
6695 * flag with the last block of forwarded data, which would cause an
6696 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006697 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006698 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006699 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006700
Willy Tarreaud98cf932009-12-27 22:54:55 +01006701 /* the session handler will take care of timeouts and errors */
6702 return 0;
6703
Willy Tarreau40dba092010-03-04 18:14:51 +01006704 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006705 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006706 if (objt_server(s->target))
6707 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006708
6709 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006710 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006711 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006712 compressing = 0;
6713 }
6714
Willy Tarreauc623c172014-04-18 09:53:50 +02006715 /* we may have some pending data starting at res->buf->p */
6716 if (s->comp_algo == NULL) {
6717 b_adv(res->buf, msg->next);
6718 msg->next = 0;
6719 }
6720
Willy Tarreaud98cf932009-12-27 22:54:55 +01006721 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006722 /* don't send any error message as we're in the body */
6723 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006724 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006725 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006726 if (objt_server(s->target))
6727 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006728
6729 if (!(s->flags & SN_ERR_MASK))
6730 s->flags |= SN_ERR_PRXCOND;
6731 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006732 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006733 return 0;
6734
6735 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006736 if (unlikely(compressing)) {
6737 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6738 compressing = 0;
6739 }
6740
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006741 txn->rsp.msg_state = HTTP_MSG_ERROR;
6742 /* don't send any error message as we're in the body */
6743 stream_int_retnclose(res->cons, NULL);
6744 res->analysers = 0;
6745 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6746
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006747 s->fe->fe_counters.cli_aborts++;
6748 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006749 if (objt_server(s->target))
6750 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006751
6752 if (!(s->flags & SN_ERR_MASK))
6753 s->flags |= SN_ERR_CLICL;
6754 if (!(s->flags & SN_FINST_MASK))
6755 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006756 return 0;
6757}
6758
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006759/* Iterate the same filter through all request headers.
6760 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006761 * Since it can manage the switch to another backend, it updates the per-proxy
6762 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006763 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006764int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006765{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006766 char *cur_ptr, *cur_end, *cur_next;
6767 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006768 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006769 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006770 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006771
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006772 last_hdr = 0;
6773
Willy Tarreau9b28e032012-10-12 23:49:43 +02006774 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006775 old_idx = 0;
6776
6777 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006778 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006779 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006780 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006781 (exp->action == ACT_ALLOW ||
6782 exp->action == ACT_DENY ||
6783 exp->action == ACT_TARPIT))
6784 return 0;
6785
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006786 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006787 if (!cur_idx)
6788 break;
6789
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006790 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006791 cur_ptr = cur_next;
6792 cur_end = cur_ptr + cur_hdr->len;
6793 cur_next = cur_end + cur_hdr->cr + 1;
6794
6795 /* Now we have one header between cur_ptr and cur_end,
6796 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006797 */
6798
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02006799 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006800 switch (exp->action) {
6801 case ACT_SETBE:
6802 /* It is not possible to jump a second time.
6803 * FIXME: should we return an HTTP/500 here so that
6804 * the admin knows there's a problem ?
6805 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006806 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006807 break;
6808
6809 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006810 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006811 last_hdr = 1;
6812 break;
6813
6814 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006815 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006816 last_hdr = 1;
6817 break;
6818
6819 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006820 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006821 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006822 break;
6823
6824 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006825 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006826 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006827 break;
6828
6829 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006830 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6831 if (trash.len < 0)
6832 return -1;
6833
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006834 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006835 /* FIXME: if the user adds a newline in the replacement, the
6836 * index will not be recalculated for now, and the new line
6837 * will not be counted as a new header.
6838 */
6839
6840 cur_end += delta;
6841 cur_next += delta;
6842 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006843 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006844 break;
6845
6846 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006847 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006848 cur_next += delta;
6849
Willy Tarreaufa355d42009-11-29 18:12:29 +01006850 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006851 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6852 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006853 cur_hdr->len = 0;
6854 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006855 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006856 break;
6857
6858 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006859 }
6860
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006861 /* keep the link from this header to next one in case of later
6862 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006863 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006864 old_idx = cur_idx;
6865 }
6866 return 0;
6867}
6868
6869
6870/* Apply the filter to the request line.
6871 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6872 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006873 * Since it can manage the switch to another backend, it updates the per-proxy
6874 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006875 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006876int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006877{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006878 char *cur_ptr, *cur_end;
6879 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006880 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006881 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006882
Willy Tarreau3d300592007-03-18 18:34:41 +01006883 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006884 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006885 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006886 (exp->action == ACT_ALLOW ||
6887 exp->action == ACT_DENY ||
6888 exp->action == ACT_TARPIT))
6889 return 0;
6890 else if (exp->action == ACT_REMOVE)
6891 return 0;
6892
6893 done = 0;
6894
Willy Tarreau9b28e032012-10-12 23:49:43 +02006895 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006896 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006897
6898 /* Now we have the request line between cur_ptr and cur_end */
6899
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02006900 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006901 switch (exp->action) {
6902 case ACT_SETBE:
6903 /* It is not possible to jump a second time.
6904 * FIXME: should we return an HTTP/500 here so that
6905 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006906 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006907 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006908 break;
6909
6910 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006911 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006912 done = 1;
6913 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006914
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006915 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006916 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006917 done = 1;
6918 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006919
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006920 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006921 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006922 done = 1;
6923 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006924
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006925 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006926 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006927 done = 1;
6928 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006929
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006930 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006931 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6932 if (trash.len < 0)
6933 return -1;
6934
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006935 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006936 /* FIXME: if the user adds a newline in the replacement, the
6937 * index will not be recalculated for now, and the new line
6938 * will not be counted as a new header.
6939 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006940
Willy Tarreaufa355d42009-11-29 18:12:29 +01006941 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006942 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006943 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006944 HTTP_MSG_RQMETH,
6945 cur_ptr, cur_end + 1,
6946 NULL, NULL);
6947 if (unlikely(!cur_end))
6948 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006949
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006950 /* we have a full request and we know that we have either a CR
6951 * or an LF at <ptr>.
6952 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006953 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6954 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006955 /* there is no point trying this regex on headers */
6956 return 1;
6957 }
6958 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006959 return done;
6960}
Willy Tarreau97de6242006-12-27 17:18:38 +01006961
Willy Tarreau58f10d72006-12-04 02:26:12 +01006962
Willy Tarreau58f10d72006-12-04 02:26:12 +01006963
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006964/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006965 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006966 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006967 * unparsable request. Since it can manage the switch to another backend, it
6968 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006969 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006970int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006971{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006972 struct http_txn *txn = &s->txn;
6973 struct hdr_exp *exp;
6974
6975 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006976 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006977
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006978 /*
6979 * The interleaving of transformations and verdicts
6980 * makes it difficult to decide to continue or stop
6981 * the evaluation.
6982 */
6983
Willy Tarreau6c123b12010-01-28 20:22:06 +01006984 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6985 break;
6986
Willy Tarreau3d300592007-03-18 18:34:41 +01006987 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006988 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006989 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006990 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006991
6992 /* if this filter had a condition, evaluate it now and skip to
6993 * next filter if the condition does not match.
6994 */
6995 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006996 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006997 ret = acl_pass(ret);
6998 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6999 ret = !ret;
7000
7001 if (!ret)
7002 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007003 }
7004
7005 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007006 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007007 if (unlikely(ret < 0))
7008 return -1;
7009
7010 if (likely(ret == 0)) {
7011 /* The filter did not match the request, it can be
7012 * iterated through all headers.
7013 */
Willy Tarreaua256fae2015-01-30 20:58:58 +01007014 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7015 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007016 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007017 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007018 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007019}
7020
7021
Willy Tarreaua15645d2007-03-18 16:22:39 +01007022
Willy Tarreau58f10d72006-12-04 02:26:12 +01007023/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007024 * Try to retrieve the server associated to the appsession.
7025 * If the server is found, it's assigned to the session.
7026 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007027void manage_client_side_appsession(struct session *s, const char *buf, int len) {
7028 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007029 appsess *asession = NULL;
7030 char *sessid_temp = NULL;
7031
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007032 if (len > s->be->appsession_len) {
7033 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007034 }
7035
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007036 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007037 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007038 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007039 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007040 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007041 }
7042
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007043 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007044 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007045 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007046 return;
7047 }
7048
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007049 memcpy(txn->sessid, buf, len);
7050 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007051 }
7052
7053 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7054 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007055 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007056 return;
7057 }
7058
Cyril Bontéb21570a2009-11-29 20:04:48 +01007059 memcpy(sessid_temp, buf, len);
7060 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007061
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007062 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007063 /* free previously allocated memory */
7064 pool_free2(apools.sessid, sessid_temp);
7065
7066 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007067 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7068 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007069 asession->request_count++;
7070
7071 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007072 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007073
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007074 while (srv) {
7075 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007076 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007077 (s->be->options & PR_O_PERSIST) ||
7078 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007079 /* we found the server and it's usable */
7080 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007081 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007082 s->flags |= SN_DIRECT | SN_ASSIGNED;
7083 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007084
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007085 break;
7086 } else {
7087 txn->flags &= ~TX_CK_MASK;
7088 txn->flags |= TX_CK_DOWN;
7089 }
7090 }
7091 srv = srv->next;
7092 }
7093 }
7094 }
7095}
7096
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007097/* Find the end of a cookie value contained between <s> and <e>. It works the
7098 * same way as with headers above except that the semi-colon also ends a token.
7099 * See RFC2965 for more information. Note that it requires a valid header to
7100 * return a valid result.
7101 */
7102char *find_cookie_value_end(char *s, const char *e)
7103{
7104 int quoted, qdpair;
7105
7106 quoted = qdpair = 0;
7107 for (; s < e; s++) {
7108 if (qdpair) qdpair = 0;
7109 else if (quoted) {
7110 if (*s == '\\') qdpair = 1;
7111 else if (*s == '"') quoted = 0;
7112 }
7113 else if (*s == '"') quoted = 1;
7114 else if (*s == ',' || *s == ';') return s;
7115 }
7116 return s;
7117}
7118
7119/* Delete a value in a header between delimiters <from> and <next> in buffer
7120 * <buf>. The number of characters displaced is returned, and the pointer to
7121 * the first delimiter is updated if required. The function tries as much as
7122 * possible to respect the following principles :
7123 * - replace <from> delimiter by the <next> one unless <from> points to a
7124 * colon, in which case <next> is simply removed
7125 * - set exactly one space character after the new first delimiter, unless
7126 * there are not enough characters in the block being moved to do so.
7127 * - remove unneeded spaces before the previous delimiter and after the new
7128 * one.
7129 *
7130 * It is the caller's responsibility to ensure that :
7131 * - <from> points to a valid delimiter or the colon ;
7132 * - <next> points to a valid delimiter or the final CR/LF ;
7133 * - there are non-space chars before <from> ;
7134 * - there is a CR/LF at or after <next>.
7135 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007136int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007137{
7138 char *prev = *from;
7139
7140 if (*prev == ':') {
7141 /* We're removing the first value, preserve the colon and add a
7142 * space if possible.
7143 */
7144 if (!http_is_crlf[(unsigned char)*next])
7145 next++;
7146 prev++;
7147 if (prev < next)
7148 *prev++ = ' ';
7149
7150 while (http_is_spht[(unsigned char)*next])
7151 next++;
7152 } else {
7153 /* Remove useless spaces before the old delimiter. */
7154 while (http_is_spht[(unsigned char)*(prev-1)])
7155 prev--;
7156 *from = prev;
7157
7158 /* copy the delimiter and if possible a space if we're
7159 * not at the end of the line.
7160 */
7161 if (!http_is_crlf[(unsigned char)*next]) {
7162 *prev++ = *next++;
7163 if (prev + 1 < next)
7164 *prev++ = ' ';
7165 while (http_is_spht[(unsigned char)*next])
7166 next++;
7167 }
7168 }
7169 return buffer_replace2(buf, prev, next, NULL, 0);
7170}
7171
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007172/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007173 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007174 * desirable to call it only when needed. This code is quite complex because
7175 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7176 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007177 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007178void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007179{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007180 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007181 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007182 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007183 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7184 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007185
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007186 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007187 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007188 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007189
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007190 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007191 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007192 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007193
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007194 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007195 hdr_beg = hdr_next;
7196 hdr_end = hdr_beg + cur_hdr->len;
7197 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007198
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007199 /* We have one full header between hdr_beg and hdr_end, and the
7200 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007201 * "Cookie:" headers.
7202 */
7203
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007204 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007205 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007206 old_idx = cur_idx;
7207 continue;
7208 }
7209
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007210 del_from = NULL; /* nothing to be deleted */
7211 preserve_hdr = 0; /* assume we may kill the whole header */
7212
Willy Tarreau58f10d72006-12-04 02:26:12 +01007213 /* Now look for cookies. Conforming to RFC2109, we have to support
7214 * attributes whose name begin with a '$', and associate them with
7215 * the right cookie, if we want to delete this cookie.
7216 * So there are 3 cases for each cookie read :
7217 * 1) it's a special attribute, beginning with a '$' : ignore it.
7218 * 2) it's a server id cookie that we *MAY* want to delete : save
7219 * some pointers on it (last semi-colon, beginning of cookie...)
7220 * 3) it's an application cookie : we *MAY* have to delete a previous
7221 * "special" cookie.
7222 * At the end of loop, if a "special" cookie remains, we may have to
7223 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007224 * *MUST* delete it.
7225 *
7226 * Note: RFC2965 is unclear about the processing of spaces around
7227 * the equal sign in the ATTR=VALUE form. A careful inspection of
7228 * the RFC explicitly allows spaces before it, and not within the
7229 * tokens (attrs or values). An inspection of RFC2109 allows that
7230 * too but section 10.1.3 lets one think that spaces may be allowed
7231 * after the equal sign too, resulting in some (rare) buggy
7232 * implementations trying to do that. So let's do what servers do.
7233 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7234 * allowed quoted strings in values, with any possible character
7235 * after a backslash, including control chars and delimitors, which
7236 * causes parsing to become ambiguous. Browsers also allow spaces
7237 * within values even without quotes.
7238 *
7239 * We have to keep multiple pointers in order to support cookie
7240 * removal at the beginning, middle or end of header without
7241 * corrupting the header. All of these headers are valid :
7242 *
7243 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7244 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7245 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7246 * | | | | | | | | |
7247 * | | | | | | | | hdr_end <--+
7248 * | | | | | | | +--> next
7249 * | | | | | | +----> val_end
7250 * | | | | | +-----------> val_beg
7251 * | | | | +--------------> equal
7252 * | | | +----------------> att_end
7253 * | | +---------------------> att_beg
7254 * | +--------------------------> prev
7255 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007256 */
7257
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007258 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7259 /* Iterate through all cookies on this line */
7260
7261 /* find att_beg */
7262 att_beg = prev + 1;
7263 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7264 att_beg++;
7265
7266 /* find att_end : this is the first character after the last non
7267 * space before the equal. It may be equal to hdr_end.
7268 */
7269 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007270
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007271 while (equal < hdr_end) {
7272 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007273 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007274 if (http_is_spht[(unsigned char)*equal++])
7275 continue;
7276 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007277 }
7278
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007279 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7280 * is between <att_beg> and <equal>, both may be identical.
7281 */
7282
7283 /* look for end of cookie if there is an equal sign */
7284 if (equal < hdr_end && *equal == '=') {
7285 /* look for the beginning of the value */
7286 val_beg = equal + 1;
7287 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7288 val_beg++;
7289
7290 /* find the end of the value, respecting quotes */
7291 next = find_cookie_value_end(val_beg, hdr_end);
7292
7293 /* make val_end point to the first white space or delimitor after the value */
7294 val_end = next;
7295 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7296 val_end--;
7297 } else {
7298 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007299 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007300
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007301 /* We have nothing to do with attributes beginning with '$'. However,
7302 * they will automatically be removed if a header before them is removed,
7303 * since they're supposed to be linked together.
7304 */
7305 if (*att_beg == '$')
7306 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007307
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007308 /* Ignore cookies with no equal sign */
7309 if (equal == next) {
7310 /* This is not our cookie, so we must preserve it. But if we already
7311 * scheduled another cookie for removal, we cannot remove the
7312 * complete header, but we can remove the previous block itself.
7313 */
7314 preserve_hdr = 1;
7315 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007316 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007317 val_end += delta;
7318 next += delta;
7319 hdr_end += delta;
7320 hdr_next += delta;
7321 cur_hdr->len += delta;
7322 http_msg_move_end(&txn->req, delta);
7323 prev = del_from;
7324 del_from = NULL;
7325 }
7326 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007327 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007328
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007329 /* if there are spaces around the equal sign, we need to
7330 * strip them otherwise we'll get trouble for cookie captures,
7331 * or even for rewrites. Since this happens extremely rarely,
7332 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007333 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007334 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7335 int stripped_before = 0;
7336 int stripped_after = 0;
7337
7338 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007339 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007340 equal += stripped_before;
7341 val_beg += stripped_before;
7342 }
7343
7344 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007345 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007346 val_beg += stripped_after;
7347 stripped_before += stripped_after;
7348 }
7349
7350 val_end += stripped_before;
7351 next += stripped_before;
7352 hdr_end += stripped_before;
7353 hdr_next += stripped_before;
7354 cur_hdr->len += stripped_before;
7355 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007356 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007357 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007358
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007359 /* First, let's see if we want to capture this cookie. We check
7360 * that we don't already have a client side cookie, because we
7361 * can only capture one. Also as an optimisation, we ignore
7362 * cookies shorter than the declared name.
7363 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007364 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7365 (val_end - att_beg >= s->fe->capture_namelen) &&
7366 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007367 int log_len = val_end - att_beg;
7368
7369 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7370 Alert("HTTP logging : out of memory.\n");
7371 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007372 if (log_len > s->fe->capture_len)
7373 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007374 memcpy(txn->cli_cookie, att_beg, log_len);
7375 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007376 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007377 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007378
Willy Tarreaubca99692010-10-06 19:25:55 +02007379 /* Persistence cookies in passive, rewrite or insert mode have the
7380 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007381 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007382 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007383 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007384 * For cookies in prefix mode, the form is :
7385 *
7386 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007387 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007388 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7389 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7390 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007391 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007392
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007393 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7394 * have the server ID between val_beg and delim, and the original cookie between
7395 * delim+1 and val_end. Otherwise, delim==val_end :
7396 *
7397 * Cookie: NAME=SRV; # in all but prefix modes
7398 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7399 * | || || | |+-> next
7400 * | || || | +--> val_end
7401 * | || || +---------> delim
7402 * | || |+------------> val_beg
7403 * | || +-------------> att_end = equal
7404 * | |+-----------------> att_beg
7405 * | +------------------> prev
7406 * +-------------------------> hdr_beg
7407 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007408
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007409 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007410 for (delim = val_beg; delim < val_end; delim++)
7411 if (*delim == COOKIE_DELIM)
7412 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007413 } else {
7414 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007415 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007416 /* Now check if the cookie contains a date field, which would
7417 * appear after a vertical bar ('|') just after the server name
7418 * and before the delimiter.
7419 */
7420 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7421 if (vbar1) {
7422 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007423 * right is the last seen date. It is a base64 encoded
7424 * 30-bit value representing the UNIX date since the
7425 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007426 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007427 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007428 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007429 if (val_end - vbar1 >= 5) {
7430 val = b64tos30(vbar1);
7431 if (val > 0)
7432 txn->cookie_last_date = val << 2;
7433 }
7434 /* look for a second vertical bar */
7435 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7436 if (vbar1 && (val_end - vbar1 > 5)) {
7437 val = b64tos30(vbar1 + 1);
7438 if (val > 0)
7439 txn->cookie_first_date = val << 2;
7440 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007441 }
7442 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007443
Willy Tarreauf64d1412010-10-07 20:06:11 +02007444 /* if the cookie has an expiration date and the proxy wants to check
7445 * it, then we do that now. We first check if the cookie is too old,
7446 * then only if it has expired. We detect strict overflow because the
7447 * time resolution here is not great (4 seconds). Cookies with dates
7448 * in the future are ignored if their offset is beyond one day. This
7449 * allows an admin to fix timezone issues without expiring everyone
7450 * and at the same time avoids keeping unwanted side effects for too
7451 * long.
7452 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007453 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7454 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007455 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007456 txn->flags &= ~TX_CK_MASK;
7457 txn->flags |= TX_CK_OLD;
7458 delim = val_beg; // let's pretend we have not found the cookie
7459 txn->cookie_first_date = 0;
7460 txn->cookie_last_date = 0;
7461 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007462 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7463 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007464 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007465 txn->flags &= ~TX_CK_MASK;
7466 txn->flags |= TX_CK_EXPIRED;
7467 delim = val_beg; // let's pretend we have not found the cookie
7468 txn->cookie_first_date = 0;
7469 txn->cookie_last_date = 0;
7470 }
7471
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007472 /* Here, we'll look for the first running server which supports the cookie.
7473 * This allows to share a same cookie between several servers, for example
7474 * to dedicate backup servers to specific servers only.
7475 * However, to prevent clients from sticking to cookie-less backup server
7476 * when they have incidentely learned an empty cookie, we simply ignore
7477 * empty cookies and mark them as invalid.
7478 * The same behaviour is applied when persistence must be ignored.
7479 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007480 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007481 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007482
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007483 while (srv) {
7484 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7485 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007486 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007487 (s->be->options & PR_O_PERSIST) ||
7488 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007489 /* we found the server and we can use it */
7490 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007491 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007492 s->flags |= SN_DIRECT | SN_ASSIGNED;
7493 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007494 break;
7495 } else {
7496 /* we found a server, but it's down,
7497 * mark it as such and go on in case
7498 * another one is available.
7499 */
7500 txn->flags &= ~TX_CK_MASK;
7501 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007502 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007503 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007504 srv = srv->next;
7505 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007506
Willy Tarreauf64d1412010-10-07 20:06:11 +02007507 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007508 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007509 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007510 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007511 txn->flags |= TX_CK_UNUSED;
7512 else
7513 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007514 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007515
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007516 /* depending on the cookie mode, we may have to either :
7517 * - delete the complete cookie if we're in insert+indirect mode, so that
7518 * the server never sees it ;
7519 * - remove the server id from the cookie value, and tag the cookie as an
7520 * application cookie so that it does not get accidentely removed later,
7521 * if we're in cookie prefix mode
7522 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007523 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007524 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007525
Willy Tarreau9b28e032012-10-12 23:49:43 +02007526 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007527 val_end += delta;
7528 next += delta;
7529 hdr_end += delta;
7530 hdr_next += delta;
7531 cur_hdr->len += delta;
7532 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007533
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007534 del_from = NULL;
7535 preserve_hdr = 1; /* we want to keep this cookie */
7536 }
7537 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007538 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007539 del_from = prev;
7540 }
7541 } else {
7542 /* This is not our cookie, so we must preserve it. But if we already
7543 * scheduled another cookie for removal, we cannot remove the
7544 * complete header, but we can remove the previous block itself.
7545 */
7546 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007547
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007548 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007549 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007550 if (att_beg >= del_from)
7551 att_beg += delta;
7552 if (att_end >= del_from)
7553 att_end += delta;
7554 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007555 val_end += delta;
7556 next += delta;
7557 hdr_end += delta;
7558 hdr_next += delta;
7559 cur_hdr->len += delta;
7560 http_msg_move_end(&txn->req, delta);
7561 prev = del_from;
7562 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007563 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007564 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007565
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007566 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007567 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007568 int cmp_len, value_len;
7569 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007570
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007571 if (s->be->options2 & PR_O2_AS_PFX) {
7572 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7573 value_begin = att_beg + s->be->appsession_name_len;
7574 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007575 } else {
7576 cmp_len = att_end - att_beg;
7577 value_begin = val_beg;
7578 value_len = val_end - val_beg;
7579 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007580
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007581 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007582 if (cmp_len == s->be->appsession_name_len &&
7583 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7584 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007585 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007586 }
7587
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007588 /* continue with next cookie on this header line */
7589 att_beg = next;
7590 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007591
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007592 /* There are no more cookies on this line.
7593 * We may still have one (or several) marked for deletion at the
7594 * end of the line. We must do this now in two ways :
7595 * - if some cookies must be preserved, we only delete from the
7596 * mark to the end of line ;
7597 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007598 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007599 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007600 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007601 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007602 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007603 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007604 cur_hdr->len += delta;
7605 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007606 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007607
7608 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007609 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7610 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007611 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007612 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007613 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007614 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007615 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007616 }
7617
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007618 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007619 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007620 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007621}
7622
7623
Willy Tarreaua15645d2007-03-18 16:22:39 +01007624/* Iterate the same filter through all response headers contained in <rtr>.
7625 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7626 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007627int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007628{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007629 char *cur_ptr, *cur_end, *cur_next;
7630 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007631 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007632 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007633 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007634
7635 last_hdr = 0;
7636
Willy Tarreau9b28e032012-10-12 23:49:43 +02007637 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007638 old_idx = 0;
7639
7640 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007641 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007642 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007643 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007644 (exp->action == ACT_ALLOW ||
7645 exp->action == ACT_DENY))
7646 return 0;
7647
7648 cur_idx = txn->hdr_idx.v[old_idx].next;
7649 if (!cur_idx)
7650 break;
7651
7652 cur_hdr = &txn->hdr_idx.v[cur_idx];
7653 cur_ptr = cur_next;
7654 cur_end = cur_ptr + cur_hdr->len;
7655 cur_next = cur_end + cur_hdr->cr + 1;
7656
7657 /* Now we have one header between cur_ptr and cur_end,
7658 * and the next header starts at cur_next.
7659 */
7660
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007661 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007662 switch (exp->action) {
7663 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007664 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007665 last_hdr = 1;
7666 break;
7667
7668 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007669 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007670 last_hdr = 1;
7671 break;
7672
7673 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007674 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7675 if (trash.len < 0)
7676 return -1;
7677
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007678 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007679 /* FIXME: if the user adds a newline in the replacement, the
7680 * index will not be recalculated for now, and the new line
7681 * will not be counted as a new header.
7682 */
7683
7684 cur_end += delta;
7685 cur_next += delta;
7686 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007687 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007688 break;
7689
7690 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007691 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007692 cur_next += delta;
7693
Willy Tarreaufa355d42009-11-29 18:12:29 +01007694 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007695 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7696 txn->hdr_idx.used--;
7697 cur_hdr->len = 0;
7698 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007699 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007700 break;
7701
7702 }
7703 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007704
7705 /* keep the link from this header to next one in case of later
7706 * removal of next header.
7707 */
7708 old_idx = cur_idx;
7709 }
7710 return 0;
7711}
7712
7713
7714/* Apply the filter to the status line in the response buffer <rtr>.
7715 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7716 * or -1 if a replacement resulted in an invalid status line.
7717 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007718int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007719{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007720 char *cur_ptr, *cur_end;
7721 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007722 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007723 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007724
7725
Willy Tarreau3d300592007-03-18 18:34:41 +01007726 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007727 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007728 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007729 (exp->action == ACT_ALLOW ||
7730 exp->action == ACT_DENY))
7731 return 0;
7732 else if (exp->action == ACT_REMOVE)
7733 return 0;
7734
7735 done = 0;
7736
Willy Tarreau9b28e032012-10-12 23:49:43 +02007737 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007738 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007739
7740 /* Now we have the status line between cur_ptr and cur_end */
7741
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007742 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007743 switch (exp->action) {
7744 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007745 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007746 done = 1;
7747 break;
7748
7749 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007750 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007751 done = 1;
7752 break;
7753
7754 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007755 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7756 if (trash.len < 0)
7757 return -1;
7758
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007759 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007760 /* FIXME: if the user adds a newline in the replacement, the
7761 * index will not be recalculated for now, and the new line
7762 * will not be counted as a new header.
7763 */
7764
Willy Tarreaufa355d42009-11-29 18:12:29 +01007765 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007766 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007767 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007768 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007769 cur_ptr, cur_end + 1,
7770 NULL, NULL);
7771 if (unlikely(!cur_end))
7772 return -1;
7773
7774 /* we have a full respnse and we know that we have either a CR
7775 * or an LF at <ptr>.
7776 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007777 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007778 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007779 /* there is no point trying this regex on headers */
7780 return 1;
7781 }
7782 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007783 return done;
7784}
7785
7786
7787
7788/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007789 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007790 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7791 * unparsable response.
7792 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007793int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007794{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007795 struct http_txn *txn = &s->txn;
7796 struct hdr_exp *exp;
7797
7798 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007799 int ret;
7800
7801 /*
7802 * The interleaving of transformations and verdicts
7803 * makes it difficult to decide to continue or stop
7804 * the evaluation.
7805 */
7806
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007807 if (txn->flags & TX_SVDENY)
7808 break;
7809
Willy Tarreau3d300592007-03-18 18:34:41 +01007810 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007811 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7812 exp->action == ACT_PASS)) {
7813 exp = exp->next;
7814 continue;
7815 }
7816
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007817 /* if this filter had a condition, evaluate it now and skip to
7818 * next filter if the condition does not match.
7819 */
7820 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007821 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007822 ret = acl_pass(ret);
7823 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7824 ret = !ret;
7825 if (!ret)
7826 continue;
7827 }
7828
Willy Tarreaua15645d2007-03-18 16:22:39 +01007829 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007830 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007831 if (unlikely(ret < 0))
7832 return -1;
7833
7834 if (likely(ret == 0)) {
7835 /* The filter did not match the response, it can be
7836 * iterated through all headers.
7837 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007838 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7839 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007840 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007841 }
7842 return 0;
7843}
7844
7845
Willy Tarreaua15645d2007-03-18 16:22:39 +01007846/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007847 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007848 * desirable to call it only when needed. This function is also used when we
7849 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007850 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007851void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007852{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007853 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007854 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007855 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007856 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007857 char *hdr_beg, *hdr_end, *hdr_next;
7858 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007859
Willy Tarreaua15645d2007-03-18 16:22:39 +01007860 /* Iterate through the headers.
7861 * we start with the start line.
7862 */
7863 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007864 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007865
7866 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7867 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007868 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007869
7870 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007871 hdr_beg = hdr_next;
7872 hdr_end = hdr_beg + cur_hdr->len;
7873 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007874
Willy Tarreau24581ba2010-08-31 22:39:35 +02007875 /* We have one full header between hdr_beg and hdr_end, and the
7876 * next header starts at hdr_next. We're only interested in
7877 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007878 */
7879
Willy Tarreau24581ba2010-08-31 22:39:35 +02007880 is_cookie2 = 0;
7881 prev = hdr_beg + 10;
7882 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007883 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007884 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7885 if (!val) {
7886 old_idx = cur_idx;
7887 continue;
7888 }
7889 is_cookie2 = 1;
7890 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007891 }
7892
Willy Tarreau24581ba2010-08-31 22:39:35 +02007893 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7894 * <prev> points to the colon.
7895 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007896 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007897
Willy Tarreau24581ba2010-08-31 22:39:35 +02007898 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7899 * check-cache is enabled) and we are not interested in checking
7900 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007901 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007902 if (s->be->cookie_name == NULL &&
7903 s->be->appsession_name == NULL &&
7904 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007905 return;
7906
Willy Tarreau24581ba2010-08-31 22:39:35 +02007907 /* OK so now we know we have to process this response cookie.
7908 * The format of the Set-Cookie header is slightly different
7909 * from the format of the Cookie header in that it does not
7910 * support the comma as a cookie delimiter (thus the header
7911 * cannot be folded) because the Expires attribute described in
7912 * the original Netscape's spec may contain an unquoted date
7913 * with a comma inside. We have to live with this because
7914 * many browsers don't support Max-Age and some browsers don't
7915 * support quoted strings. However the Set-Cookie2 header is
7916 * clean.
7917 *
7918 * We have to keep multiple pointers in order to support cookie
7919 * removal at the beginning, middle or end of header without
7920 * corrupting the header (in case of set-cookie2). A special
7921 * pointer, <scav> points to the beginning of the set-cookie-av
7922 * fields after the first semi-colon. The <next> pointer points
7923 * either to the end of line (set-cookie) or next unquoted comma
7924 * (set-cookie2). All of these headers are valid :
7925 *
7926 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7927 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7928 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7929 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7930 * | | | | | | | | | |
7931 * | | | | | | | | +-> next hdr_end <--+
7932 * | | | | | | | +------------> scav
7933 * | | | | | | +--------------> val_end
7934 * | | | | | +--------------------> val_beg
7935 * | | | | +----------------------> equal
7936 * | | | +------------------------> att_end
7937 * | | +----------------------------> att_beg
7938 * | +------------------------------> prev
7939 * +-----------------------------------------> hdr_beg
7940 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007941
Willy Tarreau24581ba2010-08-31 22:39:35 +02007942 for (; prev < hdr_end; prev = next) {
7943 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007944
Willy Tarreau24581ba2010-08-31 22:39:35 +02007945 /* find att_beg */
7946 att_beg = prev + 1;
7947 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7948 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007949
Willy Tarreau24581ba2010-08-31 22:39:35 +02007950 /* find att_end : this is the first character after the last non
7951 * space before the equal. It may be equal to hdr_end.
7952 */
7953 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007954
Willy Tarreau24581ba2010-08-31 22:39:35 +02007955 while (equal < hdr_end) {
7956 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7957 break;
7958 if (http_is_spht[(unsigned char)*equal++])
7959 continue;
7960 att_end = equal;
7961 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007962
Willy Tarreau24581ba2010-08-31 22:39:35 +02007963 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7964 * is between <att_beg> and <equal>, both may be identical.
7965 */
7966
7967 /* look for end of cookie if there is an equal sign */
7968 if (equal < hdr_end && *equal == '=') {
7969 /* look for the beginning of the value */
7970 val_beg = equal + 1;
7971 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7972 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007973
Willy Tarreau24581ba2010-08-31 22:39:35 +02007974 /* find the end of the value, respecting quotes */
7975 next = find_cookie_value_end(val_beg, hdr_end);
7976
7977 /* make val_end point to the first white space or delimitor after the value */
7978 val_end = next;
7979 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7980 val_end--;
7981 } else {
7982 /* <equal> points to next comma, semi-colon or EOL */
7983 val_beg = val_end = next = equal;
7984 }
7985
7986 if (next < hdr_end) {
7987 /* Set-Cookie2 supports multiple cookies, and <next> points to
7988 * a colon or semi-colon before the end. So skip all attr-value
7989 * pairs and look for the next comma. For Set-Cookie, since
7990 * commas are permitted in values, skip to the end.
7991 */
7992 if (is_cookie2)
7993 next = find_hdr_value_end(next, hdr_end);
7994 else
7995 next = hdr_end;
7996 }
7997
7998 /* Now everything is as on the diagram above */
7999
8000 /* Ignore cookies with no equal sign */
8001 if (equal == val_end)
8002 continue;
8003
8004 /* If there are spaces around the equal sign, we need to
8005 * strip them otherwise we'll get trouble for cookie captures,
8006 * or even for rewrites. Since this happens extremely rarely,
8007 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008008 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008009 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8010 int stripped_before = 0;
8011 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008012
Willy Tarreau24581ba2010-08-31 22:39:35 +02008013 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008014 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008015 equal += stripped_before;
8016 val_beg += stripped_before;
8017 }
8018
8019 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008020 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008021 val_beg += stripped_after;
8022 stripped_before += stripped_after;
8023 }
8024
8025 val_end += stripped_before;
8026 next += stripped_before;
8027 hdr_end += stripped_before;
8028 hdr_next += stripped_before;
8029 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008030 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008031 }
8032
8033 /* First, let's see if we want to capture this cookie. We check
8034 * that we don't already have a server side cookie, because we
8035 * can only capture one. Also as an optimisation, we ignore
8036 * cookies shorter than the declared name.
8037 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008038 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008039 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008040 (val_end - att_beg >= s->fe->capture_namelen) &&
8041 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008042 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008043 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008044 Alert("HTTP logging : out of memory.\n");
8045 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008046 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008047 if (log_len > s->fe->capture_len)
8048 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008049 memcpy(txn->srv_cookie, att_beg, log_len);
8050 txn->srv_cookie[log_len] = 0;
8051 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008052 }
8053
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008054 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008055 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008056 if (!(s->flags & SN_IGNORE_PRST) &&
8057 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8058 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008059 /* assume passive cookie by default */
8060 txn->flags &= ~TX_SCK_MASK;
8061 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008062
8063 /* If the cookie is in insert mode on a known server, we'll delete
8064 * this occurrence because we'll insert another one later.
8065 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008066 * a direct access.
8067 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008068 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008069 /* The "preserve" flag was set, we don't want to touch the
8070 * server's cookie.
8071 */
8072 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008073 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8074 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008075 /* this cookie must be deleted */
8076 if (*prev == ':' && next == hdr_end) {
8077 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008078 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008079 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8080 txn->hdr_idx.used--;
8081 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008082 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008083 hdr_next += delta;
8084 http_msg_move_end(&txn->rsp, delta);
8085 /* note: while both invalid now, <next> and <hdr_end>
8086 * are still equal, so the for() will stop as expected.
8087 */
8088 } else {
8089 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008090 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008091 next = prev;
8092 hdr_end += delta;
8093 hdr_next += delta;
8094 cur_hdr->len += delta;
8095 http_msg_move_end(&txn->rsp, delta);
8096 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008097 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008098 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008099 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008100 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008101 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008102 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008103 * with this server since we know it.
8104 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008105 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008106 next += delta;
8107 hdr_end += delta;
8108 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008109 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008110 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008111
Willy Tarreauf1348312010-10-07 15:54:11 +02008112 txn->flags &= ~TX_SCK_MASK;
8113 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008114 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008115 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008116 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008117 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008118 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008119 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008120 next += delta;
8121 hdr_end += delta;
8122 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008123 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008124 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008125
Willy Tarreau827aee92011-03-10 16:55:02 +01008126 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008127 txn->flags &= ~TX_SCK_MASK;
8128 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008129 }
8130 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008131 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008132 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008133 int cmp_len, value_len;
8134 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008135
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008136 if (s->be->options2 & PR_O2_AS_PFX) {
8137 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8138 value_begin = att_beg + s->be->appsession_name_len;
8139 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008140 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008141 cmp_len = att_end - att_beg;
8142 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008143 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008144 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008145
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008146 if ((cmp_len == s->be->appsession_name_len) &&
8147 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008148 /* free a possibly previously allocated memory */
8149 pool_free2(apools.sessid, txn->sessid);
8150
Cyril Bontéb21570a2009-11-29 20:04:48 +01008151 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008152 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008153 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008154 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008155 return;
8156 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008157 memcpy(txn->sessid, value_begin, value_len);
8158 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008159 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008160 }
8161 /* that's done for this cookie, check the next one on the same
8162 * line when next != hdr_end (only if is_cookie2).
8163 */
8164 }
8165 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008166 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008167 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008168
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008169 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008170 appsess *asession = NULL;
8171 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008172 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008173 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008174 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008175 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8176 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008177 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008178 return;
8179 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008180 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8181
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008182 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8183 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008184 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8185 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008186 return;
8187 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008188 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8189 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008190
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008191 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008192 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008193 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008194 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8195 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008196 return;
8197 }
8198 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008199 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008200
8201 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008202 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008203 }
8204
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008205 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008206 asession->request_count++;
8207 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008208}
8209
8210
Willy Tarreaua15645d2007-03-18 16:22:39 +01008211/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008212 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008213 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008214void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008215{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008216 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008217 char *p1, *p2;
8218
8219 char *cur_ptr, *cur_end, *cur_next;
8220 int cur_idx;
8221
Willy Tarreau5df51872007-11-25 16:20:08 +01008222 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008223 return;
8224
8225 /* Iterate through the headers.
8226 * we start with the start line.
8227 */
8228 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008229 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008230
8231 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8232 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008233 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008234
8235 cur_hdr = &txn->hdr_idx.v[cur_idx];
8236 cur_ptr = cur_next;
8237 cur_end = cur_ptr + cur_hdr->len;
8238 cur_next = cur_end + cur_hdr->cr + 1;
8239
8240 /* We have one full header between cur_ptr and cur_end, and the
8241 * next header starts at cur_next. We're only interested in
8242 * "Cookie:" headers.
8243 */
8244
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008245 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8246 if (val) {
8247 if ((cur_end - (cur_ptr + val) >= 8) &&
8248 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8249 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8250 return;
8251 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008252 }
8253
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008254 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8255 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008256 continue;
8257
8258 /* OK, right now we know we have a cache-control header at cur_ptr */
8259
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008260 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008261
8262 if (p1 >= cur_end) /* no more info */
8263 continue;
8264
8265 /* p1 is at the beginning of the value */
8266 p2 = p1;
8267
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008268 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008269 p2++;
8270
8271 /* we have a complete value between p1 and p2 */
8272 if (p2 < cur_end && *p2 == '=') {
8273 /* we have something of the form no-cache="set-cookie" */
8274 if ((cur_end - p1 >= 21) &&
8275 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8276 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008277 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008278 continue;
8279 }
8280
8281 /* OK, so we know that either p2 points to the end of string or to a comma */
8282 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008283 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008284 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8285 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8286 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008287 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008288 return;
8289 }
8290
8291 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008292 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008293 continue;
8294 }
8295 }
8296}
8297
8298
Willy Tarreau58f10d72006-12-04 02:26:12 +01008299/*
8300 * Try to retrieve a known appsession in the URI, then the associated server.
8301 * If the server is found, it's assigned to the session.
8302 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008303void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008304{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008305 char *end_params, *first_param, *cur_param, *next_param;
8306 char separator;
8307 int value_len;
8308
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008309 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008310
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008311 if (s->be->appsession_name == NULL ||
8312 (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 +01008313 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008314 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008315
Cyril Bontéb21570a2009-11-29 20:04:48 +01008316 first_param = NULL;
8317 switch (mode) {
8318 case PR_O2_AS_M_PP:
8319 first_param = memchr(begin, ';', len);
8320 break;
8321 case PR_O2_AS_M_QS:
8322 first_param = memchr(begin, '?', len);
8323 break;
8324 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008325
Cyril Bontéb21570a2009-11-29 20:04:48 +01008326 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008327 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008328 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008329
Cyril Bontéb21570a2009-11-29 20:04:48 +01008330 switch (mode) {
8331 case PR_O2_AS_M_PP:
8332 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8333 end_params = (char *) begin + len;
8334 }
8335 separator = ';';
8336 break;
8337 case PR_O2_AS_M_QS:
8338 end_params = (char *) begin + len;
8339 separator = '&';
8340 break;
8341 default:
8342 /* unknown mode, shouldn't happen */
8343 return;
8344 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008345
Cyril Bontéb21570a2009-11-29 20:04:48 +01008346 cur_param = next_param = end_params;
8347 while (cur_param > first_param) {
8348 cur_param--;
8349 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8350 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008351 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8352 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8353 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008354 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008355 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8356 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008357 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008358 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008359 }
8360 break;
8361 }
8362 next_param = cur_param;
8363 }
8364 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008365#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008366 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008367 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008368#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008369}
8370
Willy Tarreaub2513902006-12-17 14:52:38 +01008371/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008372 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008373 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008374 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008375 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008376 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008377 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008378 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008379 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008380int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008381{
8382 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008383 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008384 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008385
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008386 if (!uri_auth)
8387 return 0;
8388
Cyril Bonté70be45d2010-10-12 00:14:35 +02008389 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008390 return 0;
8391
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008392 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008393 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008394 return 0;
8395
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008396 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008397 return 0;
8398
Willy Tarreaub2513902006-12-17 14:52:38 +01008399 return 1;
8400}
8401
Willy Tarreau4076a152009-04-02 15:18:36 +02008402/*
8403 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008404 * By default it tries to report the error position as msg->err_pos. However if
8405 * this one is not set, it will then report msg->next, which is the last known
8406 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008407 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008408 */
8409void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008410 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008411 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008412{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008413 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008414 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008415
Willy Tarreau9b28e032012-10-12 23:49:43 +02008416 es->len = MIN(chn->buf->i, sizeof(es->buf));
8417 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008418 len1 = MIN(len1, es->len);
8419 len2 = es->len - len1; /* remaining data if buffer wraps */
8420
Willy Tarreau9b28e032012-10-12 23:49:43 +02008421 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008422 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008423 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008424
Willy Tarreau4076a152009-04-02 15:18:36 +02008425 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008426 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008427 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008428 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008429
Willy Tarreau4076a152009-04-02 15:18:36 +02008430 es->when = date; // user-visible date
8431 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008432 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008433 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008434 if (objt_conn(s->req->prod->end))
8435 es->src = __objt_conn(s->req->prod->end)->addr.from;
8436 else
8437 memset(&es->src, 0, sizeof(es->src));
8438
Willy Tarreau078272e2010-12-12 12:46:33 +01008439 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008440 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008441 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008442 es->s_flags = s->flags;
8443 es->t_flags = s->txn.flags;
8444 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008445 es->b_out = chn->buf->o;
8446 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008447 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008448 es->m_clen = msg->chunk_len;
8449 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008450}
Willy Tarreaub2513902006-12-17 14:52:38 +01008451
Willy Tarreau294c4732011-12-16 21:35:50 +01008452/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8453 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8454 * performed over the whole headers. Otherwise it must contain a valid header
8455 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8456 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8457 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8458 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008459 * -1. The value fetch stops at commas, so this function is suited for use with
8460 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008461 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008462 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008463unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008464 struct hdr_idx *idx, int occ,
8465 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008466{
Willy Tarreau294c4732011-12-16 21:35:50 +01008467 struct hdr_ctx local_ctx;
8468 char *ptr_hist[MAX_HDR_HISTORY];
8469 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008470 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008471 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008472
Willy Tarreau294c4732011-12-16 21:35:50 +01008473 if (!ctx) {
8474 local_ctx.idx = 0;
8475 ctx = &local_ctx;
8476 }
8477
Willy Tarreaubce70882009-09-07 11:51:47 +02008478 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008479 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008480 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008481 occ--;
8482 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008483 *vptr = ctx->line + ctx->val;
8484 *vlen = ctx->vlen;
8485 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008486 }
8487 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008488 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008489 }
8490
8491 /* negative occurrence, we scan all the list then walk back */
8492 if (-occ > MAX_HDR_HISTORY)
8493 return 0;
8494
Willy Tarreau294c4732011-12-16 21:35:50 +01008495 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008496 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008497 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8498 len_hist[hist_ptr] = ctx->vlen;
8499 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008500 hist_ptr = 0;
8501 found++;
8502 }
8503 if (-occ > found)
8504 return 0;
8505 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008506 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8507 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8508 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008509 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008510 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008511 if (hist_ptr >= MAX_HDR_HISTORY)
8512 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008513 *vptr = ptr_hist[hist_ptr];
8514 *vlen = len_hist[hist_ptr];
8515 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008516}
8517
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008518/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8519 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8520 * performed over the whole headers. Otherwise it must contain a valid header
8521 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8522 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8523 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8524 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8525 * -1. This function differs from http_get_hdr() in that it only returns full
8526 * line header values and does not stop at commas.
8527 * The return value is 0 if nothing was found, or non-zero otherwise.
8528 */
8529unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8530 struct hdr_idx *idx, int occ,
8531 struct hdr_ctx *ctx, char **vptr, int *vlen)
8532{
8533 struct hdr_ctx local_ctx;
8534 char *ptr_hist[MAX_HDR_HISTORY];
8535 int len_hist[MAX_HDR_HISTORY];
8536 unsigned int hist_ptr;
8537 int found;
8538
8539 if (!ctx) {
8540 local_ctx.idx = 0;
8541 ctx = &local_ctx;
8542 }
8543
8544 if (occ >= 0) {
8545 /* search from the beginning */
8546 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8547 occ--;
8548 if (occ <= 0) {
8549 *vptr = ctx->line + ctx->val;
8550 *vlen = ctx->vlen;
8551 return 1;
8552 }
8553 }
8554 return 0;
8555 }
8556
8557 /* negative occurrence, we scan all the list then walk back */
8558 if (-occ > MAX_HDR_HISTORY)
8559 return 0;
8560
8561 found = hist_ptr = 0;
8562 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8563 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8564 len_hist[hist_ptr] = ctx->vlen;
8565 if (++hist_ptr >= MAX_HDR_HISTORY)
8566 hist_ptr = 0;
8567 found++;
8568 }
8569 if (-occ > found)
8570 return 0;
8571 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8572 * find occurrence -occ, so we have to check [hist_ptr+occ].
8573 */
8574 hist_ptr += occ;
8575 if (hist_ptr >= MAX_HDR_HISTORY)
8576 hist_ptr -= MAX_HDR_HISTORY;
8577 *vptr = ptr_hist[hist_ptr];
8578 *vlen = len_hist[hist_ptr];
8579 return 1;
8580}
8581
Willy Tarreaubaaee002006-06-26 02:48:02 +02008582/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008583 * Print a debug line with a header. Always stop at the first CR or LF char,
8584 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8585 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008586 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008587void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008588{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008589 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008590 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008591 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008592 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8593 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 +02008594
8595 for (max = 0; start + max < end; max++)
8596 if (start[max] == '\r' || start[max] == '\n')
8597 break;
8598
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008599 UBOUND(max, trash.size - trash.len - 3);
8600 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8601 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008602 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008603}
8604
Willy Tarreau0937bc42009-12-22 15:03:09 +01008605/*
8606 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8607 * the required fields are properly allocated and that we only need to (re)init
8608 * them. This should be used before processing any new request.
8609 */
8610void http_init_txn(struct session *s)
8611{
8612 struct http_txn *txn = &s->txn;
8613 struct proxy *fe = s->fe;
8614
8615 txn->flags = 0;
8616 txn->status = -1;
8617
Willy Tarreauf64d1412010-10-07 20:06:11 +02008618 txn->cookie_first_date = 0;
8619 txn->cookie_last_date = 0;
8620
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008621 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008622 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008623 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008624 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008625 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008626 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008627 txn->req.chunk_len = 0LL;
8628 txn->req.body_len = 0LL;
8629 txn->rsp.chunk_len = 0LL;
8630 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008631 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8632 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008633 txn->req.chn = s->req;
8634 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008635
8636 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008637
8638 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8639 if (fe->options2 & PR_O2_REQBUG_OK)
8640 txn->req.err_pos = -1; /* let buggy requests pass */
8641
Willy Tarreau46023632010-01-07 22:51:47 +01008642 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008643 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8644
Willy Tarreau46023632010-01-07 22:51:47 +01008645 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008646 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8647
8648 if (txn->hdr_idx.v)
8649 hdr_idx_init(&txn->hdr_idx);
8650}
8651
8652/* to be used at the end of a transaction */
8653void http_end_txn(struct session *s)
8654{
8655 struct http_txn *txn = &s->txn;
8656
Willy Tarreau75195602014-03-11 15:48:55 +01008657 /* release any possible compression context */
8658 if (s->flags & SN_COMP_READY)
8659 s->comp_algo->end(&s->comp_ctx);
8660 s->comp_algo = NULL;
8661 s->flags &= ~SN_COMP_READY;
8662
Willy Tarreau0937bc42009-12-22 15:03:09 +01008663 /* these ones will have been dynamically allocated */
8664 pool_free2(pool2_requri, txn->uri);
8665 pool_free2(pool2_capture, txn->cli_cookie);
8666 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008667 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008668 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008669
William Lallemanda73203e2012-03-12 12:48:57 +01008670 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008671 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008672 txn->uri = NULL;
8673 txn->srv_cookie = NULL;
8674 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008675
8676 if (txn->req.cap) {
8677 struct cap_hdr *h;
8678 for (h = s->fe->req_cap; h; h = h->next)
8679 pool_free2(h->pool, txn->req.cap[h->index]);
8680 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8681 }
8682
8683 if (txn->rsp.cap) {
8684 struct cap_hdr *h;
8685 for (h = s->fe->rsp_cap; h; h = h->next)
8686 pool_free2(h->pool, txn->rsp.cap[h->index]);
8687 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8688 }
8689
Willy Tarreau0937bc42009-12-22 15:03:09 +01008690}
8691
8692/* to be used at the end of a transaction to prepare a new one */
8693void http_reset_txn(struct session *s)
8694{
8695 http_end_txn(s);
8696 http_init_txn(s);
8697
8698 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008699 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008700 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008701 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008702 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008703 /* re-init store persistence */
8704 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008705 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008706
Willy Tarreau0937bc42009-12-22 15:03:09 +01008707 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008708
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008709 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008710
Willy Tarreau739cfba2010-01-25 23:11:14 +01008711 /* We must trim any excess data from the response buffer, because we
8712 * may have blocked an invalid response from a server that we don't
8713 * want to accidentely forward once we disable the analysers, nor do
8714 * we want those data to come along with next response. A typical
8715 * example of such data would be from a buggy server responding to
8716 * a HEAD with some data, or sending more than the advertised
8717 * content-length.
8718 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008719 if (unlikely(s->rep->buf->i))
8720 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008721
Willy Tarreau0937bc42009-12-22 15:03:09 +01008722 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008723 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008724
Willy Tarreaud04e8582010-05-31 12:31:35 +02008725 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008726 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008727
8728 s->req->rex = TICK_ETERNITY;
8729 s->req->wex = TICK_ETERNITY;
8730 s->req->analyse_exp = TICK_ETERNITY;
8731 s->rep->rex = TICK_ETERNITY;
8732 s->rep->wex = TICK_ETERNITY;
8733 s->rep->analyse_exp = TICK_ETERNITY;
8734}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008735
Sasha Pachev218f0642014-06-16 12:05:59 -06008736void free_http_res_rules(struct list *r)
8737{
8738 struct http_res_rule *tr, *pr;
8739
8740 list_for_each_entry_safe(pr, tr, r, list) {
8741 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008742 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008743 free(pr);
8744 }
8745}
8746
8747void free_http_req_rules(struct list *r)
8748{
Willy Tarreauff011f22011-01-06 17:51:27 +01008749 struct http_req_rule *tr, *pr;
8750
8751 list_for_each_entry_safe(pr, tr, r, list) {
8752 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008753 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008754 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008755
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008756 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008757 free(pr);
8758 }
8759}
8760
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008761/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008762struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8763{
8764 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008765 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008766 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008767 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008768
8769 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8770 if (!rule) {
8771 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008772 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008773 }
8774
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008775 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008776 rule->action = HTTP_REQ_ACT_ALLOW;
8777 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008778 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008779 rule->action = HTTP_REQ_ACT_DENY;
8780 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008781 } else if (!strcmp(args[0], "tarpit")) {
8782 rule->action = HTTP_REQ_ACT_TARPIT;
8783 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008784 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008785 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008786 cur_arg = 1;
8787
8788 while(*args[cur_arg]) {
8789 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008790 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008791 cur_arg+=2;
8792 continue;
8793 } else
8794 break;
8795 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008796 } else if (!strcmp(args[0], "set-nice")) {
8797 rule->action = HTTP_REQ_ACT_SET_NICE;
8798 cur_arg = 1;
8799
8800 if (!*args[cur_arg] ||
8801 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8802 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8803 file, linenum, args[0]);
8804 goto out_err;
8805 }
8806 rule->arg.nice = atoi(args[cur_arg]);
8807 if (rule->arg.nice < -1024)
8808 rule->arg.nice = -1024;
8809 else if (rule->arg.nice > 1024)
8810 rule->arg.nice = 1024;
8811 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008812 } else if (!strcmp(args[0], "set-tos")) {
8813#ifdef IP_TOS
8814 char *err;
8815 rule->action = HTTP_REQ_ACT_SET_TOS;
8816 cur_arg = 1;
8817
8818 if (!*args[cur_arg] ||
8819 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8820 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8821 file, linenum, args[0]);
8822 goto out_err;
8823 }
8824
8825 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8826 if (err && *err != '\0') {
8827 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8828 file, linenum, err, args[0]);
8829 goto out_err;
8830 }
8831 cur_arg++;
8832#else
8833 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8834 goto out_err;
8835#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008836 } else if (!strcmp(args[0], "set-mark")) {
8837#ifdef SO_MARK
8838 char *err;
8839 rule->action = HTTP_REQ_ACT_SET_MARK;
8840 cur_arg = 1;
8841
8842 if (!*args[cur_arg] ||
8843 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8844 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8845 file, linenum, args[0]);
8846 goto out_err;
8847 }
8848
8849 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8850 if (err && *err != '\0') {
8851 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8852 file, linenum, err, args[0]);
8853 goto out_err;
8854 }
8855 cur_arg++;
8856 global.last_checks |= LSTCHK_NETADM;
8857#else
8858 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8859 goto out_err;
8860#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008861 } else if (!strcmp(args[0], "set-log-level")) {
8862 rule->action = HTTP_REQ_ACT_SET_LOGL;
8863 cur_arg = 1;
8864
8865 if (!*args[cur_arg] ||
8866 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8867 bad_log_level:
8868 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8869 file, linenum, args[0]);
8870 goto out_err;
8871 }
8872 if (strcmp(args[cur_arg], "silent") == 0)
8873 rule->arg.loglevel = -1;
8874 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8875 goto bad_log_level;
8876 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008877 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8878 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8879 cur_arg = 1;
8880
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008881 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8882 (*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 +01008883 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8884 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008885 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008886 }
8887
8888 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8889 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8890 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008891
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008892 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008893 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008894 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8895 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008896 free(proxy->conf.lfs_file);
8897 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8898 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008899 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008900 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
8901 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008902 cur_arg = 1;
8903
8904 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann8f249f42014-06-24 11:10:00 +02008905 (*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 -06008906 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8907 file, linenum, args[0]);
8908 goto out_err;
8909 }
8910
8911 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8912 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8913 LIST_INIT(&rule->arg.hdr_add.fmt);
8914
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008915 error = NULL;
8916 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8917 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8918 args[cur_arg + 1], error);
8919 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008920 goto out_err;
8921 }
8922
8923 proxy->conf.args.ctx = ARGC_HRQ;
8924 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
8925 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8926 file, linenum);
8927
8928 free(proxy->conf.lfs_file);
8929 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8930 proxy->conf.lfs_line = proxy->conf.args.line;
8931 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008932 } else if (strcmp(args[0], "del-header") == 0) {
8933 rule->action = HTTP_REQ_ACT_DEL_HDR;
8934 cur_arg = 1;
8935
8936 if (!*args[cur_arg] ||
8937 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8938 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8939 file, linenum, args[0]);
8940 goto out_err;
8941 }
8942
8943 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8944 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8945
8946 proxy->conf.args.ctx = ARGC_HRQ;
8947 free(proxy->conf.lfs_file);
8948 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8949 proxy->conf.lfs_line = proxy->conf.args.line;
8950 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008951 } else if (strcmp(args[0], "redirect") == 0) {
8952 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008953 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008954
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008955 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008956 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8957 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8958 goto out_err;
8959 }
8960
8961 /* this redirect rule might already contain a parsed condition which
8962 * we'll pass to the http-request rule.
8963 */
8964 rule->action = HTTP_REQ_ACT_REDIR;
8965 rule->arg.redir = redir;
8966 rule->cond = redir->cond;
8967 redir->cond = NULL;
8968 cur_arg = 2;
8969 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008970 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8971 /* http-request add-acl(<reference (acl name)>) <key pattern> */
8972 rule->action = HTTP_REQ_ACT_ADD_ACL;
8973 /*
8974 * '+ 8' for 'add-acl('
8975 * '- 9' for 'add-acl(' + trailing ')'
8976 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008977 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008978
8979 cur_arg = 1;
8980
8981 if (!*args[cur_arg] ||
8982 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8983 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8984 file, linenum, args[0]);
8985 goto out_err;
8986 }
8987
8988 LIST_INIT(&rule->arg.map.key);
8989 proxy->conf.args.ctx = ARGC_HRQ;
8990 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8991 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8992 file, linenum);
8993 free(proxy->conf.lfs_file);
8994 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8995 proxy->conf.lfs_line = proxy->conf.args.line;
8996 cur_arg += 1;
8997 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8998 /* http-request del-acl(<reference (acl name)>) <key pattern> */
8999 rule->action = HTTP_REQ_ACT_DEL_ACL;
9000 /*
9001 * '+ 8' for 'del-acl('
9002 * '- 9' for 'del-acl(' + trailing ')'
9003 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009004 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009005
9006 cur_arg = 1;
9007
9008 if (!*args[cur_arg] ||
9009 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9010 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9011 file, linenum, args[0]);
9012 goto out_err;
9013 }
9014
9015 LIST_INIT(&rule->arg.map.key);
9016 proxy->conf.args.ctx = ARGC_HRQ;
9017 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9018 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9019 file, linenum);
9020 free(proxy->conf.lfs_file);
9021 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9022 proxy->conf.lfs_line = proxy->conf.args.line;
9023 cur_arg += 1;
9024 } else if (strncmp(args[0], "del-map", 7) == 0) {
9025 /* http-request del-map(<reference (map name)>) <key pattern> */
9026 rule->action = HTTP_REQ_ACT_DEL_MAP;
9027 /*
9028 * '+ 8' for 'del-map('
9029 * '- 9' for 'del-map(' + trailing ')'
9030 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009031 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009032
9033 cur_arg = 1;
9034
9035 if (!*args[cur_arg] ||
9036 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9037 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9038 file, linenum, args[0]);
9039 goto out_err;
9040 }
9041
9042 LIST_INIT(&rule->arg.map.key);
9043 proxy->conf.args.ctx = ARGC_HRQ;
9044 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9045 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9046 file, linenum);
9047 free(proxy->conf.lfs_file);
9048 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9049 proxy->conf.lfs_line = proxy->conf.args.line;
9050 cur_arg += 1;
9051 } else if (strncmp(args[0], "set-map", 7) == 0) {
9052 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9053 rule->action = HTTP_REQ_ACT_SET_MAP;
9054 /*
9055 * '+ 8' for 'set-map('
9056 * '- 9' for 'set-map(' + trailing ')'
9057 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009058 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009059
9060 cur_arg = 1;
9061
9062 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9063 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9064 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9065 file, linenum, args[0]);
9066 goto out_err;
9067 }
9068
9069 LIST_INIT(&rule->arg.map.key);
9070 LIST_INIT(&rule->arg.map.value);
9071 proxy->conf.args.ctx = ARGC_HRQ;
9072
9073 /* key pattern */
9074 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9075 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9076 file, linenum);
9077
9078 /* value pattern */
9079 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, 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
9086 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009087 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9088 char *errmsg = NULL;
9089 cur_arg = 1;
9090 /* try in the module list */
9091 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9092 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9093 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9094 free(errmsg);
9095 goto out_err;
9096 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009097 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009098 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 +01009099 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009100 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009101 }
9102
9103 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9104 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009105 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009106
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009107 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9108 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9109 file, linenum, args[0], errmsg);
9110 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009111 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009112 }
9113 rule->cond = cond;
9114 }
9115 else if (*args[cur_arg]) {
9116 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9117 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9118 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009119 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009120 }
9121
9122 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009123 out_err:
9124 free(rule);
9125 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009126}
9127
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009128/* parse an "http-respose" rule */
9129struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9130{
9131 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009132 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009133 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009134 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009135
9136 rule = calloc(1, sizeof(*rule));
9137 if (!rule) {
9138 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9139 goto out_err;
9140 }
9141
9142 if (!strcmp(args[0], "allow")) {
9143 rule->action = HTTP_RES_ACT_ALLOW;
9144 cur_arg = 1;
9145 } else if (!strcmp(args[0], "deny")) {
9146 rule->action = HTTP_RES_ACT_DENY;
9147 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009148 } else if (!strcmp(args[0], "set-nice")) {
9149 rule->action = HTTP_RES_ACT_SET_NICE;
9150 cur_arg = 1;
9151
9152 if (!*args[cur_arg] ||
9153 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9154 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9155 file, linenum, args[0]);
9156 goto out_err;
9157 }
9158 rule->arg.nice = atoi(args[cur_arg]);
9159 if (rule->arg.nice < -1024)
9160 rule->arg.nice = -1024;
9161 else if (rule->arg.nice > 1024)
9162 rule->arg.nice = 1024;
9163 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009164 } else if (!strcmp(args[0], "set-tos")) {
9165#ifdef IP_TOS
9166 char *err;
9167 rule->action = HTTP_RES_ACT_SET_TOS;
9168 cur_arg = 1;
9169
9170 if (!*args[cur_arg] ||
9171 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9172 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9173 file, linenum, args[0]);
9174 goto out_err;
9175 }
9176
9177 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9178 if (err && *err != '\0') {
9179 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9180 file, linenum, err, args[0]);
9181 goto out_err;
9182 }
9183 cur_arg++;
9184#else
9185 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9186 goto out_err;
9187#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009188 } else if (!strcmp(args[0], "set-mark")) {
9189#ifdef SO_MARK
9190 char *err;
9191 rule->action = HTTP_RES_ACT_SET_MARK;
9192 cur_arg = 1;
9193
9194 if (!*args[cur_arg] ||
9195 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9196 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9197 file, linenum, args[0]);
9198 goto out_err;
9199 }
9200
9201 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9202 if (err && *err != '\0') {
9203 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9204 file, linenum, err, args[0]);
9205 goto out_err;
9206 }
9207 cur_arg++;
9208 global.last_checks |= LSTCHK_NETADM;
9209#else
9210 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9211 goto out_err;
9212#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009213 } else if (!strcmp(args[0], "set-log-level")) {
9214 rule->action = HTTP_RES_ACT_SET_LOGL;
9215 cur_arg = 1;
9216
9217 if (!*args[cur_arg] ||
9218 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9219 bad_log_level:
9220 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9221 file, linenum, args[0]);
9222 goto out_err;
9223 }
9224 if (strcmp(args[cur_arg], "silent") == 0)
9225 rule->arg.loglevel = -1;
9226 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9227 goto bad_log_level;
9228 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009229 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9230 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9231 cur_arg = 1;
9232
9233 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9234 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9235 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9236 file, linenum, args[0]);
9237 goto out_err;
9238 }
9239
9240 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9241 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9242 LIST_INIT(&rule->arg.hdr_add.fmt);
9243
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009244 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009245 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009246 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9247 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009248 free(proxy->conf.lfs_file);
9249 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9250 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009251 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009252 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009253 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009254 cur_arg = 1;
9255
9256 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmanna772b942014-08-08 17:29:06 +02009257 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9258 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009259 file, linenum, args[0]);
9260 goto out_err;
9261 }
9262
9263 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9264 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9265 LIST_INIT(&rule->arg.hdr_add.fmt);
9266
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009267 error = NULL;
9268 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9269 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9270 args[cur_arg + 1], error);
9271 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009272 goto out_err;
9273 }
9274
9275 proxy->conf.args.ctx = ARGC_HRQ;
9276 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9277 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9278 file, linenum);
9279
9280 free(proxy->conf.lfs_file);
9281 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9282 proxy->conf.lfs_line = proxy->conf.args.line;
9283 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009284 } else if (strcmp(args[0], "del-header") == 0) {
9285 rule->action = HTTP_RES_ACT_DEL_HDR;
9286 cur_arg = 1;
9287
9288 if (!*args[cur_arg] ||
9289 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9290 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9291 file, linenum, args[0]);
9292 goto out_err;
9293 }
9294
9295 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9296 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9297
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009298 proxy->conf.args.ctx = ARGC_HRS;
9299 free(proxy->conf.lfs_file);
9300 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9301 proxy->conf.lfs_line = proxy->conf.args.line;
9302 cur_arg += 1;
9303 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9304 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9305 rule->action = HTTP_RES_ACT_ADD_ACL;
9306 /*
9307 * '+ 8' for 'add-acl('
9308 * '- 9' for 'add-acl(' + trailing ')'
9309 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009310 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009311
9312 cur_arg = 1;
9313
9314 if (!*args[cur_arg] ||
9315 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9316 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9317 file, linenum, args[0]);
9318 goto out_err;
9319 }
9320
9321 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009322 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009323 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9324 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9325 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009326 free(proxy->conf.lfs_file);
9327 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9328 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009329
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009330 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009331 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9332 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9333 rule->action = HTTP_RES_ACT_DEL_ACL;
9334 /*
9335 * '+ 8' for 'del-acl('
9336 * '- 9' for 'del-acl(' + trailing ')'
9337 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009338 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009339
9340 cur_arg = 1;
9341
9342 if (!*args[cur_arg] ||
9343 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9344 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9345 file, linenum, args[0]);
9346 goto out_err;
9347 }
9348
9349 LIST_INIT(&rule->arg.map.key);
9350 proxy->conf.args.ctx = ARGC_HRS;
9351 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9352 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9353 file, linenum);
9354 free(proxy->conf.lfs_file);
9355 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9356 proxy->conf.lfs_line = proxy->conf.args.line;
9357 cur_arg += 1;
9358 } else if (strncmp(args[0], "del-map", 7) == 0) {
9359 /* http-response del-map(<reference (map name)>) <key pattern> */
9360 rule->action = HTTP_RES_ACT_DEL_MAP;
9361 /*
9362 * '+ 8' for 'del-map('
9363 * '- 9' for 'del-map(' + trailing ')'
9364 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009365 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009366
9367 cur_arg = 1;
9368
9369 if (!*args[cur_arg] ||
9370 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9371 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9372 file, linenum, args[0]);
9373 goto out_err;
9374 }
9375
9376 LIST_INIT(&rule->arg.map.key);
9377 proxy->conf.args.ctx = ARGC_HRS;
9378 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9379 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9380 file, linenum);
9381 free(proxy->conf.lfs_file);
9382 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9383 proxy->conf.lfs_line = proxy->conf.args.line;
9384 cur_arg += 1;
9385 } else if (strncmp(args[0], "set-map", 7) == 0) {
9386 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9387 rule->action = HTTP_RES_ACT_SET_MAP;
9388 /*
9389 * '+ 8' for 'set-map('
9390 * '- 9' for 'set-map(' + trailing ')'
9391 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009392 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009393
9394 cur_arg = 1;
9395
9396 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9397 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9398 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9399 file, linenum, args[0]);
9400 goto out_err;
9401 }
9402
9403 LIST_INIT(&rule->arg.map.key);
9404 LIST_INIT(&rule->arg.map.value);
9405
9406 proxy->conf.args.ctx = ARGC_HRS;
9407
9408 /* key pattern */
9409 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9410 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9411 file, linenum);
9412
9413 /* value pattern */
9414 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9415 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9416 file, linenum);
9417
9418 free(proxy->conf.lfs_file);
9419 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9420 proxy->conf.lfs_line = proxy->conf.args.line;
9421
9422 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009423 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9424 char *errmsg = NULL;
9425 cur_arg = 1;
9426 /* try in the module list */
9427 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9428 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9429 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9430 free(errmsg);
9431 goto out_err;
9432 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009433 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009434 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 +02009435 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9436 goto out_err;
9437 }
9438
9439 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9440 struct acl_cond *cond;
9441 char *errmsg = NULL;
9442
9443 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9444 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9445 file, linenum, args[0], errmsg);
9446 free(errmsg);
9447 goto out_err;
9448 }
9449 rule->cond = cond;
9450 }
9451 else if (*args[cur_arg]) {
9452 Alert("parsing [%s:%d]: 'http-response %s' expects"
9453 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9454 file, linenum, args[0], args[cur_arg]);
9455 goto out_err;
9456 }
9457
9458 return rule;
9459 out_err:
9460 free(rule);
9461 return NULL;
9462}
9463
Willy Tarreau4baae242012-12-27 12:00:31 +01009464/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009465 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9466 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009467 */
9468struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009469 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009470{
9471 struct redirect_rule *rule;
9472 int cur_arg;
9473 int type = REDIRECT_TYPE_NONE;
9474 int code = 302;
9475 const char *destination = NULL;
9476 const char *cookie = NULL;
9477 int cookie_set = 0;
9478 unsigned int flags = REDIRECT_FLAG_NONE;
9479 struct acl_cond *cond = NULL;
9480
9481 cur_arg = 0;
9482 while (*(args[cur_arg])) {
9483 if (strcmp(args[cur_arg], "location") == 0) {
9484 if (!*args[cur_arg + 1])
9485 goto missing_arg;
9486
9487 type = REDIRECT_TYPE_LOCATION;
9488 cur_arg++;
9489 destination = args[cur_arg];
9490 }
9491 else if (strcmp(args[cur_arg], "prefix") == 0) {
9492 if (!*args[cur_arg + 1])
9493 goto missing_arg;
9494
9495 type = REDIRECT_TYPE_PREFIX;
9496 cur_arg++;
9497 destination = args[cur_arg];
9498 }
9499 else if (strcmp(args[cur_arg], "scheme") == 0) {
9500 if (!*args[cur_arg + 1])
9501 goto missing_arg;
9502
9503 type = REDIRECT_TYPE_SCHEME;
9504 cur_arg++;
9505 destination = args[cur_arg];
9506 }
9507 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9508 if (!*args[cur_arg + 1])
9509 goto missing_arg;
9510
9511 cur_arg++;
9512 cookie = args[cur_arg];
9513 cookie_set = 1;
9514 }
9515 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9516 if (!*args[cur_arg + 1])
9517 goto missing_arg;
9518
9519 cur_arg++;
9520 cookie = args[cur_arg];
9521 cookie_set = 0;
9522 }
9523 else if (strcmp(args[cur_arg], "code") == 0) {
9524 if (!*args[cur_arg + 1])
9525 goto missing_arg;
9526
9527 cur_arg++;
9528 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009529 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009530 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009531 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009532 args[cur_arg - 1], args[cur_arg]);
9533 return NULL;
9534 }
9535 }
9536 else if (!strcmp(args[cur_arg],"drop-query")) {
9537 flags |= REDIRECT_FLAG_DROP_QS;
9538 }
9539 else if (!strcmp(args[cur_arg],"append-slash")) {
9540 flags |= REDIRECT_FLAG_APPEND_SLASH;
9541 }
9542 else if (strcmp(args[cur_arg], "if") == 0 ||
9543 strcmp(args[cur_arg], "unless") == 0) {
9544 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9545 if (!cond) {
9546 memprintf(errmsg, "error in condition: %s", *errmsg);
9547 return NULL;
9548 }
9549 break;
9550 }
9551 else {
9552 memprintf(errmsg,
9553 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9554 args[cur_arg]);
9555 return NULL;
9556 }
9557 cur_arg++;
9558 }
9559
9560 if (type == REDIRECT_TYPE_NONE) {
9561 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9562 return NULL;
9563 }
9564
9565 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9566 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009567 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009568
9569 if (!use_fmt) {
9570 /* old-style static redirect rule */
9571 rule->rdr_str = strdup(destination);
9572 rule->rdr_len = strlen(destination);
9573 }
9574 else {
9575 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009576
9577 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9578 * if prefix == "/", we don't want to add anything, otherwise it
9579 * makes it hard for the user to configure a self-redirection.
9580 */
Godbach543b9782014-12-18 15:44:58 +08009581 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009582 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009583 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009584 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9585 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009586 free(curproxy->conf.lfs_file);
9587 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9588 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009589 }
9590 }
9591
Willy Tarreau4baae242012-12-27 12:00:31 +01009592 if (cookie) {
9593 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9594 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9595 */
9596 rule->cookie_len = strlen(cookie);
9597 if (cookie_set) {
9598 rule->cookie_str = malloc(rule->cookie_len + 10);
9599 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9600 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9601 rule->cookie_len += 9;
9602 } else {
9603 rule->cookie_str = malloc(rule->cookie_len + 21);
9604 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9605 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9606 rule->cookie_len += 20;
9607 }
9608 }
9609 rule->type = type;
9610 rule->code = code;
9611 rule->flags = flags;
9612 LIST_INIT(&rule->list);
9613 return rule;
9614
9615 missing_arg:
9616 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9617 return NULL;
9618}
9619
Willy Tarreau8797c062007-05-07 00:55:35 +02009620/************************************************************************/
9621/* The code below is dedicated to ACL parsing and matching */
9622/************************************************************************/
9623
9624
Willy Tarreau14174bc2012-04-16 14:34:04 +02009625/* This function ensures that the prerequisites for an L7 fetch are ready,
9626 * which means that a request or response is ready. If some data is missing,
9627 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009628 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9629 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009630 *
9631 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009632 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9633 * decide whether or not an HTTP message is present ;
9634 * 0 if the requested data cannot be fetched or if it is certain that
9635 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009636 * 1 if an HTTP message is ready
9637 */
9638static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009639smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009640 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009641{
9642 struct http_txn *txn = l7;
9643 struct http_msg *msg = &txn->req;
9644
9645 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9646 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9647 */
9648
9649 if (unlikely(!s || !txn))
9650 return 0;
9651
9652 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009653 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009654
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009655 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009656 if (unlikely(!s->req))
9657 return 0;
9658
Willy Tarreauaae75e32013-03-29 12:31:49 +01009659 /* If the buffer does not leave enough free space at the end,
9660 * we must first realign it.
9661 */
9662 if (s->req->buf->p > s->req->buf->data &&
9663 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9664 buffer_slow_realign(s->req->buf);
9665
Willy Tarreau14174bc2012-04-16 14:34:04 +02009666 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009667 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009668 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009669
9670 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009671 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009672 http_msg_analyzer(msg, &txn->hdr_idx);
9673
9674 /* Still no valid request ? */
9675 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009676 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009677 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009678 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009679 }
9680 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009681 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009682 return 0;
9683 }
9684
9685 /* OK we just got a valid HTTP request. We have some minor
9686 * preparation to perform so that further checks can rely
9687 * on HTTP tests.
9688 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009689
9690 /* If the request was parsed but was too large, we must absolutely
9691 * return an error so that it is not processed. At the moment this
9692 * cannot happen, but if the parsers are to change in the future,
9693 * we want this check to be maintained.
9694 */
9695 if (unlikely(s->req->buf->i + s->req->buf->p >
9696 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9697 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009698 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009699 return 1;
9700 }
9701
Willy Tarreau9b28e032012-10-12 23:49:43 +02009702 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009703 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9704 s->flags |= SN_REDIRECTABLE;
9705
Willy Tarreau506d0502013-07-06 13:29:24 +02009706 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9707 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009708 }
9709
Willy Tarreau506d0502013-07-06 13:29:24 +02009710 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009711 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009712 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009713
9714 /* otherwise everything's ready for the request */
9715 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009716 else {
9717 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009718 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9719 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009720 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009721 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009722 }
9723
9724 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009725 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009726 return 1;
9727}
Willy Tarreau8797c062007-05-07 00:55:35 +02009728
Willy Tarreaua4ba9db2014-06-25 16:56:41 +02009729/* Note: these functinos *do* modify the sample. Even in case of success, at
9730 * least the type and uint value are modified.
9731 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009732#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009733 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 +02009734
Willy Tarreau24e32d82012-04-23 23:55:44 +02009735#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009736 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 +02009737
Willy Tarreau8797c062007-05-07 00:55:35 +02009738
9739/* 1. Check on METHOD
9740 * We use the pre-parsed method if it is known, and store its number as an
9741 * integer. If it is unknown, we use the pointer and the length.
9742 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009743static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009744{
9745 int len, meth;
9746
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009747 len = strlen(text);
9748 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009749
9750 pattern->val.i = meth;
9751 if (meth == HTTP_METH_OTHER) {
Willy Tarreau71196f32014-08-29 15:15:50 +02009752 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009753 pattern->len = len;
9754 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009755 else {
9756 pattern->ptr.str = NULL;
9757 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009758 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009759 return 1;
9760}
9761
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009762/* This function fetches the method of current HTTP request and stores
9763 * it in the global pattern struct as a chunk. There are two possibilities :
9764 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9765 * in <len> and <ptr> is NULL ;
9766 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9767 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009768 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009769 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009770static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009771smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009772 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009773{
9774 int meth;
9775 struct http_txn *txn = l7;
9776
Willy Tarreau24e32d82012-04-23 23:55:44 +02009777 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009778
Willy Tarreau8797c062007-05-07 00:55:35 +02009779 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009780 smp->type = SMP_T_METH;
9781 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009782 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009783 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9784 /* ensure the indexes are not affected */
9785 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009786 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009787 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9788 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009789 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009790 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009791 return 1;
9792}
9793
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009794/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009795static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009796{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009797 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009798 struct pattern_list *lst;
9799 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009800
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009801 list_for_each_entry(lst, &expr->patterns, list) {
9802 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009803
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009804 /* well-known method */
9805 if (pattern->val.i != HTTP_METH_OTHER) {
9806 if (smp->data.meth.meth == pattern->val.i)
9807 return pattern;
9808 else
9809 continue;
9810 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009811
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009812 /* Other method, we must compare the strings */
9813 if (pattern->len != smp->data.meth.str.len)
9814 continue;
9815
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009816 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreauc62a0c62014-08-28 20:42:57 +02009817 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
9818 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009819 return pattern;
9820 }
9821 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009822}
9823
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009824static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009825smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009826 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009827{
9828 struct http_txn *txn = l7;
9829 char *ptr;
9830 int len;
9831
Willy Tarreauc0239e02012-04-16 14:42:55 +02009832 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009833
Willy Tarreau8797c062007-05-07 00:55:35 +02009834 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009835 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009836
9837 while ((len-- > 0) && (*ptr++ != '/'));
9838 if (len <= 0)
9839 return 0;
9840
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009841 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009842 smp->data.str.str = ptr;
9843 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009844
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009845 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009846 return 1;
9847}
9848
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009849static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009850smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009851 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009852{
9853 struct http_txn *txn = l7;
9854 char *ptr;
9855 int len;
9856
Willy Tarreauc0239e02012-04-16 14:42:55 +02009857 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009858
Willy Tarreauf26b2522012-12-14 08:33:14 +01009859 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9860 return 0;
9861
Willy Tarreau8797c062007-05-07 00:55:35 +02009862 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009863 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009864
9865 while ((len-- > 0) && (*ptr++ != '/'));
9866 if (len <= 0)
9867 return 0;
9868
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009869 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009870 smp->data.str.str = ptr;
9871 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009872
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009873 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009874 return 1;
9875}
9876
9877/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009878static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009879smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009880 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009881{
9882 struct http_txn *txn = l7;
9883 char *ptr;
9884 int len;
9885
Willy Tarreauc0239e02012-04-16 14:42:55 +02009886 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009887
Willy Tarreauf26b2522012-12-14 08:33:14 +01009888 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9889 return 0;
9890
Willy Tarreau8797c062007-05-07 00:55:35 +02009891 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009892 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009893
Willy Tarreauf853c462012-04-23 18:53:56 +02009894 smp->type = SMP_T_UINT;
9895 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009896 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009897 return 1;
9898}
9899
9900/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009901static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009902smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009903 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009904{
9905 struct http_txn *txn = l7;
9906
Willy Tarreauc0239e02012-04-16 14:42:55 +02009907 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009908
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009909 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009910 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009911 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009912 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009913 return 1;
9914}
9915
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009916static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009917smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009918 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009919{
9920 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009921 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009922
Willy Tarreauc0239e02012-04-16 14:42:55 +02009923 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009924
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009925 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 +02009926 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009927 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009928
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009929 smp->type = SMP_T_IPV4;
9930 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009931 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009932 return 1;
9933}
9934
9935static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009936smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009937 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009938{
9939 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009940 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009941
Willy Tarreauc0239e02012-04-16 14:42:55 +02009942 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009943
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009944 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 +02009945 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9946 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009947
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009948 smp->type = SMP_T_UINT;
9949 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009950 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009951 return 1;
9952}
9953
Willy Tarreau185b5c42012-04-26 15:11:51 +02009954/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9955 * Accepts an optional argument of type string containing the header field name,
9956 * and an optional argument of type signed or unsigned integer to request an
9957 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009958 * headers are considered from the first one. It does not stop on commas and
9959 * returns full lines instead (useful for User-Agent or Date for example).
9960 */
9961static int
9962smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009963 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009964{
9965 struct http_txn *txn = l7;
9966 struct hdr_idx *idx = &txn->hdr_idx;
9967 struct hdr_ctx *ctx = smp->ctx.a[0];
9968 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9969 int occ = 0;
9970 const char *name_str = NULL;
9971 int name_len = 0;
9972
9973 if (!ctx) {
9974 /* first call */
9975 ctx = &static_hdr_ctx;
9976 ctx->idx = 0;
9977 smp->ctx.a[0] = ctx;
9978 }
9979
9980 if (args) {
9981 if (args[0].type != ARGT_STR)
9982 return 0;
9983 name_str = args[0].data.str.str;
9984 name_len = args[0].data.str.len;
9985
9986 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9987 occ = args[1].data.uint;
9988 }
9989
9990 CHECK_HTTP_MESSAGE_FIRST();
9991
9992 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9993 /* search for header from the beginning */
9994 ctx->idx = 0;
9995
9996 if (!occ && !(opt & SMP_OPT_ITERATE))
9997 /* no explicit occurrence and single fetch => last header by default */
9998 occ = -1;
9999
10000 if (!occ)
10001 /* prepare to report multiple occurrences for ACL fetches */
10002 smp->flags |= SMP_F_NOT_LAST;
10003
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010004 smp->type = SMP_T_STR;
10005 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010006 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10007 return 1;
10008
10009 smp->flags &= ~SMP_F_NOT_LAST;
10010 return 0;
10011}
10012
10013/* 6. Check on HTTP header count. The number of occurrences is returned.
10014 * Accepts exactly 1 argument of type string. It does not stop on commas and
10015 * returns full lines instead (useful for User-Agent or Date for example).
10016 */
10017static int
10018smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010019 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010020{
10021 struct http_txn *txn = l7;
10022 struct hdr_idx *idx = &txn->hdr_idx;
10023 struct hdr_ctx ctx;
10024 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10025 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010026 const char *name = NULL;
10027 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010028
Willy Tarreau29437342015-04-01 19:16:09 +020010029 if (args && args->type == ARGT_STR) {
10030 name = args->data.str.str;
10031 len = args->data.str.len;
10032 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010033
10034 CHECK_HTTP_MESSAGE_FIRST();
10035
10036 ctx.idx = 0;
10037 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010038 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010039 cnt++;
10040
10041 smp->type = SMP_T_UINT;
10042 smp->data.uint = cnt;
10043 smp->flags = SMP_F_VOL_HDR;
10044 return 1;
10045}
10046
10047/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10048 * Accepts an optional argument of type string containing the header field name,
10049 * and an optional argument of type signed or unsigned integer to request an
10050 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010051 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010052 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010053static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010054smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010055 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010056{
10057 struct http_txn *txn = l7;
10058 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010059 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010060 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010061 int occ = 0;
10062 const char *name_str = NULL;
10063 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010064
Willy Tarreaua890d072013-04-02 12:01:06 +020010065 if (!ctx) {
10066 /* first call */
10067 ctx = &static_hdr_ctx;
10068 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010069 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010070 }
10071
Willy Tarreau185b5c42012-04-26 15:11:51 +020010072 if (args) {
10073 if (args[0].type != ARGT_STR)
10074 return 0;
10075 name_str = args[0].data.str.str;
10076 name_len = args[0].data.str.len;
10077
10078 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10079 occ = args[1].data.uint;
10080 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010081
Willy Tarreaue333ec92012-04-16 16:26:40 +020010082 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010083
Willy Tarreau185b5c42012-04-26 15:11:51 +020010084 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010085 /* search for header from the beginning */
10086 ctx->idx = 0;
10087
Willy Tarreau185b5c42012-04-26 15:11:51 +020010088 if (!occ && !(opt & SMP_OPT_ITERATE))
10089 /* no explicit occurrence and single fetch => last header by default */
10090 occ = -1;
10091
10092 if (!occ)
10093 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010094 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010095
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010096 smp->type = SMP_T_STR;
10097 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010098 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 +020010099 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010100
Willy Tarreau37406352012-04-23 16:16:37 +020010101 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010102 return 0;
10103}
10104
Willy Tarreauc11416f2007-06-17 16:58:38 +020010105/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010106 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010107 */
10108static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010109smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010110 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010111{
10112 struct http_txn *txn = l7;
10113 struct hdr_idx *idx = &txn->hdr_idx;
10114 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010115 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010116 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010117 const char *name = NULL;
10118 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010119
Willy Tarreau29437342015-04-01 19:16:09 +020010120 if (args && args->type == ARGT_STR) {
10121 name = args->data.str.str;
10122 len = args->data.str.len;
10123 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010124
Willy Tarreaue333ec92012-04-16 16:26:40 +020010125 CHECK_HTTP_MESSAGE_FIRST();
10126
Willy Tarreau33a7e692007-06-10 19:45:56 +020010127 ctx.idx = 0;
10128 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010129 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010130 cnt++;
10131
Willy Tarreauf853c462012-04-23 18:53:56 +020010132 smp->type = SMP_T_UINT;
10133 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010134 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010135 return 1;
10136}
10137
Willy Tarreau185b5c42012-04-26 15:11:51 +020010138/* Fetch an HTTP header's integer value. The integer value is returned. It
10139 * takes a mandatory argument of type string and an optional one of type int
10140 * to designate a specific occurrence. It returns an unsigned integer, which
10141 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010142 */
10143static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010144smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010145 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010146{
Willy Tarreauef38c392013-07-22 16:29:32 +020010147 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010148
Willy Tarreauf853c462012-04-23 18:53:56 +020010149 if (ret > 0) {
10150 smp->type = SMP_T_UINT;
10151 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10152 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010153
Willy Tarreaud53e2422012-04-16 17:21:11 +020010154 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010155}
10156
Cyril Bonté69fa9922012-10-25 00:01:06 +020010157/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10158 * and an optional one of type int to designate a specific occurrence.
10159 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010160 */
10161static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010162smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010163 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +020010164{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010165 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010166
Willy Tarreauef38c392013-07-22 16:29:32 +020010167 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010168 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10169 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010170 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010171 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010172 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010173 if (smp->data.str.len < temp->size - 1) {
10174 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10175 temp->str[smp->data.str.len] = '\0';
10176 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10177 smp->type = SMP_T_IPV6;
10178 break;
10179 }
10180 }
10181 }
10182
Willy Tarreaud53e2422012-04-16 17:21:11 +020010183 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010184 if (!(smp->flags & SMP_F_NOT_LAST))
10185 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010186 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010187 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010188}
10189
Willy Tarreau737b0c12007-06-10 21:28:46 +020010190/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10191 * the first '/' after the possible hostname, and ends before the possible '?'.
10192 */
10193static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010194smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010195 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010196{
10197 struct http_txn *txn = l7;
10198 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010199
Willy Tarreauc0239e02012-04-16 14:42:55 +020010200 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010201
Willy Tarreau9b28e032012-10-12 23:49:43 +020010202 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010203 ptr = http_get_path(txn);
10204 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010205 return 0;
10206
10207 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010208 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010209 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010210
10211 while (ptr < end && *ptr != '?')
10212 ptr++;
10213
Willy Tarreauf853c462012-04-23 18:53:56 +020010214 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010215 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010216 return 1;
10217}
10218
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010219/* This produces a concatenation of the first occurrence of the Host header
10220 * followed by the path component if it begins with a slash ('/'). This means
10221 * that '*' will not be added, resulting in exactly the first Host entry.
10222 * If no Host header is found, then the path is returned as-is. The returned
10223 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010224 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010225 */
10226static int
10227smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010228 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010229{
10230 struct http_txn *txn = l7;
10231 char *ptr, *end, *beg;
10232 struct hdr_ctx ctx;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010233 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010234
10235 CHECK_HTTP_MESSAGE_FIRST();
10236
10237 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010238 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +020010239 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010240
10241 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010242 temp = get_trash_chunk();
10243 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010244 smp->type = SMP_T_STR;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010245 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010246 smp->data.str.len = ctx.vlen;
10247
10248 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010249 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010250 beg = http_get_path(txn);
10251 if (!beg)
10252 beg = end;
10253
10254 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10255
10256 if (beg < ptr && *beg == '/') {
10257 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10258 smp->data.str.len += ptr - beg;
10259 }
10260
10261 smp->flags = SMP_F_VOL_1ST;
10262 return 1;
10263}
10264
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010265/* This produces a 32-bit hash of the concatenation of the first occurrence of
10266 * the Host header followed by the path component if it begins with a slash ('/').
10267 * This means that '*' will not be added, resulting in exactly the first Host
10268 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010269 * is hashed using the path hash followed by a full avalanche hash and provides a
10270 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010271 * high-traffic sites without having to store whole paths.
10272 */
10273static int
10274smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010275 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010276{
10277 struct http_txn *txn = l7;
10278 struct hdr_ctx ctx;
10279 unsigned int hash = 0;
10280 char *ptr, *beg, *end;
10281 int len;
10282
10283 CHECK_HTTP_MESSAGE_FIRST();
10284
10285 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010286 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010287 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10288 ptr = ctx.line + ctx.val;
10289 len = ctx.vlen;
10290 while (len--)
10291 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10292 }
10293
10294 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010295 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010296 beg = http_get_path(txn);
10297 if (!beg)
10298 beg = end;
10299
10300 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10301
10302 if (beg < ptr && *beg == '/') {
10303 while (beg < ptr)
10304 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10305 }
10306 hash = full_hash(hash);
10307
10308 smp->type = SMP_T_UINT;
10309 smp->data.uint = hash;
10310 smp->flags = SMP_F_VOL_1ST;
10311 return 1;
10312}
10313
Willy Tarreau4a550602012-12-09 14:53:32 +010010314/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010315 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10316 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10317 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010318 * that in environments where IPv6 is insignificant, truncating the output to
10319 * 8 bytes would still work.
10320 */
10321static int
10322smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010323 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +010010324{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010325 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010326 struct connection *cli_conn = objt_conn(l4->si[0].end);
10327
10328 if (!cli_conn)
10329 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010330
Willy Tarreauef38c392013-07-22 16:29:32 +020010331 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +010010332 return 0;
10333
Willy Tarreau47ca5452012-12-23 20:22:19 +010010334 temp = get_trash_chunk();
Willy Tarreau0dff81c2014-07-15 21:34:06 +020010335 *(unsigned int *)temp->str = htonl(smp->data.uint);
10336 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010337
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010338 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010339 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010340 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010341 temp->len += 4;
10342 break;
10343 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010344 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010345 temp->len += 16;
10346 break;
10347 default:
10348 return 0;
10349 }
10350
10351 smp->data.str = *temp;
10352 smp->type = SMP_T_BIN;
10353 return 1;
10354}
10355
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010356static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010357smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010358 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010359{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010360 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10361 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10362 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010363
Willy Tarreau24e32d82012-04-23 23:55:44 +020010364 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010365
Willy Tarreauf853c462012-04-23 18:53:56 +020010366 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010367 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010368 return 1;
10369}
10370
Willy Tarreau7f18e522010-10-22 20:04:13 +020010371/* return a valid test if the current request is the first one on the connection */
10372static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010373smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010374 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010375{
10376 if (!s)
10377 return 0;
10378
Willy Tarreauf853c462012-04-23 18:53:56 +020010379 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010380 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010381 return 1;
10382}
10383
Willy Tarreau34db1082012-04-19 17:16:54 +020010384/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010385static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010386smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010387 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010388{
10389
Willy Tarreau24e32d82012-04-23 23:55:44 +020010390 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010391 return 0;
10392
Willy Tarreauc0239e02012-04-16 14:42:55 +020010393 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010394
Willy Tarreauc0239e02012-04-16 14:42:55 +020010395 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010396 return 0;
10397
Willy Tarreauf853c462012-04-23 18:53:56 +020010398 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010399 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010400 return 1;
10401}
Willy Tarreau8797c062007-05-07 00:55:35 +020010402
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010403/* Accepts exactly 1 argument of type userlist */
10404static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010405smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010406 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010407{
10408
10409 if (!args || args->type != ARGT_USR)
10410 return 0;
10411
10412 CHECK_HTTP_MESSAGE_FIRST();
10413
10414 if (!get_http_auth(l4))
10415 return 0;
10416
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010417 /* if the user does not belong to the userlist or has a wrong password,
10418 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010419 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010420 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010421 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10422 return 0;
10423
10424 /* pat_match_auth() will need the user list */
10425 smp->ctx.a[0] = args->data.usr;
10426
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010427 smp->type = SMP_T_STR;
10428 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010429 smp->data.str.str = l4->txn.auth.user;
10430 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010431
10432 return 1;
10433}
10434
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010435/* Try to find the next occurrence of a cookie name in a cookie header value.
10436 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10437 * the cookie value is returned into *value and *value_l, and the function
10438 * returns a pointer to the next pointer to search from if the value was found.
10439 * Otherwise if the cookie was not found, NULL is returned and neither value
10440 * nor value_l are touched. The input <hdr> string should first point to the
10441 * header's value, and the <hdr_end> pointer must point to the first character
10442 * not part of the value. <list> must be non-zero if value may represent a list
10443 * of values (cookie headers). This makes it faster to abort parsing when no
10444 * list is expected.
10445 */
10446static char *
10447extract_cookie_value(char *hdr, const char *hdr_end,
10448 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010449 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010450{
10451 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10452 char *next;
10453
10454 /* we search at least a cookie name followed by an equal, and more
10455 * generally something like this :
10456 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10457 */
10458 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10459 /* Iterate through all cookies on this line */
10460
10461 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10462 att_beg++;
10463
10464 /* find att_end : this is the first character after the last non
10465 * space before the equal. It may be equal to hdr_end.
10466 */
10467 equal = att_end = att_beg;
10468
10469 while (equal < hdr_end) {
10470 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10471 break;
10472 if (http_is_spht[(unsigned char)*equal++])
10473 continue;
10474 att_end = equal;
10475 }
10476
10477 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10478 * is between <att_beg> and <equal>, both may be identical.
10479 */
10480
10481 /* look for end of cookie if there is an equal sign */
10482 if (equal < hdr_end && *equal == '=') {
10483 /* look for the beginning of the value */
10484 val_beg = equal + 1;
10485 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10486 val_beg++;
10487
10488 /* find the end of the value, respecting quotes */
10489 next = find_cookie_value_end(val_beg, hdr_end);
10490
10491 /* make val_end point to the first white space or delimitor after the value */
10492 val_end = next;
10493 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10494 val_end--;
10495 } else {
10496 val_beg = val_end = next = equal;
10497 }
10498
10499 /* We have nothing to do with attributes beginning with '$'. However,
10500 * they will automatically be removed if a header before them is removed,
10501 * since they're supposed to be linked together.
10502 */
10503 if (*att_beg == '$')
10504 continue;
10505
10506 /* Ignore cookies with no equal sign */
10507 if (equal == next)
10508 continue;
10509
10510 /* Now we have the cookie name between att_beg and att_end, and
10511 * its value between val_beg and val_end.
10512 */
10513
10514 if (att_end - att_beg == cookie_name_l &&
10515 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10516 /* let's return this value and indicate where to go on from */
10517 *value = val_beg;
10518 *value_l = val_end - val_beg;
10519 return next + 1;
10520 }
10521
10522 /* Set-Cookie headers only have the name in the first attr=value part */
10523 if (!list)
10524 break;
10525 }
10526
10527 return NULL;
10528}
10529
William Lallemanda43ba4e2014-01-28 18:14:25 +010010530/* Fetch a captured HTTP request header. The index is the position of
10531 * the "capture" option in the configuration file
10532 */
10533static int
10534smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10535 const struct arg *args, struct sample *smp, const char *kw)
10536{
10537 struct proxy *fe = l4->fe;
10538 struct http_txn *txn = l7;
10539 int idx;
10540
10541 if (!args || args->type != ARGT_UINT)
10542 return 0;
10543
10544 idx = args->data.uint;
10545
10546 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10547 return 0;
10548
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010549 smp->type = SMP_T_STR;
10550 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010551 smp->data.str.str = txn->req.cap[idx];
10552 smp->data.str.len = strlen(txn->req.cap[idx]);
10553
10554 return 1;
10555}
10556
10557/* Fetch a captured HTTP response header. The index is the position of
10558 * the "capture" option in the configuration file
10559 */
10560static int
10561smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10562 const struct arg *args, struct sample *smp, const char *kw)
10563{
10564 struct proxy *fe = l4->fe;
10565 struct http_txn *txn = l7;
10566 int idx;
10567
10568 if (!args || args->type != ARGT_UINT)
10569 return 0;
10570
10571 idx = args->data.uint;
10572
10573 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10574 return 0;
10575
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010576 smp->type = SMP_T_STR;
10577 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010578 smp->data.str.str = txn->rsp.cap[idx];
10579 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10580
10581 return 1;
10582}
10583
William Lallemand65ad6e12014-01-31 15:08:02 +010010584/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10585static int
10586smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10587 const struct arg *args, struct sample *smp, const char *kw)
10588{
10589 struct chunk *temp;
10590 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010591 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010592
10593 if (!txn->uri)
10594 return 0;
10595
William Lallemand96a77852014-02-05 00:30:02 +010010596 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010597
William Lallemand96a77852014-02-05 00:30:02 +010010598 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10599 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010600
William Lallemand96a77852014-02-05 00:30:02 +010010601 temp = get_trash_chunk();
10602 temp->str = txn->uri;
10603 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010604 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010605 smp->type = SMP_T_STR;
10606 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010607
10608 return 1;
10609
10610}
10611
10612/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10613static int
10614smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10615 const struct arg *args, struct sample *smp, const char *kw)
10616{
10617 struct chunk *temp;
10618 struct http_txn *txn = l7;
10619 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010620
10621 if (!txn->uri)
10622 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010623
William Lallemand65ad6e12014-01-31 15:08:02 +010010624 ptr = txn->uri;
10625
10626 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10627 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010628
William Lallemand65ad6e12014-01-31 15:08:02 +010010629 if (!*ptr)
10630 return 0;
10631
10632 ptr++; /* skip the space */
10633
10634 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010635 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010636 if (!ptr)
10637 return 0;
10638 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10639 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010640
10641 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010642 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010643 smp->type = SMP_T_STR;
10644 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010645
10646 return 1;
10647}
10648
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010649/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10650 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10651 */
10652static int
10653smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10654 const struct arg *args, struct sample *smp, const char *kw)
10655{
10656 struct http_txn *txn = l7;
10657
10658 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10659 return 0;
10660
10661 if (txn->req.flags & HTTP_MSGF_VER_11)
10662 smp->data.str.str = "HTTP/1.1";
10663 else
10664 smp->data.str.str = "HTTP/1.0";
10665
10666 smp->data.str.len = 8;
10667 smp->type = SMP_T_STR;
10668 smp->flags = SMP_F_CONST;
10669 return 1;
10670
10671}
10672
10673/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10674 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10675 */
10676static int
10677smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10678 const struct arg *args, struct sample *smp, const char *kw)
10679{
10680 struct http_txn *txn = l7;
10681
10682 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10683 return 0;
10684
10685 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10686 smp->data.str.str = "HTTP/1.1";
10687 else
10688 smp->data.str.str = "HTTP/1.0";
10689
10690 smp->data.str.len = 8;
10691 smp->type = SMP_T_STR;
10692 smp->flags = SMP_F_CONST;
10693 return 1;
10694
10695}
10696
William Lallemand65ad6e12014-01-31 15:08:02 +010010697
Willy Tarreaue333ec92012-04-16 16:26:40 +020010698/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010699 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010700 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010701 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010702 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010703 * Accepts exactly 1 argument of type string. If the input options indicate
10704 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010705 * The returned sample is of type CSTR. Can be used to parse cookies in other
10706 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010707 */
William Lallemand07c8b242014-05-02 17:11:07 +020010708int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010709 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010710{
10711 struct http_txn *txn = l7;
10712 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010713 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010714 const struct http_msg *msg;
10715 const char *hdr_name;
10716 int hdr_name_len;
10717 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010718 int occ = 0;
10719 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010720
Willy Tarreau24e32d82012-04-23 23:55:44 +020010721 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010722 return 0;
10723
Willy Tarreaua890d072013-04-02 12:01:06 +020010724 if (!ctx) {
10725 /* first call */
10726 ctx = &static_hdr_ctx;
10727 ctx->idx = 0;
10728 smp->ctx.a[2] = ctx;
10729 }
10730
Willy Tarreaue333ec92012-04-16 16:26:40 +020010731 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010732
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010733 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010734 msg = &txn->req;
10735 hdr_name = "Cookie";
10736 hdr_name_len = 6;
10737 } else {
10738 msg = &txn->rsp;
10739 hdr_name = "Set-Cookie";
10740 hdr_name_len = 10;
10741 }
10742
Willy Tarreau28376d62012-04-26 21:26:10 +020010743 if (!occ && !(opt & SMP_OPT_ITERATE))
10744 /* no explicit occurrence and single fetch => last cookie by default */
10745 occ = -1;
10746
10747 /* OK so basically here, either we want only one value and it's the
10748 * last one, or we want to iterate over all of them and we fetch the
10749 * next one.
10750 */
10751
Willy Tarreau9b28e032012-10-12 23:49:43 +020010752 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010753 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010754 /* search for the header from the beginning, we must first initialize
10755 * the search parameters.
10756 */
Willy Tarreau37406352012-04-23 16:16:37 +020010757 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010758 ctx->idx = 0;
10759 }
10760
Willy Tarreau28376d62012-04-26 21:26:10 +020010761 smp->flags |= SMP_F_VOL_HDR;
10762
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010763 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010764 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10765 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010766 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10767 goto out;
10768
Willy Tarreau24e32d82012-04-23 23:55:44 +020010769 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010770 continue;
10771
Willy Tarreau37406352012-04-23 16:16:37 +020010772 smp->ctx.a[0] = ctx->line + ctx->val;
10773 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010774 }
10775
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010776 smp->type = SMP_T_STR;
10777 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010778 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010779 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010780 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010781 &smp->data.str.str,
10782 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010783 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010784 found = 1;
10785 if (occ >= 0) {
10786 /* one value was returned into smp->data.str.{str,len} */
10787 smp->flags |= SMP_F_NOT_LAST;
10788 return 1;
10789 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010790 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010791 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010792 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010793 /* all cookie headers and values were scanned. If we're looking for the
10794 * last occurrence, we may return it now.
10795 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010796 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010797 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010798 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010799}
10800
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010801/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010802 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010803 * multiple cookies may be parsed on the same line. The returned sample is of
10804 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010805 */
10806static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010807smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010808 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010809{
10810 struct http_txn *txn = l7;
10811 struct hdr_idx *idx = &txn->hdr_idx;
10812 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010813 const struct http_msg *msg;
10814 const char *hdr_name;
10815 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010816 int cnt;
10817 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010818 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010819
Willy Tarreau24e32d82012-04-23 23:55:44 +020010820 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010821 return 0;
10822
Willy Tarreaue333ec92012-04-16 16:26:40 +020010823 CHECK_HTTP_MESSAGE_FIRST();
10824
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010825 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010826 msg = &txn->req;
10827 hdr_name = "Cookie";
10828 hdr_name_len = 6;
10829 } else {
10830 msg = &txn->rsp;
10831 hdr_name = "Set-Cookie";
10832 hdr_name_len = 10;
10833 }
10834
Willy Tarreau9b28e032012-10-12 23:49:43 +020010835 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010836 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010837 ctx.idx = 0;
10838 cnt = 0;
10839
10840 while (1) {
10841 /* Note: val_beg == NULL every time we need to fetch a new header */
10842 if (!val_beg) {
10843 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10844 break;
10845
Willy Tarreau24e32d82012-04-23 23:55:44 +020010846 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010847 continue;
10848
10849 val_beg = ctx.line + ctx.val;
10850 val_end = val_beg + ctx.vlen;
10851 }
10852
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010853 smp->type = SMP_T_STR;
10854 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010855 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010856 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010857 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010858 &smp->data.str.str,
10859 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010860 cnt++;
10861 }
10862 }
10863
Willy Tarreaub169eba2013-12-16 15:14:43 +010010864 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010865 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010866 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010867 return 1;
10868}
10869
Willy Tarreau51539362012-05-08 12:46:28 +020010870/* Fetch an cookie's integer value. The integer value is returned. It
10871 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10872 */
10873static int
10874smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010875 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010876{
Willy Tarreauef38c392013-07-22 16:29:32 +020010877 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010878
10879 if (ret > 0) {
10880 smp->type = SMP_T_UINT;
10881 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10882 }
10883
10884 return ret;
10885}
10886
Willy Tarreau8797c062007-05-07 00:55:35 +020010887/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010888/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010889/************************************************************************/
10890
David Cournapeau16023ee2010-12-23 20:55:41 +090010891/*
10892 * Given a path string and its length, find the position of beginning of the
10893 * query string. Returns NULL if no query string is found in the path.
10894 *
10895 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10896 *
10897 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10898 */
bedis4c75cca2012-10-05 08:38:24 +020010899static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010900{
10901 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010902
bedis4c75cca2012-10-05 08:38:24 +020010903 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010904 return p ? p + 1 : NULL;
10905}
10906
bedis4c75cca2012-10-05 08:38:24 +020010907static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010908{
bedis4c75cca2012-10-05 08:38:24 +020010909 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010910}
10911
10912/*
10913 * Given a url parameter, find the starting position of the first occurence,
10914 * or NULL if the parameter is not found.
10915 *
10916 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10917 * the function will return query_string+8.
10918 */
10919static char*
10920find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010921 char* url_param_name, size_t url_param_name_l,
10922 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010923{
10924 char *pos, *last;
10925
10926 pos = query_string;
10927 last = query_string + query_string_l - url_param_name_l - 1;
10928
10929 while (pos <= last) {
10930 if (pos[url_param_name_l] == '=') {
10931 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10932 return pos;
10933 pos += url_param_name_l + 1;
10934 }
bedis4c75cca2012-10-05 08:38:24 +020010935 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010936 pos++;
10937 pos++;
10938 }
10939 return NULL;
10940}
10941
10942/*
10943 * Given a url parameter name, returns its value and size into *value and
10944 * *value_l respectively, and returns non-zero. If the parameter is not found,
10945 * zero is returned and value/value_l are not touched.
10946 */
10947static int
10948find_url_param_value(char* path, size_t path_l,
10949 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010950 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010951{
10952 char *query_string, *qs_end;
10953 char *arg_start;
10954 char *value_start, *value_end;
10955
bedis4c75cca2012-10-05 08:38:24 +020010956 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010957 if (!query_string)
10958 return 0;
10959
10960 qs_end = path + path_l;
10961 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010962 url_param_name, url_param_name_l,
10963 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010964 if (!arg_start)
10965 return 0;
10966
10967 value_start = arg_start + url_param_name_l + 1;
10968 value_end = value_start;
10969
bedis4c75cca2012-10-05 08:38:24 +020010970 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010971 value_end++;
10972
10973 *value = value_start;
10974 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010975 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010976}
10977
10978static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010979smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010980 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010981{
bedis4c75cca2012-10-05 08:38:24 +020010982 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010983 struct http_txn *txn = l7;
10984 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010985
bedis4c75cca2012-10-05 08:38:24 +020010986 if (!args || args[0].type != ARGT_STR ||
10987 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010988 return 0;
10989
10990 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010991
bedis4c75cca2012-10-05 08:38:24 +020010992 if (args[1].type)
10993 delim = *args[1].data.str.str;
10994
Willy Tarreau9b28e032012-10-12 23:49:43 +020010995 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010996 args->data.str.str, args->data.str.len,
10997 &smp->data.str.str, &smp->data.str.len,
10998 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010999 return 0;
11000
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011001 smp->type = SMP_T_STR;
11002 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011003 return 1;
11004}
11005
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011006/* Return the signed integer value for the specified url parameter (see url_param
11007 * above).
11008 */
11009static int
11010smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011011 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011012{
Willy Tarreauef38c392013-07-22 16:29:32 +020011013 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011014
11015 if (ret > 0) {
11016 smp->type = SMP_T_UINT;
11017 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11018 }
11019
11020 return ret;
11021}
11022
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011023/* This produces a 32-bit hash of the concatenation of the first occurrence of
11024 * the Host header followed by the path component if it begins with a slash ('/').
11025 * This means that '*' will not be added, resulting in exactly the first Host
11026 * entry. If no Host header is found, then the path is used. The resulting value
11027 * is hashed using the url hash followed by a full avalanche hash and provides a
11028 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11029 * high-traffic sites without having to store whole paths.
11030 * this differs from the base32 functions in that it includes the url parameters
11031 * as well as the path
11032 */
11033static int
11034smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011035 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011036{
11037 struct http_txn *txn = l7;
11038 struct hdr_ctx ctx;
11039 unsigned int hash = 0;
11040 char *ptr, *beg, *end;
11041 int len;
11042
11043 CHECK_HTTP_MESSAGE_FIRST();
11044
11045 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011046 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011047 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11048 ptr = ctx.line + ctx.val;
11049 len = ctx.vlen;
11050 while (len--)
11051 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11052 }
11053
11054 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011055 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 +000011056 beg = http_get_path(txn);
11057 if (!beg)
11058 beg = end;
11059
11060 for (ptr = beg; ptr < end ; ptr++);
11061
11062 if (beg < ptr && *beg == '/') {
11063 while (beg < ptr)
11064 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11065 }
11066 hash = full_hash(hash);
11067
11068 smp->type = SMP_T_UINT;
11069 smp->data.uint = hash;
11070 smp->flags = SMP_F_VOL_1ST;
11071 return 1;
11072}
11073
11074/* This concatenates the source address with the 32-bit hash of the Host and
11075 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11076 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11077 * on the source address length. The URL hash is stored before the address so
11078 * that in environments where IPv6 is insignificant, truncating the output to
11079 * 8 bytes would still work.
11080 */
11081static int
11082smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011083 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011084{
11085 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011086 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011087
Willy Tarreaue155ec22013-11-18 18:33:22 +010011088 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011089 return 0;
11090
11091 temp = get_trash_chunk();
11092 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11093 temp->len += sizeof(smp->data.uint);
11094
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011095 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011096 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011097 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011098 temp->len += 4;
11099 break;
11100 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011101 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011102 temp->len += 16;
11103 break;
11104 default:
11105 return 0;
11106 }
11107
11108 smp->data.str = *temp;
11109 smp->type = SMP_T_BIN;
11110 return 1;
11111}
11112
Willy Tarreau185b5c42012-04-26 15:11:51 +020011113/* This function is used to validate the arguments passed to any "hdr" fetch
11114 * keyword. These keywords support an optional positive or negative occurrence
11115 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11116 * is assumed that the types are already the correct ones. Returns 0 on error,
11117 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11118 * error message in case of error, that the caller is responsible for freeing.
11119 * The initial location must either be freeable or NULL.
11120 */
11121static int val_hdr(struct arg *arg, char **err_msg)
11122{
11123 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011124 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011125 return 0;
11126 }
11127 return 1;
11128}
11129
Willy Tarreau276fae92013-07-25 14:36:01 +020011130/* takes an UINT value on input supposed to represent the time since EPOCH,
11131 * adds an optional offset found in args[0] and emits a string representing
11132 * the date in RFC-1123/5322 format.
11133 */
11134static int sample_conv_http_date(const struct arg *args, struct sample *smp)
11135{
11136 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11137 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11138 struct chunk *temp;
11139 struct tm *tm;
11140 time_t curr_date = smp->data.uint;
11141
11142 /* add offset */
11143 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11144 curr_date += args[0].data.sint;
11145
11146 tm = gmtime(&curr_date);
11147
11148 temp = get_trash_chunk();
11149 temp->len = snprintf(temp->str, temp->size - temp->len,
11150 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11151 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11152 tm->tm_hour, tm->tm_min, tm->tm_sec);
11153
11154 smp->data.str = *temp;
11155 smp->type = SMP_T_STR;
11156 return 1;
11157}
11158
Thierry FOURNIERad903512014-04-11 17:51:01 +020011159/* Match language range with language tag. RFC2616 14.4:
11160 *
11161 * A language-range matches a language-tag if it exactly equals
11162 * the tag, or if it exactly equals a prefix of the tag such
11163 * that the first tag character following the prefix is "-".
11164 *
11165 * Return 1 if the strings match, else return 0.
11166 */
11167static inline int language_range_match(const char *range, int range_len,
11168 const char *tag, int tag_len)
11169{
11170 const char *end = range + range_len;
11171 const char *tend = tag + tag_len;
11172 while (range < end) {
11173 if (*range == '-' && tag == tend)
11174 return 1;
11175 if (*range != *tag || tag == tend)
11176 return 0;
11177 range++;
11178 tag++;
11179 }
11180 /* Return true only if the last char of the tag is matched. */
11181 return tag == tend;
11182}
11183
11184/* Arguments: The list of expected value, the number of parts returned and the separator */
11185static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
11186{
11187 const char *al = smp->data.str.str;
11188 const char *end = al + smp->data.str.len;
11189 const char *token;
11190 int toklen;
11191 int qvalue;
11192 const char *str;
11193 const char *w;
11194 int best_q = 0;
11195
11196 /* Set the constant to the sample, because the output of the
11197 * function will be peek in the constant configuration string.
11198 */
11199 smp->flags |= SMP_F_CONST;
11200 smp->data.str.size = 0;
11201 smp->data.str.str = "";
11202 smp->data.str.len = 0;
11203
11204 /* Parse the accept language */
11205 while (1) {
11206
11207 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011208 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011209 al++;
11210 if (al >= end)
11211 break;
11212
11213 /* Start of the fisrt word. */
11214 token = al;
11215
11216 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011217 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011218 al++;
11219 if (al == token)
11220 goto expect_comma;
11221
11222 /* Length of the token. */
11223 toklen = al - token;
11224 qvalue = 1000;
11225
11226 /* Check if the token exists in the list. If the token not exists,
11227 * jump to the next token.
11228 */
11229 str = args[0].data.str.str;
11230 w = str;
11231 while (1) {
11232 if (*str == ';' || *str == '\0') {
11233 if (language_range_match(token, toklen, w, str-w))
11234 goto look_for_q;
11235 if (*str == '\0')
11236 goto expect_comma;
11237 w = str + 1;
11238 }
11239 str++;
11240 }
11241 goto expect_comma;
11242
11243look_for_q:
11244
11245 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011246 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011247 al++;
11248 if (al >= end)
11249 goto process_value;
11250
11251 /* If ',' is found, process the result */
11252 if (*al == ',')
11253 goto process_value;
11254
11255 /* If the character is different from ';', look
11256 * for the end of the header part in best effort.
11257 */
11258 if (*al != ';')
11259 goto expect_comma;
11260
11261 /* Assumes that the char is ';', now expect "q=". */
11262 al++;
11263
11264 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011265 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011266 al++;
11267 if (al >= end)
11268 goto process_value;
11269
11270 /* Expect 'q'. If no 'q', continue in best effort */
11271 if (*al != 'q')
11272 goto process_value;
11273 al++;
11274
11275 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011276 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011277 al++;
11278 if (al >= end)
11279 goto process_value;
11280
11281 /* Expect '='. If no '=', continue in best effort */
11282 if (*al != '=')
11283 goto process_value;
11284 al++;
11285
11286 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011287 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011288 al++;
11289 if (al >= end)
11290 goto process_value;
11291
11292 /* Parse the q value. */
11293 qvalue = parse_qvalue(al, &al);
11294
11295process_value:
11296
11297 /* If the new q value is the best q value, then store the associated
11298 * language in the response. If qvalue is the biggest value (1000),
11299 * break the process.
11300 */
11301 if (qvalue > best_q) {
11302 smp->data.str.str = (char *)w;
11303 smp->data.str.len = str - w;
11304 if (qvalue >= 1000)
11305 break;
11306 best_q = qvalue;
11307 }
11308
11309expect_comma:
11310
11311 /* Expect comma or end. If the end is detected, quit the loop. */
11312 while (al < end && *al != ',')
11313 al++;
11314 if (al >= end)
11315 break;
11316
11317 /* Comma is found, jump it and restart the analyzer. */
11318 al++;
11319 }
11320
11321 /* Set default value if required. */
11322 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11323 smp->data.str.str = args[1].data.str.str;
11324 smp->data.str.len = args[1].data.str.len;
11325 }
11326
11327 /* Return true only if a matching language was found. */
11328 return smp->data.str.len != 0;
11329}
11330
William Lallemand73025dd2014-04-24 14:38:37 +020011331/*
11332 * Return the struct http_req_action_kw associated to a keyword.
11333 */
11334struct http_req_action_kw *action_http_req_custom(const char *kw)
11335{
11336 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11337 struct http_req_action_kw_list *kw_list;
11338 int i;
11339
11340 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11341 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11342 if (!strcmp(kw, kw_list->kw[i].kw))
11343 return &kw_list->kw[i];
11344 }
11345 }
11346 }
11347 return NULL;
11348}
11349
11350/*
11351 * Return the struct http_res_action_kw associated to a keyword.
11352 */
11353struct http_res_action_kw *action_http_res_custom(const char *kw)
11354{
11355 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11356 struct http_res_action_kw_list *kw_list;
11357 int i;
11358
11359 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11360 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11361 if (!strcmp(kw, kw_list->kw[i].kw))
11362 return &kw_list->kw[i];
11363 }
11364 }
11365 }
11366 return NULL;
11367}
11368
Willy Tarreau4a568972010-05-12 08:08:50 +020011369/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011370/* All supported ACL keywords must be declared here. */
11371/************************************************************************/
11372
11373/* Note: must not be declared <const> as its list will be overwritten.
11374 * Please take care of keeping this list alphabetically sorted.
11375 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011376static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011377 { "base", "base", PAT_MATCH_STR },
11378 { "base_beg", "base", PAT_MATCH_BEG },
11379 { "base_dir", "base", PAT_MATCH_DIR },
11380 { "base_dom", "base", PAT_MATCH_DOM },
11381 { "base_end", "base", PAT_MATCH_END },
11382 { "base_len", "base", PAT_MATCH_LEN },
11383 { "base_reg", "base", PAT_MATCH_REG },
11384 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011385
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011386 { "cook", "req.cook", PAT_MATCH_STR },
11387 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11388 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11389 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11390 { "cook_end", "req.cook", PAT_MATCH_END },
11391 { "cook_len", "req.cook", PAT_MATCH_LEN },
11392 { "cook_reg", "req.cook", PAT_MATCH_REG },
11393 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011394
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011395 { "hdr", "req.hdr", PAT_MATCH_STR },
11396 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11397 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11398 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11399 { "hdr_end", "req.hdr", PAT_MATCH_END },
11400 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11401 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11402 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011403
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011404 /* these two declarations uses strings with list storage (in place
11405 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11406 * and delete functions are relative to the list management. The parse
11407 * and match method are related to the corresponding fetch methods. This
11408 * is very particular ACL declaration mode.
11409 */
11410 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11411 { "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 +020011412
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011413 { "path", "path", PAT_MATCH_STR },
11414 { "path_beg", "path", PAT_MATCH_BEG },
11415 { "path_dir", "path", PAT_MATCH_DIR },
11416 { "path_dom", "path", PAT_MATCH_DOM },
11417 { "path_end", "path", PAT_MATCH_END },
11418 { "path_len", "path", PAT_MATCH_LEN },
11419 { "path_reg", "path", PAT_MATCH_REG },
11420 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011421
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011422 { "req_ver", "req.ver", PAT_MATCH_STR },
11423 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011424
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011425 { "scook", "res.cook", PAT_MATCH_STR },
11426 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11427 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11428 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11429 { "scook_end", "res.cook", PAT_MATCH_END },
11430 { "scook_len", "res.cook", PAT_MATCH_LEN },
11431 { "scook_reg", "res.cook", PAT_MATCH_REG },
11432 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011433
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011434 { "shdr", "res.hdr", PAT_MATCH_STR },
11435 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11436 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11437 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11438 { "shdr_end", "res.hdr", PAT_MATCH_END },
11439 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11440 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11441 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011442
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011443 { "url", "url", PAT_MATCH_STR },
11444 { "url_beg", "url", PAT_MATCH_BEG },
11445 { "url_dir", "url", PAT_MATCH_DIR },
11446 { "url_dom", "url", PAT_MATCH_DOM },
11447 { "url_end", "url", PAT_MATCH_END },
11448 { "url_len", "url", PAT_MATCH_LEN },
11449 { "url_reg", "url", PAT_MATCH_REG },
11450 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011451
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011452 { "urlp", "urlp", PAT_MATCH_STR },
11453 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11454 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11455 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11456 { "urlp_end", "urlp", PAT_MATCH_END },
11457 { "urlp_len", "urlp", PAT_MATCH_LEN },
11458 { "urlp_reg", "urlp", PAT_MATCH_REG },
11459 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011460
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011461 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011462}};
11463
11464/************************************************************************/
11465/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011466/************************************************************************/
11467/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011468static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011469 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011470 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11471 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11472
William Lallemanda43ba4e2014-01-28 18:14:25 +010011473 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011474 { "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 +020011475
11476 /* retrieve these captures from the HTTP logs */
11477 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11478 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11479 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11480
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011481 { "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 +020011482 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011483
Willy Tarreau409bcde2013-01-08 00:31:00 +010011484 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11485 * are only here to match the ACL's name, are request-only and are used
11486 * for ACL compatibility only.
11487 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011488 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11489 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011490 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11491 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11492
11493 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11494 * only here to match the ACL's name, are request-only and are used for
11495 * ACL compatibility only.
11496 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011497 { "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 +010011498 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11499 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11500 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11501
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011502 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011503 { "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 +010011504 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011505 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011506 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011507
11508 /* HTTP protocol on the request path */
11509 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011510 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011511
11512 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011513 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11514 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011515
11516 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011517 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11518 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011519
Willy Tarreau18ed2562013-01-14 15:56:36 +010011520 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011521 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011522 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11523 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11524
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011525 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011526 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011527 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011528 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11529 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11530 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11531
11532 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011533 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011534 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11535 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11536
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011537 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011538 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011539 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011540 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11541 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11542 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11543
Willy Tarreau409bcde2013-01-08 00:31:00 +010011544 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011545 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011546 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11547 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011548 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011549
11550 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011551 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011552 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11553 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11554 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11555
11556 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011557 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011558 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11559 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011560 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11561 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011562 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11563 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011564 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11565 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011566}};
11567
Willy Tarreau8797c062007-05-07 00:55:35 +020011568
Willy Tarreau276fae92013-07-25 14:36:01 +020011569/* Note: must not be declared <const> as its list will be overwritten */
11570static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011571 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11572 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011573 { NULL, NULL, 0, 0, 0 },
11574}};
11575
Willy Tarreau8797c062007-05-07 00:55:35 +020011576__attribute__((constructor))
11577static void __http_protocol_init(void)
11578{
11579 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011580 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011581 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020011582}
11583
11584
Willy Tarreau58f10d72006-12-04 02:26:12 +010011585/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011586 * Local variables:
11587 * c-indent-level: 8
11588 * c-basic-offset: 8
11589 * End:
11590 */