blob: 3fffc5bb461782b85346269357304d5dc7d86f27 [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"
Godbach1f1fae62014-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 */
734 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
735 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200736 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100737 return ctx->idx;
738 }
739
740 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200741 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
742 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100743 */
744
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200745 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200746 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200747 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100748 NULL, 0);
749 hdr->len += delta;
750 http_msg_move_end(msg, delta);
751 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200752 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100753 return ctx->idx;
754}
755
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100756/* This function handles a server error at the stream interface level. The
757 * stream interface is assumed to be already in a closed state. An optional
758 * message is copied into the input buffer, and an HTTP status code stored.
759 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100760 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200761 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200762static void http_server_error(struct session *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100763 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200764{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200765 channel_auto_read(si->ob);
766 channel_abort(si->ob);
767 channel_auto_close(si->ob);
768 channel_erase(si->ob);
769 channel_auto_close(si->ib);
770 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100771 if (status > 0 && msg) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200772 s->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200773 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200774 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200775 if (!(s->flags & SN_ERR_MASK))
776 s->flags |= err;
777 if (!(s->flags & SN_FINST_MASK))
778 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200779}
780
Willy Tarreau80587432006-12-24 17:47:20 +0100781/* This function returns the appropriate error location for the given session
782 * and message.
783 */
784
Willy Tarreau783f2582012-09-04 12:19:04 +0200785struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100786{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200787 if (s->be->errmsg[msgnum].str)
788 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100789 else if (s->fe->errmsg[msgnum].str)
790 return &s->fe->errmsg[msgnum];
791 else
792 return &http_err_chunks[msgnum];
793}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200794
Willy Tarreau53b6c742006-12-17 13:37:46 +0100795/*
796 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
797 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
798 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100799enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100800{
801 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100802 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100803
804 m = ((unsigned)*str - 'A');
805
806 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100807 for (h = http_methods[m]; h->len > 0; h++) {
808 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100809 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100810 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100811 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100812 };
813 return HTTP_METH_OTHER;
814 }
815 return HTTP_METH_NONE;
816
817}
818
Willy Tarreau21d2af32008-02-14 20:25:24 +0100819/* Parse the URI from the given transaction (which is assumed to be in request
820 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
821 * It is returned otherwise.
822 */
823static char *
824http_get_path(struct http_txn *txn)
825{
826 char *ptr, *end;
827
Willy Tarreau9b28e032012-10-12 23:49:43 +0200828 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100829 end = ptr + txn->req.sl.rq.u_l;
830
831 if (ptr >= end)
832 return NULL;
833
834 /* RFC2616, par. 5.1.2 :
835 * Request-URI = "*" | absuri | abspath | authority
836 */
837
838 if (*ptr == '*')
839 return NULL;
840
841 if (isalpha((unsigned char)*ptr)) {
842 /* this is a scheme as described by RFC3986, par. 3.1 */
843 ptr++;
844 while (ptr < end &&
845 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
846 ptr++;
847 /* skip '://' */
848 if (ptr == end || *ptr++ != ':')
849 return NULL;
850 if (ptr == end || *ptr++ != '/')
851 return NULL;
852 if (ptr == end || *ptr++ != '/')
853 return NULL;
854 }
855 /* skip [user[:passwd]@]host[:[port]] */
856
857 while (ptr < end && *ptr != '/')
858 ptr++;
859
860 if (ptr == end)
861 return NULL;
862
863 /* OK, we got the '/' ! */
864 return ptr;
865}
866
William Lallemand65ad6e12014-01-31 15:08:02 +0100867/* Parse the URI from the given string and look for the "/" beginning the PATH.
868 * If not found, return NULL. It is returned otherwise.
869 */
870static char *
871http_get_path_from_string(char *str)
872{
873 char *ptr = str;
874
875 /* RFC2616, par. 5.1.2 :
876 * Request-URI = "*" | absuri | abspath | authority
877 */
878
879 if (*ptr == '*')
880 return NULL;
881
882 if (isalpha((unsigned char)*ptr)) {
883 /* this is a scheme as described by RFC3986, par. 3.1 */
884 ptr++;
885 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
886 ptr++;
887 /* skip '://' */
888 if (*ptr == '\0' || *ptr++ != ':')
889 return NULL;
890 if (*ptr == '\0' || *ptr++ != '/')
891 return NULL;
892 if (*ptr == '\0' || *ptr++ != '/')
893 return NULL;
894 }
895 /* skip [user[:passwd]@]host[:[port]] */
896
897 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
898 ptr++;
899
900 if (*ptr == '\0' || *ptr == ' ')
901 return NULL;
902
903 /* OK, we got the '/' ! */
904 return ptr;
905}
906
Willy Tarreau71241ab2012-12-27 11:30:54 +0100907/* Returns a 302 for a redirectable request that reaches a server working in
908 * in redirect mode. This may only be called just after the stream interface
909 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
910 * follow normal proxy processing. NOTE: this function is designed to support
911 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100912 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100913void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100914{
915 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100916 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100917 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200918 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100919
920 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100921 trash.len = strlen(HTTP_302);
922 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100923
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100924 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100925
Willy Tarreauefb453c2008-10-26 20:49:47 +0100926 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100927 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100928 return;
929
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100930 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100931 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100932 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
933 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100934 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100935
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200936 /* 3: add the request URI. Since it was already forwarded, we need
937 * to temporarily rewind the buffer.
938 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100939 txn = &s->txn;
Willy Tarreau211cdec2014-04-17 20:18:08 +0200940 b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200941
Willy Tarreauefb453c2008-10-26 20:49:47 +0100942 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200943 len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200944
Willy Tarreau9b28e032012-10-12 23:49:43 +0200945 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200946
Willy Tarreauefb453c2008-10-26 20:49:47 +0100947 if (!path)
948 return;
949
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100950 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100951 return;
952
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100953 memcpy(trash.str + trash.len, path, len);
954 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100955
956 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100957 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
958 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100959 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100960 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
961 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100962 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100963
964 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200965 si_shutr(si);
966 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100967 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100968 si->state = SI_ST_CLO;
969
970 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200971 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100972
973 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100974 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500975 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100976}
977
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100978/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100979 * that the server side is closed. Note that err_type is actually a
980 * bitmask, where almost only aborts may be cumulated with other
981 * values. We consider that aborted operations are more important
982 * than timeouts or errors due to the fact that nobody else in the
983 * logs might explain incomplete retries. All others should avoid
984 * being cumulated. It should normally not be possible to have multiple
985 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100986 * Note that connection errors appearing on the second request of a keep-alive
987 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100988 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100989void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100991 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100992
993 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100994 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200995 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100996 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100997 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100998 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
999 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001000 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001001 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001002 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001003 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001004 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001005 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001006 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001007 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001008 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1009 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001010 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001011 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001012 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001013 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001014 else if (err_type & SI_ET_CONN_RES)
1015 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001016 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1017 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001018 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001019 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001020 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001021}
1022
Willy Tarreau42250582007-04-01 01:30:43 +02001023extern const char sess_term_cond[8];
1024extern const char sess_fin_state[8];
1025extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001026struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001027struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001028struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001029
Willy Tarreau117f59e2007-03-04 18:17:17 +01001030/*
1031 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001032 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001033 */
1034void capture_headers(char *som, struct hdr_idx *idx,
1035 char **cap, struct cap_hdr *cap_hdr)
1036{
1037 char *eol, *sol, *col, *sov;
1038 int cur_idx;
1039 struct cap_hdr *h;
1040 int len;
1041
1042 sol = som + hdr_idx_first_pos(idx);
1043 cur_idx = hdr_idx_first_idx(idx);
1044
1045 while (cur_idx) {
1046 eol = sol + idx->v[cur_idx].len;
1047
1048 col = sol;
1049 while (col < eol && *col != ':')
1050 col++;
1051
1052 sov = col + 1;
1053 while (sov < eol && http_is_lws[(unsigned char)*sov])
1054 sov++;
1055
1056 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001057 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001058 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1059 if (cap[h->index] == NULL)
1060 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001061 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001062
1063 if (cap[h->index] == NULL) {
1064 Alert("HTTP capture : out of memory.\n");
1065 continue;
1066 }
1067
1068 len = eol - sov;
1069 if (len > h->len)
1070 len = h->len;
1071
1072 memcpy(cap[h->index], sov, len);
1073 cap[h->index][len]=0;
1074 }
1075 }
1076 sol = eol + idx->v[cur_idx].cr + 1;
1077 cur_idx = idx->v[cur_idx].next;
1078 }
1079}
1080
1081
Willy Tarreau42250582007-04-01 01:30:43 +02001082/* either we find an LF at <ptr> or we jump to <bad>.
1083 */
1084#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1085
1086/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1087 * otherwise to <http_msg_ood> with <state> set to <st>.
1088 */
1089#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1090 ptr++; \
1091 if (likely(ptr < end)) \
1092 goto good; \
1093 else { \
1094 state = (st); \
1095 goto http_msg_ood; \
1096 } \
1097 } while (0)
1098
1099
Willy Tarreaubaaee002006-06-26 02:48:02 +02001100/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001101 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001102 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1103 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1104 * will give undefined results.
1105 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1106 * and that msg->sol points to the beginning of the response.
1107 * If a complete line is found (which implies that at least one CR or LF is
1108 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1109 * returned indicating an incomplete line (which does not mean that parts have
1110 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1111 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1112 * upon next call.
1113 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001114 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001115 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1116 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001117 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001118 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001119const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001120 enum ht_state state, const char *ptr, const char *end,
1121 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001122{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001123 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001124
Willy Tarreau8973c702007-01-21 23:58:29 +01001125 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001126 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001127 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001128 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001129 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1130
1131 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001132 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001133 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1134 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001135 state = HTTP_MSG_ERROR;
1136 break;
1137
Willy Tarreau8973c702007-01-21 23:58:29 +01001138 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001139 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001140 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001141 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001142 goto http_msg_rpcode;
1143 }
1144 if (likely(HTTP_IS_SPHT(*ptr)))
1145 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1146 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001147 state = HTTP_MSG_ERROR;
1148 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001149
Willy Tarreau8973c702007-01-21 23:58:29 +01001150 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001151 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001152 if (likely(!HTTP_IS_LWS(*ptr)))
1153 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1154
1155 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001156 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001157 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1158 }
1159
1160 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001161 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001162 http_msg_rsp_reason:
1163 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001164 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001165 msg->sl.st.r_l = 0;
1166 goto http_msg_rpline_eol;
1167
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001169 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001170 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001171 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001172 goto http_msg_rpreason;
1173 }
1174 if (likely(HTTP_IS_SPHT(*ptr)))
1175 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1176 /* so it's a CR/LF, so there is no reason phrase */
1177 goto http_msg_rsp_reason;
1178
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001180 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001181 if (likely(!HTTP_IS_CRLF(*ptr)))
1182 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001183 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001184 http_msg_rpline_eol:
1185 /* We have seen the end of line. Note that we do not
1186 * necessarily have the \n yet, but at least we know that we
1187 * have EITHER \r OR \n, otherwise the response would not be
1188 * complete. We can then record the response length and return
1189 * to the caller which will be able to register it.
1190 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001191 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001192 return ptr;
1193
Willy Tarreau8973c702007-01-21 23:58:29 +01001194 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001195#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001196 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1197 exit(1);
1198#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001199 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001200 }
1201
1202 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001203 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001204 if (ret_state)
1205 *ret_state = state;
1206 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001207 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001208 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001209}
1210
Willy Tarreau8973c702007-01-21 23:58:29 +01001211/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001212 * This function parses a request line between <ptr> and <end>, starting with
1213 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1214 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1215 * will give undefined results.
1216 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1217 * and that msg->sol points to the beginning of the request.
1218 * If a complete line is found (which implies that at least one CR or LF is
1219 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1220 * returned indicating an incomplete line (which does not mean that parts have
1221 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1222 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1223 * upon next call.
1224 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001225 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1227 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001228 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001229 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001230const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001231 enum ht_state state, const char *ptr, const char *end,
1232 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001233{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001234 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001235
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001238 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239 if (likely(HTTP_IS_TOKEN(*ptr)))
1240 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001241
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001242 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001243 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001244 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1245 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001246
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001247 if (likely(HTTP_IS_CRLF(*ptr))) {
1248 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001249 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001250 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001251 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001252 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001253 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001254 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001255 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001256 msg->sl.rq.v_l = 0;
1257 goto http_msg_rqline_eol;
1258 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001259 state = HTTP_MSG_ERROR;
1260 break;
1261
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001262 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001263 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001265 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001266 goto http_msg_rquri;
1267 }
1268 if (likely(HTTP_IS_SPHT(*ptr)))
1269 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1270 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1271 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001272
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001274 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001275 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001277
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001279 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001280 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1281 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001282
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001283 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001284 /* non-ASCII chars are forbidden unless option
1285 * accept-invalid-http-request is enabled in the frontend.
1286 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001287 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001288 if (msg->err_pos < -1)
1289 goto invalid_char;
1290 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001291 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001292 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1293 }
1294
1295 if (likely(HTTP_IS_CRLF(*ptr))) {
1296 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1297 goto http_msg_req09_uri_e;
1298 }
1299
1300 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001301 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001302 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001303 state = HTTP_MSG_ERROR;
1304 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001307 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001309 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 goto http_msg_rqver;
1311 }
1312 if (likely(HTTP_IS_SPHT(*ptr)))
1313 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1314 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1315 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001318 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001319 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001321
1322 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001323 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001324 http_msg_rqline_eol:
1325 /* We have seen the end of line. Note that we do not
1326 * necessarily have the \n yet, but at least we know that we
1327 * have EITHER \r OR \n, otherwise the request would not be
1328 * complete. We can then record the request length and return
1329 * to the caller which will be able to register it.
1330 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001331 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001332 return ptr;
1333 }
1334
1335 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001336 state = HTTP_MSG_ERROR;
1337 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001338
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001339 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001340#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001341 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1342 exit(1);
1343#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001344 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001345 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001348 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001349 if (ret_state)
1350 *ret_state = state;
1351 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001352 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001353 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001355
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001356/*
1357 * Returns the data from Authorization header. Function may be called more
1358 * than once so data is stored in txn->auth_data. When no header is found
1359 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001360 * searching again for something we are unable to find anyway. However, if
1361 * the result if valid, the cache is not reused because we would risk to
1362 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001363 */
1364
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001365/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1366 * set according to global.tune.bufsize.
1367 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001368char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001369
1370int
1371get_http_auth(struct session *s)
1372{
1373
1374 struct http_txn *txn = &s->txn;
1375 struct chunk auth_method;
1376 struct hdr_ctx ctx;
1377 char *h, *p;
1378 int len;
1379
1380#ifdef DEBUG_AUTH
1381 printf("Auth for session %p: %d\n", s, txn->auth.method);
1382#endif
1383
1384 if (txn->auth.method == HTTP_AUTH_WRONG)
1385 return 0;
1386
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001387 txn->auth.method = HTTP_AUTH_WRONG;
1388
1389 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001390
1391 if (txn->flags & TX_USE_PX_CONN) {
1392 h = "Proxy-Authorization";
1393 len = strlen(h);
1394 } else {
1395 h = "Authorization";
1396 len = strlen(h);
1397 }
1398
Willy Tarreau9b28e032012-10-12 23:49:43 +02001399 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001400 return 0;
1401
1402 h = ctx.line + ctx.val;
1403
1404 p = memchr(h, ' ', ctx.vlen);
1405 if (!p || p == h)
1406 return 0;
1407
1408 chunk_initlen(&auth_method, h, 0, p-h);
1409 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1410
1411 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1412
1413 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001414 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001415
1416 if (len < 0)
1417 return 0;
1418
1419
1420 get_http_auth_buff[len] = '\0';
1421
1422 p = strchr(get_http_auth_buff, ':');
1423
1424 if (!p)
1425 return 0;
1426
1427 txn->auth.user = get_http_auth_buff;
1428 *p = '\0';
1429 txn->auth.pass = p+1;
1430
1431 txn->auth.method = HTTP_AUTH_BASIC;
1432 return 1;
1433 }
1434
1435 return 0;
1436}
1437
Willy Tarreau58f10d72006-12-04 02:26:12 +01001438
Willy Tarreau8973c702007-01-21 23:58:29 +01001439/*
1440 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001441 * depending on the initial msg->msg_state. The caller is responsible for
1442 * ensuring that the message does not wrap. The function can be preempted
1443 * everywhere when data are missing and recalled at the exact same location
1444 * with no information loss. The message may even be realigned between two
1445 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001446 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001447 * fields. Note that msg->sol will be initialized after completing the first
1448 * state, so that none of the msg pointers has to be initialized prior to the
1449 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001451void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452{
Willy Tarreau3770f232013-12-07 00:01:53 +01001453 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001455 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001456
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001457 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001458 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001459 ptr = buf->p + msg->next;
1460 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001461
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 if (unlikely(ptr >= end))
1463 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001466 /*
1467 * First, states that are specific to the response only.
1468 * We check them first so that request and headers are
1469 * closer to each other (accessed more often).
1470 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001471 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001472 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001473 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001474 /* we have a start of message, but we have to check
1475 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001476 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001477 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001478 if (unlikely(ptr != buf->p)) {
1479 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001480 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001481 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001482 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001483 }
Willy Tarreau26927362012-05-18 23:22:52 +02001484 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001485 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001486 hdr_idx_init(idx);
1487 state = HTTP_MSG_RPVER;
1488 goto http_msg_rpver;
1489 }
1490
1491 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1492 goto http_msg_invalid;
1493
1494 if (unlikely(*ptr == '\n'))
1495 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1496 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1497 /* stop here */
1498
Willy Tarreau8973c702007-01-21 23:58:29 +01001499 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001500 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001501 EXPECT_LF_HERE(ptr, http_msg_invalid);
1502 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1503 /* stop here */
1504
Willy Tarreau8973c702007-01-21 23:58:29 +01001505 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001506 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001507 case HTTP_MSG_RPVER_SP:
1508 case HTTP_MSG_RPCODE:
1509 case HTTP_MSG_RPCODE_SP:
1510 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001511 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001512 state, ptr, end,
1513 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001514 if (unlikely(!ptr))
1515 return;
1516
1517 /* we have a full response and we know that we have either a CR
1518 * or an LF at <ptr>.
1519 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001520 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1521
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001522 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001523 if (likely(*ptr == '\r'))
1524 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1525 goto http_msg_rpline_end;
1526
Willy Tarreau8973c702007-01-21 23:58:29 +01001527 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001528 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001529 /* msg->sol must point to the first of CR or LF. */
1530 EXPECT_LF_HERE(ptr, http_msg_invalid);
1531 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1532 /* stop here */
1533
1534 /*
1535 * Second, states that are specific to the request only
1536 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001538 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001540 /* we have a start of message, but we have to check
1541 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001542 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001543 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001544 if (likely(ptr != buf->p)) {
1545 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001546 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001547 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001548 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 }
Willy Tarreau26927362012-05-18 23:22:52 +02001550 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001551 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001552 state = HTTP_MSG_RQMETH;
1553 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001555
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1557 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 if (unlikely(*ptr == '\n'))
1560 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1561 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001562 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001565 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 EXPECT_LF_HERE(ptr, http_msg_invalid);
1567 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001568 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001571 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 case HTTP_MSG_RQMETH_SP:
1573 case HTTP_MSG_RQURI:
1574 case HTTP_MSG_RQURI_SP:
1575 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001576 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001577 state, ptr, end,
1578 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 if (unlikely(!ptr))
1580 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001581
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 /* we have a full request and we know that we have either a CR
1583 * or an LF at <ptr>.
1584 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001586
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001587 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 if (likely(*ptr == '\r'))
1589 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001591
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001593 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 /* check for HTTP/0.9 request : no version information available.
1595 * msg->sol must point to the first of CR or LF.
1596 */
1597 if (unlikely(msg->sl.rq.v_l == 0))
1598 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 EXPECT_LF_HERE(ptr, http_msg_invalid);
1601 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001602 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001603
Willy Tarreau8973c702007-01-21 23:58:29 +01001604 /*
1605 * Common states below
1606 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001608 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001609 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 if (likely(!HTTP_IS_CRLF(*ptr))) {
1611 goto http_msg_hdr_name;
1612 }
1613
1614 if (likely(*ptr == '\r'))
1615 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1616 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001617
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001619 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 /* assumes msg->sol points to the first char */
1621 if (likely(HTTP_IS_TOKEN(*ptr)))
1622 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001623
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001624 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001626
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001627 if (likely(msg->err_pos < -1) || *ptr == '\n')
1628 goto http_msg_invalid;
1629
1630 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001631 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001632
1633 /* and we still accept this non-token character */
1634 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001635
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001637 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001638 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 if (likely(HTTP_IS_SPHT(*ptr)))
1640 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001643 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001644
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 if (likely(!HTTP_IS_CRLF(*ptr))) {
1646 goto http_msg_hdr_val;
1647 }
1648
1649 if (likely(*ptr == '\r'))
1650 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1651 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001654 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001655 EXPECT_LF_HERE(ptr, http_msg_invalid);
1656 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001657
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001659 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660 if (likely(HTTP_IS_SPHT(*ptr))) {
1661 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001662 for (; buf->p + msg->sov < ptr; msg->sov++)
1663 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 goto http_msg_hdr_l1_sp;
1665 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001666 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001667 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 goto http_msg_complete_header;
1669
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001670 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001671 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001672 /* assumes msg->sol points to the first char, and msg->sov
1673 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 */
1675 if (likely(!HTTP_IS_CRLF(*ptr)))
1676 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001677
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001678 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 /* Note: we could also copy eol into ->eoh so that we have the
1680 * real header end in case it ends with lots of LWS, but is this
1681 * really needed ?
1682 */
1683 if (likely(*ptr == '\r'))
1684 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1685 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001686
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001687 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001688 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 EXPECT_LF_HERE(ptr, http_msg_invalid);
1690 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001691
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001693 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1695 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001696 for (; buf->p + msg->eol < ptr; msg->eol++)
1697 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 goto http_msg_hdr_val;
1699 }
1700 http_msg_complete_header:
1701 /*
1702 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001703 * Assumes msg->sol points to the first char, msg->sov points
1704 * to the first character of the value and msg->eol to the
1705 * first CR or LF so we know how the line ends. We insert last
1706 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001707 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001708 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 idx, idx->tail) < 0))
1710 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001711
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001712 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001713 if (likely(!HTTP_IS_CRLF(*ptr))) {
1714 goto http_msg_hdr_name;
1715 }
1716
1717 if (likely(*ptr == '\r'))
1718 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1719 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001720
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001722 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001723 /* Assumes msg->sol points to the first of either CR or LF.
1724 * Sets ->sov and ->next to the total header length, ->eoh to
1725 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1726 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001727 EXPECT_LF_HERE(ptr, http_msg_invalid);
1728 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001729 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001730 msg->eoh = msg->sol;
1731 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001732 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001733 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001735
1736 case HTTP_MSG_ERROR:
1737 /* this may only happen if we call http_msg_analyser() twice with an error */
1738 break;
1739
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001741#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1743 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001744#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001745 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 }
1747 http_msg_ood:
1748 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001749 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001750 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001752
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 http_msg_invalid:
1754 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001755 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001756 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 return;
1758}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001759
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001760/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1761 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1762 * nothing is done and 1 is returned.
1763 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001764static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001765{
1766 int delta;
1767 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001768 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001769
1770 if (msg->sl.rq.v_l != 0)
1771 return 1;
1772
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001773 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1774 if (txn->meth != HTTP_METH_GET)
1775 return 0;
1776
Willy Tarreau9b28e032012-10-12 23:49:43 +02001777 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001778 delta = 0;
1779
1780 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001781 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1782 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001783 }
1784 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001785 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001786 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001787 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001788 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001789 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001790 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001791 NULL, NULL);
1792 if (unlikely(!cur_end))
1793 return 0;
1794
1795 /* we have a full HTTP/1.0 request now and we know that
1796 * we have either a CR or an LF at <ptr>.
1797 */
1798 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1799 return 1;
1800}
1801
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001802/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001803 * and "keep-alive" values. If we already know that some headers may safely
1804 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001805 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1806 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001807 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001808 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1809 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1810 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001811 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001812 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001813void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001814{
Willy Tarreau5b154472009-12-21 20:11:07 +01001815 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001816 const char *hdr_val = "Connection";
1817 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001818
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001819 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001820 return;
1821
Willy Tarreau88d349d2010-01-25 12:15:43 +01001822 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1823 hdr_val = "Proxy-Connection";
1824 hdr_len = 16;
1825 }
1826
Willy Tarreau5b154472009-12-21 20:11:07 +01001827 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001828 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001829 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001830 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1831 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001832 if (to_del & 2)
1833 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001834 else
1835 txn->flags |= TX_CON_KAL_SET;
1836 }
1837 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1838 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001839 if (to_del & 1)
1840 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001841 else
1842 txn->flags |= TX_CON_CLO_SET;
1843 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001844 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1845 txn->flags |= TX_HDR_CONN_UPG;
1846 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001847 }
1848
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001849 txn->flags |= TX_HDR_CONN_PRS;
1850 return;
1851}
Willy Tarreau5b154472009-12-21 20:11:07 +01001852
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001853/* Apply desired changes on the Connection: header. Values may be removed and/or
1854 * added depending on the <wanted> flags, which are exclusively composed of
1855 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1856 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1857 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001858void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001859{
1860 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001861 const char *hdr_val = "Connection";
1862 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001863
1864 ctx.idx = 0;
1865
Willy Tarreau88d349d2010-01-25 12:15:43 +01001866
1867 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1868 hdr_val = "Proxy-Connection";
1869 hdr_len = 16;
1870 }
1871
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001872 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001873 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001874 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1875 if (wanted & TX_CON_KAL_SET)
1876 txn->flags |= TX_CON_KAL_SET;
1877 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001878 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001879 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001880 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1881 if (wanted & TX_CON_CLO_SET)
1882 txn->flags |= TX_CON_CLO_SET;
1883 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001884 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001885 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001886 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001887
1888 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1889 return;
1890
1891 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1892 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001893 hdr_val = "Connection: close";
1894 hdr_len = 17;
1895 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1896 hdr_val = "Proxy-Connection: close";
1897 hdr_len = 23;
1898 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001899 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001900 }
1901
1902 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1903 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001904 hdr_val = "Connection: keep-alive";
1905 hdr_len = 22;
1906 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1907 hdr_val = "Proxy-Connection: keep-alive";
1908 hdr_len = 28;
1909 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001910 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001911 }
1912 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001913}
1914
Willy Tarreauc24715e2014-04-17 15:21:20 +02001915/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1916 * the first byte of data after the chunk size, so that we know we can forward
1917 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1918 * the chunk size. That way it is always possible to differentiate between the
1919 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001920 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001922 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001923static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001924{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001925 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001926 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001927 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001928 const char *end = buf->data + buf->size;
1929 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001930 unsigned int chunk = 0;
1931
1932 /* The chunk size is in the following form, though we are only
1933 * interested in the size and CRLF :
1934 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1935 */
1936 while (1) {
1937 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001938 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001939 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001941 if (c < 0) /* not a hex digit anymore */
1942 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001943 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001944 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001945 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001946 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001947 chunk = (chunk << 4) + c;
1948 }
1949
Willy Tarreaud98cf932009-12-27 22:54:55 +01001950 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001951 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001952 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953
1954 while (http_is_spht[(unsigned char)*ptr]) {
1955 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001956 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001957 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001958 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001959 }
1960
Willy Tarreaud98cf932009-12-27 22:54:55 +01001961 /* Up to there, we know that at least one byte is present at *ptr. Check
1962 * for the end of chunk size.
1963 */
1964 while (1) {
1965 if (likely(HTTP_IS_CRLF(*ptr))) {
1966 /* we now have a CR or an LF at ptr */
1967 if (likely(*ptr == '\r')) {
1968 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001969 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001970 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001971 return 0;
1972 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001973
Willy Tarreaud98cf932009-12-27 22:54:55 +01001974 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001975 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001976 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001977 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001978 /* done */
1979 break;
1980 }
1981 else if (*ptr == ';') {
1982 /* chunk extension, ends at next CRLF */
1983 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001984 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001985 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001986 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001987
1988 while (!HTTP_IS_CRLF(*ptr)) {
1989 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001990 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001991 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001992 return 0;
1993 }
1994 /* we have a CRLF now, loop above */
1995 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001996 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001997 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001998 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001999 }
2000
Willy Tarreaud98cf932009-12-27 22:54:55 +01002001 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002002 * which may or may not be present. We save that into ->next,
2003 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002004 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002005 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002006 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002007 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002008 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002009 msg->chunk_len = chunk;
2010 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002011 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002012 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002013 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002014 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002015 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002016}
2017
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002018/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002019 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002020 * the trailers is found, it is automatically scheduled to be forwarded,
2021 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2022 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002023 * except maybe msg->next if it could parse some lines, and returns zero.
2024 * If a parse error is encountered, the function returns < 0 and does not
2025 * change anything except maybe msg->next. Note that the message must
2026 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002027 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002028 * forwarding, and that msg->next exactly matches the length of trailers
2029 * already parsed and not forwarded. It is also important to note that this
2030 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002031 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002032static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002033{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002034 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002035
Willy Tarreaua458b672012-03-05 11:17:50 +01002036 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002037 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002038 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002039 const char *ptr = b_ptr(buf, msg->next);
2040 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002041 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002042
2043 /* scan current line and stop at LF or CRLF */
2044 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002045 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002046 return 0;
2047
2048 if (*ptr == '\n') {
2049 if (!p1)
2050 p1 = ptr;
2051 p2 = ptr;
2052 break;
2053 }
2054
2055 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002056 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002057 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002058 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002059 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002060 p1 = ptr;
2061 }
2062
2063 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002064 if (ptr >= buf->data + buf->size)
2065 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002066 }
2067
2068 /* after LF; point to beginning of next line */
2069 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002070 if (p2 >= buf->data + buf->size)
2071 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002072
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002073 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002074 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002075 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002076
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002077 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002078 /* LF/CRLF at beginning of line => end of trailers at p2.
2079 * Everything was scheduled for forwarding, there's nothing
2080 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002081 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002082 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002083 msg->msg_state = HTTP_MSG_DONE;
2084 return 1;
2085 }
2086 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002087 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002088 }
2089}
2090
Willy Tarreauc24715e2014-04-17 15:21:20 +02002091/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2092 * or a possible LF alone at the end of a chunk. It automatically adjusts
2093 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002094 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2096 * not enough data are available, the function does not change anything and
2097 * returns zero. If a parse error is encountered, the function returns < 0 and
2098 * does not change anything. Note: this function is designed to parse wrapped
2099 * CRLF at the end of the buffer.
2100 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002101static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002102{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002103 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002104 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002105 int bytes;
2106
2107 /* NB: we'll check data availabilty at the end. It's not a
2108 * problem because whatever we match first will be checked
2109 * against the correct length.
2110 */
2111 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002112 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002113 if (*ptr == '\r') {
2114 bytes++;
2115 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002116 if (ptr >= buf->data + buf->size)
2117 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002118 }
2119
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002120 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002121 return 0;
2122
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002123 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002124 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002125 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002126 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002127
2128 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002129 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002130 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002131 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002132 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002133 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2134 return 1;
2135}
Willy Tarreau5b154472009-12-21 20:11:07 +01002136
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002137/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2138 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2139 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2140 * Unparsable qvalues return 1000 as "q=1.000".
2141 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002142int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002143{
2144 int q = 1000;
2145
Willy Tarreau506c69a2014-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 Tarreau506c69a2014-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 Tarreau506c69a2014-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 Tarreau506c69a2014-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 Tarreau4e21ff92014-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 Tarreau7d59e902014-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 Tarreau32b47f42009-10-18 20:55:02 +02002941 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002942 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002943 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002944 signed long long cl;
2945
Willy Tarreauad14f752011-09-02 20:33:27 +02002946 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002947 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002948 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002949 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002950
Willy Tarreauad14f752011-09-02 20:33:27 +02002951 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002952 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002953 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002954 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002955
Willy Tarreauad14f752011-09-02 20:33:27 +02002956 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002957 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002958 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002959 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002960
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002961 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002962 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002963 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002964 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002965
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002966 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002967 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002968 }
2969
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002970 /* bodyless requests have a known length */
2971 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002972 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002973
Willy Tarreau179085c2014-04-28 16:48:56 +02002974 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
2975 * only change if both the request and the config reference something else.
2976 * Option httpclose by itself sets tunnel mode where headers are mangled.
2977 * However, if another mode is set, it will affect it (eg: server-close/
2978 * keep-alive + httpclose = close). Note that we avoid to redo the same work
2979 * if FE and BE have the same settings (common). The method consists in
2980 * checking if options changed between the two calls (implying that either
2981 * one is non-null, or one of them is non-null and we are there for the first
2982 * time.
2983 */
2984 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau4e21ff92014-09-30 18:44:22 +02002985 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
2986 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02002987
Willy Tarreaud787e662009-07-07 10:14:51 +02002988 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002989 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002990 req->analyse_exp = TICK_ETERNITY;
2991 return 1;
2992
2993 return_bad_req:
2994 /* We centralize bad requests processing here */
2995 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2996 /* we detected a parsing error. We want to archive this request
2997 * in the dedicated proxy area for later troubleshooting.
2998 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002999 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003000 }
3001
3002 txn->req.msg_state = HTTP_MSG_ERROR;
3003 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003004 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003005
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003006 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003007 if (s->listener->counters)
3008 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003009
3010 return_prx_cond:
3011 if (!(s->flags & SN_ERR_MASK))
3012 s->flags |= SN_ERR_PRXCOND;
3013 if (!(s->flags & SN_FINST_MASK))
3014 s->flags |= SN_FINST_R;
3015
3016 req->analysers = 0;
3017 req->analyse_exp = TICK_ETERNITY;
3018 return 0;
3019}
3020
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003021
Willy Tarreau347a35d2013-11-22 17:51:09 +01003022/* This function prepares an applet to handle the stats. It can deal with the
3023 * "100-continue" expectation, check that admin rules are met for POST requests,
3024 * and program a response message if something was unexpected. It cannot fail
3025 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003026 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003027 * s->target which is supposed to already point to the stats applet. The caller
3028 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003029 */
3030int http_handle_stats(struct session *s, struct channel *req)
3031{
3032 struct stats_admin_rule *stats_admin_rule;
3033 struct stream_interface *si = s->rep->prod;
3034 struct http_txn *txn = &s->txn;
3035 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003036 struct uri_auth *uri_auth = s->be->uri_auth;
3037 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003038 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003039
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003040 appctx = si_appctx(si);
3041 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3042 appctx->st1 = appctx->st2 = 0;
3043 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3044 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003045 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3046 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003047
3048 uri = msg->chn->buf->p + msg->sl.rq.u;
3049 lookup = uri + uri_auth->uri_len;
3050
3051 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3052 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003053 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003054 break;
3055 }
3056 }
3057
3058 if (uri_auth->refresh) {
3059 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3060 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003061 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003062 break;
3063 }
3064 }
3065 }
3066
3067 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3068 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003069 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003070 break;
3071 }
3072 }
3073
3074 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3075 if (memcmp(h, ";st=", 4) == 0) {
3076 int i;
3077 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003078 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003079 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3080 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003081 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003082 break;
3083 }
3084 }
3085 break;
3086 }
3087 }
3088
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003089 appctx->ctx.stats.scope_str = 0;
3090 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003091 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3092 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3093 int itx = 0;
3094 const char *h2;
3095 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3096 const char *err;
3097
3098 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3099 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003100 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003101 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3102 itx++;
3103 h++;
3104 }
3105
3106 if (itx > STAT_SCOPE_TXT_MAXLEN)
3107 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003108 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003109
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003110 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003111 memcpy(scope_txt, h2, itx);
3112 scope_txt[itx] = '\0';
3113 err = invalid_char(scope_txt);
3114 if (err) {
3115 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003116 appctx->ctx.stats.scope_str = 0;
3117 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003118 }
3119 break;
3120 }
3121 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003122
3123 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003124 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003125 int ret = 1;
3126
3127 if (stats_admin_rule->cond) {
3128 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3129 ret = acl_pass(ret);
3130 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3131 ret = !ret;
3132 }
3133
3134 if (ret) {
3135 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003136 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003137 break;
3138 }
3139 }
3140
3141 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003142 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003143 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003144 /* we'll need the request body, possibly after sending 100-continue */
3145 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003146 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003147 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003148 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003149 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3150 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003151 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003152 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003153 else {
3154 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003155 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003156 }
3157
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003158 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003159 return 1;
3160}
3161
Lukas Tribus67db8df2013-06-23 17:37:13 +02003162/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3163 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3164 */
3165static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3166{
3167#ifdef IP_TOS
3168 if (from.ss_family == AF_INET)
3169 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3170#endif
3171#ifdef IPV6_TCLASS
3172 if (from.ss_family == AF_INET6) {
3173 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3174 /* v4-mapped addresses need IP_TOS */
3175 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3176 else
3177 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3178 }
3179#endif
3180}
3181
Sasha Pachev218f0642014-06-16 12:05:59 -06003182/* Returns the number of characters written to destination,
3183 * -1 on internal error and -2 if no replacement took place.
3184 */
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003185static int http_replace_header(struct my_regex *re, char *dst, uint dst_size, char *val, int len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003186 const char *rep_str)
Sasha Pachev218f0642014-06-16 12:05:59 -06003187{
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003188 if (!regex_exec_match2(re, val, len, MAX_MATCH, pmatch))
Sasha Pachev218f0642014-06-16 12:05:59 -06003189 return -2;
3190
3191 return exp_replace(dst, dst_size, val, rep_str, pmatch);
3192}
3193
3194/* Returns the number of characters written to destination,
3195 * -1 on internal error and -2 if no replacement took place.
3196 */
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003197static int http_replace_value(struct my_regex *re, char *dst, uint dst_size, char *val, int len, char delim,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003198 const char *rep_str)
Sasha Pachev218f0642014-06-16 12:05:59 -06003199{
3200 char* p = val;
3201 char* dst_end = dst + dst_size;
3202 char* dst_p = dst;
3203
3204 for (;;) {
3205 char *p_delim;
Sasha Pachev218f0642014-06-16 12:05:59 -06003206
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003207 /* look for delim. */
3208 p_delim = p;
3209 while (p_delim < p + len && *p_delim != delim)
3210 p_delim++;
Sasha Pachev218f0642014-06-16 12:05:59 -06003211
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003212 if (regex_exec_match2(re, p, p_delim-p, MAX_MATCH, pmatch)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06003213 int replace_n = exp_replace(dst_p, dst_end - dst_p, p, rep_str, pmatch);
3214
3215 if (replace_n < 0)
3216 return -1;
3217
3218 dst_p += replace_n;
3219 } else {
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003220 uint len = p_delim - p;
Sasha Pachev218f0642014-06-16 12:05:59 -06003221
3222 if (dst_p + len >= dst_end)
3223 return -1;
3224
3225 memcpy(dst_p, p, len);
3226 dst_p += len;
3227 }
3228
3229 if (dst_p >= dst_end)
3230 return -1;
3231
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003232 /* end of the replacements. */
3233 if (p_delim >= p + len)
Sasha Pachev218f0642014-06-16 12:05:59 -06003234 break;
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003235
3236 /* Next part. */
3237 *dst_p++ = delim;
3238 p = p_delim + 1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003239 }
3240
3241 return dst_p - dst;
3242}
3243
3244static int http_transform_header(struct session* s, struct http_msg *msg, const char* name, uint name_len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003245 char* buf, struct hdr_idx* idx, struct list *fmt, struct my_regex *re,
Sasha Pachev218f0642014-06-16 12:05:59 -06003246 struct hdr_ctx* ctx, int action)
3247{
3248 ctx->idx = 0;
3249
3250 while (http_find_full_header2(name, name_len, buf, idx, ctx)) {
3251 struct hdr_idx_elem *hdr = idx->v + ctx->idx;
3252 int delta;
3253 char* val = (char*)ctx->line + name_len + 2;
3254 char* val_end = (char*)ctx->line + hdr->len;
Sasha Pachev218f0642014-06-16 12:05:59 -06003255 char* reg_dst_buf;
3256 uint reg_dst_buf_size;
3257 int n_replaced;
3258
Sasha Pachev218f0642014-06-16 12:05:59 -06003259 trash.len = build_logline(s, trash.str, trash.size, fmt);
3260
3261 if (trash.len >= trash.size - 1)
3262 return -1;
3263
3264 reg_dst_buf = trash.str + trash.len + 1;
3265 reg_dst_buf_size = trash.size - trash.len - 1;
3266
3267 switch (action) {
3268 case HTTP_REQ_ACT_REPLACE_VAL:
3269 case HTTP_RES_ACT_REPLACE_VAL:
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003270 n_replaced = http_replace_value(re, reg_dst_buf, reg_dst_buf_size, val, val_end-val, ',', trash.str);
Sasha Pachev218f0642014-06-16 12:05:59 -06003271 break;
3272 case HTTP_REQ_ACT_REPLACE_HDR:
3273 case HTTP_RES_ACT_REPLACE_HDR:
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02003274 n_replaced = http_replace_header(re, reg_dst_buf, reg_dst_buf_size, val, val_end-val, trash.str);
Sasha Pachev218f0642014-06-16 12:05:59 -06003275 break;
3276 default: /* impossible */
3277 return -1;
3278 }
3279
Sasha Pachev218f0642014-06-16 12:05:59 -06003280 switch (n_replaced) {
3281 case -1: return -1;
3282 case -2: continue;
3283 }
3284
3285 delta = buffer_replace2(msg->chn->buf, val, val_end, reg_dst_buf, n_replaced);
3286
3287 hdr->len += delta;
3288 http_msg_move_end(msg, delta);
3289 }
3290
3291 return 0;
3292}
3293
Willy Tarreau20b0de52012-12-24 15:45:22 +01003294/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003295 * transaction <txn>. Returns the verdict of the first rule that prevents
3296 * further processing of the request (auth, deny, ...), and defaults to
3297 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3298 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3299 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003300 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003301enum rule_result
Willy Tarreau96257ec2012-12-27 10:46:37 +01003302http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003303{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003304 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003305 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003306 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003307 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003308
Willy Tarreauff011f22011-01-06 17:51:27 +01003309 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003310 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003311 continue;
3312
Willy Tarreau96257ec2012-12-27 10:46:37 +01003313 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003314 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003315 int ret;
3316
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003317 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003318 ret = acl_pass(ret);
3319
Willy Tarreauff011f22011-01-06 17:51:27 +01003320 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003321 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003322
3323 if (!ret) /* condition not matched */
3324 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003325 }
3326
Willy Tarreau20b0de52012-12-24 15:45:22 +01003327
Willy Tarreau96257ec2012-12-27 10:46:37 +01003328 switch (rule->action) {
3329 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003330 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003331
3332 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003333 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003334
Willy Tarreauccbcc372012-12-27 12:37:57 +01003335 case HTTP_REQ_ACT_TARPIT:
3336 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003337 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003338
Willy Tarreau96257ec2012-12-27 10:46:37 +01003339 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003340 /* Auth might be performed on regular http-req rules as well as on stats */
3341 auth_realm = rule->arg.auth.realm;
3342 if (!auth_realm) {
3343 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3344 auth_realm = STATS_DEFAULT_REALM;
3345 else
3346 auth_realm = px->id;
3347 }
3348 /* send 401/407 depending on whether we use a proxy or not. We still
3349 * count one error, because normal browsing won't significantly
3350 * increase the counter but brute force attempts will.
3351 */
3352 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3353 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3354 stream_int_retnclose(&s->si[0], &trash);
3355 session_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003356 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003357
Willy Tarreau81499eb2012-12-27 12:19:02 +01003358 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003359 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3360 return HTTP_RULE_RES_BADREQ;
3361 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003362
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003363 case HTTP_REQ_ACT_SET_NICE:
3364 s->task->nice = rule->arg.nice;
3365 break;
3366
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003367 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003368 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003369 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003370 break;
3371
Willy Tarreau51347ed2013-06-11 19:34:13 +02003372 case HTTP_REQ_ACT_SET_MARK:
3373#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003374 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003375 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003376#endif
3377 break;
3378
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003379 case HTTP_REQ_ACT_SET_LOGL:
3380 s->logs.level = rule->arg.loglevel;
3381 break;
3382
Sasha Pachev218f0642014-06-16 12:05:59 -06003383 case HTTP_REQ_ACT_REPLACE_HDR:
3384 case HTTP_REQ_ACT_REPLACE_VAL:
3385 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3386 txn->req.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003387 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003388 return HTTP_RULE_RES_BADREQ;
3389 break;
3390
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003391 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003392 case HTTP_REQ_ACT_SET_HDR:
3393 ctx.idx = 0;
3394 /* remove all occurrences of the header */
3395 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3396 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3397 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003398 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003399 if (rule->action == HTTP_REQ_ACT_DEL_HDR)
3400 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003401 /* now fall through to header addition */
3402
3403 case HTTP_REQ_ACT_ADD_HDR:
3404 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3405 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3406 trash.len = rule->arg.hdr_add.name_len;
3407 trash.str[trash.len++] = ':';
3408 trash.str[trash.len++] = ' ';
3409 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3410 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3411 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003412
3413 case HTTP_REQ_ACT_DEL_ACL:
3414 case HTTP_REQ_ACT_DEL_MAP: {
3415 struct pat_ref *ref;
3416 char *key;
3417 int len;
3418
3419 /* collect reference */
3420 ref = pat_ref_lookup(rule->arg.map.ref);
3421 if (!ref)
3422 continue;
3423
3424 /* collect key */
3425 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3426 key = trash.str;
3427 key[len] = '\0';
3428
3429 /* perform update */
3430 /* returned code: 1=ok, 0=ko */
3431 pat_ref_delete(ref, key);
3432
3433 break;
3434 }
3435
3436 case HTTP_REQ_ACT_ADD_ACL: {
3437 struct pat_ref *ref;
3438 char *key;
3439 struct chunk *trash_key;
3440 int len;
3441
3442 trash_key = get_trash_chunk();
3443
3444 /* collect reference */
3445 ref = pat_ref_lookup(rule->arg.map.ref);
3446 if (!ref)
3447 continue;
3448
3449 /* collect key */
3450 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3451 key = trash_key->str;
3452 key[len] = '\0';
3453
3454 /* perform update */
3455 /* add entry only if it does not already exist */
3456 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003457 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003458
3459 break;
3460 }
3461
3462 case HTTP_REQ_ACT_SET_MAP: {
3463 struct pat_ref *ref;
3464 char *key, *value;
3465 struct chunk *trash_key, *trash_value;
3466 int len;
3467
3468 trash_key = get_trash_chunk();
3469 trash_value = get_trash_chunk();
3470
3471 /* collect reference */
3472 ref = pat_ref_lookup(rule->arg.map.ref);
3473 if (!ref)
3474 continue;
3475
3476 /* collect key */
3477 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3478 key = trash_key->str;
3479 key[len] = '\0';
3480
3481 /* collect value */
3482 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3483 value = trash_value->str;
3484 value[len] = '\0';
3485
3486 /* perform update */
3487 if (pat_ref_find_elt(ref, key) != NULL)
3488 /* update entry if it exists */
3489 pat_ref_set(ref, key, value, NULL);
3490 else
3491 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003492 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003493
3494 break;
3495 }
William Lallemand73025dd2014-04-24 14:38:37 +02003496
3497 case HTTP_REQ_ACT_CUSTOM_CONT:
3498 rule->action_ptr(rule, px, s, txn);
3499 break;
3500
3501 case HTTP_REQ_ACT_CUSTOM_STOP:
3502 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003503 return HTTP_RULE_RES_DONE;
Willy Tarreau09448f72014-06-25 18:12:15 +02003504
3505 case HTTP_REQ_ACT_TRK_SC0 ... HTTP_REQ_ACT_TRK_SCMAX:
3506 /* Note: only the first valid tracking parameter of each
3507 * applies.
3508 */
3509
3510 if (stkctr_entry(&s->stkctr[http_req_trk_idx(rule->action)]) == NULL) {
3511 struct stktable *t;
3512 struct stksess *ts;
3513 struct stktable_key *key;
3514 void *ptr;
3515
3516 t = rule->act_prm.trk_ctr.table.t;
3517 key = stktable_fetch_key(t, s->be, s, &s->txn, SMP_OPT_DIR_REQ | SMP_OPT_FINAL, rule->act_prm.trk_ctr.expr, NULL);
3518
3519 if (key && (ts = stktable_get_entry(t, key))) {
3520 session_track_stkctr(&s->stkctr[http_req_trk_idx(rule->action)], t, ts);
3521
3522 /* let's count a new HTTP request as it's the first time we do it */
3523 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
3524 if (ptr)
3525 stktable_data_cast(ptr, http_req_cnt)++;
3526
3527 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
3528 if (ptr)
3529 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
3530 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
3531
3532 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_CONTENT);
3533 if (s->fe != s->be)
3534 stkctr_set_flags(&s->stkctr[http_req_trk_idx(rule->action)], STKCTR_TRACK_BACKEND);
3535 }
3536 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003537 }
3538 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003539
3540 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003541 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003542}
3543
Willy Tarreau71241ab2012-12-27 11:30:54 +01003544
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003545/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3546 * transaction <txn>. Returns the first rule that prevents further processing
3547 * of the response (deny, ...) or NULL if it executed all rules or stopped
3548 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3549 * rule.
3550 */
3551static struct http_res_rule *
3552http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3553{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003554 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003555 struct http_res_rule *rule;
3556 struct hdr_ctx ctx;
3557
3558 list_for_each_entry(rule, rules, list) {
3559 if (rule->action >= HTTP_RES_ACT_MAX)
3560 continue;
3561
3562 /* check optional condition */
3563 if (rule->cond) {
3564 int ret;
3565
3566 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3567 ret = acl_pass(ret);
3568
3569 if (rule->cond->pol == ACL_COND_UNLESS)
3570 ret = !ret;
3571
3572 if (!ret) /* condition not matched */
3573 continue;
3574 }
3575
3576
3577 switch (rule->action) {
3578 case HTTP_RES_ACT_ALLOW:
3579 return NULL; /* "allow" rules are OK */
3580
3581 case HTTP_RES_ACT_DENY:
3582 txn->flags |= TX_SVDENY;
3583 return rule;
3584
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003585 case HTTP_RES_ACT_SET_NICE:
3586 s->task->nice = rule->arg.nice;
3587 break;
3588
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003589 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003590 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003591 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003592 break;
3593
Willy Tarreau51347ed2013-06-11 19:34:13 +02003594 case HTTP_RES_ACT_SET_MARK:
3595#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003596 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003597 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003598#endif
3599 break;
3600
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003601 case HTTP_RES_ACT_SET_LOGL:
3602 s->logs.level = rule->arg.loglevel;
3603 break;
3604
Sasha Pachev218f0642014-06-16 12:05:59 -06003605 case HTTP_RES_ACT_REPLACE_HDR:
3606 case HTTP_RES_ACT_REPLACE_VAL:
3607 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3608 txn->rsp.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003609 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003610 return NULL; /* note: we should report an error here */
3611 break;
3612
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003613 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003614 case HTTP_RES_ACT_SET_HDR:
3615 ctx.idx = 0;
3616 /* remove all occurrences of the header */
3617 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3618 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3619 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3620 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003621 if (rule->action == HTTP_RES_ACT_DEL_HDR)
3622 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003623 /* now fall through to header addition */
3624
3625 case HTTP_RES_ACT_ADD_HDR:
3626 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3627 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3628 trash.len = rule->arg.hdr_add.name_len;
3629 trash.str[trash.len++] = ':';
3630 trash.str[trash.len++] = ' ';
3631 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3632 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3633 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003634
3635 case HTTP_RES_ACT_DEL_ACL:
3636 case HTTP_RES_ACT_DEL_MAP: {
3637 struct pat_ref *ref;
3638 char *key;
3639 int len;
3640
3641 /* collect reference */
3642 ref = pat_ref_lookup(rule->arg.map.ref);
3643 if (!ref)
3644 continue;
3645
3646 /* collect key */
3647 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3648 key = trash.str;
3649 key[len] = '\0';
3650
3651 /* perform update */
3652 /* returned code: 1=ok, 0=ko */
3653 pat_ref_delete(ref, key);
3654
3655 break;
3656 }
3657
3658 case HTTP_RES_ACT_ADD_ACL: {
3659 struct pat_ref *ref;
3660 char *key;
3661 struct chunk *trash_key;
3662 int len;
3663
3664 trash_key = get_trash_chunk();
3665
3666 /* collect reference */
3667 ref = pat_ref_lookup(rule->arg.map.ref);
3668 if (!ref)
3669 continue;
3670
3671 /* collect key */
3672 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3673 key = trash_key->str;
3674 key[len] = '\0';
3675
3676 /* perform update */
3677 /* check if the entry already exists */
3678 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003679 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003680
3681 break;
3682 }
3683
3684 case HTTP_RES_ACT_SET_MAP: {
3685 struct pat_ref *ref;
3686 char *key, *value;
3687 struct chunk *trash_key, *trash_value;
3688 int len;
3689
3690 trash_key = get_trash_chunk();
3691 trash_value = get_trash_chunk();
3692
3693 /* collect reference */
3694 ref = pat_ref_lookup(rule->arg.map.ref);
3695 if (!ref)
3696 continue;
3697
3698 /* collect key */
3699 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3700 key = trash_key->str;
3701 key[len] = '\0';
3702
3703 /* collect value */
3704 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3705 value = trash_value->str;
3706 value[len] = '\0';
3707
3708 /* perform update */
3709 if (pat_ref_find_elt(ref, key) != NULL)
3710 /* update entry if it exists */
3711 pat_ref_set(ref, key, value, NULL);
3712 else
3713 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003714 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003715
3716 break;
3717 }
William Lallemand73025dd2014-04-24 14:38:37 +02003718
3719 case HTTP_RES_ACT_CUSTOM_CONT:
3720 rule->action_ptr(rule, px, s, txn);
3721 break;
3722
3723 case HTTP_RES_ACT_CUSTOM_STOP:
3724 rule->action_ptr(rule, px, s, txn);
3725 return rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003726 }
3727 }
3728
3729 /* we reached the end of the rules, nothing to report */
3730 return NULL;
3731}
3732
3733
Willy Tarreau71241ab2012-12-27 11:30:54 +01003734/* Perform an HTTP redirect based on the information in <rule>. The function
3735 * returns non-zero on success, or zero in case of a, irrecoverable error such
3736 * as too large a request to build a valid response.
3737 */
3738static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3739{
3740 struct http_msg *msg = &txn->req;
3741 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003742 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003743
3744 /* build redirect message */
3745 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003746 case 308:
3747 msg_fmt = HTTP_308;
3748 break;
3749 case 307:
3750 msg_fmt = HTTP_307;
3751 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003752 case 303:
3753 msg_fmt = HTTP_303;
3754 break;
3755 case 301:
3756 msg_fmt = HTTP_301;
3757 break;
3758 case 302:
3759 default:
3760 msg_fmt = HTTP_302;
3761 break;
3762 }
3763
3764 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3765 return 0;
3766
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003767 location = trash.str + trash.len;
3768
Willy Tarreau71241ab2012-12-27 11:30:54 +01003769 switch(rule->type) {
3770 case REDIRECT_TYPE_SCHEME: {
3771 const char *path;
3772 const char *host;
3773 struct hdr_ctx ctx;
3774 int pathlen;
3775 int hostlen;
3776
3777 host = "";
3778 hostlen = 0;
3779 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003780 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003781 host = ctx.line + ctx.val;
3782 hostlen = ctx.vlen;
3783 }
3784
3785 path = http_get_path(txn);
3786 /* build message using path */
3787 if (path) {
3788 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3789 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3790 int qs = 0;
3791 while (qs < pathlen) {
3792 if (path[qs] == '?') {
3793 pathlen = qs;
3794 break;
3795 }
3796 qs++;
3797 }
3798 }
3799 } else {
3800 path = "/";
3801 pathlen = 1;
3802 }
3803
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003804 if (rule->rdr_str) { /* this is an old "redirect" rule */
3805 /* check if we can add scheme + "://" + host + path */
3806 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3807 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003808
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003809 /* add scheme */
3810 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3811 trash.len += rule->rdr_len;
3812 }
3813 else {
3814 /* add scheme with executing log format */
3815 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003816
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003817 /* check if we can add scheme + "://" + host + path */
3818 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3819 return 0;
3820 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003821 /* add "://" */
3822 memcpy(trash.str + trash.len, "://", 3);
3823 trash.len += 3;
3824
3825 /* add host */
3826 memcpy(trash.str + trash.len, host, hostlen);
3827 trash.len += hostlen;
3828
3829 /* add path */
3830 memcpy(trash.str + trash.len, path, pathlen);
3831 trash.len += pathlen;
3832
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003833 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003834 if (trash.len && trash.str[trash.len - 1] != '/' &&
3835 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3836 if (trash.len > trash.size - 5)
3837 return 0;
3838 trash.str[trash.len] = '/';
3839 trash.len++;
3840 }
3841
3842 break;
3843 }
3844 case REDIRECT_TYPE_PREFIX: {
3845 const char *path;
3846 int pathlen;
3847
3848 path = http_get_path(txn);
3849 /* build message using path */
3850 if (path) {
3851 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3852 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3853 int qs = 0;
3854 while (qs < pathlen) {
3855 if (path[qs] == '?') {
3856 pathlen = qs;
3857 break;
3858 }
3859 qs++;
3860 }
3861 }
3862 } else {
3863 path = "/";
3864 pathlen = 1;
3865 }
3866
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003867 if (rule->rdr_str) { /* this is an old "redirect" rule */
3868 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3869 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003870
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003871 /* add prefix. Note that if prefix == "/", we don't want to
3872 * add anything, otherwise it makes it hard for the user to
3873 * configure a self-redirection.
3874 */
3875 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3876 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3877 trash.len += rule->rdr_len;
3878 }
3879 }
3880 else {
3881 /* add prefix with executing log format */
3882 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3883
3884 /* Check length */
3885 if (trash.len + pathlen > trash.size - 4)
3886 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003887 }
3888
3889 /* add path */
3890 memcpy(trash.str + trash.len, path, pathlen);
3891 trash.len += pathlen;
3892
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003893 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003894 if (trash.len && trash.str[trash.len - 1] != '/' &&
3895 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3896 if (trash.len > trash.size - 5)
3897 return 0;
3898 trash.str[trash.len] = '/';
3899 trash.len++;
3900 }
3901
3902 break;
3903 }
3904 case REDIRECT_TYPE_LOCATION:
3905 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003906 if (rule->rdr_str) { /* this is an old "redirect" rule */
3907 if (trash.len + rule->rdr_len > trash.size - 4)
3908 return 0;
3909
3910 /* add location */
3911 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3912 trash.len += rule->rdr_len;
3913 }
3914 else {
3915 /* add location with executing log format */
3916 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003917
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003918 /* Check left length */
3919 if (trash.len > trash.size - 4)
3920 return 0;
3921 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003922 break;
3923 }
3924
3925 if (rule->cookie_len) {
3926 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3927 trash.len += 14;
3928 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3929 trash.len += rule->cookie_len;
3930 memcpy(trash.str + trash.len, "\r\n", 2);
3931 trash.len += 2;
3932 }
3933
3934 /* add end of headers and the keep-alive/close status.
3935 * We may choose to set keep-alive if the Location begins
3936 * with a slash, because the client will come back to the
3937 * same server.
3938 */
3939 txn->status = rule->code;
3940 /* let's log the request time */
3941 s->logs.tv_request = now;
3942
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003943 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003944 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3945 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3946 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3947 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3948 /* keep-alive possible */
3949 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3950 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3951 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3952 trash.len += 30;
3953 } else {
3954 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3955 trash.len += 24;
3956 }
3957 }
3958 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3959 trash.len += 4;
3960 bo_inject(txn->rsp.chn, trash.str, trash.len);
3961 /* "eat" the request */
3962 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02003963 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003964 msg->sov = 0;
3965 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3966 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3967 txn->req.msg_state = HTTP_MSG_CLOSED;
3968 txn->rsp.msg_state = HTTP_MSG_DONE;
3969 } else {
3970 /* keep-alive not possible */
3971 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3972 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3973 trash.len += 29;
3974 } else {
3975 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3976 trash.len += 23;
3977 }
3978 stream_int_retnclose(txn->req.chn->prod, &trash);
3979 txn->req.chn->analysers = 0;
3980 }
3981
3982 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003983 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003984 if (!(s->flags & SN_FINST_MASK))
3985 s->flags |= SN_FINST_R;
3986
3987 return 1;
3988}
3989
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003990/* This stream analyser runs all HTTP request processing which is common to
3991 * frontends and backends, which means blocking ACLs, filters, connection-close,
3992 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003993 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003994 * either needs more data or wants to immediately abort the request (eg: deny,
3995 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003996 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003997int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003998{
Willy Tarreaud787e662009-07-07 10:14:51 +02003999 struct http_txn *txn = &s->txn;
4000 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004001 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004002 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02004003 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02004004
Willy Tarreau655dce92009-11-08 13:10:58 +01004005 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004006 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004007 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004008 return 0;
4009 }
4010
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004011 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 +02004012 now_ms, __FUNCTION__,
4013 s,
4014 req,
4015 req->rex, req->wex,
4016 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004017 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004018 req->analysers);
4019
Willy Tarreau65410832014-04-28 21:25:43 +02004020 /* just in case we have some per-backend tracking */
4021 session_inc_be_http_req_ctr(s);
4022
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004023 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004024 if (!LIST_ISEMPTY(&px->http_req_rules)) {
4025 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01004026
Willy Tarreau0b748332014-04-29 00:13:29 +02004027 switch (verdict) {
4028 case HTTP_RULE_RES_CONT:
4029 case HTTP_RULE_RES_STOP: /* nothing to do */
4030 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004031
Willy Tarreau0b748332014-04-29 00:13:29 +02004032 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4033 if (txn->flags & TX_CLTARPIT)
4034 goto tarpit;
4035 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004036
Willy Tarreau0b748332014-04-29 00:13:29 +02004037 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4038 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004039
Willy Tarreau0b748332014-04-29 00:13:29 +02004040 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004041 goto done;
4042
Willy Tarreau0b748332014-04-29 00:13:29 +02004043 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4044 goto return_bad_req;
4045 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004046 }
4047
Willy Tarreau52542592014-04-28 18:33:26 +02004048 /* OK at this stage, we know that the request was accepted according to
4049 * the http-request rules, we can check for the stats. Note that the
4050 * URI is detected *before* the req* rules in order not to be affected
4051 * by a possible reqrep, while they are processed *after* so that a
4052 * reqdeny can still block them. This clearly needs to change in 1.6!
4053 */
4054 if (stats_check_uri(s->rep->prod, txn, px)) {
4055 s->target = &http_stats_applet.obj_type;
4056 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
4057 txn->status = 500;
4058 s->logs.tv_request = now;
4059 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004060
Willy Tarreau52542592014-04-28 18:33:26 +02004061 if (!(s->flags & SN_ERR_MASK))
4062 s->flags |= SN_ERR_RESOURCE;
4063 goto return_prx_cond;
4064 }
4065
4066 /* parse the whole stats request and extract the relevant information */
4067 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004068 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4069 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004070
Willy Tarreau0b748332014-04-29 00:13:29 +02004071 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4072 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004073
Willy Tarreau0b748332014-04-29 00:13:29 +02004074 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4075 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004076 }
4077
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004078 /* evaluate the req* rules except reqadd */
4079 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004080 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004081 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004082
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004083 if (txn->flags & TX_CLDENY)
4084 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004085
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004086 if (txn->flags & TX_CLTARPIT)
4087 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004088 }
Willy Tarreau06619262006-12-17 08:37:22 +01004089
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004090 /* add request headers from the rule sets in the same order */
4091 list_for_each_entry(wl, &px->req_add, list) {
4092 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004093 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004094 ret = acl_pass(ret);
4095 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4096 ret = !ret;
4097 if (!ret)
4098 continue;
4099 }
4100
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004101 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004102 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004103 }
4104
Willy Tarreau52542592014-04-28 18:33:26 +02004105
4106 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004107 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004108 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004109 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
4110 s->fe->fe_counters.intercepted_req++;
4111
4112 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
4113 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
4114 if (!(s->flags & SN_FINST_MASK))
4115 s->flags |= SN_FINST_R;
4116
Willy Tarreau70730dd2014-04-24 18:06:27 +02004117 /* we may want to compress the stats page */
4118 if (s->fe->comp || s->be->comp)
4119 select_compression_request_header(s, req->buf);
4120
4121 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreau5506e3f2014-11-20 22:23:10 +01004122 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004123 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004124 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004125
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004126 /* check whether we have some ACLs set to redirect this request */
4127 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004128 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004129 int ret;
4130
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004131 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004132 ret = acl_pass(ret);
4133 if (rule->cond->pol == ACL_COND_UNLESS)
4134 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004135 if (!ret)
4136 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004137 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004138 if (!http_apply_redirect_rule(rule, s, txn))
4139 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004140 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004141 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004142
Willy Tarreau2be39392010-01-03 17:24:51 +01004143 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4144 * If this happens, then the data will not come immediately, so we must
4145 * send all what we have without waiting. Note that due to the small gain
4146 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004147 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004148 * itself once used.
4149 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004150 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004151
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004152 done: /* done with this analyser, continue with next ones that the calling
4153 * points will have set, if any.
4154 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004155 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004156 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4157 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004158 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004159
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004160 tarpit:
4161 /* When a connection is tarpitted, we use the tarpit timeout,
4162 * which may be the same as the connect timeout if unspecified.
4163 * If unset, then set it to zero because we really want it to
4164 * eventually expire. We build the tarpit as an analyser.
4165 */
4166 channel_erase(s->req);
4167
4168 /* wipe the request out so that we can drop the connection early
4169 * if the client closes first.
4170 */
4171 channel_dont_connect(req);
4172 req->analysers = 0; /* remove switching rules etc... */
4173 req->analysers |= AN_REQ_HTTP_TARPIT;
4174 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4175 if (!req->analyse_exp)
4176 req->analyse_exp = tick_add(now_ms, 0);
4177 session_inc_http_err_ctr(s);
4178 s->fe->fe_counters.denied_req++;
4179 if (s->fe != s->be)
4180 s->be->be_counters.denied_req++;
4181 if (s->listener->counters)
4182 s->listener->counters->denied_req++;
Thierry FOURNIER7566e302014-08-22 06:55:26 +02004183 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004184
4185 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004186 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004187 txn->status = 403;
4188 s->logs.tv_request = now;
4189 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4190 session_inc_http_err_ctr(s);
4191 s->fe->fe_counters.denied_req++;
4192 if (s->fe != s->be)
4193 s->be->be_counters.denied_req++;
4194 if (s->listener->counters)
4195 s->listener->counters->denied_req++;
4196 goto return_prx_cond;
4197
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004198 return_bad_req:
4199 /* We centralize bad requests processing here */
4200 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4201 /* we detected a parsing error. We want to archive this request
4202 * in the dedicated proxy area for later troubleshooting.
4203 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004204 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004205 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004206
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004207 txn->req.msg_state = HTTP_MSG_ERROR;
4208 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004209 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004210
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004211 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004212 if (s->listener->counters)
4213 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004214
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004215 return_prx_cond:
4216 if (!(s->flags & SN_ERR_MASK))
4217 s->flags |= SN_ERR_PRXCOND;
4218 if (!(s->flags & SN_FINST_MASK))
4219 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004220
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004221 req->analysers = 0;
4222 req->analyse_exp = TICK_ETERNITY;
4223 return 0;
4224}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004225
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004226/* This function performs all the processing enabled for the current request.
4227 * It returns 1 if the processing can continue on next analysers, or zero if it
4228 * needs more data, encounters an error, or wants to immediately abort the
4229 * request. It relies on buffers flags, and updates s->req->analysers.
4230 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004231int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004232{
4233 struct http_txn *txn = &s->txn;
4234 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004235 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004236
Willy Tarreau655dce92009-11-08 13:10:58 +01004237 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004238 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004239 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004240 return 0;
4241 }
4242
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004243 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 +02004244 now_ms, __FUNCTION__,
4245 s,
4246 req,
4247 req->rex, req->wex,
4248 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004249 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004250 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004251
William Lallemand82fe75c2012-10-23 10:25:10 +02004252 if (s->fe->comp || s->be->comp)
4253 select_compression_request_header(s, req->buf);
4254
Willy Tarreau59234e92008-11-30 23:51:27 +01004255 /*
4256 * Right now, we know that we have processed the entire headers
4257 * and that unwanted requests have been filtered out. We can do
4258 * whatever we want with the remaining request. Also, now we
4259 * may have separate values for ->fe, ->be.
4260 */
Willy Tarreau06619262006-12-17 08:37:22 +01004261
Willy Tarreau59234e92008-11-30 23:51:27 +01004262 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004263 * If HTTP PROXY is set we simply get remote server address parsing
4264 * incoming request. Note that this requires that a connection is
4265 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004266 */
4267 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004268 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004269 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004270
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004271 /* Note that for now we don't reuse existing proxy connections */
4272 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004273 txn->req.msg_state = HTTP_MSG_ERROR;
4274 txn->status = 500;
4275 req->analysers = 0;
4276 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4277
4278 if (!(s->flags & SN_ERR_MASK))
4279 s->flags |= SN_ERR_RESOURCE;
4280 if (!(s->flags & SN_FINST_MASK))
4281 s->flags |= SN_FINST_R;
4282
4283 return 0;
4284 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004285
4286 path = http_get_path(txn);
4287 url2sa(req->buf->p + msg->sl.rq.u,
4288 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004289 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004290 /* if the path was found, we have to remove everything between
4291 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4292 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4293 * u_l characters by a single "/".
4294 */
4295 if (path) {
4296 char *cur_ptr = req->buf->p;
4297 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4298 int delta;
4299
4300 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4301 http_msg_move_end(&txn->req, delta);
4302 cur_end += delta;
4303 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4304 goto return_bad_req;
4305 }
4306 else {
4307 char *cur_ptr = req->buf->p;
4308 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4309 int delta;
4310
4311 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4312 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4313 http_msg_move_end(&txn->req, delta);
4314 cur_end += delta;
4315 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4316 goto return_bad_req;
4317 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004318 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004319
Willy Tarreau59234e92008-11-30 23:51:27 +01004320 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004321 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004322 * Note that doing so might move headers in the request, but
4323 * the fields will stay coherent and the URI will not move.
4324 * This should only be performed in the backend.
4325 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004326 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004327 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4328 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004329
Willy Tarreau59234e92008-11-30 23:51:27 +01004330 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004331 * 8: the appsession cookie was looked up very early in 1.2,
4332 * so let's do the same now.
4333 */
4334
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004335 /* It needs to look into the URI unless persistence must be ignored */
4336 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004337 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 +01004338 }
4339
William Lallemanda73203e2012-03-12 12:48:57 +01004340 /* add unique-id if "header-unique-id" is specified */
4341
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004342 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4343 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4344 goto return_bad_req;
4345 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004346 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004347 }
William Lallemanda73203e2012-03-12 12:48:57 +01004348
4349 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004350 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4351 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004352 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004353 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004354 goto return_bad_req;
4355 }
4356
Cyril Bontéb21570a2009-11-29 20:04:48 +01004357 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004358 * 9: add X-Forwarded-For if either the frontend or the backend
4359 * asks for it.
4360 */
4361 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004362 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004363 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004364 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4365 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004366 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004367 /* The header is set to be added only if none is present
4368 * and we found it, so don't do anything.
4369 */
4370 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004371 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004372 /* Add an X-Forwarded-For header unless the source IP is
4373 * in the 'except' network range.
4374 */
4375 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004376 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004377 != s->fe->except_net.s_addr) &&
4378 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004379 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004380 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004381 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004382 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004383 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004384
4385 /* Note: we rely on the backend to get the header name to be used for
4386 * x-forwarded-for, because the header is really meant for the backends.
4387 * However, if the backend did not specify any option, we have to rely
4388 * on the frontend's header name.
4389 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004390 if (s->be->fwdfor_hdr_len) {
4391 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004392 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004393 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004394 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004395 memcpy(trash.str, s->fe->fwdfor_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]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01004398
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004399 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004400 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004401 }
4402 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004403 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004404 /* FIXME: for the sake of completeness, we should also support
4405 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004406 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004407 int len;
4408 char pn[INET6_ADDRSTRLEN];
4409 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004410 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004411 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004412
Willy Tarreau59234e92008-11-30 23:51:27 +01004413 /* Note: we rely on the backend to get the header name to be used for
4414 * x-forwarded-for, because the header is really meant for the backends.
4415 * However, if the backend did not specify any option, we have to rely
4416 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004417 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004418 if (s->be->fwdfor_hdr_len) {
4419 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004420 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004421 } else {
4422 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004423 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004424 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004425 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004426
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004427 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004428 goto return_bad_req;
4429 }
4430 }
4431
4432 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004433 * 10: add X-Original-To if either the frontend or the backend
4434 * asks for it.
4435 */
4436 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4437
4438 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004439 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004440 /* Add an X-Original-To header unless the destination IP is
4441 * in the 'except' network range.
4442 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004443 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004444
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004445 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004446 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004447 (((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 +02004448 != s->fe->except_to.s_addr) &&
4449 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004450 (((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 +02004451 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004452 int len;
4453 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004454 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004455
4456 /* Note: we rely on the backend to get the header name to be used for
4457 * x-original-to, because the header is really meant for the backends.
4458 * However, if the backend did not specify any option, we have to rely
4459 * on the frontend's header name.
4460 */
4461 if (s->be->orgto_hdr_len) {
4462 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004463 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004464 } else {
4465 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004466 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004467 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004468 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 +02004469
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004470 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004471 goto return_bad_req;
4472 }
4473 }
4474 }
4475
Willy Tarreau50fc7772012-11-11 22:19:57 +01004476 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4477 * If an "Upgrade" token is found, the header is left untouched in order not to have
4478 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4479 * "Upgrade" is present in the Connection header.
4480 */
4481 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4482 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004483 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4484 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004485 unsigned int want_flags = 0;
4486
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004487 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004488 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004489 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4490 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004491 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004492 want_flags |= TX_CON_CLO_SET;
4493 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004494 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004495 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4496 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004497 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004498 want_flags |= TX_CON_KAL_SET;
4499 }
4500
4501 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004502 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004503 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004504
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004505
Willy Tarreau522d6c02009-12-06 18:49:18 +01004506 /* If we have no server assigned yet and we're balancing on url_param
4507 * with a POST request, we may be interested in checking the body for
4508 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004509 */
4510 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4511 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004512 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004513 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004514 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004515 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004516
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004517 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004518 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004519#ifdef TCP_QUICKACK
4520 /* We expect some data from the client. Unless we know for sure
4521 * we already have a full request, we have to re-enable quick-ack
4522 * in case we previously disabled it, otherwise we might cause
4523 * the client to delay further data.
4524 */
4525 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004526 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004527 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004528 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004529 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004530#endif
4531 }
Willy Tarreau03945942009-12-22 16:50:27 +01004532
Willy Tarreau59234e92008-11-30 23:51:27 +01004533 /*************************************************************
4534 * OK, that's finished for the headers. We have done what we *
4535 * could. Let's switch to the DATA state. *
4536 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004537 req->analyse_exp = TICK_ETERNITY;
4538 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004539
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004540 /* if the server closes the connection, we want to immediately react
4541 * and close the socket to save packets and syscalls.
4542 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004543 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4544 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004545
Willy Tarreau59234e92008-11-30 23:51:27 +01004546 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004547 /* OK let's go on with the BODY now */
4548 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004549
Willy Tarreau59234e92008-11-30 23:51:27 +01004550 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004551 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004552 /* we detected a parsing error. We want to archive this request
4553 * in the dedicated proxy area for later troubleshooting.
4554 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004555 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004556 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004557
Willy Tarreau59234e92008-11-30 23:51:27 +01004558 txn->req.msg_state = HTTP_MSG_ERROR;
4559 txn->status = 400;
4560 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004561 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004562
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004563 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004564 if (s->listener->counters)
4565 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004566
Willy Tarreau59234e92008-11-30 23:51:27 +01004567 if (!(s->flags & SN_ERR_MASK))
4568 s->flags |= SN_ERR_PRXCOND;
4569 if (!(s->flags & SN_FINST_MASK))
4570 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004571 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004572}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004573
Willy Tarreau60b85b02008-11-30 23:28:40 +01004574/* This function is an analyser which processes the HTTP tarpit. It always
4575 * returns zero, at the beginning because it prevents any other processing
4576 * from occurring, and at the end because it terminates the request.
4577 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004578int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004579{
4580 struct http_txn *txn = &s->txn;
4581
4582 /* This connection is being tarpitted. The CLIENT side has
4583 * already set the connect expiration date to the right
4584 * timeout. We just have to check that the client is still
4585 * there and that the timeout has not expired.
4586 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004587 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004588 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004589 !tick_is_expired(req->analyse_exp, now_ms))
4590 return 0;
4591
4592 /* We will set the queue timer to the time spent, just for
4593 * logging purposes. We fake a 500 server error, so that the
4594 * attacker will not suspect his connection has been tarpitted.
4595 * It will not cause trouble to the logs because we can exclude
4596 * the tarpitted connections by filtering on the 'PT' status flags.
4597 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004598 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4599
4600 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004601 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004602 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004603
4604 req->analysers = 0;
4605 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004606
Willy Tarreau60b85b02008-11-30 23:28:40 +01004607 if (!(s->flags & SN_ERR_MASK))
4608 s->flags |= SN_ERR_PRXCOND;
4609 if (!(s->flags & SN_FINST_MASK))
4610 s->flags |= SN_FINST_T;
4611 return 0;
4612}
4613
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004614/* This function is an analyser which waits for the HTTP request body. It waits
4615 * for either the buffer to be full, or the full advertised contents to have
4616 * reached the buffer. It must only be called after the standard HTTP request
4617 * processing has occurred, because it expects the request to be parsed and will
4618 * look for the Expect header. It may send a 100-Continue interim response. It
4619 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4620 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4621 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004622 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004623int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004624{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004625 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004626 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004627
4628 /* We have to parse the HTTP request body to find any required data.
4629 * "balance url_param check_post" should have been the only way to get
4630 * into this. We were brought here after HTTP header analysis, so all
4631 * related structures are ready.
4632 */
4633
Willy Tarreau890988f2014-04-10 11:59:33 +02004634 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4635 /* This is the first call */
4636 if (msg->msg_state < HTTP_MSG_BODY)
4637 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004638
Willy Tarreau890988f2014-04-10 11:59:33 +02004639 if (msg->msg_state < HTTP_MSG_100_SENT) {
4640 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4641 * send an HTTP/1.1 100 Continue intermediate response.
4642 */
4643 if (msg->flags & HTTP_MSGF_VER_11) {
4644 struct hdr_ctx ctx;
4645 ctx.idx = 0;
4646 /* Expect is allowed in 1.1, look for it */
4647 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4648 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4649 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4650 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004651 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004652 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004653 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004654
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004655 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004656 * req->buf->p still points to the beginning of the message. We
4657 * must save the body in msg->next because it survives buffer
4658 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004659 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004660 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004661
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004662 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004663 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4664 else
4665 msg->msg_state = HTTP_MSG_DATA;
4666 }
4667
Willy Tarreau890988f2014-04-10 11:59:33 +02004668 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4669 /* We're in content-length mode, we just have to wait for enough data. */
4670 if (req->buf->i - msg->sov < msg->body_len)
4671 goto missing_data;
4672
4673 /* OK we have everything we need now */
4674 goto http_end;
4675 }
4676
4677 /* OK here we're parsing a chunked-encoded message */
4678
Willy Tarreau522d6c02009-12-06 18:49:18 +01004679 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004680 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004681 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004682 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004683 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004684 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004685
Willy Tarreau115acb92009-12-26 13:56:06 +01004686 if (!ret)
4687 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004688 else if (ret < 0) {
4689 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004690 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004691 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004692 }
4693
Willy Tarreaud98cf932009-12-27 22:54:55 +01004694 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004695 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004696 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004697 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004698 if (msg->msg_state == HTTP_MSG_TRAILERS)
4699 goto http_end;
4700
Willy Tarreau226071e2014-04-10 11:55:45 +02004701 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004702 goto http_end;
4703
4704 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004705 /* we get here if we need to wait for more data. If the buffer is full,
4706 * we have the maximum we can expect.
4707 */
4708 if (buffer_full(req->buf, global.tune.maxrewrite))
4709 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004710
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004711 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004712 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004713 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004714
4715 if (!(s->flags & SN_ERR_MASK))
4716 s->flags |= SN_ERR_CLITO;
4717 if (!(s->flags & SN_FINST_MASK))
4718 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004719 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004720 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004721
4722 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004723 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004724 /* Not enough data. We'll re-use the http-request
4725 * timeout here. Ideally, we should set the timeout
4726 * relative to the accept() date. We just set the
4727 * request timeout once at the beginning of the
4728 * request.
4729 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004730 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004731 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004732 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004733 return 0;
4734 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004735
4736 http_end:
4737 /* The situation will not evolve, so let's give up on the analysis. */
4738 s->logs.tv_request = now; /* update the request timer to reflect full request */
4739 req->analysers &= ~an_bit;
4740 req->analyse_exp = TICK_ETERNITY;
4741 return 1;
4742
4743 return_bad_req: /* let's centralize all bad requests */
4744 txn->req.msg_state = HTTP_MSG_ERROR;
4745 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004746 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004747
Willy Tarreau79ebac62010-06-07 13:47:49 +02004748 if (!(s->flags & SN_ERR_MASK))
4749 s->flags |= SN_ERR_PRXCOND;
4750 if (!(s->flags & SN_FINST_MASK))
4751 s->flags |= SN_FINST_R;
4752
Willy Tarreau522d6c02009-12-06 18:49:18 +01004753 return_err_msg:
4754 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004755 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004756 if (s->listener->counters)
4757 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004758 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004759}
4760
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004761/* send a server's name with an outgoing request over an established connection.
4762 * Note: this function is designed to be called once the request has been scheduled
4763 * for being forwarded. This is the reason why it rewinds the buffer before
4764 * proceeding.
4765 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004766int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004767
4768 struct hdr_ctx ctx;
4769
Mark Lamourinec2247f02012-01-04 13:02:01 -05004770 char *hdr_name = be->server_id_hdr_name;
4771 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004772 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004773 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004774 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004775
William Lallemandd9e90662012-01-30 17:27:17 +01004776 ctx.idx = 0;
4777
Willy Tarreau211cdec2014-04-17 20:18:08 +02004778 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004779 if (old_o) {
4780 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004781 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004782 txn->req.next += old_o;
4783 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004784 }
4785
Willy Tarreau9b28e032012-10-12 23:49:43 +02004786 old_i = chn->buf->i;
4787 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 -05004788 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004789 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004790 }
4791
4792 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004793 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004794 memcpy(hdr_val, hdr_name, hdr_name_len);
4795 hdr_val += hdr_name_len;
4796 *hdr_val++ = ':';
4797 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004798 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4799 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004800
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004801 if (old_o) {
4802 /* If this was a forwarded request, we must readjust the amount of
4803 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004804 * variations. Note that the current state is >= HTTP_MSG_BODY,
4805 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004806 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004807 old_o += chn->buf->i - old_i;
4808 b_adv(chn->buf, old_o);
4809 txn->req.next -= old_o;
4810 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004811 }
4812
Mark Lamourinec2247f02012-01-04 13:02:01 -05004813 return 0;
4814}
4815
Willy Tarreau610ecce2010-01-04 21:15:02 +01004816/* Terminate current transaction and prepare a new one. This is very tricky
4817 * right now but it works.
4818 */
4819void http_end_txn_clean_session(struct session *s)
4820{
Willy Tarreau068621e2013-12-23 15:11:25 +01004821 int prev_status = s->txn.status;
4822
Willy Tarreau610ecce2010-01-04 21:15:02 +01004823 /* FIXME: We need a more portable way of releasing a backend's and a
4824 * server's connections. We need a safer way to reinitialize buffer
4825 * flags. We also need a more accurate method for computing per-request
4826 * data.
4827 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004828
Willy Tarreau4213a112013-12-15 10:25:42 +01004829 /* unless we're doing keep-alive, we want to quickly close the connection
4830 * to the server.
4831 */
4832 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4833 !si_conn_ready(s->req->cons)) {
4834 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4835 si_shutr(s->req->cons);
4836 si_shutw(s->req->cons);
4837 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004838
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004839 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004840 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004841 if (unlikely(s->srv_conn))
4842 sess_change_server(s, NULL);
4843 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004844
4845 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4846 session_process_counters(s);
4847
4848 if (s->txn.status) {
4849 int n;
4850
4851 n = s->txn.status / 100;
4852 if (n < 1 || n > 5)
4853 n = 0;
4854
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004855 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004856 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004857 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004858 s->fe->fe_counters.p.http.comp_rsp++;
4859 }
Willy Tarreau24657792010-02-26 10:30:28 +01004860 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004861 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004862 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004863 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004864 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004865 s->be->be_counters.p.http.comp_rsp++;
4866 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004867 }
4868
4869 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004870 s->logs.bytes_in -= s->req->buf->i;
4871 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004872
4873 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004874 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004875 !(s->flags & SN_MONITOR) &&
4876 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4877 s->do_log(s);
4878 }
4879
Willy Tarreaud713bcc2014-06-25 15:36:04 +02004880 /* stop tracking content-based counters */
4881 session_stop_content_counters(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004882 session_update_time_stats(s);
4883
Willy Tarreau610ecce2010-01-04 21:15:02 +01004884 s->logs.accept_date = date; /* user-visible date for logging */
4885 s->logs.tv_accept = now; /* corrected date for internal use */
4886 tv_zero(&s->logs.tv_request);
4887 s->logs.t_queue = -1;
4888 s->logs.t_connect = -1;
4889 s->logs.t_data = -1;
4890 s->logs.t_close = 0;
4891 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4892 s->logs.srv_queue_size = 0; /* we will get this number soon */
4893
Willy Tarreau9b28e032012-10-12 23:49:43 +02004894 s->logs.bytes_in = s->req->total = s->req->buf->i;
4895 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004896
4897 if (s->pend_pos)
4898 pendconn_free(s->pend_pos);
4899
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004900 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004901 if (s->flags & SN_CURR_SESS) {
4902 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004903 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004904 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004905 if (may_dequeue_tasks(objt_server(s->target), s->be))
4906 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004907 }
4908
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004909 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004910
Willy Tarreau4213a112013-12-15 10:25:42 +01004911 /* only release our endpoint if we don't intend to reuse the
4912 * connection.
4913 */
4914 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4915 !si_conn_ready(s->req->cons)) {
4916 si_release_endpoint(s->req->cons);
4917 }
4918
Willy Tarreau610ecce2010-01-04 21:15:02 +01004919 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004920 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004921 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004922 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004923 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau9dc1c612014-09-01 20:35:55 +02004924 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);
4925 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 +02004926 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 +01004927 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Cyril Bontéa83a50b2014-10-22 22:30:13 +02004928 s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004929
Willy Tarreau610ecce2010-01-04 21:15:02 +01004930 s->txn.meth = 0;
4931 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004932 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004933
4934 if (prev_status == 401 || prev_status == 407) {
4935 /* In HTTP keep-alive mode, if we receive a 401, we still have
4936 * a chance of being able to send the visitor again to the same
4937 * server over the same connection. This is required by some
4938 * broken protocols such as NTLM, and anyway whenever there is
4939 * an opportunity for sending the challenge to the proper place,
4940 * it's better to do it (at least it helps with debugging).
4941 */
4942 s->txn.flags |= TX_PREFER_LAST;
4943 }
4944
Willy Tarreauee55dc02010-06-01 10:56:34 +02004945 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004946 s->req->cons->flags |= SI_FL_INDEP_STR;
4947
Willy Tarreau96e31212011-05-30 18:10:30 +02004948 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004949 s->req->flags |= CF_NEVER_WAIT;
4950 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004951 }
4952
Willy Tarreau610ecce2010-01-04 21:15:02 +01004953 /* if the request buffer is not empty, it means we're
4954 * about to process another request, so send pending
4955 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004956 * Just don't do this if the buffer is close to be full,
4957 * because the request will wait for it to flush a little
4958 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004959 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004960 if (s->req->buf->i) {
4961 if (s->rep->buf->o &&
4962 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4963 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004964 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004965 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004966
4967 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004968 channel_auto_read(s->req);
4969 channel_auto_close(s->req);
4970 channel_auto_read(s->rep);
4971 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004972
Willy Tarreau27375622013-12-17 00:00:28 +01004973 /* we're in keep-alive with an idle connection, monitor it */
4974 si_idle_conn(s->req->cons);
4975
Willy Tarreau342b11c2010-11-24 16:22:09 +01004976 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004977 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004978}
4979
4980
4981/* This function updates the request state machine according to the response
4982 * state machine and buffer flags. It returns 1 if it changes anything (flag
4983 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4984 * it is only used to find when a request/response couple is complete. Both
4985 * this function and its equivalent should loop until both return zero. It
4986 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4987 */
4988int http_sync_req_state(struct session *s)
4989{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004990 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004991 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004992 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004993 unsigned int old_state = txn->req.msg_state;
4994
Willy Tarreau610ecce2010-01-04 21:15:02 +01004995 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4996 return 0;
4997
4998 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004999 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005000 * We can shut the read side unless we want to abort_on_close,
5001 * or we have a POST request. The issue with POST requests is
5002 * that some browsers still send a CRLF after the request, and
5003 * this CRLF must be read so that it does not remain in the kernel
5004 * buffers, otherwise a close could cause an RST on some systems
5005 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005006 * Note that if we're using keep-alive on the client side, we'd
5007 * rather poll now and keep the polling enabled for the whole
5008 * session's life than enabling/disabling it between each
5009 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005010 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005011 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5012 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5013 !(s->be->options & PR_O_ABRT_CLOSE) &&
5014 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005015 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005016
Willy Tarreau40f151a2012-12-20 12:10:09 +01005017 /* if the server closes the connection, we want to immediately react
5018 * and close the socket to save packets and syscalls.
5019 */
5020 chn->cons->flags |= SI_FL_NOHALF;
5021
Willy Tarreau610ecce2010-01-04 21:15:02 +01005022 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5023 goto wait_other_side;
5024
5025 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5026 /* The server has not finished to respond, so we
5027 * don't want to move in order not to upset it.
5028 */
5029 goto wait_other_side;
5030 }
5031
5032 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5033 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005034 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005035 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005036 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005037 goto wait_other_side;
5038 }
5039
5040 /* When we get here, it means that both the request and the
5041 * response have finished receiving. Depending on the connection
5042 * mode, we'll have to wait for the last bytes to leave in either
5043 * direction, and sometimes for a close to be effective.
5044 */
5045
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005046 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5047 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005048 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5049 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005050 }
5051 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5052 /* Option forceclose is set, or either side wants to close,
5053 * let's enforce it now that we're not expecting any new
5054 * data to come. The caller knows the session is complete
5055 * once both states are CLOSED.
5056 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005057 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5058 channel_shutr_now(chn);
5059 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005060 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005061 }
5062 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005063 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5064 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005065 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005066 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5067 channel_auto_read(chn);
5068 txn->req.msg_state = HTTP_MSG_TUNNEL;
5069 chn->flags |= CF_NEVER_WAIT;
5070 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005071 }
5072
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005073 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005074 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005075 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005076
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005077 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005078 txn->req.msg_state = HTTP_MSG_CLOSING;
5079 goto http_msg_closing;
5080 }
5081 else {
5082 txn->req.msg_state = HTTP_MSG_CLOSED;
5083 goto http_msg_closed;
5084 }
5085 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005086 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005087 }
5088
5089 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5090 http_msg_closing:
5091 /* nothing else to forward, just waiting for the output buffer
5092 * to be empty and for the shutw_now to take effect.
5093 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005094 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005095 txn->req.msg_state = HTTP_MSG_CLOSED;
5096 goto http_msg_closed;
5097 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005098 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005099 txn->req.msg_state = HTTP_MSG_ERROR;
5100 goto wait_other_side;
5101 }
5102 }
5103
5104 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5105 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005106 /* see above in MSG_DONE why we only do this in these states */
5107 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5108 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5109 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005110 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005111 goto wait_other_side;
5112 }
5113
5114 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005115 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005116}
5117
5118
5119/* This function updates the response state machine according to the request
5120 * state machine and buffer flags. It returns 1 if it changes anything (flag
5121 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5122 * it is only used to find when a request/response couple is complete. Both
5123 * this function and its equivalent should loop until both return zero. It
5124 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5125 */
5126int http_sync_res_state(struct session *s)
5127{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005128 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005129 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005130 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005131 unsigned int old_state = txn->rsp.msg_state;
5132
Willy Tarreau610ecce2010-01-04 21:15:02 +01005133 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5134 return 0;
5135
5136 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5137 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005138 * still monitor the server connection for a possible close
5139 * while the request is being uploaded, so we don't disable
5140 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005141 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005142 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005143
5144 if (txn->req.msg_state == HTTP_MSG_ERROR)
5145 goto wait_other_side;
5146
5147 if (txn->req.msg_state < HTTP_MSG_DONE) {
5148 /* The client seems to still be sending data, probably
5149 * because we got an error response during an upload.
5150 * We have the choice of either breaking the connection
5151 * or letting it pass through. Let's do the later.
5152 */
5153 goto wait_other_side;
5154 }
5155
5156 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5157 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005158 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005159 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005160 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005161 goto wait_other_side;
5162 }
5163
5164 /* When we get here, it means that both the request and the
5165 * response have finished receiving. Depending on the connection
5166 * mode, we'll have to wait for the last bytes to leave in either
5167 * direction, and sometimes for a close to be effective.
5168 */
5169
5170 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5171 /* Server-close mode : shut read and wait for the request
5172 * side to close its output buffer. The caller will detect
5173 * when we're in DONE and the other is in CLOSED and will
5174 * catch that for the final cleanup.
5175 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005176 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5177 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005178 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005179 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5180 /* Option forceclose is set, or either side wants to close,
5181 * let's enforce it now that we're not expecting any new
5182 * data to come. The caller knows the session is complete
5183 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005184 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005185 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5186 channel_shutr_now(chn);
5187 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005188 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005189 }
5190 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005191 /* The last possible modes are keep-alive and tunnel. Tunnel will
5192 * need to forward remaining data. Keep-alive will need to monitor
5193 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005194 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005195 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005196 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005197 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5198 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005199 }
5200
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005201 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005202 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005203 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005204 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5205 goto http_msg_closing;
5206 }
5207 else {
5208 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5209 goto http_msg_closed;
5210 }
5211 }
5212 goto wait_other_side;
5213 }
5214
5215 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5216 http_msg_closing:
5217 /* nothing else to forward, just waiting for the output buffer
5218 * to be empty and for the shutw_now to take effect.
5219 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005220 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005221 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5222 goto http_msg_closed;
5223 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005224 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005225 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005226 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005227 if (objt_server(s->target))
5228 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005229 goto wait_other_side;
5230 }
5231 }
5232
5233 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5234 http_msg_closed:
5235 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005236 bi_erase(chn);
5237 channel_auto_close(chn);
5238 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005239 goto wait_other_side;
5240 }
5241
5242 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005243 /* We force the response to leave immediately if we're waiting for the
5244 * other side, since there is no pending shutdown to push it out.
5245 */
5246 if (!channel_is_empty(chn))
5247 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005248 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005249}
5250
5251
5252/* Resync the request and response state machines. Return 1 if either state
5253 * changes.
5254 */
5255int http_resync_states(struct session *s)
5256{
5257 struct http_txn *txn = &s->txn;
5258 int old_req_state = txn->req.msg_state;
5259 int old_res_state = txn->rsp.msg_state;
5260
Willy Tarreau610ecce2010-01-04 21:15:02 +01005261 http_sync_req_state(s);
5262 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005263 if (!http_sync_res_state(s))
5264 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005265 if (!http_sync_req_state(s))
5266 break;
5267 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005268
Willy Tarreau610ecce2010-01-04 21:15:02 +01005269 /* OK, both state machines agree on a compatible state.
5270 * There are a few cases we're interested in :
5271 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5272 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5273 * directions, so let's simply disable both analysers.
5274 * - HTTP_MSG_CLOSED on the response only means we must abort the
5275 * request.
5276 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5277 * with server-close mode means we've completed one request and we
5278 * must re-initialize the server connection.
5279 */
5280
5281 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5282 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5283 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5284 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5285 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005286 channel_auto_close(s->req);
5287 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005288 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005289 channel_auto_close(s->rep);
5290 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005291 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005292 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5293 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005294 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005295 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005296 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005297 channel_auto_close(s->rep);
5298 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01005299 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005300 channel_abort(s->req);
5301 channel_auto_close(s->req);
5302 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005303 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005304 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005305 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5306 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005307 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005308 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5309 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5310 /* server-close/keep-alive: terminate this transaction,
5311 * possibly killing the server connection and reinitialize
5312 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005313 */
5314 http_end_txn_clean_session(s);
5315 }
5316
Willy Tarreau610ecce2010-01-04 21:15:02 +01005317 return txn->req.msg_state != old_req_state ||
5318 txn->rsp.msg_state != old_res_state;
5319}
5320
Willy Tarreaud98cf932009-12-27 22:54:55 +01005321/* This function is an analyser which forwards request body (including chunk
5322 * sizes if any). It is called as soon as we must forward, even if we forward
5323 * zero byte. The only situation where it must not be called is when we're in
5324 * tunnel mode and we want to forward till the close. It's used both to forward
5325 * remaining data and to resync after end of body. It expects the msg_state to
5326 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5327 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005328 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005329 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005330 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005331int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005332{
5333 struct http_txn *txn = &s->txn;
5334 struct http_msg *msg = &s->txn.req;
5335
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005336 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5337 return 0;
5338
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005339 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005340 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005341 /* Output closed while we were sending data. We must abort and
5342 * wake the other side up.
5343 */
5344 msg->msg_state = HTTP_MSG_ERROR;
5345 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005346 return 1;
5347 }
5348
Willy Tarreaud98cf932009-12-27 22:54:55 +01005349 /* Note that we don't have to send 100-continue back because we don't
5350 * need the data to complete our job, and it's up to the server to
5351 * decide whether to return 100, 417 or anything else in return of
5352 * an "Expect: 100-continue" header.
5353 */
5354
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005355 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005356 /* we have msg->sov which points to the first byte of message
5357 * body, and req->buf.p still points to the beginning of the
5358 * message. We forward the headers now, as we don't need them
5359 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005360 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005361 b_adv(req->buf, msg->sov);
5362 msg->next -= msg->sov;
5363 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005364
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005365 /* The previous analysers guarantee that the state is somewhere
5366 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5367 * msg->next are always correct.
5368 */
5369 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5370 if (msg->flags & HTTP_MSGF_TE_CHNK)
5371 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5372 else
5373 msg->msg_state = HTTP_MSG_DATA;
5374 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005375 }
5376
Willy Tarreau7ba23542014-04-17 21:50:00 +02005377 /* Some post-connect processing might want us to refrain from starting to
5378 * forward data. Currently, the only reason for this is "balance url_param"
5379 * whichs need to parse/process the request after we've enabled forwarding.
5380 */
5381 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5382 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5383 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005384 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005385 goto missing_data;
5386 }
5387 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5388 }
5389
Willy Tarreau80a92c02014-03-12 10:41:13 +01005390 /* in most states, we should abort in case of early close */
5391 channel_auto_close(req);
5392
Willy Tarreauefdf0942014-04-24 20:08:57 +02005393 if (req->to_forward) {
5394 /* We can't process the buffer's contents yet */
5395 req->flags |= CF_WAKE_WRITE;
5396 goto missing_data;
5397 }
5398
Willy Tarreaud98cf932009-12-27 22:54:55 +01005399 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005400 if (msg->msg_state == HTTP_MSG_DATA) {
5401 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005402 /* we may have some pending data starting at req->buf->p */
5403 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005404 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005405 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005406 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005407 msg->next += msg->chunk_len;
5408 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005409
5410 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005411 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005412 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005413 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005414 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005415 }
5416 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005417 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005418 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005419 * TRAILERS state.
5420 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005421 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005422
Willy Tarreau54d23df2012-10-25 19:04:45 +02005423 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005424 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005425 else if (ret < 0) {
5426 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005427 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005428 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005429 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005430 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005431 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005432 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005433 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005434 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005435 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005436
5437 if (ret == 0)
5438 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005439 else if (ret < 0) {
5440 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005441 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005442 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005443 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005444 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005445 /* we're in MSG_CHUNK_SIZE now */
5446 }
5447 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005448 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005449
5450 if (ret == 0)
5451 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005452 else if (ret < 0) {
5453 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005454 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005455 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005456 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005457 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005458 /* we're in HTTP_MSG_DONE now */
5459 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005460 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005461 int old_state = msg->msg_state;
5462
Willy Tarreau610ecce2010-01-04 21:15:02 +01005463 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005464
5465 /* we may have some pending data starting at req->buf->p
5466 * such as last chunk of data or trailers.
5467 */
5468 b_adv(req->buf, msg->next);
Willy Tarreau9dc1c612014-09-01 20:35:55 +02005469 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005470 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005471 msg->next = 0;
5472
Willy Tarreau4fe41902010-06-07 22:27:41 +02005473 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005474 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5475 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005476 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005477 if (http_resync_states(s)) {
5478 /* some state changes occurred, maybe the analyser
5479 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005480 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005481 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005482 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005483 /* request errors are most likely due to
5484 * the server aborting the transfer.
5485 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005486 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005487 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005488 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005489 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005490 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005491 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005492 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005493 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005494
5495 /* If "option abortonclose" is set on the backend, we
5496 * want to monitor the client's connection and forward
5497 * any shutdown notification to the server, which will
5498 * decide whether to close or to go on processing the
5499 * request.
5500 */
5501 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005502 channel_auto_read(req);
5503 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005504 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005505 else if (s->txn.meth == HTTP_METH_POST) {
5506 /* POST requests may require to read extra CRLF
5507 * sent by broken browsers and which could cause
5508 * an RST to be sent upon close on some systems
5509 * (eg: Linux).
5510 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005511 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005512 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005513
Willy Tarreau610ecce2010-01-04 21:15:02 +01005514 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005515 }
5516 }
5517
Willy Tarreaud98cf932009-12-27 22:54:55 +01005518 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005519 /* we may have some pending data starting at req->buf->p */
5520 b_adv(req->buf, msg->next);
Willy Tarreau9dc1c612014-09-01 20:35:55 +02005521 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreaubb2e6692014-07-10 19:06:10 +02005522 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5523
Willy Tarreaubed410e2014-04-22 08:19:34 +02005524 msg->next = 0;
5525 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5526
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005527 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005528 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005529 if (!(s->flags & SN_ERR_MASK))
5530 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005531 if (!(s->flags & SN_FINST_MASK)) {
5532 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5533 s->flags |= SN_FINST_H;
5534 else
5535 s->flags |= SN_FINST_D;
5536 }
5537
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005538 s->fe->fe_counters.cli_aborts++;
5539 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005540 if (objt_server(s->target))
5541 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005542
5543 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005544 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005545
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005546 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005547 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005548 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005549
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005550 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005551 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005552 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005553 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005554 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005555
Willy Tarreau5c620922011-05-11 19:56:11 +02005556 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005557 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005558 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005559 * modes are already handled by the stream sock layer. We must not do
5560 * this in content-length mode because it could present the MSG_MORE
5561 * flag with the last block of forwarded data, which would cause an
5562 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005563 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005564 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005565 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005566
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005567 return 0;
5568
Willy Tarreaud98cf932009-12-27 22:54:55 +01005569 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005570 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005571 if (s->listener->counters)
5572 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005573
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005574 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005575 /* we may have some pending data starting at req->buf->p */
5576 b_adv(req->buf, msg->next);
5577 msg->next = 0;
5578
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005579 txn->req.msg_state = HTTP_MSG_ERROR;
5580 if (txn->status) {
5581 /* Note: we don't send any error if some data were already sent */
5582 stream_int_retnclose(req->prod, NULL);
5583 } else {
5584 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005585 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005586 }
5587 req->analysers = 0;
5588 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005589
5590 if (!(s->flags & SN_ERR_MASK))
5591 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005592 if (!(s->flags & SN_FINST_MASK)) {
5593 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5594 s->flags |= SN_FINST_H;
5595 else
5596 s->flags |= SN_FINST_D;
5597 }
5598 return 0;
5599
5600 aborted_xfer:
5601 txn->req.msg_state = HTTP_MSG_ERROR;
5602 if (txn->status) {
5603 /* Note: we don't send any error if some data were already sent */
5604 stream_int_retnclose(req->prod, NULL);
5605 } else {
5606 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005607 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005608 }
5609 req->analysers = 0;
5610 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5611
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005612 s->fe->fe_counters.srv_aborts++;
5613 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005614 if (objt_server(s->target))
5615 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005616
5617 if (!(s->flags & SN_ERR_MASK))
5618 s->flags |= SN_ERR_SRVCL;
5619 if (!(s->flags & SN_FINST_MASK)) {
5620 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5621 s->flags |= SN_FINST_H;
5622 else
5623 s->flags |= SN_FINST_D;
5624 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005625 return 0;
5626}
5627
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005628/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5629 * processing can continue on next analysers, or zero if it either needs more
5630 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5631 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5632 * when it has nothing left to do, and may remove any analyser when it wants to
5633 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005634 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005635int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005636{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005637 struct http_txn *txn = &s->txn;
5638 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005639 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005640 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005641 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005642 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005643
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005644 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 +02005645 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005646 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005647 rep,
5648 rep->rex, rep->wex,
5649 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005650 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005651 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005652
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005653 /*
5654 * Now parse the partial (or complete) lines.
5655 * We will check the response syntax, and also join multi-line
5656 * headers. An index of all the lines will be elaborated while
5657 * parsing.
5658 *
5659 * For the parsing, we use a 28 states FSM.
5660 *
5661 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005662 * rep->buf->p = beginning of response
5663 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5664 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005665 * msg->eol = end of current header or line (LF or CRLF)
5666 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005667 */
5668
Willy Tarreau628c40c2014-04-24 19:11:26 +02005669 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005670 /* There's a protected area at the end of the buffer for rewriting
5671 * purposes. We don't want to start to parse the request if the
5672 * protected area is affected, because we may have to move processed
5673 * data later, which is much more complicated.
5674 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005675 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005676 if (unlikely(!channel_reserved(rep))) {
5677 /* some data has still not left the buffer, wake us once that's done */
5678 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5679 goto abort_response;
5680 channel_dont_close(rep);
5681 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005682 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005683 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005684 }
5685
Willy Tarreau379357a2013-06-08 12:55:46 +02005686 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5687 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5688 buffer_slow_realign(rep->buf);
5689
Willy Tarreau9b28e032012-10-12 23:49:43 +02005690 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005691 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005692 }
5693
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005694 /* 1: we might have to print this header in debug mode */
5695 if (unlikely((global.mode & MODE_DEBUG) &&
5696 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau7d59e902014-10-21 19:36:09 +02005697 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005698 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005699
Willy Tarreau9b28e032012-10-12 23:49:43 +02005700 sol = rep->buf->p;
5701 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005702 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005703
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005704 sol += hdr_idx_first_pos(&txn->hdr_idx);
5705 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005706
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005707 while (cur_idx) {
5708 eol = sol + txn->hdr_idx.v[cur_idx].len;
5709 debug_hdr("srvhdr", s, sol, eol);
5710 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5711 cur_idx = txn->hdr_idx.v[cur_idx].next;
5712 }
5713 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005714
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005715 /*
5716 * Now we quickly check if we have found a full valid response.
5717 * If not so, we check the FD and buffer states before leaving.
5718 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005719 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005720 * responses are checked first.
5721 *
5722 * Depending on whether the client is still there or not, we
5723 * may send an error response back or not. Note that normally
5724 * we should only check for HTTP status there, and check I/O
5725 * errors somewhere else.
5726 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005727
Willy Tarreau655dce92009-11-08 13:10:58 +01005728 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005729 /* Invalid response */
5730 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5731 /* we detected a parsing error. We want to archive this response
5732 * in the dedicated proxy area for later troubleshooting.
5733 */
5734 hdr_response_bad:
5735 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005736 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005737
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005738 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005739 if (objt_server(s->target)) {
5740 objt_server(s->target)->counters.failed_resp++;
5741 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005742 }
Willy Tarreau64648412010-03-05 10:41:54 +01005743 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005744 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005745 rep->analysers = 0;
5746 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005747 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005748 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005749 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005750
5751 if (!(s->flags & SN_ERR_MASK))
5752 s->flags |= SN_ERR_PRXCOND;
5753 if (!(s->flags & SN_FINST_MASK))
5754 s->flags |= SN_FINST_H;
5755
5756 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005757 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005758
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005759 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005760 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005761 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005762 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005763 goto hdr_response_bad;
5764 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005765
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005766 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005767 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005768 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005769 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005770 else if (txn->flags & TX_NOT_FIRST)
5771 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005772
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005773 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005774 if (objt_server(s->target)) {
5775 objt_server(s->target)->counters.failed_resp++;
5776 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005777 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005778
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005779 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005780 rep->analysers = 0;
5781 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005782 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005783 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005784 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005785
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005786 if (!(s->flags & SN_ERR_MASK))
5787 s->flags |= SN_ERR_SRVCL;
5788 if (!(s->flags & SN_FINST_MASK))
5789 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005790 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005791 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005792
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02005793 /* read timeout : return a 504 to the client. */
5794 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005795 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005796 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005797 else if (txn->flags & TX_NOT_FIRST)
5798 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005799
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005800 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005801 if (objt_server(s->target)) {
5802 objt_server(s->target)->counters.failed_resp++;
5803 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005804 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005805
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005806 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005807 rep->analysers = 0;
5808 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005809 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005810 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005811 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005812
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005813 if (!(s->flags & SN_ERR_MASK))
5814 s->flags |= SN_ERR_SRVTO;
5815 if (!(s->flags & SN_FINST_MASK))
5816 s->flags |= SN_FINST_H;
5817 return 0;
5818 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005819
Willy Tarreauf003d372012-11-26 13:35:37 +01005820 /* client abort with an abortonclose */
5821 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5822 s->fe->fe_counters.cli_aborts++;
5823 s->be->be_counters.cli_aborts++;
5824 if (objt_server(s->target))
5825 objt_server(s->target)->counters.cli_aborts++;
5826
5827 rep->analysers = 0;
5828 channel_auto_close(rep);
5829
5830 txn->status = 400;
5831 bi_erase(rep);
5832 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5833
5834 if (!(s->flags & SN_ERR_MASK))
5835 s->flags |= SN_ERR_CLICL;
5836 if (!(s->flags & SN_FINST_MASK))
5837 s->flags |= SN_FINST_H;
5838
5839 /* process_session() will take care of the error */
5840 return 0;
5841 }
5842
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005843 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005844 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005845 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005846 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005847 else if (txn->flags & TX_NOT_FIRST)
5848 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005849
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005850 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005851 if (objt_server(s->target)) {
5852 objt_server(s->target)->counters.failed_resp++;
5853 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005854 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005855
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005856 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005857 rep->analysers = 0;
5858 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005859 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005860 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005861 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005862
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005863 if (!(s->flags & SN_ERR_MASK))
5864 s->flags |= SN_ERR_SRVCL;
5865 if (!(s->flags & SN_FINST_MASK))
5866 s->flags |= SN_FINST_H;
5867 return 0;
5868 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005869
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005870 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005871 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005872 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005873 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005874 else if (txn->flags & TX_NOT_FIRST)
5875 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005876
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005877 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005878 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005879 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005880
5881 if (!(s->flags & SN_ERR_MASK))
5882 s->flags |= SN_ERR_CLICL;
5883 if (!(s->flags & SN_FINST_MASK))
5884 s->flags |= SN_FINST_H;
5885
5886 /* process_session() will take care of the error */
5887 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005888 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005889
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005890 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005891 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005892 return 0;
5893 }
5894
5895 /* More interesting part now : we know that we have a complete
5896 * response which at least looks like HTTP. We have an indicator
5897 * of each header's length, so we can parse them quickly.
5898 */
5899
5900 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005901 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005902
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005903 /*
5904 * 1: get the status code
5905 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005906 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005907 if (n < 1 || n > 5)
5908 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005909 /* when the client triggers a 4xx from the server, it's most often due
5910 * to a missing object or permission. These events should be tracked
5911 * because if they happen often, it may indicate a brute force or a
5912 * vulnerability scan.
5913 */
5914 if (n == 4)
5915 session_inc_http_err_ctr(s);
5916
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005917 if (objt_server(s->target))
5918 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005919
Willy Tarreau5b154472009-12-21 20:11:07 +01005920 /* check if the response is HTTP/1.1 or above */
5921 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005922 ((rep->buf->p[5] > '1') ||
5923 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005924 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005925
5926 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005927 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 +01005928
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005929 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005930 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005931
Willy Tarreau9b28e032012-10-12 23:49:43 +02005932 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005933
Willy Tarreau39650402010-03-15 19:44:39 +01005934 /* Adjust server's health based on status code. Note: status codes 501
5935 * and 505 are triggered on demand by client request, so we must not
5936 * count them as server failures.
5937 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005938 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005939 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005940 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005941 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005942 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005943 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005944
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005945 /*
5946 * 2: check for cacheability.
5947 */
5948
5949 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005950 case 100:
5951 /*
5952 * We may be facing a 100-continue response, in which case this
5953 * is not the right response, and we're waiting for the next one.
5954 * Let's allow this response to go to the client and wait for the
5955 * next one.
5956 */
5957 hdr_idx_init(&txn->hdr_idx);
5958 msg->next -= channel_forward(rep, msg->next);
5959 msg->msg_state = HTTP_MSG_RPBEFORE;
5960 txn->status = 0;
5961 s->logs.t_data = -1; /* was not a response yet */
5962 goto next_one;
5963
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005964 case 200:
5965 case 203:
5966 case 206:
5967 case 300:
5968 case 301:
5969 case 410:
5970 /* RFC2616 @13.4:
5971 * "A response received with a status code of
5972 * 200, 203, 206, 300, 301 or 410 MAY be stored
5973 * by a cache (...) unless a cache-control
5974 * directive prohibits caching."
5975 *
5976 * RFC2616 @9.5: POST method :
5977 * "Responses to this method are not cacheable,
5978 * unless the response includes appropriate
5979 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005980 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005981 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005982 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005983 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5984 break;
5985 default:
5986 break;
5987 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005988
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005989 /*
5990 * 3: we may need to capture headers
5991 */
5992 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005993 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005994 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005995 txn->rsp.cap, s->fe->rsp_cap);
5996
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005997 /* 4: determine the transfer-length.
5998 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5999 * the presence of a message-body in a RESPONSE and its transfer length
6000 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006001 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006002 * All responses to the HEAD request method MUST NOT include a
6003 * message-body, even though the presence of entity-header fields
6004 * might lead one to believe they do. All 1xx (informational), 204
6005 * (No Content), and 304 (Not Modified) responses MUST NOT include a
6006 * message-body. All other responses do include a message-body,
6007 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006008 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006009 * 1. Any response which "MUST NOT" include a message-body (such as the
6010 * 1xx, 204 and 304 responses and any response to a HEAD request) is
6011 * always terminated by the first empty line after the header fields,
6012 * regardless of the entity-header fields present in the message.
6013 *
6014 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
6015 * the "chunked" transfer-coding (Section 6.2) is used, the
6016 * transfer-length is defined by the use of this transfer-coding.
6017 * If a Transfer-Encoding header field is present and the "chunked"
6018 * transfer-coding is not present, the transfer-length is defined by
6019 * the sender closing the connection.
6020 *
6021 * 3. If a Content-Length header field is present, its decimal value in
6022 * OCTETs represents both the entity-length and the transfer-length.
6023 * If a message is received with both a Transfer-Encoding header
6024 * field and a Content-Length header field, the latter MUST be ignored.
6025 *
6026 * 4. If the message uses the media type "multipart/byteranges", and
6027 * the transfer-length is not otherwise specified, then this self-
6028 * delimiting media type defines the transfer-length. This media
6029 * type MUST NOT be used unless the sender knows that the recipient
6030 * can parse it; the presence in a request of a Range header with
6031 * multiple byte-range specifiers from a 1.1 client implies that the
6032 * client can parse multipart/byteranges responses.
6033 *
6034 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006035 */
6036
6037 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006038 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006039 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006040 * FIXME: should we parse anyway and return an error on chunked encoding ?
6041 */
6042 if (txn->meth == HTTP_METH_HEAD ||
6043 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006044 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006045 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006046 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006047 goto skip_content_length;
6048 }
6049
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006050 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006051 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006052 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006053 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006054 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006055 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6056 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006057 /* bad transfer-encoding (chunked followed by something else) */
6058 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006059 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006060 break;
6061 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006062 }
6063
6064 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
6065 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006066 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02006067 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006068 signed long long cl;
6069
Willy Tarreauad14f752011-09-02 20:33:27 +02006070 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006071 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006072 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006073 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006074
Willy Tarreauad14f752011-09-02 20:33:27 +02006075 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006076 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006077 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006078 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006079
Willy Tarreauad14f752011-09-02 20:33:27 +02006080 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006081 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006082 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006083 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006084
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006085 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006086 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006087 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006088 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006089
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006090 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006091 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006092 }
6093
William Lallemand82fe75c2012-10-23 10:25:10 +02006094 if (s->fe->comp || s->be->comp)
6095 select_compression_response_header(s, rep->buf);
6096
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006097skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006098 /* Now we have to check if we need to modify the Connection header.
6099 * This is more difficult on the response than it is on the request,
6100 * because we can have two different HTTP versions and we don't know
6101 * how the client will interprete a response. For instance, let's say
6102 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6103 * HTTP/1.1 response without any header. Maybe it will bound itself to
6104 * HTTP/1.0 because it only knows about it, and will consider the lack
6105 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6106 * the lack of header as a keep-alive. Thus we will use two flags
6107 * indicating how a request MAY be understood by the client. In case
6108 * of multiple possibilities, we'll fix the header to be explicit. If
6109 * ambiguous cases such as both close and keepalive are seen, then we
6110 * will fall back to explicit close. Note that we won't take risks with
6111 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006112 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006113 */
6114
Willy Tarreaudc008c52010-02-01 16:20:08 +01006115 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6116 txn->status == 101)) {
6117 /* Either we've established an explicit tunnel, or we're
6118 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006119 * to understand the next protocols. We have to switch to tunnel
6120 * mode, so that we transfer the request and responses then let
6121 * this protocol pass unmodified. When we later implement specific
6122 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006123 * header which contains information about that protocol for
6124 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006125 */
6126 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6127 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006128 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6129 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006130 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6131 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006132 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006133
Willy Tarreau70dffda2014-01-30 03:07:23 +01006134 /* this situation happens when combining pretend-keepalive with httpclose. */
6135 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006136 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6137 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006138 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6139
Willy Tarreau60466522010-01-18 19:08:45 +01006140 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006141 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006142 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6143 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006144
Willy Tarreau60466522010-01-18 19:08:45 +01006145 /* now adjust header transformations depending on current state */
6146 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6147 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6148 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006149 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006150 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006151 }
Willy Tarreau60466522010-01-18 19:08:45 +01006152 else { /* SCL / KAL */
6153 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006154 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006155 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006156 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006157
Willy Tarreau60466522010-01-18 19:08:45 +01006158 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006159 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006160
Willy Tarreau60466522010-01-18 19:08:45 +01006161 /* Some keep-alive responses are converted to Server-close if
6162 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006163 */
Willy Tarreau60466522010-01-18 19:08:45 +01006164 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6165 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006166 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006167 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006168 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006169 }
6170
Willy Tarreau7959a552013-09-23 16:44:27 +02006171 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006172 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006173
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006174 /* end of job, return OK */
6175 rep->analysers &= ~an_bit;
6176 rep->analyse_exp = TICK_ETERNITY;
6177 channel_auto_close(rep);
6178 return 1;
6179
6180 abort_keep_alive:
6181 /* A keep-alive request to the server failed on a network error.
6182 * The client is required to retry. We need to close without returning
6183 * any other information so that the client retries.
6184 */
6185 txn->status = 0;
6186 rep->analysers = 0;
6187 s->req->analysers = 0;
6188 channel_auto_close(rep);
6189 s->logs.logwait = 0;
6190 s->logs.level = 0;
6191 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
6192 bi_erase(rep);
6193 stream_int_retnclose(rep->cons, NULL);
6194 return 0;
6195}
6196
6197/* This function performs all the processing enabled for the current response.
6198 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6199 * and updates s->rep->analysers. It might make sense to explode it into several
6200 * other functions. It works like process_request (see indications above).
6201 */
6202int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6203{
6204 struct http_txn *txn = &s->txn;
6205 struct http_msg *msg = &txn->rsp;
6206 struct proxy *cur_proxy;
6207 struct cond_wordlist *wl;
6208 struct http_res_rule *http_res_last_rule = NULL;
6209
6210 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6211 now_ms, __FUNCTION__,
6212 s,
6213 rep,
6214 rep->rex, rep->wex,
6215 rep->flags,
6216 rep->buf->i,
6217 rep->analysers);
6218
6219 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6220 return 0;
6221
6222 rep->analysers &= ~an_bit;
6223 rep->analyse_exp = TICK_ETERNITY;
6224
Willy Tarreau70730dd2014-04-24 18:06:27 +02006225 /* The stats applet needs to adjust the Connection header but we don't
6226 * apply any filter there.
6227 */
6228 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6229 goto skip_filters;
6230
Willy Tarreau58975672014-04-24 21:13:57 +02006231 /*
6232 * We will have to evaluate the filters.
6233 * As opposed to version 1.2, now they will be evaluated in the
6234 * filters order and not in the header order. This means that
6235 * each filter has to be validated among all headers.
6236 *
6237 * Filters are tried with ->be first, then with ->fe if it is
6238 * different from ->be.
6239 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006240
Willy Tarreau58975672014-04-24 21:13:57 +02006241 cur_proxy = s->be;
6242 while (1) {
6243 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006244
Willy Tarreau58975672014-04-24 21:13:57 +02006245 /* evaluate http-response rules */
6246 if (!http_res_last_rule)
6247 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 +02006248
Willy Tarreau58975672014-04-24 21:13:57 +02006249 /* try headers filters */
6250 if (rule_set->rsp_exp != NULL) {
6251 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6252 return_bad_resp:
6253 if (objt_server(s->target)) {
6254 objt_server(s->target)->counters.failed_resp++;
6255 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006256 }
Willy Tarreau58975672014-04-24 21:13:57 +02006257 s->be->be_counters.failed_resp++;
6258 return_srv_prx_502:
6259 rep->analysers = 0;
6260 txn->status = 502;
6261 s->logs.t_data = -1; /* was not a valid response */
6262 rep->prod->flags |= SI_FL_NOLINGER;
6263 bi_erase(rep);
6264 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6265 if (!(s->flags & SN_ERR_MASK))
6266 s->flags |= SN_ERR_PRXCOND;
6267 if (!(s->flags & SN_FINST_MASK))
6268 s->flags |= SN_FINST_H;
6269 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006270 }
Willy Tarreau58975672014-04-24 21:13:57 +02006271 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006272
Willy Tarreau58975672014-04-24 21:13:57 +02006273 /* has the response been denied ? */
6274 if (txn->flags & TX_SVDENY) {
6275 if (objt_server(s->target))
6276 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006277
Willy Tarreau58975672014-04-24 21:13:57 +02006278 s->be->be_counters.denied_resp++;
6279 s->fe->fe_counters.denied_resp++;
6280 if (s->listener->counters)
6281 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006282
Willy Tarreau58975672014-04-24 21:13:57 +02006283 goto return_srv_prx_502;
6284 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006285
Willy Tarreau58975672014-04-24 21:13:57 +02006286 /* add response headers from the rule sets in the same order */
6287 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreauce730de2014-09-16 10:40:38 +02006288 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006289 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006290 if (wl->cond) {
6291 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6292 ret = acl_pass(ret);
6293 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6294 ret = !ret;
6295 if (!ret)
6296 continue;
6297 }
6298 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6299 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006300 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006301
Willy Tarreau58975672014-04-24 21:13:57 +02006302 /* check whether we're already working on the frontend */
6303 if (cur_proxy == s->fe)
6304 break;
6305 cur_proxy = s->fe;
6306 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006307
Willy Tarreau58975672014-04-24 21:13:57 +02006308 /* OK that's all we can do for 1xx responses */
Willy Tarreauce730de2014-09-16 10:40:38 +02006309 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006310 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006311
Willy Tarreau58975672014-04-24 21:13:57 +02006312 /*
6313 * Now check for a server cookie.
6314 */
6315 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6316 (s->be->options & PR_O_CHK_CACHE))
6317 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006318
Willy Tarreau58975672014-04-24 21:13:57 +02006319 /*
6320 * Check for cache-control or pragma headers if required.
6321 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006322 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 +02006323 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006324
Willy Tarreau58975672014-04-24 21:13:57 +02006325 /*
6326 * Add server cookie in the response if needed
6327 */
6328 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6329 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6330 (!(s->flags & SN_DIRECT) ||
6331 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6332 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6333 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6334 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6335 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6336 !(s->flags & SN_IGNORE_PRST)) {
6337 /* the server is known, it's not the one the client requested, or the
6338 * cookie's last seen date needs to be refreshed. We have to
6339 * insert a set-cookie here, except if we want to insert only on POST
6340 * requests and this one isn't. Note that servers which don't have cookies
6341 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006342 */
Willy Tarreau58975672014-04-24 21:13:57 +02006343 if (!objt_server(s->target)->cookie) {
6344 chunk_printf(&trash,
6345 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6346 s->be->cookie_name);
6347 }
6348 else {
6349 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006350
Willy Tarreau58975672014-04-24 21:13:57 +02006351 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6352 /* emit last_date, which is mandatory */
6353 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6354 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6355 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006356
Willy Tarreau58975672014-04-24 21:13:57 +02006357 if (s->be->cookie_maxlife) {
6358 /* emit first_date, which is either the original one or
6359 * the current date.
6360 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006361 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006362 s30tob64(txn->cookie_first_date ?
6363 txn->cookie_first_date >> 2 :
6364 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006365 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006366 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006367 }
Willy Tarreau58975672014-04-24 21:13:57 +02006368 chunk_appendf(&trash, "; path=/");
6369 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006370
Willy Tarreau58975672014-04-24 21:13:57 +02006371 if (s->be->cookie_domain)
6372 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006373
Willy Tarreau58975672014-04-24 21:13:57 +02006374 if (s->be->ck_opts & PR_CK_HTTPONLY)
6375 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006376
Willy Tarreau58975672014-04-24 21:13:57 +02006377 if (s->be->ck_opts & PR_CK_SECURE)
6378 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006379
Willy Tarreau58975672014-04-24 21:13:57 +02006380 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6381 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006382
Willy Tarreau58975672014-04-24 21:13:57 +02006383 txn->flags &= ~TX_SCK_MASK;
6384 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6385 /* the server did not change, only the date was updated */
6386 txn->flags |= TX_SCK_UPDATED;
6387 else
6388 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006389
Willy Tarreau58975672014-04-24 21:13:57 +02006390 /* Here, we will tell an eventual cache on the client side that we don't
6391 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6392 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6393 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006394 */
Willy Tarreau58975672014-04-24 21:13:57 +02006395 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006396
Willy Tarreau58975672014-04-24 21:13:57 +02006397 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006398
Willy Tarreau58975672014-04-24 21:13:57 +02006399 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6400 "Cache-control: private", 22) < 0))
6401 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006402 }
Willy Tarreau58975672014-04-24 21:13:57 +02006403 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006404
Willy Tarreau58975672014-04-24 21:13:57 +02006405 /*
6406 * Check if result will be cacheable with a cookie.
6407 * We'll block the response if security checks have caught
6408 * nasty things such as a cacheable cookie.
6409 */
6410 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6411 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6412 (s->be->options & PR_O_CHK_CACHE)) {
6413 /* we're in presence of a cacheable response containing
6414 * a set-cookie header. We'll block it as requested by
6415 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006416 */
Willy Tarreau58975672014-04-24 21:13:57 +02006417 if (objt_server(s->target))
6418 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006419
Willy Tarreau58975672014-04-24 21:13:57 +02006420 s->be->be_counters.denied_resp++;
6421 s->fe->fe_counters.denied_resp++;
6422 if (s->listener->counters)
6423 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006424
Willy Tarreau58975672014-04-24 21:13:57 +02006425 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6426 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6427 send_log(s->be, LOG_ALERT,
6428 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6429 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6430 goto return_srv_prx_502;
6431 }
Willy Tarreau03945942009-12-22 16:50:27 +01006432
Willy Tarreau70730dd2014-04-24 18:06:27 +02006433 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006434 /*
6435 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6436 * If an "Upgrade" token is found, the header is left untouched in order
6437 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreauce730de2014-09-16 10:40:38 +02006438 * if anything but "Upgrade" is present in the Connection header. We don't
6439 * want to touch any 101 response either since it's switching to another
6440 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006441 */
Willy Tarreauce730de2014-09-16 10:40:38 +02006442 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006443 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6444 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6445 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6446 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006447
Willy Tarreau58975672014-04-24 21:13:57 +02006448 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6449 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6450 /* we want a keep-alive response here. Keep-alive header
6451 * required if either side is not 1.1.
6452 */
6453 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6454 want_flags |= TX_CON_KAL_SET;
6455 }
6456 else {
6457 /* we want a close response here. Close header required if
6458 * the server is 1.1, regardless of the client.
6459 */
6460 if (msg->flags & HTTP_MSGF_VER_11)
6461 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006462 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006463
Willy Tarreau58975672014-04-24 21:13:57 +02006464 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6465 http_change_connection_header(txn, msg, want_flags);
6466 }
6467
6468 skip_header_mangling:
6469 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6470 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6471 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006472
Willy Tarreau58975672014-04-24 21:13:57 +02006473 /* if the user wants to log as soon as possible, without counting
6474 * bytes from the server, then this is the right moment. We have
6475 * to temporarily assign bytes_out to log what we currently have.
6476 */
6477 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6478 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6479 s->logs.bytes_out = txn->rsp.eoh;
6480 s->do_log(s);
6481 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006482 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006483 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006484}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006485
Willy Tarreaud98cf932009-12-27 22:54:55 +01006486/* This function is an analyser which forwards response body (including chunk
6487 * sizes if any). It is called as soon as we must forward, even if we forward
6488 * zero byte. The only situation where it must not be called is when we're in
6489 * tunnel mode and we want to forward till the close. It's used both to forward
6490 * remaining data and to resync after end of body. It expects the msg_state to
6491 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6492 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006493 *
6494 * It is capable of compressing response data both in content-length mode and
6495 * in chunked mode. The state machines follows different flows depending on
6496 * whether content-length and chunked modes are used, since there are no
6497 * trailers in content-length :
6498 *
6499 * chk-mode cl-mode
6500 * ,----- BODY -----.
6501 * / \
6502 * V size > 0 V chk-mode
6503 * .--> SIZE -------------> DATA -------------> CRLF
6504 * | | size == 0 | last byte |
6505 * | v final crlf v inspected |
6506 * | TRAILERS -----------> DONE |
6507 * | |
6508 * `----------------------------------------------'
6509 *
6510 * Compression only happens in the DATA state, and must be flushed in final
6511 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6512 * is performed at once on final states for all bytes parsed, or when leaving
6513 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006514 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006515int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006516{
6517 struct http_txn *txn = &s->txn;
6518 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006519 static struct buffer *tmpbuf = NULL;
6520 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006521 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006522
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006523 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6524 return 0;
6525
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006526 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006527 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006528 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006529 /* Output closed while we were sending data. We must abort and
6530 * wake the other side up.
6531 */
6532 msg->msg_state = HTTP_MSG_ERROR;
6533 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006534 return 1;
6535 }
6536
Willy Tarreau4fe41902010-06-07 22:27:41 +02006537 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006538 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006539
Willy Tarreaubb2e6692014-07-10 19:06:10 +02006540 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006541 /* we have msg->sov which points to the first byte of message
6542 * body, and res->buf.p still points to the beginning of the
6543 * message. We forward the headers now, as we don't need them
6544 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006545 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006546 b_adv(res->buf, msg->sov);
6547 msg->next -= msg->sov;
6548 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006549
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006550 /* The previous analysers guarantee that the state is somewhere
6551 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6552 * msg->next are always correct.
6553 */
6554 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6555 if (msg->flags & HTTP_MSGF_TE_CHNK)
6556 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6557 else
6558 msg->msg_state = HTTP_MSG_DATA;
6559 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006560 }
6561
Willy Tarreauefdf0942014-04-24 20:08:57 +02006562 if (res->to_forward) {
6563 /* We can't process the buffer's contents yet */
6564 res->flags |= CF_WAKE_WRITE;
6565 goto missing_data;
6566 }
6567
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006568 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6569 /* We need a compression buffer in the DATA state to put the
6570 * output of compressed data, and in CRLF state to let the
6571 * TRAILERS state finish the job of removing the trailing CRLF.
6572 */
6573 if (unlikely(tmpbuf == NULL)) {
6574 /* this is the first time we need the compression buffer */
6575 tmpbuf = pool_alloc2(pool2_buffer);
6576 if (tmpbuf == NULL)
6577 goto aborted_xfer; /* no memory */
6578 }
6579
6580 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006581 if (ret < 0) {
6582 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006583 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006584 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006585 compressing = 1;
6586 }
6587
Willy Tarreaud98cf932009-12-27 22:54:55 +01006588 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006589 switch (msg->msg_state - HTTP_MSG_DATA) {
6590 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006591 /* we may have some pending data starting at res->buf->p */
6592 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006593 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006594 if (ret < 0)
6595 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006596
Willy Tarreaud5a67832014-04-21 10:54:27 +02006597 if (msg->chunk_len) {
6598 /* input empty or output full */
6599 if (res->buf->i > msg->next)
6600 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006601 goto missing_data;
6602 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006603 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006604 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006605 if (msg->chunk_len > res->buf->i - msg->next) {
6606 /* output full */
6607 res->flags |= CF_WAKE_WRITE;
6608 goto missing_data;
6609 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006610 msg->next += msg->chunk_len;
6611 msg->chunk_len = 0;
6612 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006613
6614 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006615 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006616 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006617 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006618 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006619 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006620 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006621 /* fall through for HTTP_MSG_CHUNK_CRLF */
6622
6623 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6624 /* we want the CRLF after the data */
6625
6626 ret = http_skip_chunk_crlf(msg);
6627 if (ret == 0)
6628 goto missing_data;
6629 else if (ret < 0) {
6630 if (msg->err_pos >= 0)
6631 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6632 goto return_bad_res;
6633 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006634 /* we're in MSG_CHUNK_SIZE now, fall through */
6635
6636 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006637 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006638 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006639 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006640 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006641
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006642 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006643 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006644 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006645 else if (ret < 0) {
6646 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006647 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006648 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006649 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006650 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006651 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006652
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006653 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006654 if (unlikely(compressing)) {
6655 /* we need to flush output contents before syncing FSMs */
6656 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6657 compressing = 0;
6658 }
6659
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006660 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006661 if (ret == 0)
6662 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006663 else if (ret < 0) {
6664 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006665 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006666 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006667 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006668 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006669
6670 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006671 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006672 if (unlikely(compressing)) {
6673 /* we need to flush output contents before syncing FSMs */
6674 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6675 compressing = 0;
6676 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006677
Willy Tarreauc623c172014-04-18 09:53:50 +02006678 /* we may have some pending data starting at res->buf->p
6679 * such as a last chunk of data or trailers.
6680 */
6681 b_adv(res->buf, msg->next);
6682 msg->next = 0;
6683
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006684 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006685 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006686 if ((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 Tarreau3ce10ff2014-04-22 08:24:38 +02006689
Willy Tarreau610ecce2010-01-04 21:15:02 +01006690 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006691 /* some state changes occurred, maybe the analyser
6692 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006693 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006694 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006695 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006696 /* response errors are most likely due to
6697 * the client aborting the transfer.
6698 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006699 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006700 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006701 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006702 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006703 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006704 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006705 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006706 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006707 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006708 }
6709 }
6710
Willy Tarreaud98cf932009-12-27 22:54:55 +01006711 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006712 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006713 if (unlikely(compressing)) {
6714 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006715 compressing = 0;
6716 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006717
Willy Tarreauc623c172014-04-18 09:53:50 +02006718 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6719 b_adv(res->buf, msg->next);
6720 msg->next = 0;
6721 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6722 }
6723
Willy Tarreauf003d372012-11-26 13:35:37 +01006724 if (res->flags & CF_SHUTW)
6725 goto aborted_xfer;
6726
6727 /* stop waiting for data if the input is closed before the end. If the
6728 * client side was already closed, it means that the client has aborted,
6729 * so we don't want to count this as a server abort. Otherwise it's a
6730 * server abort.
6731 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006732 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006733 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006734 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006735 if (!(s->flags & SN_ERR_MASK))
6736 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006737 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006738 if (objt_server(s->target))
6739 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006740 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006741 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006742
Willy Tarreau40dba092010-03-04 18:14:51 +01006743 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006744 if (!s->req->analysers)
6745 goto return_bad_res;
6746
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006747 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006748 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006749 * Similarly, with keep-alive on the client side, we don't want to forward a
6750 * close.
6751 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006752 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006753 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6754 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006755 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006756
Willy Tarreau5c620922011-05-11 19:56:11 +02006757 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006758 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006759 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006760 * modes are already handled by the stream sock layer. We must not do
6761 * this in content-length mode because it could present the MSG_MORE
6762 * flag with the last block of forwarded data, which would cause an
6763 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006764 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006765 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006766 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006767
Willy Tarreaud98cf932009-12-27 22:54:55 +01006768 /* the session handler will take care of timeouts and errors */
6769 return 0;
6770
Willy Tarreau40dba092010-03-04 18:14:51 +01006771 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006772 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006773 if (objt_server(s->target))
6774 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006775
6776 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006777 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006778 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006779 compressing = 0;
6780 }
6781
Willy Tarreauc623c172014-04-18 09:53:50 +02006782 /* we may have some pending data starting at res->buf->p */
6783 if (s->comp_algo == NULL) {
6784 b_adv(res->buf, msg->next);
6785 msg->next = 0;
6786 }
6787
Willy Tarreaud98cf932009-12-27 22:54:55 +01006788 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006789 /* don't send any error message as we're in the body */
6790 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006791 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006792 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006793 if (objt_server(s->target))
6794 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006795
6796 if (!(s->flags & SN_ERR_MASK))
6797 s->flags |= SN_ERR_PRXCOND;
6798 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006799 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006800 return 0;
6801
6802 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006803 if (unlikely(compressing)) {
6804 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6805 compressing = 0;
6806 }
6807
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006808 txn->rsp.msg_state = HTTP_MSG_ERROR;
6809 /* don't send any error message as we're in the body */
6810 stream_int_retnclose(res->cons, NULL);
6811 res->analysers = 0;
6812 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6813
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006814 s->fe->fe_counters.cli_aborts++;
6815 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006816 if (objt_server(s->target))
6817 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006818
6819 if (!(s->flags & SN_ERR_MASK))
6820 s->flags |= SN_ERR_CLICL;
6821 if (!(s->flags & SN_FINST_MASK))
6822 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006823 return 0;
6824}
6825
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006826/* Iterate the same filter through all request headers.
6827 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006828 * Since it can manage the switch to another backend, it updates the per-proxy
6829 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006830 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006831int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006832{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006833 char *cur_ptr, *cur_end, *cur_next;
6834 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006835 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006836 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006837 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006838
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006839 last_hdr = 0;
6840
Willy Tarreau9b28e032012-10-12 23:49:43 +02006841 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006842 old_idx = 0;
6843
6844 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006845 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006846 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006847 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006848 (exp->action == ACT_ALLOW ||
6849 exp->action == ACT_DENY ||
6850 exp->action == ACT_TARPIT))
6851 return 0;
6852
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006853 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006854 if (!cur_idx)
6855 break;
6856
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006857 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006858 cur_ptr = cur_next;
6859 cur_end = cur_ptr + cur_hdr->len;
6860 cur_next = cur_end + cur_hdr->cr + 1;
6861
6862 /* Now we have one header between cur_ptr and cur_end,
6863 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006864 */
6865
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02006866 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006867 switch (exp->action) {
6868 case ACT_SETBE:
6869 /* It is not possible to jump a second time.
6870 * FIXME: should we return an HTTP/500 here so that
6871 * the admin knows there's a problem ?
6872 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006873 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006874 break;
6875
6876 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006877 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006878 last_hdr = 1;
6879 break;
6880
6881 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006882 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006883 last_hdr = 1;
6884 break;
6885
6886 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006887 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006888 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006889 break;
6890
6891 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006892 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006893 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006894 break;
6895
6896 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006897 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6898 if (trash.len < 0)
6899 return -1;
6900
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006901 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006902 /* FIXME: if the user adds a newline in the replacement, the
6903 * index will not be recalculated for now, and the new line
6904 * will not be counted as a new header.
6905 */
6906
6907 cur_end += delta;
6908 cur_next += delta;
6909 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006910 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006911 break;
6912
6913 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006914 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006915 cur_next += delta;
6916
Willy Tarreaufa355d42009-11-29 18:12:29 +01006917 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006918 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6919 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006920 cur_hdr->len = 0;
6921 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006922 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006923 break;
6924
6925 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006926 }
6927
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006928 /* keep the link from this header to next one in case of later
6929 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006930 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006931 old_idx = cur_idx;
6932 }
6933 return 0;
6934}
6935
6936
6937/* Apply the filter to the request line.
6938 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6939 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006940 * Since it can manage the switch to another backend, it updates the per-proxy
6941 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006942 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006943int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006944{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006945 char *cur_ptr, *cur_end;
6946 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006947 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006948 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006949
Willy Tarreau3d300592007-03-18 18:34:41 +01006950 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006951 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006952 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006953 (exp->action == ACT_ALLOW ||
6954 exp->action == ACT_DENY ||
6955 exp->action == ACT_TARPIT))
6956 return 0;
6957 else if (exp->action == ACT_REMOVE)
6958 return 0;
6959
6960 done = 0;
6961
Willy Tarreau9b28e032012-10-12 23:49:43 +02006962 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006963 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006964
6965 /* Now we have the request line between cur_ptr and cur_end */
6966
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02006967 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006968 switch (exp->action) {
6969 case ACT_SETBE:
6970 /* It is not possible to jump a second time.
6971 * FIXME: should we return an HTTP/500 here so that
6972 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006973 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006974 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006975 break;
6976
6977 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006978 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006979 done = 1;
6980 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006981
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006982 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006983 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006984 done = 1;
6985 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006986
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006987 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006988 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006989 done = 1;
6990 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006991
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006992 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006993 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006994 done = 1;
6995 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006996
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006997 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006998 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6999 if (trash.len < 0)
7000 return -1;
7001
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007002 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007003 /* FIXME: if the user adds a newline in the replacement, the
7004 * index will not be recalculated for now, and the new line
7005 * will not be counted as a new header.
7006 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007007
Willy Tarreaufa355d42009-11-29 18:12:29 +01007008 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007009 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007010 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007011 HTTP_MSG_RQMETH,
7012 cur_ptr, cur_end + 1,
7013 NULL, NULL);
7014 if (unlikely(!cur_end))
7015 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007016
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007017 /* we have a full request and we know that we have either a CR
7018 * or an LF at <ptr>.
7019 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007020 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7021 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007022 /* there is no point trying this regex on headers */
7023 return 1;
7024 }
7025 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007026 return done;
7027}
Willy Tarreau97de6242006-12-27 17:18:38 +01007028
Willy Tarreau58f10d72006-12-04 02:26:12 +01007029
Willy Tarreau58f10d72006-12-04 02:26:12 +01007030
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007031/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01007032 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007033 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007034 * unparsable request. Since it can manage the switch to another backend, it
7035 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007036 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007037int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007038{
Willy Tarreau6c123b12010-01-28 20:22:06 +01007039 struct http_txn *txn = &s->txn;
7040 struct hdr_exp *exp;
7041
7042 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007043 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007044
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007045 /*
7046 * The interleaving of transformations and verdicts
7047 * makes it difficult to decide to continue or stop
7048 * the evaluation.
7049 */
7050
Willy Tarreau6c123b12010-01-28 20:22:06 +01007051 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7052 break;
7053
Willy Tarreau3d300592007-03-18 18:34:41 +01007054 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007055 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007056 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007057 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007058
7059 /* if this filter had a condition, evaluate it now and skip to
7060 * next filter if the condition does not match.
7061 */
7062 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007063 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007064 ret = acl_pass(ret);
7065 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7066 ret = !ret;
7067
7068 if (!ret)
7069 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007070 }
7071
7072 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007073 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007074 if (unlikely(ret < 0))
7075 return -1;
7076
7077 if (likely(ret == 0)) {
7078 /* The filter did not match the request, it can be
7079 * iterated through all headers.
7080 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007081 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007082 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007083 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007084 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007085}
7086
7087
Willy Tarreaua15645d2007-03-18 16:22:39 +01007088
Willy Tarreau58f10d72006-12-04 02:26:12 +01007089/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007090 * Try to retrieve the server associated to the appsession.
7091 * If the server is found, it's assigned to the session.
7092 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007093void manage_client_side_appsession(struct session *s, const char *buf, int len) {
7094 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007095 appsess *asession = NULL;
7096 char *sessid_temp = NULL;
7097
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007098 if (len > s->be->appsession_len) {
7099 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007100 }
7101
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007102 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007103 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007104 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007105 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007106 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007107 }
7108
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007109 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007110 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007111 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007112 return;
7113 }
7114
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007115 memcpy(txn->sessid, buf, len);
7116 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007117 }
7118
7119 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7120 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007121 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007122 return;
7123 }
7124
Cyril Bontéb21570a2009-11-29 20:04:48 +01007125 memcpy(sessid_temp, buf, len);
7126 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007127
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007128 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007129 /* free previously allocated memory */
7130 pool_free2(apools.sessid, sessid_temp);
7131
7132 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007133 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7134 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007135 asession->request_count++;
7136
7137 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007138 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007139
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007140 while (srv) {
7141 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007142 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007143 (s->be->options & PR_O_PERSIST) ||
7144 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007145 /* we found the server and it's usable */
7146 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007147 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007148 s->flags |= SN_DIRECT | SN_ASSIGNED;
7149 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007150
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007151 break;
7152 } else {
7153 txn->flags &= ~TX_CK_MASK;
7154 txn->flags |= TX_CK_DOWN;
7155 }
7156 }
7157 srv = srv->next;
7158 }
7159 }
7160 }
7161}
7162
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007163/* Find the end of a cookie value contained between <s> and <e>. It works the
7164 * same way as with headers above except that the semi-colon also ends a token.
7165 * See RFC2965 for more information. Note that it requires a valid header to
7166 * return a valid result.
7167 */
7168char *find_cookie_value_end(char *s, const char *e)
7169{
7170 int quoted, qdpair;
7171
7172 quoted = qdpair = 0;
7173 for (; s < e; s++) {
7174 if (qdpair) qdpair = 0;
7175 else if (quoted) {
7176 if (*s == '\\') qdpair = 1;
7177 else if (*s == '"') quoted = 0;
7178 }
7179 else if (*s == '"') quoted = 1;
7180 else if (*s == ',' || *s == ';') return s;
7181 }
7182 return s;
7183}
7184
7185/* Delete a value in a header between delimiters <from> and <next> in buffer
7186 * <buf>. The number of characters displaced is returned, and the pointer to
7187 * the first delimiter is updated if required. The function tries as much as
7188 * possible to respect the following principles :
7189 * - replace <from> delimiter by the <next> one unless <from> points to a
7190 * colon, in which case <next> is simply removed
7191 * - set exactly one space character after the new first delimiter, unless
7192 * there are not enough characters in the block being moved to do so.
7193 * - remove unneeded spaces before the previous delimiter and after the new
7194 * one.
7195 *
7196 * It is the caller's responsibility to ensure that :
7197 * - <from> points to a valid delimiter or the colon ;
7198 * - <next> points to a valid delimiter or the final CR/LF ;
7199 * - there are non-space chars before <from> ;
7200 * - there is a CR/LF at or after <next>.
7201 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007202int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007203{
7204 char *prev = *from;
7205
7206 if (*prev == ':') {
7207 /* We're removing the first value, preserve the colon and add a
7208 * space if possible.
7209 */
7210 if (!http_is_crlf[(unsigned char)*next])
7211 next++;
7212 prev++;
7213 if (prev < next)
7214 *prev++ = ' ';
7215
7216 while (http_is_spht[(unsigned char)*next])
7217 next++;
7218 } else {
7219 /* Remove useless spaces before the old delimiter. */
7220 while (http_is_spht[(unsigned char)*(prev-1)])
7221 prev--;
7222 *from = prev;
7223
7224 /* copy the delimiter and if possible a space if we're
7225 * not at the end of the line.
7226 */
7227 if (!http_is_crlf[(unsigned char)*next]) {
7228 *prev++ = *next++;
7229 if (prev + 1 < next)
7230 *prev++ = ' ';
7231 while (http_is_spht[(unsigned char)*next])
7232 next++;
7233 }
7234 }
7235 return buffer_replace2(buf, prev, next, NULL, 0);
7236}
7237
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007238/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007239 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007240 * desirable to call it only when needed. This code is quite complex because
7241 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7242 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007243 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007244void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007245{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007246 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007247 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007248 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007249 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7250 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007251
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007252 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007253 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007254 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007255
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007256 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007257 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007258 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007259
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007260 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007261 hdr_beg = hdr_next;
7262 hdr_end = hdr_beg + cur_hdr->len;
7263 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007264
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007265 /* We have one full header between hdr_beg and hdr_end, and the
7266 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007267 * "Cookie:" headers.
7268 */
7269
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007270 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007271 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007272 old_idx = cur_idx;
7273 continue;
7274 }
7275
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007276 del_from = NULL; /* nothing to be deleted */
7277 preserve_hdr = 0; /* assume we may kill the whole header */
7278
Willy Tarreau58f10d72006-12-04 02:26:12 +01007279 /* Now look for cookies. Conforming to RFC2109, we have to support
7280 * attributes whose name begin with a '$', and associate them with
7281 * the right cookie, if we want to delete this cookie.
7282 * So there are 3 cases for each cookie read :
7283 * 1) it's a special attribute, beginning with a '$' : ignore it.
7284 * 2) it's a server id cookie that we *MAY* want to delete : save
7285 * some pointers on it (last semi-colon, beginning of cookie...)
7286 * 3) it's an application cookie : we *MAY* have to delete a previous
7287 * "special" cookie.
7288 * At the end of loop, if a "special" cookie remains, we may have to
7289 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007290 * *MUST* delete it.
7291 *
7292 * Note: RFC2965 is unclear about the processing of spaces around
7293 * the equal sign in the ATTR=VALUE form. A careful inspection of
7294 * the RFC explicitly allows spaces before it, and not within the
7295 * tokens (attrs or values). An inspection of RFC2109 allows that
7296 * too but section 10.1.3 lets one think that spaces may be allowed
7297 * after the equal sign too, resulting in some (rare) buggy
7298 * implementations trying to do that. So let's do what servers do.
7299 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7300 * allowed quoted strings in values, with any possible character
7301 * after a backslash, including control chars and delimitors, which
7302 * causes parsing to become ambiguous. Browsers also allow spaces
7303 * within values even without quotes.
7304 *
7305 * We have to keep multiple pointers in order to support cookie
7306 * removal at the beginning, middle or end of header without
7307 * corrupting the header. All of these headers are valid :
7308 *
7309 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7310 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7311 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7312 * | | | | | | | | |
7313 * | | | | | | | | hdr_end <--+
7314 * | | | | | | | +--> next
7315 * | | | | | | +----> val_end
7316 * | | | | | +-----------> val_beg
7317 * | | | | +--------------> equal
7318 * | | | +----------------> att_end
7319 * | | +---------------------> att_beg
7320 * | +--------------------------> prev
7321 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007322 */
7323
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007324 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7325 /* Iterate through all cookies on this line */
7326
7327 /* find att_beg */
7328 att_beg = prev + 1;
7329 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7330 att_beg++;
7331
7332 /* find att_end : this is the first character after the last non
7333 * space before the equal. It may be equal to hdr_end.
7334 */
7335 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007336
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007337 while (equal < hdr_end) {
7338 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007339 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007340 if (http_is_spht[(unsigned char)*equal++])
7341 continue;
7342 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007343 }
7344
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007345 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7346 * is between <att_beg> and <equal>, both may be identical.
7347 */
7348
7349 /* look for end of cookie if there is an equal sign */
7350 if (equal < hdr_end && *equal == '=') {
7351 /* look for the beginning of the value */
7352 val_beg = equal + 1;
7353 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7354 val_beg++;
7355
7356 /* find the end of the value, respecting quotes */
7357 next = find_cookie_value_end(val_beg, hdr_end);
7358
7359 /* make val_end point to the first white space or delimitor after the value */
7360 val_end = next;
7361 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7362 val_end--;
7363 } else {
7364 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007365 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007366
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007367 /* We have nothing to do with attributes beginning with '$'. However,
7368 * they will automatically be removed if a header before them is removed,
7369 * since they're supposed to be linked together.
7370 */
7371 if (*att_beg == '$')
7372 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007373
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007374 /* Ignore cookies with no equal sign */
7375 if (equal == next) {
7376 /* This is not our cookie, so we must preserve it. But if we already
7377 * scheduled another cookie for removal, we cannot remove the
7378 * complete header, but we can remove the previous block itself.
7379 */
7380 preserve_hdr = 1;
7381 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007382 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007383 val_end += delta;
7384 next += delta;
7385 hdr_end += delta;
7386 hdr_next += delta;
7387 cur_hdr->len += delta;
7388 http_msg_move_end(&txn->req, delta);
7389 prev = del_from;
7390 del_from = NULL;
7391 }
7392 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007393 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007394
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007395 /* if there are spaces around the equal sign, we need to
7396 * strip them otherwise we'll get trouble for cookie captures,
7397 * or even for rewrites. Since this happens extremely rarely,
7398 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007399 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007400 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7401 int stripped_before = 0;
7402 int stripped_after = 0;
7403
7404 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007405 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007406 equal += stripped_before;
7407 val_beg += stripped_before;
7408 }
7409
7410 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007411 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007412 val_beg += stripped_after;
7413 stripped_before += stripped_after;
7414 }
7415
7416 val_end += stripped_before;
7417 next += stripped_before;
7418 hdr_end += stripped_before;
7419 hdr_next += stripped_before;
7420 cur_hdr->len += stripped_before;
7421 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007422 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007423 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007424
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007425 /* First, let's see if we want to capture this cookie. We check
7426 * that we don't already have a client side cookie, because we
7427 * can only capture one. Also as an optimisation, we ignore
7428 * cookies shorter than the declared name.
7429 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007430 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7431 (val_end - att_beg >= s->fe->capture_namelen) &&
7432 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007433 int log_len = val_end - att_beg;
7434
7435 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7436 Alert("HTTP logging : out of memory.\n");
7437 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007438 if (log_len > s->fe->capture_len)
7439 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007440 memcpy(txn->cli_cookie, att_beg, log_len);
7441 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007442 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007443 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007444
Willy Tarreaubca99692010-10-06 19:25:55 +02007445 /* Persistence cookies in passive, rewrite or insert mode have the
7446 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007447 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007448 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007449 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007450 * For cookies in prefix mode, the form is :
7451 *
7452 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007453 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007454 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7455 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7456 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007457 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007458
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007459 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7460 * have the server ID between val_beg and delim, and the original cookie between
7461 * delim+1 and val_end. Otherwise, delim==val_end :
7462 *
7463 * Cookie: NAME=SRV; # in all but prefix modes
7464 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7465 * | || || | |+-> next
7466 * | || || | +--> val_end
7467 * | || || +---------> delim
7468 * | || |+------------> val_beg
7469 * | || +-------------> att_end = equal
7470 * | |+-----------------> att_beg
7471 * | +------------------> prev
7472 * +-------------------------> hdr_beg
7473 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007474
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007475 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007476 for (delim = val_beg; delim < val_end; delim++)
7477 if (*delim == COOKIE_DELIM)
7478 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007479 } else {
7480 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007481 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007482 /* Now check if the cookie contains a date field, which would
7483 * appear after a vertical bar ('|') just after the server name
7484 * and before the delimiter.
7485 */
7486 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7487 if (vbar1) {
7488 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007489 * right is the last seen date. It is a base64 encoded
7490 * 30-bit value representing the UNIX date since the
7491 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007492 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007493 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007494 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007495 if (val_end - vbar1 >= 5) {
7496 val = b64tos30(vbar1);
7497 if (val > 0)
7498 txn->cookie_last_date = val << 2;
7499 }
7500 /* look for a second vertical bar */
7501 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7502 if (vbar1 && (val_end - vbar1 > 5)) {
7503 val = b64tos30(vbar1 + 1);
7504 if (val > 0)
7505 txn->cookie_first_date = val << 2;
7506 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007507 }
7508 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007509
Willy Tarreauf64d1412010-10-07 20:06:11 +02007510 /* if the cookie has an expiration date and the proxy wants to check
7511 * it, then we do that now. We first check if the cookie is too old,
7512 * then only if it has expired. We detect strict overflow because the
7513 * time resolution here is not great (4 seconds). Cookies with dates
7514 * in the future are ignored if their offset is beyond one day. This
7515 * allows an admin to fix timezone issues without expiring everyone
7516 * and at the same time avoids keeping unwanted side effects for too
7517 * long.
7518 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007519 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7520 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007521 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007522 txn->flags &= ~TX_CK_MASK;
7523 txn->flags |= TX_CK_OLD;
7524 delim = val_beg; // let's pretend we have not found the cookie
7525 txn->cookie_first_date = 0;
7526 txn->cookie_last_date = 0;
7527 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007528 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7529 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007530 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007531 txn->flags &= ~TX_CK_MASK;
7532 txn->flags |= TX_CK_EXPIRED;
7533 delim = val_beg; // let's pretend we have not found the cookie
7534 txn->cookie_first_date = 0;
7535 txn->cookie_last_date = 0;
7536 }
7537
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007538 /* Here, we'll look for the first running server which supports the cookie.
7539 * This allows to share a same cookie between several servers, for example
7540 * to dedicate backup servers to specific servers only.
7541 * However, to prevent clients from sticking to cookie-less backup server
7542 * when they have incidentely learned an empty cookie, we simply ignore
7543 * empty cookies and mark them as invalid.
7544 * The same behaviour is applied when persistence must be ignored.
7545 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007546 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007547 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007548
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007549 while (srv) {
7550 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7551 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007552 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007553 (s->be->options & PR_O_PERSIST) ||
7554 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007555 /* we found the server and we can use it */
7556 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007557 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007558 s->flags |= SN_DIRECT | SN_ASSIGNED;
7559 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007560 break;
7561 } else {
7562 /* we found a server, but it's down,
7563 * mark it as such and go on in case
7564 * another one is available.
7565 */
7566 txn->flags &= ~TX_CK_MASK;
7567 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007568 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007569 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007570 srv = srv->next;
7571 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007572
Willy Tarreauf64d1412010-10-07 20:06:11 +02007573 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007574 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007575 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007576 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007577 txn->flags |= TX_CK_UNUSED;
7578 else
7579 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007580 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007581
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007582 /* depending on the cookie mode, we may have to either :
7583 * - delete the complete cookie if we're in insert+indirect mode, so that
7584 * the server never sees it ;
7585 * - remove the server id from the cookie value, and tag the cookie as an
7586 * application cookie so that it does not get accidentely removed later,
7587 * if we're in cookie prefix mode
7588 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007589 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007590 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007591
Willy Tarreau9b28e032012-10-12 23:49:43 +02007592 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007593 val_end += delta;
7594 next += delta;
7595 hdr_end += delta;
7596 hdr_next += delta;
7597 cur_hdr->len += delta;
7598 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007599
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007600 del_from = NULL;
7601 preserve_hdr = 1; /* we want to keep this cookie */
7602 }
7603 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007604 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007605 del_from = prev;
7606 }
7607 } else {
7608 /* This is not our cookie, so we must preserve it. But if we already
7609 * scheduled another cookie for removal, we cannot remove the
7610 * complete header, but we can remove the previous block itself.
7611 */
7612 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007613
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007614 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007615 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007616 if (att_beg >= del_from)
7617 att_beg += delta;
7618 if (att_end >= del_from)
7619 att_end += delta;
7620 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007621 val_end += delta;
7622 next += delta;
7623 hdr_end += delta;
7624 hdr_next += delta;
7625 cur_hdr->len += delta;
7626 http_msg_move_end(&txn->req, delta);
7627 prev = del_from;
7628 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007629 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007630 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007631
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007632 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007633 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007634 int cmp_len, value_len;
7635 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007636
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007637 if (s->be->options2 & PR_O2_AS_PFX) {
7638 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7639 value_begin = att_beg + s->be->appsession_name_len;
7640 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007641 } else {
7642 cmp_len = att_end - att_beg;
7643 value_begin = val_beg;
7644 value_len = val_end - val_beg;
7645 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007646
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007647 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007648 if (cmp_len == s->be->appsession_name_len &&
7649 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7650 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007651 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007652 }
7653
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007654 /* continue with next cookie on this header line */
7655 att_beg = next;
7656 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007657
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007658 /* There are no more cookies on this line.
7659 * We may still have one (or several) marked for deletion at the
7660 * end of the line. We must do this now in two ways :
7661 * - if some cookies must be preserved, we only delete from the
7662 * mark to the end of line ;
7663 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007664 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007665 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007666 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007667 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007668 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007669 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007670 cur_hdr->len += delta;
7671 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007672 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007673
7674 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007675 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7676 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007677 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007678 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007679 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007680 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007681 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007682 }
7683
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007684 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007685 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007686 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007687}
7688
7689
Willy Tarreaua15645d2007-03-18 16:22:39 +01007690/* Iterate the same filter through all response headers contained in <rtr>.
7691 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7692 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007693int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007694{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007695 char *cur_ptr, *cur_end, *cur_next;
7696 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007697 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007698 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007699 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007700
7701 last_hdr = 0;
7702
Willy Tarreau9b28e032012-10-12 23:49:43 +02007703 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007704 old_idx = 0;
7705
7706 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007707 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007708 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007709 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007710 (exp->action == ACT_ALLOW ||
7711 exp->action == ACT_DENY))
7712 return 0;
7713
7714 cur_idx = txn->hdr_idx.v[old_idx].next;
7715 if (!cur_idx)
7716 break;
7717
7718 cur_hdr = &txn->hdr_idx.v[cur_idx];
7719 cur_ptr = cur_next;
7720 cur_end = cur_ptr + cur_hdr->len;
7721 cur_next = cur_end + cur_hdr->cr + 1;
7722
7723 /* Now we have one header between cur_ptr and cur_end,
7724 * and the next header starts at cur_next.
7725 */
7726
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007727 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007728 switch (exp->action) {
7729 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007730 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007731 last_hdr = 1;
7732 break;
7733
7734 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007735 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007736 last_hdr = 1;
7737 break;
7738
7739 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007740 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7741 if (trash.len < 0)
7742 return -1;
7743
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007744 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007745 /* FIXME: if the user adds a newline in the replacement, the
7746 * index will not be recalculated for now, and the new line
7747 * will not be counted as a new header.
7748 */
7749
7750 cur_end += delta;
7751 cur_next += delta;
7752 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007753 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007754 break;
7755
7756 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007757 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007758 cur_next += delta;
7759
Willy Tarreaufa355d42009-11-29 18:12:29 +01007760 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007761 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7762 txn->hdr_idx.used--;
7763 cur_hdr->len = 0;
7764 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007765 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007766 break;
7767
7768 }
7769 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007770
7771 /* keep the link from this header to next one in case of later
7772 * removal of next header.
7773 */
7774 old_idx = cur_idx;
7775 }
7776 return 0;
7777}
7778
7779
7780/* Apply the filter to the status line in the response buffer <rtr>.
7781 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7782 * or -1 if a replacement resulted in an invalid status line.
7783 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007784int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007785{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007786 char *cur_ptr, *cur_end;
7787 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007788 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007789 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007790
7791
Willy Tarreau3d300592007-03-18 18:34:41 +01007792 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007793 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007794 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007795 (exp->action == ACT_ALLOW ||
7796 exp->action == ACT_DENY))
7797 return 0;
7798 else if (exp->action == ACT_REMOVE)
7799 return 0;
7800
7801 done = 0;
7802
Willy Tarreau9b28e032012-10-12 23:49:43 +02007803 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007804 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007805
7806 /* Now we have the status line between cur_ptr and cur_end */
7807
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007808 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007809 switch (exp->action) {
7810 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007811 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007812 done = 1;
7813 break;
7814
7815 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007816 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007817 done = 1;
7818 break;
7819
7820 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007821 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7822 if (trash.len < 0)
7823 return -1;
7824
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007825 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007826 /* FIXME: if the user adds a newline in the replacement, the
7827 * index will not be recalculated for now, and the new line
7828 * will not be counted as a new header.
7829 */
7830
Willy Tarreaufa355d42009-11-29 18:12:29 +01007831 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007832 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007833 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007834 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007835 cur_ptr, cur_end + 1,
7836 NULL, NULL);
7837 if (unlikely(!cur_end))
7838 return -1;
7839
7840 /* we have a full respnse and we know that we have either a CR
7841 * or an LF at <ptr>.
7842 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007843 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007844 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007845 /* there is no point trying this regex on headers */
7846 return 1;
7847 }
7848 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007849 return done;
7850}
7851
7852
7853
7854/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007855 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007856 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7857 * unparsable response.
7858 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007859int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007860{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007861 struct http_txn *txn = &s->txn;
7862 struct hdr_exp *exp;
7863
7864 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007865 int ret;
7866
7867 /*
7868 * The interleaving of transformations and verdicts
7869 * makes it difficult to decide to continue or stop
7870 * the evaluation.
7871 */
7872
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007873 if (txn->flags & TX_SVDENY)
7874 break;
7875
Willy Tarreau3d300592007-03-18 18:34:41 +01007876 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007877 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7878 exp->action == ACT_PASS)) {
7879 exp = exp->next;
7880 continue;
7881 }
7882
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007883 /* if this filter had a condition, evaluate it now and skip to
7884 * next filter if the condition does not match.
7885 */
7886 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007887 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007888 ret = acl_pass(ret);
7889 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7890 ret = !ret;
7891 if (!ret)
7892 continue;
7893 }
7894
Willy Tarreaua15645d2007-03-18 16:22:39 +01007895 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007896 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007897 if (unlikely(ret < 0))
7898 return -1;
7899
7900 if (likely(ret == 0)) {
7901 /* The filter did not match the response, it can be
7902 * iterated through all headers.
7903 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007904 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7905 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007906 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007907 }
7908 return 0;
7909}
7910
7911
Willy Tarreaua15645d2007-03-18 16:22:39 +01007912/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007913 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007914 * desirable to call it only when needed. This function is also used when we
7915 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007916 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007917void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007918{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007919 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007920 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007921 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007922 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007923 char *hdr_beg, *hdr_end, *hdr_next;
7924 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007925
Willy Tarreaua15645d2007-03-18 16:22:39 +01007926 /* Iterate through the headers.
7927 * we start with the start line.
7928 */
7929 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007930 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007931
7932 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7933 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007934 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007935
7936 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007937 hdr_beg = hdr_next;
7938 hdr_end = hdr_beg + cur_hdr->len;
7939 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007940
Willy Tarreau24581ba2010-08-31 22:39:35 +02007941 /* We have one full header between hdr_beg and hdr_end, and the
7942 * next header starts at hdr_next. We're only interested in
7943 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007944 */
7945
Willy Tarreau24581ba2010-08-31 22:39:35 +02007946 is_cookie2 = 0;
7947 prev = hdr_beg + 10;
7948 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007949 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007950 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7951 if (!val) {
7952 old_idx = cur_idx;
7953 continue;
7954 }
7955 is_cookie2 = 1;
7956 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007957 }
7958
Willy Tarreau24581ba2010-08-31 22:39:35 +02007959 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7960 * <prev> points to the colon.
7961 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007962 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007963
Willy Tarreau24581ba2010-08-31 22:39:35 +02007964 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7965 * check-cache is enabled) and we are not interested in checking
7966 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007967 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007968 if (s->be->cookie_name == NULL &&
7969 s->be->appsession_name == NULL &&
7970 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007971 return;
7972
Willy Tarreau24581ba2010-08-31 22:39:35 +02007973 /* OK so now we know we have to process this response cookie.
7974 * The format of the Set-Cookie header is slightly different
7975 * from the format of the Cookie header in that it does not
7976 * support the comma as a cookie delimiter (thus the header
7977 * cannot be folded) because the Expires attribute described in
7978 * the original Netscape's spec may contain an unquoted date
7979 * with a comma inside. We have to live with this because
7980 * many browsers don't support Max-Age and some browsers don't
7981 * support quoted strings. However the Set-Cookie2 header is
7982 * clean.
7983 *
7984 * We have to keep multiple pointers in order to support cookie
7985 * removal at the beginning, middle or end of header without
7986 * corrupting the header (in case of set-cookie2). A special
7987 * pointer, <scav> points to the beginning of the set-cookie-av
7988 * fields after the first semi-colon. The <next> pointer points
7989 * either to the end of line (set-cookie) or next unquoted comma
7990 * (set-cookie2). All of these headers are valid :
7991 *
7992 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7993 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7994 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7995 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7996 * | | | | | | | | | |
7997 * | | | | | | | | +-> next hdr_end <--+
7998 * | | | | | | | +------------> scav
7999 * | | | | | | +--------------> val_end
8000 * | | | | | +--------------------> val_beg
8001 * | | | | +----------------------> equal
8002 * | | | +------------------------> att_end
8003 * | | +----------------------------> att_beg
8004 * | +------------------------------> prev
8005 * +-----------------------------------------> hdr_beg
8006 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008007
Willy Tarreau24581ba2010-08-31 22:39:35 +02008008 for (; prev < hdr_end; prev = next) {
8009 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008010
Willy Tarreau24581ba2010-08-31 22:39:35 +02008011 /* find att_beg */
8012 att_beg = prev + 1;
8013 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8014 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008015
Willy Tarreau24581ba2010-08-31 22:39:35 +02008016 /* find att_end : this is the first character after the last non
8017 * space before the equal. It may be equal to hdr_end.
8018 */
8019 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008020
Willy Tarreau24581ba2010-08-31 22:39:35 +02008021 while (equal < hdr_end) {
8022 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8023 break;
8024 if (http_is_spht[(unsigned char)*equal++])
8025 continue;
8026 att_end = equal;
8027 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008028
Willy Tarreau24581ba2010-08-31 22:39:35 +02008029 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8030 * is between <att_beg> and <equal>, both may be identical.
8031 */
8032
8033 /* look for end of cookie if there is an equal sign */
8034 if (equal < hdr_end && *equal == '=') {
8035 /* look for the beginning of the value */
8036 val_beg = equal + 1;
8037 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8038 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008039
Willy Tarreau24581ba2010-08-31 22:39:35 +02008040 /* find the end of the value, respecting quotes */
8041 next = find_cookie_value_end(val_beg, hdr_end);
8042
8043 /* make val_end point to the first white space or delimitor after the value */
8044 val_end = next;
8045 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8046 val_end--;
8047 } else {
8048 /* <equal> points to next comma, semi-colon or EOL */
8049 val_beg = val_end = next = equal;
8050 }
8051
8052 if (next < hdr_end) {
8053 /* Set-Cookie2 supports multiple cookies, and <next> points to
8054 * a colon or semi-colon before the end. So skip all attr-value
8055 * pairs and look for the next comma. For Set-Cookie, since
8056 * commas are permitted in values, skip to the end.
8057 */
8058 if (is_cookie2)
8059 next = find_hdr_value_end(next, hdr_end);
8060 else
8061 next = hdr_end;
8062 }
8063
8064 /* Now everything is as on the diagram above */
8065
8066 /* Ignore cookies with no equal sign */
8067 if (equal == val_end)
8068 continue;
8069
8070 /* If there are spaces around the equal sign, we need to
8071 * strip them otherwise we'll get trouble for cookie captures,
8072 * or even for rewrites. Since this happens extremely rarely,
8073 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008074 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008075 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8076 int stripped_before = 0;
8077 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008078
Willy Tarreau24581ba2010-08-31 22:39:35 +02008079 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008080 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008081 equal += stripped_before;
8082 val_beg += stripped_before;
8083 }
8084
8085 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008086 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008087 val_beg += stripped_after;
8088 stripped_before += stripped_after;
8089 }
8090
8091 val_end += stripped_before;
8092 next += stripped_before;
8093 hdr_end += stripped_before;
8094 hdr_next += stripped_before;
8095 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008096 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008097 }
8098
8099 /* First, let's see if we want to capture this cookie. We check
8100 * that we don't already have a server side cookie, because we
8101 * can only capture one. Also as an optimisation, we ignore
8102 * cookies shorter than the declared name.
8103 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008104 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008105 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008106 (val_end - att_beg >= s->fe->capture_namelen) &&
8107 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008108 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008109 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008110 Alert("HTTP logging : out of memory.\n");
8111 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008112 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008113 if (log_len > s->fe->capture_len)
8114 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008115 memcpy(txn->srv_cookie, att_beg, log_len);
8116 txn->srv_cookie[log_len] = 0;
8117 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008118 }
8119
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008120 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008121 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008122 if (!(s->flags & SN_IGNORE_PRST) &&
8123 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8124 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008125 /* assume passive cookie by default */
8126 txn->flags &= ~TX_SCK_MASK;
8127 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008128
8129 /* If the cookie is in insert mode on a known server, we'll delete
8130 * this occurrence because we'll insert another one later.
8131 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008132 * a direct access.
8133 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008134 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008135 /* The "preserve" flag was set, we don't want to touch the
8136 * server's cookie.
8137 */
8138 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008139 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8140 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008141 /* this cookie must be deleted */
8142 if (*prev == ':' && next == hdr_end) {
8143 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008144 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008145 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8146 txn->hdr_idx.used--;
8147 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008148 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008149 hdr_next += delta;
8150 http_msg_move_end(&txn->rsp, delta);
8151 /* note: while both invalid now, <next> and <hdr_end>
8152 * are still equal, so the for() will stop as expected.
8153 */
8154 } else {
8155 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008156 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008157 next = prev;
8158 hdr_end += delta;
8159 hdr_next += delta;
8160 cur_hdr->len += delta;
8161 http_msg_move_end(&txn->rsp, delta);
8162 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008163 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008164 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008165 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008166 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008167 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008168 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008169 * with this server since we know it.
8170 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008171 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008172 next += delta;
8173 hdr_end += delta;
8174 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008175 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008176 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008177
Willy Tarreauf1348312010-10-07 15:54:11 +02008178 txn->flags &= ~TX_SCK_MASK;
8179 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008180 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008181 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008182 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008183 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008184 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008185 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008186 next += delta;
8187 hdr_end += delta;
8188 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008189 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008190 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008191
Willy Tarreau827aee92011-03-10 16:55:02 +01008192 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008193 txn->flags &= ~TX_SCK_MASK;
8194 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008195 }
8196 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008197 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008198 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008199 int cmp_len, value_len;
8200 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008201
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008202 if (s->be->options2 & PR_O2_AS_PFX) {
8203 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8204 value_begin = att_beg + s->be->appsession_name_len;
8205 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008206 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008207 cmp_len = att_end - att_beg;
8208 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008209 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008210 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008211
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008212 if ((cmp_len == s->be->appsession_name_len) &&
8213 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008214 /* free a possibly previously allocated memory */
8215 pool_free2(apools.sessid, txn->sessid);
8216
Cyril Bontéb21570a2009-11-29 20:04:48 +01008217 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008218 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008219 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008220 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008221 return;
8222 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008223 memcpy(txn->sessid, value_begin, value_len);
8224 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008225 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008226 }
8227 /* that's done for this cookie, check the next one on the same
8228 * line when next != hdr_end (only if is_cookie2).
8229 */
8230 }
8231 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008232 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008233 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008234
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008235 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008236 appsess *asession = NULL;
8237 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008238 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008239 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008240 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008241 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8242 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008243 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008244 return;
8245 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008246 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8247
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008248 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8249 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008250 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8251 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008252 return;
8253 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008254 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8255 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008256
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008257 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008258 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008259 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008260 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8261 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008262 return;
8263 }
8264 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008265 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008266
8267 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008268 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008269 }
8270
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008271 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008272 asession->request_count++;
8273 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008274}
8275
8276
Willy Tarreaua15645d2007-03-18 16:22:39 +01008277/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008278 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008279 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008280void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008281{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008282 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008283 char *p1, *p2;
8284
8285 char *cur_ptr, *cur_end, *cur_next;
8286 int cur_idx;
8287
Willy Tarreau5df51872007-11-25 16:20:08 +01008288 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008289 return;
8290
8291 /* Iterate through the headers.
8292 * we start with the start line.
8293 */
8294 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008295 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008296
8297 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8298 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008299 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008300
8301 cur_hdr = &txn->hdr_idx.v[cur_idx];
8302 cur_ptr = cur_next;
8303 cur_end = cur_ptr + cur_hdr->len;
8304 cur_next = cur_end + cur_hdr->cr + 1;
8305
8306 /* We have one full header between cur_ptr and cur_end, and the
8307 * next header starts at cur_next. We're only interested in
8308 * "Cookie:" headers.
8309 */
8310
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008311 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8312 if (val) {
8313 if ((cur_end - (cur_ptr + val) >= 8) &&
8314 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8315 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8316 return;
8317 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008318 }
8319
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008320 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8321 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008322 continue;
8323
8324 /* OK, right now we know we have a cache-control header at cur_ptr */
8325
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008326 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008327
8328 if (p1 >= cur_end) /* no more info */
8329 continue;
8330
8331 /* p1 is at the beginning of the value */
8332 p2 = p1;
8333
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008334 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008335 p2++;
8336
8337 /* we have a complete value between p1 and p2 */
8338 if (p2 < cur_end && *p2 == '=') {
8339 /* we have something of the form no-cache="set-cookie" */
8340 if ((cur_end - p1 >= 21) &&
8341 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8342 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008343 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008344 continue;
8345 }
8346
8347 /* OK, so we know that either p2 points to the end of string or to a comma */
8348 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008349 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008350 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8351 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8352 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008353 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008354 return;
8355 }
8356
8357 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008358 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008359 continue;
8360 }
8361 }
8362}
8363
8364
Willy Tarreau58f10d72006-12-04 02:26:12 +01008365/*
8366 * Try to retrieve a known appsession in the URI, then the associated server.
8367 * If the server is found, it's assigned to the session.
8368 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008369void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008370{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008371 char *end_params, *first_param, *cur_param, *next_param;
8372 char separator;
8373 int value_len;
8374
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008375 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008376
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008377 if (s->be->appsession_name == NULL ||
8378 (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 +01008379 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008380 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008381
Cyril Bontéb21570a2009-11-29 20:04:48 +01008382 first_param = NULL;
8383 switch (mode) {
8384 case PR_O2_AS_M_PP:
8385 first_param = memchr(begin, ';', len);
8386 break;
8387 case PR_O2_AS_M_QS:
8388 first_param = memchr(begin, '?', len);
8389 break;
8390 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008391
Cyril Bontéb21570a2009-11-29 20:04:48 +01008392 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008393 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008394 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008395
Cyril Bontéb21570a2009-11-29 20:04:48 +01008396 switch (mode) {
8397 case PR_O2_AS_M_PP:
8398 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8399 end_params = (char *) begin + len;
8400 }
8401 separator = ';';
8402 break;
8403 case PR_O2_AS_M_QS:
8404 end_params = (char *) begin + len;
8405 separator = '&';
8406 break;
8407 default:
8408 /* unknown mode, shouldn't happen */
8409 return;
8410 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008411
Cyril Bontéb21570a2009-11-29 20:04:48 +01008412 cur_param = next_param = end_params;
8413 while (cur_param > first_param) {
8414 cur_param--;
8415 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8416 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008417 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8418 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8419 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008420 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008421 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8422 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008423 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008424 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008425 }
8426 break;
8427 }
8428 next_param = cur_param;
8429 }
8430 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008431#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008432 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008433 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008434#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008435}
8436
Willy Tarreaub2513902006-12-17 14:52:38 +01008437/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008438 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008439 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008440 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008441 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008442 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008443 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008444 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008445 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008446int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008447{
8448 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008449 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008450 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008451
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008452 if (!uri_auth)
8453 return 0;
8454
Cyril Bonté70be45d2010-10-12 00:14:35 +02008455 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008456 return 0;
8457
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008458 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008459 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008460 return 0;
8461
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008462 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008463 return 0;
8464
Willy Tarreaub2513902006-12-17 14:52:38 +01008465 return 1;
8466}
8467
Willy Tarreau4076a152009-04-02 15:18:36 +02008468/*
8469 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008470 * By default it tries to report the error position as msg->err_pos. However if
8471 * this one is not set, it will then report msg->next, which is the last known
8472 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008473 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008474 */
8475void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008476 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008477 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008478{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008479 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008480 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008481
Willy Tarreau9b28e032012-10-12 23:49:43 +02008482 es->len = MIN(chn->buf->i, sizeof(es->buf));
8483 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008484 len1 = MIN(len1, es->len);
8485 len2 = es->len - len1; /* remaining data if buffer wraps */
8486
Willy Tarreau9b28e032012-10-12 23:49:43 +02008487 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008488 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008489 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008490
Willy Tarreau4076a152009-04-02 15:18:36 +02008491 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008492 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008493 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008494 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008495
Willy Tarreau4076a152009-04-02 15:18:36 +02008496 es->when = date; // user-visible date
8497 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008498 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008499 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008500 if (objt_conn(s->req->prod->end))
8501 es->src = __objt_conn(s->req->prod->end)->addr.from;
8502 else
8503 memset(&es->src, 0, sizeof(es->src));
8504
Willy Tarreau078272e2010-12-12 12:46:33 +01008505 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008506 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008507 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008508 es->s_flags = s->flags;
8509 es->t_flags = s->txn.flags;
8510 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008511 es->b_out = chn->buf->o;
8512 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008513 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008514 es->m_clen = msg->chunk_len;
8515 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008516}
Willy Tarreaub2513902006-12-17 14:52:38 +01008517
Willy Tarreau294c4732011-12-16 21:35:50 +01008518/* 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
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008525 * -1. The value fetch stops at commas, so this function is suited for use with
8526 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008527 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008528 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008529unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008530 struct hdr_idx *idx, int occ,
8531 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008532{
Willy Tarreau294c4732011-12-16 21:35:50 +01008533 struct hdr_ctx local_ctx;
8534 char *ptr_hist[MAX_HDR_HISTORY];
8535 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008536 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008537 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008538
Willy Tarreau294c4732011-12-16 21:35:50 +01008539 if (!ctx) {
8540 local_ctx.idx = 0;
8541 ctx = &local_ctx;
8542 }
8543
Willy Tarreaubce70882009-09-07 11:51:47 +02008544 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008545 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008546 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008547 occ--;
8548 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008549 *vptr = ctx->line + ctx->val;
8550 *vlen = ctx->vlen;
8551 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008552 }
8553 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008554 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008555 }
8556
8557 /* negative occurrence, we scan all the list then walk back */
8558 if (-occ > MAX_HDR_HISTORY)
8559 return 0;
8560
Willy Tarreau294c4732011-12-16 21:35:50 +01008561 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008562 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008563 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8564 len_hist[hist_ptr] = ctx->vlen;
8565 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008566 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
Willy Tarreau67dad272013-06-12 22:27:44 +02008572 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8573 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8574 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008575 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008576 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008577 if (hist_ptr >= MAX_HDR_HISTORY)
8578 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008579 *vptr = ptr_hist[hist_ptr];
8580 *vlen = len_hist[hist_ptr];
8581 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008582}
8583
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008584/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8585 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8586 * performed over the whole headers. Otherwise it must contain a valid header
8587 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8588 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8589 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8590 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8591 * -1. This function differs from http_get_hdr() in that it only returns full
8592 * line header values and does not stop at commas.
8593 * The return value is 0 if nothing was found, or non-zero otherwise.
8594 */
8595unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8596 struct hdr_idx *idx, int occ,
8597 struct hdr_ctx *ctx, char **vptr, int *vlen)
8598{
8599 struct hdr_ctx local_ctx;
8600 char *ptr_hist[MAX_HDR_HISTORY];
8601 int len_hist[MAX_HDR_HISTORY];
8602 unsigned int hist_ptr;
8603 int found;
8604
8605 if (!ctx) {
8606 local_ctx.idx = 0;
8607 ctx = &local_ctx;
8608 }
8609
8610 if (occ >= 0) {
8611 /* search from the beginning */
8612 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8613 occ--;
8614 if (occ <= 0) {
8615 *vptr = ctx->line + ctx->val;
8616 *vlen = ctx->vlen;
8617 return 1;
8618 }
8619 }
8620 return 0;
8621 }
8622
8623 /* negative occurrence, we scan all the list then walk back */
8624 if (-occ > MAX_HDR_HISTORY)
8625 return 0;
8626
8627 found = hist_ptr = 0;
8628 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8629 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8630 len_hist[hist_ptr] = ctx->vlen;
8631 if (++hist_ptr >= MAX_HDR_HISTORY)
8632 hist_ptr = 0;
8633 found++;
8634 }
8635 if (-occ > found)
8636 return 0;
8637 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8638 * find occurrence -occ, so we have to check [hist_ptr+occ].
8639 */
8640 hist_ptr += occ;
8641 if (hist_ptr >= MAX_HDR_HISTORY)
8642 hist_ptr -= MAX_HDR_HISTORY;
8643 *vptr = ptr_hist[hist_ptr];
8644 *vlen = len_hist[hist_ptr];
8645 return 1;
8646}
8647
Willy Tarreaubaaee002006-06-26 02:48:02 +02008648/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008649 * Print a debug line with a header. Always stop at the first CR or LF char,
8650 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8651 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008652 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008653void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008654{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008655 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008656 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008657 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008658 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8659 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 +02008660
8661 for (max = 0; start + max < end; max++)
8662 if (start[max] == '\r' || start[max] == '\n')
8663 break;
8664
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008665 UBOUND(max, trash.size - trash.len - 3);
8666 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8667 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008668 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008669}
8670
Willy Tarreau0937bc42009-12-22 15:03:09 +01008671/*
8672 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8673 * the required fields are properly allocated and that we only need to (re)init
8674 * them. This should be used before processing any new request.
8675 */
8676void http_init_txn(struct session *s)
8677{
8678 struct http_txn *txn = &s->txn;
8679 struct proxy *fe = s->fe;
8680
8681 txn->flags = 0;
8682 txn->status = -1;
8683
Willy Tarreauf64d1412010-10-07 20:06:11 +02008684 txn->cookie_first_date = 0;
8685 txn->cookie_last_date = 0;
8686
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008687 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008688 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008689 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008690 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008691 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008692 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008693 txn->req.chunk_len = 0LL;
8694 txn->req.body_len = 0LL;
8695 txn->rsp.chunk_len = 0LL;
8696 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008697 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8698 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008699 txn->req.chn = s->req;
8700 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008701
8702 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008703
8704 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8705 if (fe->options2 & PR_O2_REQBUG_OK)
8706 txn->req.err_pos = -1; /* let buggy requests pass */
8707
Willy Tarreau46023632010-01-07 22:51:47 +01008708 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008709 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8710
Willy Tarreau46023632010-01-07 22:51:47 +01008711 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008712 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8713
8714 if (txn->hdr_idx.v)
8715 hdr_idx_init(&txn->hdr_idx);
8716}
8717
8718/* to be used at the end of a transaction */
8719void http_end_txn(struct session *s)
8720{
8721 struct http_txn *txn = &s->txn;
8722
Willy Tarreau75195602014-03-11 15:48:55 +01008723 /* release any possible compression context */
8724 if (s->flags & SN_COMP_READY)
8725 s->comp_algo->end(&s->comp_ctx);
8726 s->comp_algo = NULL;
8727 s->flags &= ~SN_COMP_READY;
8728
Willy Tarreau0937bc42009-12-22 15:03:09 +01008729 /* these ones will have been dynamically allocated */
8730 pool_free2(pool2_requri, txn->uri);
8731 pool_free2(pool2_capture, txn->cli_cookie);
8732 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008733 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008734 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008735
William Lallemanda73203e2012-03-12 12:48:57 +01008736 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008737 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008738 txn->uri = NULL;
8739 txn->srv_cookie = NULL;
8740 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008741
8742 if (txn->req.cap) {
8743 struct cap_hdr *h;
8744 for (h = s->fe->req_cap; h; h = h->next)
8745 pool_free2(h->pool, txn->req.cap[h->index]);
8746 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8747 }
8748
8749 if (txn->rsp.cap) {
8750 struct cap_hdr *h;
8751 for (h = s->fe->rsp_cap; h; h = h->next)
8752 pool_free2(h->pool, txn->rsp.cap[h->index]);
8753 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8754 }
8755
Willy Tarreau0937bc42009-12-22 15:03:09 +01008756}
8757
8758/* to be used at the end of a transaction to prepare a new one */
8759void http_reset_txn(struct session *s)
8760{
8761 http_end_txn(s);
8762 http_init_txn(s);
8763
8764 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008765 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008766 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008767 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008768 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008769 /* re-init store persistence */
8770 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008771 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008772
Willy Tarreau0937bc42009-12-22 15:03:09 +01008773 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008774
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008775 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008776
Willy Tarreau739cfba2010-01-25 23:11:14 +01008777 /* We must trim any excess data from the response buffer, because we
8778 * may have blocked an invalid response from a server that we don't
8779 * want to accidentely forward once we disable the analysers, nor do
8780 * we want those data to come along with next response. A typical
8781 * example of such data would be from a buggy server responding to
8782 * a HEAD with some data, or sending more than the advertised
8783 * content-length.
8784 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008785 if (unlikely(s->rep->buf->i))
8786 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008787
Willy Tarreau0937bc42009-12-22 15:03:09 +01008788 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008789 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008790
Willy Tarreaud04e8582010-05-31 12:31:35 +02008791 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008792 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008793
8794 s->req->rex = TICK_ETERNITY;
8795 s->req->wex = TICK_ETERNITY;
8796 s->req->analyse_exp = TICK_ETERNITY;
8797 s->rep->rex = TICK_ETERNITY;
8798 s->rep->wex = TICK_ETERNITY;
8799 s->rep->analyse_exp = TICK_ETERNITY;
8800}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008801
Sasha Pachev218f0642014-06-16 12:05:59 -06008802void free_http_res_rules(struct list *r)
8803{
8804 struct http_res_rule *tr, *pr;
8805
8806 list_for_each_entry_safe(pr, tr, r, list) {
8807 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008808 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008809 free(pr);
8810 }
8811}
8812
8813void free_http_req_rules(struct list *r)
8814{
Willy Tarreauff011f22011-01-06 17:51:27 +01008815 struct http_req_rule *tr, *pr;
8816
8817 list_for_each_entry_safe(pr, tr, r, list) {
8818 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008819 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008820 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008821
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008822 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008823 free(pr);
8824 }
8825}
8826
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008827/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008828struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8829{
8830 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008831 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008832 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008833 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008834
8835 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8836 if (!rule) {
8837 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008838 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008839 }
8840
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008841 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008842 rule->action = HTTP_REQ_ACT_ALLOW;
8843 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008844 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008845 rule->action = HTTP_REQ_ACT_DENY;
8846 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008847 } else if (!strcmp(args[0], "tarpit")) {
8848 rule->action = HTTP_REQ_ACT_TARPIT;
8849 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008850 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008851 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008852 cur_arg = 1;
8853
8854 while(*args[cur_arg]) {
8855 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008856 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008857 cur_arg+=2;
8858 continue;
8859 } else
8860 break;
8861 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008862 } else if (!strcmp(args[0], "set-nice")) {
8863 rule->action = HTTP_REQ_ACT_SET_NICE;
8864 cur_arg = 1;
8865
8866 if (!*args[cur_arg] ||
8867 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8868 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8869 file, linenum, args[0]);
8870 goto out_err;
8871 }
8872 rule->arg.nice = atoi(args[cur_arg]);
8873 if (rule->arg.nice < -1024)
8874 rule->arg.nice = -1024;
8875 else if (rule->arg.nice > 1024)
8876 rule->arg.nice = 1024;
8877 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008878 } else if (!strcmp(args[0], "set-tos")) {
8879#ifdef IP_TOS
8880 char *err;
8881 rule->action = HTTP_REQ_ACT_SET_TOS;
8882 cur_arg = 1;
8883
8884 if (!*args[cur_arg] ||
8885 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8886 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8887 file, linenum, args[0]);
8888 goto out_err;
8889 }
8890
8891 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8892 if (err && *err != '\0') {
8893 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8894 file, linenum, err, args[0]);
8895 goto out_err;
8896 }
8897 cur_arg++;
8898#else
8899 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8900 goto out_err;
8901#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008902 } else if (!strcmp(args[0], "set-mark")) {
8903#ifdef SO_MARK
8904 char *err;
8905 rule->action = HTTP_REQ_ACT_SET_MARK;
8906 cur_arg = 1;
8907
8908 if (!*args[cur_arg] ||
8909 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8910 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8911 file, linenum, args[0]);
8912 goto out_err;
8913 }
8914
8915 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8916 if (err && *err != '\0') {
8917 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8918 file, linenum, err, args[0]);
8919 goto out_err;
8920 }
8921 cur_arg++;
8922 global.last_checks |= LSTCHK_NETADM;
8923#else
8924 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8925 goto out_err;
8926#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008927 } else if (!strcmp(args[0], "set-log-level")) {
8928 rule->action = HTTP_REQ_ACT_SET_LOGL;
8929 cur_arg = 1;
8930
8931 if (!*args[cur_arg] ||
8932 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8933 bad_log_level:
8934 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8935 file, linenum, args[0]);
8936 goto out_err;
8937 }
8938 if (strcmp(args[cur_arg], "silent") == 0)
8939 rule->arg.loglevel = -1;
8940 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8941 goto bad_log_level;
8942 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008943 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8944 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8945 cur_arg = 1;
8946
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008947 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8948 (*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 +01008949 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8950 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008951 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008952 }
8953
8954 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8955 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8956 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008957
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008958 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008959 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008960 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8961 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008962 free(proxy->conf.lfs_file);
8963 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8964 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008965 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02008966 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
8967 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06008968 cur_arg = 1;
8969
8970 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann92df3702014-06-24 11:10:00 +02008971 (*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 -06008972 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
8973 file, linenum, args[0]);
8974 goto out_err;
8975 }
8976
8977 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8978 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8979 LIST_INIT(&rule->arg.hdr_add.fmt);
8980
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008981 error = NULL;
8982 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
8983 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
8984 args[cur_arg + 1], error);
8985 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06008986 goto out_err;
8987 }
8988
8989 proxy->conf.args.ctx = ARGC_HRQ;
8990 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
8991 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8992 file, linenum);
8993
8994 free(proxy->conf.lfs_file);
8995 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8996 proxy->conf.lfs_line = proxy->conf.args.line;
8997 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008998 } else if (strcmp(args[0], "del-header") == 0) {
8999 rule->action = HTTP_REQ_ACT_DEL_HDR;
9000 cur_arg = 1;
9001
9002 if (!*args[cur_arg] ||
9003 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9004 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9005 file, linenum, args[0]);
9006 goto out_err;
9007 }
9008
9009 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9010 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9011
9012 proxy->conf.args.ctx = ARGC_HRQ;
9013 free(proxy->conf.lfs_file);
9014 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9015 proxy->conf.lfs_line = proxy->conf.args.line;
9016 cur_arg += 1;
Willy Tarreau09448f72014-06-25 18:12:15 +02009017 } else if (strncmp(args[0], "track-sc", 8) == 0 &&
9018 args[0][9] == '\0' && args[0][8] >= '0' &&
Willy Tarreaue1cfc1f2014-10-17 11:53:05 +02009019 args[0][8] < '0' + MAX_SESS_STKCTR) { /* track-sc 0..9 */
Willy Tarreau09448f72014-06-25 18:12:15 +02009020 struct sample_expr *expr;
9021 unsigned int where;
9022 char *err = NULL;
9023
9024 cur_arg = 1;
9025 proxy->conf.args.ctx = ARGC_TRK;
9026
9027 expr = sample_parse_expr((char **)args, &cur_arg, file, linenum, &err, &proxy->conf.args);
9028 if (!expr) {
9029 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9030 file, linenum, proxy_type_str(proxy), proxy->id, args[0], err);
9031 free(err);
9032 goto out_err;
9033 }
9034
9035 where = 0;
9036 if (proxy->cap & PR_CAP_FE)
9037 where |= SMP_VAL_FE_HRQ_HDR;
9038 if (proxy->cap & PR_CAP_BE)
9039 where |= SMP_VAL_BE_HRQ_HDR;
9040
9041 if (!(expr->fetch->val & where)) {
9042 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule :"
9043 " fetch method '%s' extracts information from '%s', none of which is available here.\n",
9044 file, linenum, proxy_type_str(proxy), proxy->id, args[0],
9045 args[cur_arg-1], sample_src_names(expr->fetch->use));
9046 free(expr);
9047 goto out_err;
9048 }
9049
9050 if (strcmp(args[cur_arg], "table") == 0) {
9051 cur_arg++;
9052 if (!args[cur_arg]) {
9053 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : missing table name.\n",
9054 file, linenum, proxy_type_str(proxy), proxy->id, args[0]);
9055 free(expr);
9056 goto out_err;
9057 }
9058 /* we copy the table name for now, it will be resolved later */
9059 rule->act_prm.trk_ctr.table.n = strdup(args[cur_arg]);
9060 cur_arg++;
9061 }
9062 rule->act_prm.trk_ctr.expr = expr;
9063 rule->action = HTTP_REQ_ACT_TRK_SC0 + args[0][8] - '0';
Willy Tarreau81499eb2012-12-27 12:19:02 +01009064 } else if (strcmp(args[0], "redirect") == 0) {
9065 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009066 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009067
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009068 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009069 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9070 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9071 goto out_err;
9072 }
9073
9074 /* this redirect rule might already contain a parsed condition which
9075 * we'll pass to the http-request rule.
9076 */
9077 rule->action = HTTP_REQ_ACT_REDIR;
9078 rule->arg.redir = redir;
9079 rule->cond = redir->cond;
9080 redir->cond = NULL;
9081 cur_arg = 2;
9082 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009083 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9084 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9085 rule->action = HTTP_REQ_ACT_ADD_ACL;
9086 /*
9087 * '+ 8' for 'add-acl('
9088 * '- 9' for 'add-acl(' + trailing ')'
9089 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009090 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009091
9092 cur_arg = 1;
9093
9094 if (!*args[cur_arg] ||
9095 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9096 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9097 file, linenum, args[0]);
9098 goto out_err;
9099 }
9100
9101 LIST_INIT(&rule->arg.map.key);
9102 proxy->conf.args.ctx = ARGC_HRQ;
9103 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9104 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9105 file, linenum);
9106 free(proxy->conf.lfs_file);
9107 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9108 proxy->conf.lfs_line = proxy->conf.args.line;
9109 cur_arg += 1;
9110 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9111 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9112 rule->action = HTTP_REQ_ACT_DEL_ACL;
9113 /*
9114 * '+ 8' for 'del-acl('
9115 * '- 9' for 'del-acl(' + trailing ')'
9116 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009117 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009118
9119 cur_arg = 1;
9120
9121 if (!*args[cur_arg] ||
9122 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9123 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9124 file, linenum, args[0]);
9125 goto out_err;
9126 }
9127
9128 LIST_INIT(&rule->arg.map.key);
9129 proxy->conf.args.ctx = ARGC_HRQ;
9130 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9131 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9132 file, linenum);
9133 free(proxy->conf.lfs_file);
9134 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9135 proxy->conf.lfs_line = proxy->conf.args.line;
9136 cur_arg += 1;
9137 } else if (strncmp(args[0], "del-map", 7) == 0) {
9138 /* http-request del-map(<reference (map name)>) <key pattern> */
9139 rule->action = HTTP_REQ_ACT_DEL_MAP;
9140 /*
9141 * '+ 8' for 'del-map('
9142 * '- 9' for 'del-map(' + trailing ')'
9143 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009144 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009145
9146 cur_arg = 1;
9147
9148 if (!*args[cur_arg] ||
9149 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9150 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9151 file, linenum, args[0]);
9152 goto out_err;
9153 }
9154
9155 LIST_INIT(&rule->arg.map.key);
9156 proxy->conf.args.ctx = ARGC_HRQ;
9157 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9158 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9159 file, linenum);
9160 free(proxy->conf.lfs_file);
9161 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9162 proxy->conf.lfs_line = proxy->conf.args.line;
9163 cur_arg += 1;
9164 } else if (strncmp(args[0], "set-map", 7) == 0) {
9165 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9166 rule->action = HTTP_REQ_ACT_SET_MAP;
9167 /*
9168 * '+ 8' for 'set-map('
9169 * '- 9' for 'set-map(' + trailing ')'
9170 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009171 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009172
9173 cur_arg = 1;
9174
9175 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9176 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9177 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9178 file, linenum, args[0]);
9179 goto out_err;
9180 }
9181
9182 LIST_INIT(&rule->arg.map.key);
9183 LIST_INIT(&rule->arg.map.value);
9184 proxy->conf.args.ctx = ARGC_HRQ;
9185
9186 /* key pattern */
9187 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9188 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9189 file, linenum);
9190
9191 /* value pattern */
9192 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9193 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9194 file, linenum);
9195 free(proxy->conf.lfs_file);
9196 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9197 proxy->conf.lfs_line = proxy->conf.args.line;
9198
9199 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009200 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9201 char *errmsg = NULL;
9202 cur_arg = 1;
9203 /* try in the module list */
9204 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9205 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9206 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9207 free(errmsg);
9208 goto out_err;
9209 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009210 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009211 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 +01009212 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009213 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009214 }
9215
9216 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9217 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009218 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009219
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009220 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9221 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9222 file, linenum, args[0], errmsg);
9223 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009224 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009225 }
9226 rule->cond = cond;
9227 }
9228 else if (*args[cur_arg]) {
9229 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9230 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9231 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009232 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009233 }
9234
9235 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009236 out_err:
9237 free(rule);
9238 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009239}
9240
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009241/* parse an "http-respose" rule */
9242struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9243{
9244 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009245 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009246 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009247 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009248
9249 rule = calloc(1, sizeof(*rule));
9250 if (!rule) {
9251 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9252 goto out_err;
9253 }
9254
9255 if (!strcmp(args[0], "allow")) {
9256 rule->action = HTTP_RES_ACT_ALLOW;
9257 cur_arg = 1;
9258 } else if (!strcmp(args[0], "deny")) {
9259 rule->action = HTTP_RES_ACT_DENY;
9260 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009261 } else if (!strcmp(args[0], "set-nice")) {
9262 rule->action = HTTP_RES_ACT_SET_NICE;
9263 cur_arg = 1;
9264
9265 if (!*args[cur_arg] ||
9266 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9267 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9268 file, linenum, args[0]);
9269 goto out_err;
9270 }
9271 rule->arg.nice = atoi(args[cur_arg]);
9272 if (rule->arg.nice < -1024)
9273 rule->arg.nice = -1024;
9274 else if (rule->arg.nice > 1024)
9275 rule->arg.nice = 1024;
9276 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009277 } else if (!strcmp(args[0], "set-tos")) {
9278#ifdef IP_TOS
9279 char *err;
9280 rule->action = HTTP_RES_ACT_SET_TOS;
9281 cur_arg = 1;
9282
9283 if (!*args[cur_arg] ||
9284 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9285 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9286 file, linenum, args[0]);
9287 goto out_err;
9288 }
9289
9290 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9291 if (err && *err != '\0') {
9292 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9293 file, linenum, err, args[0]);
9294 goto out_err;
9295 }
9296 cur_arg++;
9297#else
9298 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9299 goto out_err;
9300#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009301 } else if (!strcmp(args[0], "set-mark")) {
9302#ifdef SO_MARK
9303 char *err;
9304 rule->action = HTTP_RES_ACT_SET_MARK;
9305 cur_arg = 1;
9306
9307 if (!*args[cur_arg] ||
9308 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9309 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9310 file, linenum, args[0]);
9311 goto out_err;
9312 }
9313
9314 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9315 if (err && *err != '\0') {
9316 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9317 file, linenum, err, args[0]);
9318 goto out_err;
9319 }
9320 cur_arg++;
9321 global.last_checks |= LSTCHK_NETADM;
9322#else
9323 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9324 goto out_err;
9325#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009326 } else if (!strcmp(args[0], "set-log-level")) {
9327 rule->action = HTTP_RES_ACT_SET_LOGL;
9328 cur_arg = 1;
9329
9330 if (!*args[cur_arg] ||
9331 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9332 bad_log_level:
9333 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9334 file, linenum, args[0]);
9335 goto out_err;
9336 }
9337 if (strcmp(args[cur_arg], "silent") == 0)
9338 rule->arg.loglevel = -1;
9339 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9340 goto bad_log_level;
9341 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009342 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9343 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9344 cur_arg = 1;
9345
9346 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9347 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9348 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9349 file, linenum, args[0]);
9350 goto out_err;
9351 }
9352
9353 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9354 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9355 LIST_INIT(&rule->arg.hdr_add.fmt);
9356
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009357 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009358 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009359 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9360 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009361 free(proxy->conf.lfs_file);
9362 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9363 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009364 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009365 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009366 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009367 cur_arg = 1;
9368
9369 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann12cb00b2014-08-08 17:29:06 +02009370 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9371 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009372 file, linenum, args[0]);
9373 goto out_err;
9374 }
9375
9376 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9377 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9378 LIST_INIT(&rule->arg.hdr_add.fmt);
9379
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009380 error = NULL;
9381 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9382 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9383 args[cur_arg + 1], error);
9384 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009385 goto out_err;
9386 }
9387
9388 proxy->conf.args.ctx = ARGC_HRQ;
9389 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9390 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9391 file, linenum);
9392
9393 free(proxy->conf.lfs_file);
9394 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9395 proxy->conf.lfs_line = proxy->conf.args.line;
9396 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009397 } else if (strcmp(args[0], "del-header") == 0) {
9398 rule->action = HTTP_RES_ACT_DEL_HDR;
9399 cur_arg = 1;
9400
9401 if (!*args[cur_arg] ||
9402 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9403 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9404 file, linenum, args[0]);
9405 goto out_err;
9406 }
9407
9408 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9409 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9410
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009411 proxy->conf.args.ctx = ARGC_HRS;
9412 free(proxy->conf.lfs_file);
9413 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9414 proxy->conf.lfs_line = proxy->conf.args.line;
9415 cur_arg += 1;
9416 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9417 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9418 rule->action = HTTP_RES_ACT_ADD_ACL;
9419 /*
9420 * '+ 8' for 'add-acl('
9421 * '- 9' for 'add-acl(' + trailing ')'
9422 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009423 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009424
9425 cur_arg = 1;
9426
9427 if (!*args[cur_arg] ||
9428 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9429 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9430 file, linenum, args[0]);
9431 goto out_err;
9432 }
9433
9434 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009435 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009436 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9437 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9438 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009439 free(proxy->conf.lfs_file);
9440 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9441 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009442
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009443 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009444 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9445 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9446 rule->action = HTTP_RES_ACT_DEL_ACL;
9447 /*
9448 * '+ 8' for 'del-acl('
9449 * '- 9' for 'del-acl(' + trailing ')'
9450 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009451 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009452
9453 cur_arg = 1;
9454
9455 if (!*args[cur_arg] ||
9456 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9457 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9458 file, linenum, args[0]);
9459 goto out_err;
9460 }
9461
9462 LIST_INIT(&rule->arg.map.key);
9463 proxy->conf.args.ctx = ARGC_HRS;
9464 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9465 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9466 file, linenum);
9467 free(proxy->conf.lfs_file);
9468 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9469 proxy->conf.lfs_line = proxy->conf.args.line;
9470 cur_arg += 1;
9471 } else if (strncmp(args[0], "del-map", 7) == 0) {
9472 /* http-response del-map(<reference (map name)>) <key pattern> */
9473 rule->action = HTTP_RES_ACT_DEL_MAP;
9474 /*
9475 * '+ 8' for 'del-map('
9476 * '- 9' for 'del-map(' + trailing ')'
9477 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009478 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009479
9480 cur_arg = 1;
9481
9482 if (!*args[cur_arg] ||
9483 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9484 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9485 file, linenum, args[0]);
9486 goto out_err;
9487 }
9488
9489 LIST_INIT(&rule->arg.map.key);
9490 proxy->conf.args.ctx = ARGC_HRS;
9491 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9492 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9493 file, linenum);
9494 free(proxy->conf.lfs_file);
9495 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9496 proxy->conf.lfs_line = proxy->conf.args.line;
9497 cur_arg += 1;
9498 } else if (strncmp(args[0], "set-map", 7) == 0) {
9499 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9500 rule->action = HTTP_RES_ACT_SET_MAP;
9501 /*
9502 * '+ 8' for 'set-map('
9503 * '- 9' for 'set-map(' + trailing ')'
9504 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009505 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009506
9507 cur_arg = 1;
9508
9509 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9510 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9511 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9512 file, linenum, args[0]);
9513 goto out_err;
9514 }
9515
9516 LIST_INIT(&rule->arg.map.key);
9517 LIST_INIT(&rule->arg.map.value);
9518
9519 proxy->conf.args.ctx = ARGC_HRS;
9520
9521 /* key pattern */
9522 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9523 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9524 file, linenum);
9525
9526 /* value pattern */
9527 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9528 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9529 file, linenum);
9530
9531 free(proxy->conf.lfs_file);
9532 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9533 proxy->conf.lfs_line = proxy->conf.args.line;
9534
9535 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009536 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9537 char *errmsg = NULL;
9538 cur_arg = 1;
9539 /* try in the module list */
9540 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9541 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9542 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9543 free(errmsg);
9544 goto out_err;
9545 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009546 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009547 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 +02009548 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9549 goto out_err;
9550 }
9551
9552 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9553 struct acl_cond *cond;
9554 char *errmsg = NULL;
9555
9556 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9557 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9558 file, linenum, args[0], errmsg);
9559 free(errmsg);
9560 goto out_err;
9561 }
9562 rule->cond = cond;
9563 }
9564 else if (*args[cur_arg]) {
9565 Alert("parsing [%s:%d]: 'http-response %s' expects"
9566 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9567 file, linenum, args[0], args[cur_arg]);
9568 goto out_err;
9569 }
9570
9571 return rule;
9572 out_err:
9573 free(rule);
9574 return NULL;
9575}
9576
Willy Tarreau4baae242012-12-27 12:00:31 +01009577/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009578 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9579 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009580 */
9581struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009582 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009583{
9584 struct redirect_rule *rule;
9585 int cur_arg;
9586 int type = REDIRECT_TYPE_NONE;
9587 int code = 302;
9588 const char *destination = NULL;
9589 const char *cookie = NULL;
9590 int cookie_set = 0;
9591 unsigned int flags = REDIRECT_FLAG_NONE;
9592 struct acl_cond *cond = NULL;
9593
9594 cur_arg = 0;
9595 while (*(args[cur_arg])) {
9596 if (strcmp(args[cur_arg], "location") == 0) {
9597 if (!*args[cur_arg + 1])
9598 goto missing_arg;
9599
9600 type = REDIRECT_TYPE_LOCATION;
9601 cur_arg++;
9602 destination = args[cur_arg];
9603 }
9604 else if (strcmp(args[cur_arg], "prefix") == 0) {
9605 if (!*args[cur_arg + 1])
9606 goto missing_arg;
9607
9608 type = REDIRECT_TYPE_PREFIX;
9609 cur_arg++;
9610 destination = args[cur_arg];
9611 }
9612 else if (strcmp(args[cur_arg], "scheme") == 0) {
9613 if (!*args[cur_arg + 1])
9614 goto missing_arg;
9615
9616 type = REDIRECT_TYPE_SCHEME;
9617 cur_arg++;
9618 destination = args[cur_arg];
9619 }
9620 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9621 if (!*args[cur_arg + 1])
9622 goto missing_arg;
9623
9624 cur_arg++;
9625 cookie = args[cur_arg];
9626 cookie_set = 1;
9627 }
9628 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9629 if (!*args[cur_arg + 1])
9630 goto missing_arg;
9631
9632 cur_arg++;
9633 cookie = args[cur_arg];
9634 cookie_set = 0;
9635 }
9636 else if (strcmp(args[cur_arg], "code") == 0) {
9637 if (!*args[cur_arg + 1])
9638 goto missing_arg;
9639
9640 cur_arg++;
9641 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009642 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009643 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009644 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009645 args[cur_arg - 1], args[cur_arg]);
9646 return NULL;
9647 }
9648 }
9649 else if (!strcmp(args[cur_arg],"drop-query")) {
9650 flags |= REDIRECT_FLAG_DROP_QS;
9651 }
9652 else if (!strcmp(args[cur_arg],"append-slash")) {
9653 flags |= REDIRECT_FLAG_APPEND_SLASH;
9654 }
9655 else if (strcmp(args[cur_arg], "if") == 0 ||
9656 strcmp(args[cur_arg], "unless") == 0) {
9657 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9658 if (!cond) {
9659 memprintf(errmsg, "error in condition: %s", *errmsg);
9660 return NULL;
9661 }
9662 break;
9663 }
9664 else {
9665 memprintf(errmsg,
9666 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9667 args[cur_arg]);
9668 return NULL;
9669 }
9670 cur_arg++;
9671 }
9672
9673 if (type == REDIRECT_TYPE_NONE) {
9674 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9675 return NULL;
9676 }
9677
9678 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9679 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009680 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009681
9682 if (!use_fmt) {
9683 /* old-style static redirect rule */
9684 rule->rdr_str = strdup(destination);
9685 rule->rdr_len = strlen(destination);
9686 }
9687 else {
9688 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009689
9690 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9691 * if prefix == "/", we don't want to add anything, otherwise it
9692 * makes it hard for the user to configure a self-redirection.
9693 */
Godbachd9722032014-12-18 15:44:58 +08009694 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009695 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009696 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009697 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9698 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009699 free(curproxy->conf.lfs_file);
9700 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9701 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009702 }
9703 }
9704
Willy Tarreau4baae242012-12-27 12:00:31 +01009705 if (cookie) {
9706 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9707 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9708 */
9709 rule->cookie_len = strlen(cookie);
9710 if (cookie_set) {
9711 rule->cookie_str = malloc(rule->cookie_len + 10);
9712 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9713 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9714 rule->cookie_len += 9;
9715 } else {
9716 rule->cookie_str = malloc(rule->cookie_len + 21);
9717 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9718 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9719 rule->cookie_len += 20;
9720 }
9721 }
9722 rule->type = type;
9723 rule->code = code;
9724 rule->flags = flags;
9725 LIST_INIT(&rule->list);
9726 return rule;
9727
9728 missing_arg:
9729 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9730 return NULL;
9731}
9732
Willy Tarreau8797c062007-05-07 00:55:35 +02009733/************************************************************************/
9734/* The code below is dedicated to ACL parsing and matching */
9735/************************************************************************/
9736
9737
Willy Tarreau14174bc2012-04-16 14:34:04 +02009738/* This function ensures that the prerequisites for an L7 fetch are ready,
9739 * which means that a request or response is ready. If some data is missing,
9740 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009741 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9742 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009743 *
9744 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009745 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9746 * decide whether or not an HTTP message is present ;
9747 * 0 if the requested data cannot be fetched or if it is certain that
9748 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009749 * 1 if an HTTP message is ready
9750 */
9751static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009752smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009753 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009754{
9755 struct http_txn *txn = l7;
9756 struct http_msg *msg = &txn->req;
9757
9758 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9759 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9760 */
9761
9762 if (unlikely(!s || !txn))
9763 return 0;
9764
9765 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009766 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009767
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009768 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009769 if (unlikely(!s->req))
9770 return 0;
9771
Willy Tarreauaae75e32013-03-29 12:31:49 +01009772 /* If the buffer does not leave enough free space at the end,
9773 * we must first realign it.
9774 */
9775 if (s->req->buf->p > s->req->buf->data &&
9776 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9777 buffer_slow_realign(s->req->buf);
9778
Willy Tarreau14174bc2012-04-16 14:34:04 +02009779 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009780 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009781 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009782
9783 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009784 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009785 http_msg_analyzer(msg, &txn->hdr_idx);
9786
9787 /* Still no valid request ? */
9788 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009789 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009790 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009791 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009792 }
9793 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009794 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009795 return 0;
9796 }
9797
9798 /* OK we just got a valid HTTP request. We have some minor
9799 * preparation to perform so that further checks can rely
9800 * on HTTP tests.
9801 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009802
9803 /* If the request was parsed but was too large, we must absolutely
9804 * return an error so that it is not processed. At the moment this
9805 * cannot happen, but if the parsers are to change in the future,
9806 * we want this check to be maintained.
9807 */
9808 if (unlikely(s->req->buf->i + s->req->buf->p >
9809 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9810 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009811 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009812 return 1;
9813 }
9814
Willy Tarreau9b28e032012-10-12 23:49:43 +02009815 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009816 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9817 s->flags |= SN_REDIRECTABLE;
9818
Willy Tarreau506d0502013-07-06 13:29:24 +02009819 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9820 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009821 }
9822
Willy Tarreau506d0502013-07-06 13:29:24 +02009823 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009824 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009825 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009826
9827 /* otherwise everything's ready for the request */
9828 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009829 else {
9830 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009831 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9832 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009833 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009834 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009835 }
9836
9837 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009838 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009839 return 1;
9840}
Willy Tarreau8797c062007-05-07 00:55:35 +02009841
Willy Tarreau6c616e02014-06-25 16:56:41 +02009842/* Note: these functinos *do* modify the sample. Even in case of success, at
9843 * least the type and uint value are modified.
9844 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009845#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009846 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 +02009847
Willy Tarreau24e32d82012-04-23 23:55:44 +02009848#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009849 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 +02009850
Willy Tarreau8797c062007-05-07 00:55:35 +02009851
9852/* 1. Check on METHOD
9853 * We use the pre-parsed method if it is known, and store its number as an
9854 * integer. If it is unknown, we use the pointer and the length.
9855 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009856static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009857{
9858 int len, meth;
9859
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009860 len = strlen(text);
9861 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009862
9863 pattern->val.i = meth;
9864 if (meth == HTTP_METH_OTHER) {
Willy Tarreau912c1192014-08-29 15:15:50 +02009865 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009866 pattern->len = len;
9867 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009868 else {
9869 pattern->ptr.str = NULL;
9870 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009871 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009872 return 1;
9873}
9874
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009875/* This function fetches the method of current HTTP request and stores
9876 * it in the global pattern struct as a chunk. There are two possibilities :
9877 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9878 * in <len> and <ptr> is NULL ;
9879 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9880 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009881 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009882 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009883static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009884smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009885 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009886{
9887 int meth;
9888 struct http_txn *txn = l7;
9889
Willy Tarreau24e32d82012-04-23 23:55:44 +02009890 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009891
Willy Tarreau8797c062007-05-07 00:55:35 +02009892 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009893 smp->type = SMP_T_METH;
9894 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009895 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009896 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9897 /* ensure the indexes are not affected */
9898 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009899 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009900 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9901 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009902 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009903 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009904 return 1;
9905}
9906
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009907/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009908static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009909{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009910 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009911 struct pattern_list *lst;
9912 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009913
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009914 list_for_each_entry(lst, &expr->patterns, list) {
9915 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009916
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009917 /* well-known method */
9918 if (pattern->val.i != HTTP_METH_OTHER) {
9919 if (smp->data.meth.meth == pattern->val.i)
9920 return pattern;
9921 else
9922 continue;
9923 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009924
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009925 /* Other method, we must compare the strings */
9926 if (pattern->len != smp->data.meth.str.len)
9927 continue;
9928
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009929 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreau4de2a942014-08-28 20:42:57 +02009930 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
9931 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009932 return pattern;
9933 }
9934 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009935}
9936
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009937static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009938smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009939 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009940{
9941 struct http_txn *txn = l7;
9942 char *ptr;
9943 int len;
9944
Willy Tarreauc0239e02012-04-16 14:42:55 +02009945 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009946
Willy Tarreau8797c062007-05-07 00:55:35 +02009947 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009948 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009949
9950 while ((len-- > 0) && (*ptr++ != '/'));
9951 if (len <= 0)
9952 return 0;
9953
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009954 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009955 smp->data.str.str = ptr;
9956 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009957
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009958 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009959 return 1;
9960}
9961
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009962static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009963smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009964 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009965{
9966 struct http_txn *txn = l7;
9967 char *ptr;
9968 int len;
9969
Willy Tarreauc0239e02012-04-16 14:42:55 +02009970 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009971
Willy Tarreauf26b2522012-12-14 08:33:14 +01009972 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9973 return 0;
9974
Willy Tarreau8797c062007-05-07 00:55:35 +02009975 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009976 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009977
9978 while ((len-- > 0) && (*ptr++ != '/'));
9979 if (len <= 0)
9980 return 0;
9981
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009982 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009983 smp->data.str.str = ptr;
9984 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009985
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009986 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009987 return 1;
9988}
9989
9990/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009991static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009992smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009993 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009994{
9995 struct http_txn *txn = l7;
9996 char *ptr;
9997 int len;
9998
Willy Tarreauc0239e02012-04-16 14:42:55 +02009999 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010000
Willy Tarreauf26b2522012-12-14 08:33:14 +010010001 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10002 return 0;
10003
Willy Tarreau8797c062007-05-07 00:55:35 +020010004 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010005 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010006
Willy Tarreauf853c462012-04-23 18:53:56 +020010007 smp->type = SMP_T_UINT;
10008 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010009 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010010 return 1;
10011}
10012
10013/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010014static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010015smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010016 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +020010017{
10018 struct http_txn *txn = l7;
10019
Willy Tarreauc0239e02012-04-16 14:42:55 +020010020 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010021
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010022 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010023 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010024 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010025 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010026 return 1;
10027}
10028
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010029static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010030smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010031 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010032{
10033 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010034 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010035
Willy Tarreauc0239e02012-04-16 14:42:55 +020010036 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010037
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010038 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 +020010039 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010040 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010041
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010042 smp->type = SMP_T_IPV4;
10043 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010044 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010045 return 1;
10046}
10047
10048static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010049smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010050 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010051{
10052 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010053 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010054
Willy Tarreauc0239e02012-04-16 14:42:55 +020010055 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010056
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010057 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 +020010058 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10059 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010060
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010061 smp->type = SMP_T_UINT;
10062 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010063 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010064 return 1;
10065}
10066
Willy Tarreau185b5c42012-04-26 15:11:51 +020010067/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10068 * Accepts an optional argument of type string containing the header field name,
10069 * and an optional argument of type signed or unsigned integer to request an
10070 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010071 * headers are considered from the first one. It does not stop on commas and
10072 * returns full lines instead (useful for User-Agent or Date for example).
10073 */
10074static int
10075smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010076 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010077{
10078 struct http_txn *txn = l7;
10079 struct hdr_idx *idx = &txn->hdr_idx;
10080 struct hdr_ctx *ctx = smp->ctx.a[0];
10081 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10082 int occ = 0;
10083 const char *name_str = NULL;
10084 int name_len = 0;
10085
10086 if (!ctx) {
10087 /* first call */
10088 ctx = &static_hdr_ctx;
10089 ctx->idx = 0;
10090 smp->ctx.a[0] = ctx;
10091 }
10092
10093 if (args) {
10094 if (args[0].type != ARGT_STR)
10095 return 0;
10096 name_str = args[0].data.str.str;
10097 name_len = args[0].data.str.len;
10098
10099 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10100 occ = args[1].data.uint;
10101 }
10102
10103 CHECK_HTTP_MESSAGE_FIRST();
10104
10105 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10106 /* search for header from the beginning */
10107 ctx->idx = 0;
10108
10109 if (!occ && !(opt & SMP_OPT_ITERATE))
10110 /* no explicit occurrence and single fetch => last header by default */
10111 occ = -1;
10112
10113 if (!occ)
10114 /* prepare to report multiple occurrences for ACL fetches */
10115 smp->flags |= SMP_F_NOT_LAST;
10116
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010117 smp->type = SMP_T_STR;
10118 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010119 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10120 return 1;
10121
10122 smp->flags &= ~SMP_F_NOT_LAST;
10123 return 0;
10124}
10125
10126/* 6. Check on HTTP header count. The number of occurrences is returned.
10127 * Accepts exactly 1 argument of type string. It does not stop on commas and
10128 * returns full lines instead (useful for User-Agent or Date for example).
10129 */
10130static int
10131smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010132 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010133{
10134 struct http_txn *txn = l7;
10135 struct hdr_idx *idx = &txn->hdr_idx;
10136 struct hdr_ctx ctx;
10137 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10138 int cnt;
10139
10140 if (!args || args->type != ARGT_STR)
10141 return 0;
10142
10143 CHECK_HTTP_MESSAGE_FIRST();
10144
10145 ctx.idx = 0;
10146 cnt = 0;
10147 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
10148 cnt++;
10149
10150 smp->type = SMP_T_UINT;
10151 smp->data.uint = cnt;
10152 smp->flags = SMP_F_VOL_HDR;
10153 return 1;
10154}
10155
10156/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10157 * Accepts an optional argument of type string containing the header field name,
10158 * and an optional argument of type signed or unsigned integer to request an
10159 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010160 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010161 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010162static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010163smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010164 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010165{
10166 struct http_txn *txn = l7;
10167 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010168 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010169 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010170 int occ = 0;
10171 const char *name_str = NULL;
10172 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010173
Willy Tarreaua890d072013-04-02 12:01:06 +020010174 if (!ctx) {
10175 /* first call */
10176 ctx = &static_hdr_ctx;
10177 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010178 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010179 }
10180
Willy Tarreau185b5c42012-04-26 15:11:51 +020010181 if (args) {
10182 if (args[0].type != ARGT_STR)
10183 return 0;
10184 name_str = args[0].data.str.str;
10185 name_len = args[0].data.str.len;
10186
10187 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10188 occ = args[1].data.uint;
10189 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010190
Willy Tarreaue333ec92012-04-16 16:26:40 +020010191 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010192
Willy Tarreau185b5c42012-04-26 15:11:51 +020010193 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010194 /* search for header from the beginning */
10195 ctx->idx = 0;
10196
Willy Tarreau185b5c42012-04-26 15:11:51 +020010197 if (!occ && !(opt & SMP_OPT_ITERATE))
10198 /* no explicit occurrence and single fetch => last header by default */
10199 occ = -1;
10200
10201 if (!occ)
10202 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010203 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010204
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010205 smp->type = SMP_T_STR;
10206 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010207 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 +020010208 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010209
Willy Tarreau37406352012-04-23 16:16:37 +020010210 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010211 return 0;
10212}
10213
Willy Tarreauc11416f2007-06-17 16:58:38 +020010214/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010215 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010216 */
10217static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010218smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010219 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010220{
10221 struct http_txn *txn = l7;
10222 struct hdr_idx *idx = &txn->hdr_idx;
10223 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010224 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010225 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +020010226
Willy Tarreau24e32d82012-04-23 23:55:44 +020010227 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010228 return 0;
10229
Willy Tarreaue333ec92012-04-16 16:26:40 +020010230 CHECK_HTTP_MESSAGE_FIRST();
10231
Willy Tarreau33a7e692007-06-10 19:45:56 +020010232 ctx.idx = 0;
10233 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010234 while (http_find_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010235 cnt++;
10236
Willy Tarreauf853c462012-04-23 18:53:56 +020010237 smp->type = SMP_T_UINT;
10238 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010239 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010240 return 1;
10241}
10242
Willy Tarreau185b5c42012-04-26 15:11:51 +020010243/* Fetch an HTTP header's integer value. The integer value is returned. It
10244 * takes a mandatory argument of type string and an optional one of type int
10245 * to designate a specific occurrence. It returns an unsigned integer, which
10246 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010247 */
10248static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010249smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010250 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010251{
Willy Tarreauef38c392013-07-22 16:29:32 +020010252 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010253
Willy Tarreauf853c462012-04-23 18:53:56 +020010254 if (ret > 0) {
10255 smp->type = SMP_T_UINT;
10256 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10257 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010258
Willy Tarreaud53e2422012-04-16 17:21:11 +020010259 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010260}
10261
Cyril Bonté69fa9922012-10-25 00:01:06 +020010262/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10263 * and an optional one of type int to designate a specific occurrence.
10264 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010265 */
10266static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010267smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010268 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +020010269{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010270 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010271
Willy Tarreauef38c392013-07-22 16:29:32 +020010272 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010273 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10274 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010275 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010276 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010277 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010278 if (smp->data.str.len < temp->size - 1) {
10279 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10280 temp->str[smp->data.str.len] = '\0';
10281 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10282 smp->type = SMP_T_IPV6;
10283 break;
10284 }
10285 }
10286 }
10287
Willy Tarreaud53e2422012-04-16 17:21:11 +020010288 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010289 if (!(smp->flags & SMP_F_NOT_LAST))
10290 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010291 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010292 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010293}
10294
Willy Tarreau737b0c12007-06-10 21:28:46 +020010295/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10296 * the first '/' after the possible hostname, and ends before the possible '?'.
10297 */
10298static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010299smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010300 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010301{
10302 struct http_txn *txn = l7;
10303 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010304
Willy Tarreauc0239e02012-04-16 14:42:55 +020010305 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010306
Willy Tarreau9b28e032012-10-12 23:49:43 +020010307 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010308 ptr = http_get_path(txn);
10309 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010310 return 0;
10311
10312 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010313 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010314 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010315
10316 while (ptr < end && *ptr != '?')
10317 ptr++;
10318
Willy Tarreauf853c462012-04-23 18:53:56 +020010319 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010320 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010321 return 1;
10322}
10323
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010324/* This produces a concatenation of the first occurrence of the Host header
10325 * followed by the path component if it begins with a slash ('/'). This means
10326 * that '*' will not be added, resulting in exactly the first Host entry.
10327 * If no Host header is found, then the path is returned as-is. The returned
10328 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010329 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010330 */
10331static int
10332smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010333 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010334{
10335 struct http_txn *txn = l7;
10336 char *ptr, *end, *beg;
10337 struct hdr_ctx ctx;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010338 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010339
10340 CHECK_HTTP_MESSAGE_FIRST();
10341
10342 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010343 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +020010344 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010345
10346 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010347 temp = get_trash_chunk();
10348 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010349 smp->type = SMP_T_STR;
Willy Tarreau3caf2af2014-06-24 17:27:02 +020010350 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010351 smp->data.str.len = ctx.vlen;
10352
10353 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010354 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010355 beg = http_get_path(txn);
10356 if (!beg)
10357 beg = end;
10358
10359 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10360
10361 if (beg < ptr && *beg == '/') {
10362 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10363 smp->data.str.len += ptr - beg;
10364 }
10365
10366 smp->flags = SMP_F_VOL_1ST;
10367 return 1;
10368}
10369
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010370/* This produces a 32-bit hash of the concatenation of the first occurrence of
10371 * the Host header followed by the path component if it begins with a slash ('/').
10372 * This means that '*' will not be added, resulting in exactly the first Host
10373 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010374 * is hashed using the path hash followed by a full avalanche hash and provides a
10375 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010376 * high-traffic sites without having to store whole paths.
10377 */
Thierry FOURNIER055b9d52014-07-15 16:11:07 +020010378int
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010379smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010380 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010381{
10382 struct http_txn *txn = l7;
10383 struct hdr_ctx ctx;
10384 unsigned int hash = 0;
10385 char *ptr, *beg, *end;
10386 int len;
10387
10388 CHECK_HTTP_MESSAGE_FIRST();
10389
10390 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010391 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010392 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10393 ptr = ctx.line + ctx.val;
10394 len = ctx.vlen;
10395 while (len--)
10396 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10397 }
10398
10399 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010400 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010401 beg = http_get_path(txn);
10402 if (!beg)
10403 beg = end;
10404
10405 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10406
10407 if (beg < ptr && *beg == '/') {
10408 while (beg < ptr)
10409 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10410 }
10411 hash = full_hash(hash);
10412
10413 smp->type = SMP_T_UINT;
10414 smp->data.uint = hash;
10415 smp->flags = SMP_F_VOL_1ST;
10416 return 1;
10417}
10418
Willy Tarreau4a550602012-12-09 14:53:32 +010010419/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010420 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10421 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10422 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010423 * that in environments where IPv6 is insignificant, truncating the output to
10424 * 8 bytes would still work.
10425 */
10426static int
10427smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010428 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +010010429{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010430 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010431 struct connection *cli_conn = objt_conn(l4->si[0].end);
10432
10433 if (!cli_conn)
10434 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010435
Willy Tarreauef38c392013-07-22 16:29:32 +020010436 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +010010437 return 0;
10438
Willy Tarreau47ca5452012-12-23 20:22:19 +010010439 temp = get_trash_chunk();
Willy Tarreau5ad6e1d2014-07-15 21:34:06 +020010440 *(unsigned int *)temp->str = htonl(smp->data.uint);
10441 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010442
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010443 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010444 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010445 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010446 temp->len += 4;
10447 break;
10448 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010449 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010450 temp->len += 16;
10451 break;
10452 default:
10453 return 0;
10454 }
10455
10456 smp->data.str = *temp;
10457 smp->type = SMP_T_BIN;
10458 return 1;
10459}
10460
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010461static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010462smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010463 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010464{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010465 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10466 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10467 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010468
Willy Tarreau24e32d82012-04-23 23:55:44 +020010469 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010470
Willy Tarreauf853c462012-04-23 18:53:56 +020010471 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010472 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010473 return 1;
10474}
10475
Willy Tarreau7f18e522010-10-22 20:04:13 +020010476/* return a valid test if the current request is the first one on the connection */
10477static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010478smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010479 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010480{
10481 if (!s)
10482 return 0;
10483
Willy Tarreauf853c462012-04-23 18:53:56 +020010484 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010485 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010486 return 1;
10487}
10488
Willy Tarreau34db1082012-04-19 17:16:54 +020010489/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010490static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010491smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010492 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010493{
10494
Willy Tarreau24e32d82012-04-23 23:55:44 +020010495 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010496 return 0;
10497
Willy Tarreauc0239e02012-04-16 14:42:55 +020010498 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010499
Willy Tarreauc0239e02012-04-16 14:42:55 +020010500 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010501 return 0;
10502
Willy Tarreauf853c462012-04-23 18:53:56 +020010503 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010504 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010505 return 1;
10506}
Willy Tarreau8797c062007-05-07 00:55:35 +020010507
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010508/* Accepts exactly 1 argument of type userlist */
10509static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010510smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010511 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010512{
10513
10514 if (!args || args->type != ARGT_USR)
10515 return 0;
10516
10517 CHECK_HTTP_MESSAGE_FIRST();
10518
10519 if (!get_http_auth(l4))
10520 return 0;
10521
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010522 /* if the user does not belong to the userlist or has a wrong password,
10523 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010524 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010525 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010526 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10527 return 0;
10528
10529 /* pat_match_auth() will need the user list */
10530 smp->ctx.a[0] = args->data.usr;
10531
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010532 smp->type = SMP_T_STR;
10533 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010534 smp->data.str.str = l4->txn.auth.user;
10535 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010536
10537 return 1;
10538}
10539
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010540/* Try to find the next occurrence of a cookie name in a cookie header value.
10541 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10542 * the cookie value is returned into *value and *value_l, and the function
10543 * returns a pointer to the next pointer to search from if the value was found.
10544 * Otherwise if the cookie was not found, NULL is returned and neither value
10545 * nor value_l are touched. The input <hdr> string should first point to the
10546 * header's value, and the <hdr_end> pointer must point to the first character
10547 * not part of the value. <list> must be non-zero if value may represent a list
10548 * of values (cookie headers). This makes it faster to abort parsing when no
10549 * list is expected.
10550 */
10551static char *
10552extract_cookie_value(char *hdr, const char *hdr_end,
10553 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010554 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010555{
10556 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10557 char *next;
10558
10559 /* we search at least a cookie name followed by an equal, and more
10560 * generally something like this :
10561 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10562 */
10563 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10564 /* Iterate through all cookies on this line */
10565
10566 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10567 att_beg++;
10568
10569 /* find att_end : this is the first character after the last non
10570 * space before the equal. It may be equal to hdr_end.
10571 */
10572 equal = att_end = att_beg;
10573
10574 while (equal < hdr_end) {
10575 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10576 break;
10577 if (http_is_spht[(unsigned char)*equal++])
10578 continue;
10579 att_end = equal;
10580 }
10581
10582 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10583 * is between <att_beg> and <equal>, both may be identical.
10584 */
10585
10586 /* look for end of cookie if there is an equal sign */
10587 if (equal < hdr_end && *equal == '=') {
10588 /* look for the beginning of the value */
10589 val_beg = equal + 1;
10590 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10591 val_beg++;
10592
10593 /* find the end of the value, respecting quotes */
10594 next = find_cookie_value_end(val_beg, hdr_end);
10595
10596 /* make val_end point to the first white space or delimitor after the value */
10597 val_end = next;
10598 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10599 val_end--;
10600 } else {
10601 val_beg = val_end = next = equal;
10602 }
10603
10604 /* We have nothing to do with attributes beginning with '$'. However,
10605 * they will automatically be removed if a header before them is removed,
10606 * since they're supposed to be linked together.
10607 */
10608 if (*att_beg == '$')
10609 continue;
10610
10611 /* Ignore cookies with no equal sign */
10612 if (equal == next)
10613 continue;
10614
10615 /* Now we have the cookie name between att_beg and att_end, and
10616 * its value between val_beg and val_end.
10617 */
10618
10619 if (att_end - att_beg == cookie_name_l &&
10620 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10621 /* let's return this value and indicate where to go on from */
10622 *value = val_beg;
10623 *value_l = val_end - val_beg;
10624 return next + 1;
10625 }
10626
10627 /* Set-Cookie headers only have the name in the first attr=value part */
10628 if (!list)
10629 break;
10630 }
10631
10632 return NULL;
10633}
10634
William Lallemanda43ba4e2014-01-28 18:14:25 +010010635/* Fetch a captured HTTP request header. The index is the position of
10636 * the "capture" option in the configuration file
10637 */
10638static int
10639smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10640 const struct arg *args, struct sample *smp, const char *kw)
10641{
10642 struct proxy *fe = l4->fe;
10643 struct http_txn *txn = l7;
10644 int idx;
10645
10646 if (!args || args->type != ARGT_UINT)
10647 return 0;
10648
10649 idx = args->data.uint;
10650
10651 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10652 return 0;
10653
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010654 smp->type = SMP_T_STR;
10655 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010656 smp->data.str.str = txn->req.cap[idx];
10657 smp->data.str.len = strlen(txn->req.cap[idx]);
10658
10659 return 1;
10660}
10661
10662/* Fetch a captured HTTP response header. The index is the position of
10663 * the "capture" option in the configuration file
10664 */
10665static int
10666smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10667 const struct arg *args, struct sample *smp, const char *kw)
10668{
10669 struct proxy *fe = l4->fe;
10670 struct http_txn *txn = l7;
10671 int idx;
10672
10673 if (!args || args->type != ARGT_UINT)
10674 return 0;
10675
10676 idx = args->data.uint;
10677
10678 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10679 return 0;
10680
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010681 smp->type = SMP_T_STR;
10682 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010683 smp->data.str.str = txn->rsp.cap[idx];
10684 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10685
10686 return 1;
10687}
10688
William Lallemand65ad6e12014-01-31 15:08:02 +010010689/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10690static int
10691smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10692 const struct arg *args, struct sample *smp, const char *kw)
10693{
10694 struct chunk *temp;
10695 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010696 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010697
10698 if (!txn->uri)
10699 return 0;
10700
William Lallemand96a77852014-02-05 00:30:02 +010010701 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010702
William Lallemand96a77852014-02-05 00:30:02 +010010703 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10704 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010705
William Lallemand96a77852014-02-05 00:30:02 +010010706 temp = get_trash_chunk();
10707 temp->str = txn->uri;
10708 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010709 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010710 smp->type = SMP_T_STR;
10711 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010712
10713 return 1;
10714
10715}
10716
10717/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10718static int
10719smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10720 const struct arg *args, struct sample *smp, const char *kw)
10721{
10722 struct chunk *temp;
10723 struct http_txn *txn = l7;
10724 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010725
10726 if (!txn->uri)
10727 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010728
William Lallemand65ad6e12014-01-31 15:08:02 +010010729 ptr = txn->uri;
10730
10731 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10732 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010733
William Lallemand65ad6e12014-01-31 15:08:02 +010010734 if (!*ptr)
10735 return 0;
10736
10737 ptr++; /* skip the space */
10738
10739 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010740 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010741 if (!ptr)
10742 return 0;
10743 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10744 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010745
10746 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010747 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010748 smp->type = SMP_T_STR;
10749 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010750
10751 return 1;
10752}
10753
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010754/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10755 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10756 */
10757static int
10758smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10759 const struct arg *args, struct sample *smp, const char *kw)
10760{
10761 struct http_txn *txn = l7;
10762
10763 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10764 return 0;
10765
10766 if (txn->req.flags & HTTP_MSGF_VER_11)
10767 smp->data.str.str = "HTTP/1.1";
10768 else
10769 smp->data.str.str = "HTTP/1.0";
10770
10771 smp->data.str.len = 8;
10772 smp->type = SMP_T_STR;
10773 smp->flags = SMP_F_CONST;
10774 return 1;
10775
10776}
10777
10778/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10779 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10780 */
10781static int
10782smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10783 const struct arg *args, struct sample *smp, const char *kw)
10784{
10785 struct http_txn *txn = l7;
10786
10787 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10788 return 0;
10789
10790 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10791 smp->data.str.str = "HTTP/1.1";
10792 else
10793 smp->data.str.str = "HTTP/1.0";
10794
10795 smp->data.str.len = 8;
10796 smp->type = SMP_T_STR;
10797 smp->flags = SMP_F_CONST;
10798 return 1;
10799
10800}
10801
William Lallemand65ad6e12014-01-31 15:08:02 +010010802
Willy Tarreaue333ec92012-04-16 16:26:40 +020010803/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010804 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010805 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010806 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010807 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010808 * Accepts exactly 1 argument of type string. If the input options indicate
10809 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010810 * The returned sample is of type CSTR. Can be used to parse cookies in other
10811 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010812 */
William Lallemand07c8b242014-05-02 17:11:07 +020010813int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010814 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010815{
10816 struct http_txn *txn = l7;
10817 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010818 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010819 const struct http_msg *msg;
10820 const char *hdr_name;
10821 int hdr_name_len;
10822 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010823 int occ = 0;
10824 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010825
Willy Tarreau24e32d82012-04-23 23:55:44 +020010826 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010827 return 0;
10828
Willy Tarreaua890d072013-04-02 12:01:06 +020010829 if (!ctx) {
10830 /* first call */
10831 ctx = &static_hdr_ctx;
10832 ctx->idx = 0;
10833 smp->ctx.a[2] = ctx;
10834 }
10835
Willy Tarreaue333ec92012-04-16 16:26:40 +020010836 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010837
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010838 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010839 msg = &txn->req;
10840 hdr_name = "Cookie";
10841 hdr_name_len = 6;
10842 } else {
10843 msg = &txn->rsp;
10844 hdr_name = "Set-Cookie";
10845 hdr_name_len = 10;
10846 }
10847
Willy Tarreau28376d62012-04-26 21:26:10 +020010848 if (!occ && !(opt & SMP_OPT_ITERATE))
10849 /* no explicit occurrence and single fetch => last cookie by default */
10850 occ = -1;
10851
10852 /* OK so basically here, either we want only one value and it's the
10853 * last one, or we want to iterate over all of them and we fetch the
10854 * next one.
10855 */
10856
Willy Tarreau9b28e032012-10-12 23:49:43 +020010857 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010858 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010859 /* search for the header from the beginning, we must first initialize
10860 * the search parameters.
10861 */
Willy Tarreau37406352012-04-23 16:16:37 +020010862 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010863 ctx->idx = 0;
10864 }
10865
Willy Tarreau28376d62012-04-26 21:26:10 +020010866 smp->flags |= SMP_F_VOL_HDR;
10867
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010868 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010869 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10870 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010871 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10872 goto out;
10873
Willy Tarreau24e32d82012-04-23 23:55:44 +020010874 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010875 continue;
10876
Willy Tarreau37406352012-04-23 16:16:37 +020010877 smp->ctx.a[0] = ctx->line + ctx->val;
10878 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010879 }
10880
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010881 smp->type = SMP_T_STR;
10882 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010883 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010884 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010885 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010886 &smp->data.str.str,
10887 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010888 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010889 found = 1;
10890 if (occ >= 0) {
10891 /* one value was returned into smp->data.str.{str,len} */
10892 smp->flags |= SMP_F_NOT_LAST;
10893 return 1;
10894 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010895 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010896 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010897 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010898 /* all cookie headers and values were scanned. If we're looking for the
10899 * last occurrence, we may return it now.
10900 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010901 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010902 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010903 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010904}
10905
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010906/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010907 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010908 * multiple cookies may be parsed on the same line. The returned sample is of
10909 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010910 */
10911static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010912smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010913 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010914{
10915 struct http_txn *txn = l7;
10916 struct hdr_idx *idx = &txn->hdr_idx;
10917 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010918 const struct http_msg *msg;
10919 const char *hdr_name;
10920 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010921 int cnt;
10922 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010923 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010924
Willy Tarreau24e32d82012-04-23 23:55:44 +020010925 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010926 return 0;
10927
Willy Tarreaue333ec92012-04-16 16:26:40 +020010928 CHECK_HTTP_MESSAGE_FIRST();
10929
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010930 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010931 msg = &txn->req;
10932 hdr_name = "Cookie";
10933 hdr_name_len = 6;
10934 } else {
10935 msg = &txn->rsp;
10936 hdr_name = "Set-Cookie";
10937 hdr_name_len = 10;
10938 }
10939
Willy Tarreau9b28e032012-10-12 23:49:43 +020010940 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010941 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010942 ctx.idx = 0;
10943 cnt = 0;
10944
10945 while (1) {
10946 /* Note: val_beg == NULL every time we need to fetch a new header */
10947 if (!val_beg) {
10948 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10949 break;
10950
Willy Tarreau24e32d82012-04-23 23:55:44 +020010951 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010952 continue;
10953
10954 val_beg = ctx.line + ctx.val;
10955 val_end = val_beg + ctx.vlen;
10956 }
10957
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010958 smp->type = SMP_T_STR;
10959 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010960 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010961 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010962 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010963 &smp->data.str.str,
10964 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010965 cnt++;
10966 }
10967 }
10968
Willy Tarreaub169eba2013-12-16 15:14:43 +010010969 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010970 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010971 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010972 return 1;
10973}
10974
Willy Tarreau51539362012-05-08 12:46:28 +020010975/* Fetch an cookie's integer value. The integer value is returned. It
10976 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10977 */
10978static int
10979smp_fetch_cookie_val(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)
Willy Tarreau51539362012-05-08 12:46:28 +020010981{
Willy Tarreauef38c392013-07-22 16:29:32 +020010982 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010983
10984 if (ret > 0) {
10985 smp->type = SMP_T_UINT;
10986 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10987 }
10988
10989 return ret;
10990}
10991
Willy Tarreau8797c062007-05-07 00:55:35 +020010992/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010993/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010994/************************************************************************/
10995
David Cournapeau16023ee2010-12-23 20:55:41 +090010996/*
10997 * Given a path string and its length, find the position of beginning of the
10998 * query string. Returns NULL if no query string is found in the path.
10999 *
11000 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11001 *
11002 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11003 */
bedis4c75cca2012-10-05 08:38:24 +020011004static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011005{
11006 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011007
bedis4c75cca2012-10-05 08:38:24 +020011008 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011009 return p ? p + 1 : NULL;
11010}
11011
bedis4c75cca2012-10-05 08:38:24 +020011012static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011013{
bedis4c75cca2012-10-05 08:38:24 +020011014 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011015}
11016
11017/*
11018 * Given a url parameter, find the starting position of the first occurence,
11019 * or NULL if the parameter is not found.
11020 *
11021 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11022 * the function will return query_string+8.
11023 */
11024static char*
11025find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011026 char* url_param_name, size_t url_param_name_l,
11027 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011028{
11029 char *pos, *last;
11030
11031 pos = query_string;
11032 last = query_string + query_string_l - url_param_name_l - 1;
11033
11034 while (pos <= last) {
11035 if (pos[url_param_name_l] == '=') {
11036 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11037 return pos;
11038 pos += url_param_name_l + 1;
11039 }
bedis4c75cca2012-10-05 08:38:24 +020011040 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011041 pos++;
11042 pos++;
11043 }
11044 return NULL;
11045}
11046
11047/*
11048 * Given a url parameter name, returns its value and size into *value and
11049 * *value_l respectively, and returns non-zero. If the parameter is not found,
11050 * zero is returned and value/value_l are not touched.
11051 */
11052static int
11053find_url_param_value(char* path, size_t path_l,
11054 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011055 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011056{
11057 char *query_string, *qs_end;
11058 char *arg_start;
11059 char *value_start, *value_end;
11060
bedis4c75cca2012-10-05 08:38:24 +020011061 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011062 if (!query_string)
11063 return 0;
11064
11065 qs_end = path + path_l;
11066 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011067 url_param_name, url_param_name_l,
11068 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011069 if (!arg_start)
11070 return 0;
11071
11072 value_start = arg_start + url_param_name_l + 1;
11073 value_end = value_start;
11074
bedis4c75cca2012-10-05 08:38:24 +020011075 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011076 value_end++;
11077
11078 *value = value_start;
11079 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011080 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011081}
11082
11083static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011084smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011085 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090011086{
bedis4c75cca2012-10-05 08:38:24 +020011087 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090011088 struct http_txn *txn = l7;
11089 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011090
bedis4c75cca2012-10-05 08:38:24 +020011091 if (!args || args[0].type != ARGT_STR ||
11092 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011093 return 0;
11094
11095 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011096
bedis4c75cca2012-10-05 08:38:24 +020011097 if (args[1].type)
11098 delim = *args[1].data.str.str;
11099
Willy Tarreau9b28e032012-10-12 23:49:43 +020011100 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011101 args->data.str.str, args->data.str.len,
11102 &smp->data.str.str, &smp->data.str.len,
11103 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011104 return 0;
11105
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011106 smp->type = SMP_T_STR;
11107 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011108 return 1;
11109}
11110
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011111/* Return the signed integer value for the specified url parameter (see url_param
11112 * above).
11113 */
11114static int
11115smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011116 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011117{
Willy Tarreauef38c392013-07-22 16:29:32 +020011118 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011119
11120 if (ret > 0) {
11121 smp->type = SMP_T_UINT;
11122 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11123 }
11124
11125 return ret;
11126}
11127
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011128/* This produces a 32-bit hash of the concatenation of the first occurrence of
11129 * the Host header followed by the path component if it begins with a slash ('/').
11130 * This means that '*' will not be added, resulting in exactly the first Host
11131 * entry. If no Host header is found, then the path is used. The resulting value
11132 * is hashed using the url hash followed by a full avalanche hash and provides a
11133 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11134 * high-traffic sites without having to store whole paths.
11135 * this differs from the base32 functions in that it includes the url parameters
11136 * as well as the path
11137 */
11138static int
11139smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011140 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011141{
11142 struct http_txn *txn = l7;
11143 struct hdr_ctx ctx;
11144 unsigned int hash = 0;
11145 char *ptr, *beg, *end;
11146 int len;
11147
11148 CHECK_HTTP_MESSAGE_FIRST();
11149
11150 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011151 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011152 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11153 ptr = ctx.line + ctx.val;
11154 len = ctx.vlen;
11155 while (len--)
11156 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11157 }
11158
11159 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011160 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 +000011161 beg = http_get_path(txn);
11162 if (!beg)
11163 beg = end;
11164
11165 for (ptr = beg; ptr < end ; ptr++);
11166
11167 if (beg < ptr && *beg == '/') {
11168 while (beg < ptr)
11169 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11170 }
11171 hash = full_hash(hash);
11172
11173 smp->type = SMP_T_UINT;
11174 smp->data.uint = hash;
11175 smp->flags = SMP_F_VOL_1ST;
11176 return 1;
11177}
11178
11179/* This concatenates the source address with the 32-bit hash of the Host and
11180 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11181 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11182 * on the source address length. The URL hash is stored before the address so
11183 * that in environments where IPv6 is insignificant, truncating the output to
11184 * 8 bytes would still work.
11185 */
11186static int
11187smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011188 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011189{
11190 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011191 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011192
Willy Tarreaue155ec22013-11-18 18:33:22 +010011193 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011194 return 0;
11195
11196 temp = get_trash_chunk();
11197 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11198 temp->len += sizeof(smp->data.uint);
11199
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011200 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011201 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011202 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011203 temp->len += 4;
11204 break;
11205 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011206 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011207 temp->len += 16;
11208 break;
11209 default:
11210 return 0;
11211 }
11212
11213 smp->data.str = *temp;
11214 smp->type = SMP_T_BIN;
11215 return 1;
11216}
11217
Willy Tarreau185b5c42012-04-26 15:11:51 +020011218/* This function is used to validate the arguments passed to any "hdr" fetch
11219 * keyword. These keywords support an optional positive or negative occurrence
11220 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11221 * is assumed that the types are already the correct ones. Returns 0 on error,
11222 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11223 * error message in case of error, that the caller is responsible for freeing.
11224 * The initial location must either be freeable or NULL.
11225 */
11226static int val_hdr(struct arg *arg, char **err_msg)
11227{
11228 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011229 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011230 return 0;
11231 }
11232 return 1;
11233}
11234
Willy Tarreau276fae92013-07-25 14:36:01 +020011235/* takes an UINT value on input supposed to represent the time since EPOCH,
11236 * adds an optional offset found in args[0] and emits a string representing
11237 * the date in RFC-1123/5322 format.
11238 */
11239static int sample_conv_http_date(const struct arg *args, struct sample *smp)
11240{
11241 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11242 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11243 struct chunk *temp;
11244 struct tm *tm;
11245 time_t curr_date = smp->data.uint;
11246
11247 /* add offset */
11248 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11249 curr_date += args[0].data.sint;
11250
11251 tm = gmtime(&curr_date);
11252
11253 temp = get_trash_chunk();
11254 temp->len = snprintf(temp->str, temp->size - temp->len,
11255 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11256 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11257 tm->tm_hour, tm->tm_min, tm->tm_sec);
11258
11259 smp->data.str = *temp;
11260 smp->type = SMP_T_STR;
11261 return 1;
11262}
11263
Thierry FOURNIERad903512014-04-11 17:51:01 +020011264/* Match language range with language tag. RFC2616 14.4:
11265 *
11266 * A language-range matches a language-tag if it exactly equals
11267 * the tag, or if it exactly equals a prefix of the tag such
11268 * that the first tag character following the prefix is "-".
11269 *
11270 * Return 1 if the strings match, else return 0.
11271 */
11272static inline int language_range_match(const char *range, int range_len,
11273 const char *tag, int tag_len)
11274{
11275 const char *end = range + range_len;
11276 const char *tend = tag + tag_len;
11277 while (range < end) {
11278 if (*range == '-' && tag == tend)
11279 return 1;
11280 if (*range != *tag || tag == tend)
11281 return 0;
11282 range++;
11283 tag++;
11284 }
11285 /* Return true only if the last char of the tag is matched. */
11286 return tag == tend;
11287}
11288
11289/* Arguments: The list of expected value, the number of parts returned and the separator */
11290static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
11291{
11292 const char *al = smp->data.str.str;
11293 const char *end = al + smp->data.str.len;
11294 const char *token;
11295 int toklen;
11296 int qvalue;
11297 const char *str;
11298 const char *w;
11299 int best_q = 0;
11300
11301 /* Set the constant to the sample, because the output of the
11302 * function will be peek in the constant configuration string.
11303 */
11304 smp->flags |= SMP_F_CONST;
11305 smp->data.str.size = 0;
11306 smp->data.str.str = "";
11307 smp->data.str.len = 0;
11308
11309 /* Parse the accept language */
11310 while (1) {
11311
11312 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011313 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011314 al++;
11315 if (al >= end)
11316 break;
11317
11318 /* Start of the fisrt word. */
11319 token = al;
11320
11321 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011322 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011323 al++;
11324 if (al == token)
11325 goto expect_comma;
11326
11327 /* Length of the token. */
11328 toklen = al - token;
11329 qvalue = 1000;
11330
11331 /* Check if the token exists in the list. If the token not exists,
11332 * jump to the next token.
11333 */
11334 str = args[0].data.str.str;
11335 w = str;
11336 while (1) {
11337 if (*str == ';' || *str == '\0') {
11338 if (language_range_match(token, toklen, w, str-w))
11339 goto look_for_q;
11340 if (*str == '\0')
11341 goto expect_comma;
11342 w = str + 1;
11343 }
11344 str++;
11345 }
11346 goto expect_comma;
11347
11348look_for_q:
11349
11350 /* Jump spaces, quit if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011351 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011352 al++;
11353 if (al >= end)
11354 goto process_value;
11355
11356 /* If ',' is found, process the result */
11357 if (*al == ',')
11358 goto process_value;
11359
11360 /* If the character is different from ';', look
11361 * for the end of the header part in best effort.
11362 */
11363 if (*al != ';')
11364 goto expect_comma;
11365
11366 /* Assumes that the char is ';', now expect "q=". */
11367 al++;
11368
11369 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011370 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011371 al++;
11372 if (al >= end)
11373 goto process_value;
11374
11375 /* Expect 'q'. If no 'q', continue in best effort */
11376 if (*al != 'q')
11377 goto process_value;
11378 al++;
11379
11380 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011381 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011382 al++;
11383 if (al >= end)
11384 goto process_value;
11385
11386 /* Expect '='. If no '=', continue in best effort */
11387 if (*al != '=')
11388 goto process_value;
11389 al++;
11390
11391 /* Jump spaces, process value if the end is detected. */
Willy Tarreau506c69a2014-07-08 00:59:48 +020011392 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011393 al++;
11394 if (al >= end)
11395 goto process_value;
11396
11397 /* Parse the q value. */
11398 qvalue = parse_qvalue(al, &al);
11399
11400process_value:
11401
11402 /* If the new q value is the best q value, then store the associated
11403 * language in the response. If qvalue is the biggest value (1000),
11404 * break the process.
11405 */
11406 if (qvalue > best_q) {
11407 smp->data.str.str = (char *)w;
11408 smp->data.str.len = str - w;
11409 if (qvalue >= 1000)
11410 break;
11411 best_q = qvalue;
11412 }
11413
11414expect_comma:
11415
11416 /* Expect comma or end. If the end is detected, quit the loop. */
11417 while (al < end && *al != ',')
11418 al++;
11419 if (al >= end)
11420 break;
11421
11422 /* Comma is found, jump it and restart the analyzer. */
11423 al++;
11424 }
11425
11426 /* Set default value if required. */
11427 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11428 smp->data.str.str = args[1].data.str.str;
11429 smp->data.str.len = args[1].data.str.len;
11430 }
11431
11432 /* Return true only if a matching language was found. */
11433 return smp->data.str.len != 0;
11434}
11435
William Lallemand73025dd2014-04-24 14:38:37 +020011436/*
11437 * Return the struct http_req_action_kw associated to a keyword.
11438 */
11439struct http_req_action_kw *action_http_req_custom(const char *kw)
11440{
11441 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11442 struct http_req_action_kw_list *kw_list;
11443 int i;
11444
11445 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11446 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11447 if (!strcmp(kw, kw_list->kw[i].kw))
11448 return &kw_list->kw[i];
11449 }
11450 }
11451 }
11452 return NULL;
11453}
11454
11455/*
11456 * Return the struct http_res_action_kw associated to a keyword.
11457 */
11458struct http_res_action_kw *action_http_res_custom(const char *kw)
11459{
11460 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11461 struct http_res_action_kw_list *kw_list;
11462 int i;
11463
11464 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11465 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11466 if (!strcmp(kw, kw_list->kw[i].kw))
11467 return &kw_list->kw[i];
11468 }
11469 }
11470 }
11471 return NULL;
11472}
11473
Willy Tarreau4a568972010-05-12 08:08:50 +020011474/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011475/* All supported ACL keywords must be declared here. */
11476/************************************************************************/
11477
11478/* Note: must not be declared <const> as its list will be overwritten.
11479 * Please take care of keeping this list alphabetically sorted.
11480 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011481static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011482 { "base", "base", PAT_MATCH_STR },
11483 { "base_beg", "base", PAT_MATCH_BEG },
11484 { "base_dir", "base", PAT_MATCH_DIR },
11485 { "base_dom", "base", PAT_MATCH_DOM },
11486 { "base_end", "base", PAT_MATCH_END },
11487 { "base_len", "base", PAT_MATCH_LEN },
11488 { "base_reg", "base", PAT_MATCH_REG },
11489 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011490
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011491 { "cook", "req.cook", PAT_MATCH_STR },
11492 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11493 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11494 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11495 { "cook_end", "req.cook", PAT_MATCH_END },
11496 { "cook_len", "req.cook", PAT_MATCH_LEN },
11497 { "cook_reg", "req.cook", PAT_MATCH_REG },
11498 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011499
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011500 { "hdr", "req.hdr", PAT_MATCH_STR },
11501 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11502 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11503 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11504 { "hdr_end", "req.hdr", PAT_MATCH_END },
11505 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11506 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11507 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011508
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011509 /* these two declarations uses strings with list storage (in place
11510 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11511 * and delete functions are relative to the list management. The parse
11512 * and match method are related to the corresponding fetch methods. This
11513 * is very particular ACL declaration mode.
11514 */
11515 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11516 { "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 +020011517
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011518 { "path", "path", PAT_MATCH_STR },
11519 { "path_beg", "path", PAT_MATCH_BEG },
11520 { "path_dir", "path", PAT_MATCH_DIR },
11521 { "path_dom", "path", PAT_MATCH_DOM },
11522 { "path_end", "path", PAT_MATCH_END },
11523 { "path_len", "path", PAT_MATCH_LEN },
11524 { "path_reg", "path", PAT_MATCH_REG },
11525 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011526
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011527 { "req_ver", "req.ver", PAT_MATCH_STR },
11528 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011529
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011530 { "scook", "res.cook", PAT_MATCH_STR },
11531 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11532 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11533 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11534 { "scook_end", "res.cook", PAT_MATCH_END },
11535 { "scook_len", "res.cook", PAT_MATCH_LEN },
11536 { "scook_reg", "res.cook", PAT_MATCH_REG },
11537 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011538
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011539 { "shdr", "res.hdr", PAT_MATCH_STR },
11540 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11541 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11542 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11543 { "shdr_end", "res.hdr", PAT_MATCH_END },
11544 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11545 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11546 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011547
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011548 { "url", "url", PAT_MATCH_STR },
11549 { "url_beg", "url", PAT_MATCH_BEG },
11550 { "url_dir", "url", PAT_MATCH_DIR },
11551 { "url_dom", "url", PAT_MATCH_DOM },
11552 { "url_end", "url", PAT_MATCH_END },
11553 { "url_len", "url", PAT_MATCH_LEN },
11554 { "url_reg", "url", PAT_MATCH_REG },
11555 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011556
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011557 { "urlp", "urlp", PAT_MATCH_STR },
11558 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11559 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11560 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11561 { "urlp_end", "urlp", PAT_MATCH_END },
11562 { "urlp_len", "urlp", PAT_MATCH_LEN },
11563 { "urlp_reg", "urlp", PAT_MATCH_REG },
11564 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011565
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011566 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011567}};
11568
11569/************************************************************************/
11570/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011571/************************************************************************/
11572/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011573static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011574 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011575 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11576 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11577
William Lallemanda43ba4e2014-01-28 18:14:25 +010011578 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011579 { "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 +020011580
11581 /* retrieve these captures from the HTTP logs */
11582 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11583 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11584 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11585
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011586 { "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 +020011587 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011588
Willy Tarreau409bcde2013-01-08 00:31:00 +010011589 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11590 * are only here to match the ACL's name, are request-only and are used
11591 * for ACL compatibility only.
11592 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011593 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11594 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011595 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11596 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11597
11598 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11599 * only here to match the ACL's name, are request-only and are used for
11600 * ACL compatibility only.
11601 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011602 { "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 +010011603 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11604 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11605 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11606
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011607 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011608 { "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 +010011609 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011610 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011611 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011612
11613 /* HTTP protocol on the request path */
11614 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011615 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011616
11617 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011618 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11619 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011620
11621 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011622 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11623 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011624
Willy Tarreau18ed2562013-01-14 15:56:36 +010011625 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011626 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011627 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11628 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11629
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011630 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011631 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011632 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011633 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11634 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11635 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11636
11637 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011638 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011639 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11640 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11641
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011642 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011643 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011644 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011645 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11646 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11647 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11648
Willy Tarreau409bcde2013-01-08 00:31:00 +010011649 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011650 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011651 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11652 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011653 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011654
11655 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011656 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011657 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11658 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11659 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11660
11661 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011662 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011663 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11664 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011665 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11666 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011667 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11668 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011669 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11670 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011671}};
11672
Willy Tarreau8797c062007-05-07 00:55:35 +020011673
Willy Tarreau276fae92013-07-25 14:36:01 +020011674/* Note: must not be declared <const> as its list will be overwritten */
11675static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011676 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11677 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011678 { NULL, NULL, 0, 0, 0 },
11679}};
11680
Willy Tarreau8797c062007-05-07 00:55:35 +020011681__attribute__((constructor))
11682static void __http_protocol_init(void)
11683{
11684 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011685 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011686 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020011687}
11688
11689
Willy Tarreau58f10d72006-12-04 02:26:12 +010011690/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011691 * Local variables:
11692 * c-indent-level: 8
11693 * c-basic-offset: 8
11694 * End:
11695 */