blob: a5323e417af0245b5649b4c936ce0fd0945a823f [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreau522d6c02009-12-06 18:49:18 +010069const char HTTP_100[] =
70 "HTTP/1.1 100 Continue\r\n\r\n";
71
72const struct chunk http_100_chunk = {
73 .str = (char *)&HTTP_100,
74 .len = sizeof(HTTP_100)-1
75};
76
Willy Tarreaua9679ac2010-01-03 17:32:57 +010077/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020081 "Location: "; /* not terminated since it will be concatenated with the URL */
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
89/* same as 302 except that the browser MUST retry with the GET method */
90const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Location: "; /* not terminated since it will be concatenated with the URL */
95
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040096
97/* same as 302 except that the browser MUST retry with the same method */
98const char *HTTP_307 =
99 "HTTP/1.1 307 Temporary Redirect\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Content-length: 0\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
104/* same as 301 except that the browser MUST retry with the same method */
105const char *HTTP_308 =
106 "HTTP/1.1 308 Permanent Redirect\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
111const char *HTTP_401_fmt =
112 "HTTP/1.0 401 Unauthorized\r\n"
113 "Cache-Control: no-cache\r\n"
114 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200115 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
117 "\r\n"
118 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
119
Willy Tarreau844a7e72010-01-31 21:46:18 +0100120const char *HTTP_407_fmt =
121 "HTTP/1.0 407 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
124 "Content-Type: text/html\r\n"
125 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
Godbachb3016542014-12-17 16:32:05 +0800127 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100128
Willy Tarreau0f772532006-12-23 20:51:41 +0100129
130const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200131 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100132 [HTTP_ERR_400] = 400,
133 [HTTP_ERR_403] = 403,
134 [HTTP_ERR_408] = 408,
135 [HTTP_ERR_500] = 500,
136 [HTTP_ERR_502] = 502,
137 [HTTP_ERR_503] = 503,
138 [HTTP_ERR_504] = 504,
139};
140
Willy Tarreau80587432006-12-24 17:47:20 +0100141static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200142 [HTTP_ERR_200] =
143 "HTTP/1.0 200 OK\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
149
Willy Tarreau0f772532006-12-23 20:51:41 +0100150 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100151 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
157
158 [HTTP_ERR_403] =
159 "HTTP/1.0 403 Forbidden\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
165
166 [HTTP_ERR_408] =
167 "HTTP/1.0 408 Request Time-out\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
173
174 [HTTP_ERR_500] =
175 "HTTP/1.0 500 Server Error\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
181
182 [HTTP_ERR_502] =
183 "HTTP/1.0 502 Bad Gateway\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
189
190 [HTTP_ERR_503] =
191 "HTTP/1.0 503 Service Unavailable\r\n"
192 "Cache-Control: no-cache\r\n"
193 "Connection: close\r\n"
194 "Content-Type: text/html\r\n"
195 "\r\n"
196 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
197
198 [HTTP_ERR_504] =
199 "HTTP/1.0 504 Gateway Time-out\r\n"
200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
204 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
205
206};
207
Cyril Bonté19979e12012-04-04 12:57:21 +0200208/* status codes available for the stats admin page (strictly 4 chars length) */
209const char *stat_status_codes[STAT_STATUS_SIZE] = {
210 [STAT_STATUS_DENY] = "DENY",
211 [STAT_STATUS_DONE] = "DONE",
212 [STAT_STATUS_ERRP] = "ERRP",
213 [STAT_STATUS_EXCD] = "EXCD",
214 [STAT_STATUS_NONE] = "NONE",
215 [STAT_STATUS_PART] = "PART",
216 [STAT_STATUS_UNKN] = "UNKN",
217};
218
219
William Lallemand73025dd2014-04-24 14:38:37 +0200220/* List head of all known action keywords for "http-request" */
221struct http_req_action_kw_list http_req_keywords = {
222 .list = LIST_HEAD_INIT(http_req_keywords.list)
223};
224
225/* List head of all known action keywords for "http-response" */
226struct http_res_action_kw_list http_res_keywords = {
227 .list = LIST_HEAD_INIT(http_res_keywords.list)
228};
229
Willy Tarreau80587432006-12-24 17:47:20 +0100230/* We must put the messages here since GCC cannot initialize consts depending
231 * on strlen().
232 */
233struct chunk http_err_chunks[HTTP_ERR_SIZE];
234
Willy Tarreaua890d072013-04-02 12:01:06 +0200235/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
236static struct hdr_ctx static_hdr_ctx;
237
Willy Tarreau42250582007-04-01 01:30:43 +0200238#define FD_SETS_ARE_BITFIELDS
239#ifdef FD_SETS_ARE_BITFIELDS
240/*
241 * This map is used with all the FD_* macros to check whether a particular bit
242 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
243 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
244 * byte should be encoded. Be careful to always pass bytes from 0 to 255
245 * exclusively to the macros.
246 */
247fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
248fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100249fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200250
251#else
252#error "Check if your OS uses bitfields for fd_sets"
253#endif
254
Willy Tarreau0b748332014-04-29 00:13:29 +0200255static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn);
256
Willy Tarreau80587432006-12-24 17:47:20 +0100257void init_proto_http()
258{
Willy Tarreau42250582007-04-01 01:30:43 +0200259 int i;
260 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100261 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200262
Willy Tarreau80587432006-12-24 17:47:20 +0100263 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
264 if (!http_err_msgs[msg]) {
265 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
266 abort();
267 }
268
269 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
270 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
271 }
Willy Tarreau42250582007-04-01 01:30:43 +0200272
273 /* initialize the log header encoding map : '{|}"#' should be encoded with
274 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
275 * URL encoding only requires '"', '#' to be encoded as well as non-
276 * printable characters above.
277 */
278 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
279 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100280 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200281 for (i = 0; i < 32; i++) {
282 FD_SET(i, hdr_encode_map);
283 FD_SET(i, url_encode_map);
284 }
285 for (i = 127; i < 256; i++) {
286 FD_SET(i, hdr_encode_map);
287 FD_SET(i, url_encode_map);
288 }
289
290 tmp = "\"#{|}";
291 while (*tmp) {
292 FD_SET(*tmp, hdr_encode_map);
293 tmp++;
294 }
295
296 tmp = "\"#";
297 while (*tmp) {
298 FD_SET(*tmp, url_encode_map);
299 tmp++;
300 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200301
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100302 /* initialize the http header encoding map. The draft httpbis define the
303 * header content as:
304 *
305 * HTTP-message = start-line
306 * *( header-field CRLF )
307 * CRLF
308 * [ message-body ]
309 * header-field = field-name ":" OWS field-value OWS
310 * field-value = *( field-content / obs-fold )
311 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
312 * obs-fold = CRLF 1*( SP / HTAB )
313 * field-vchar = VCHAR / obs-text
314 * VCHAR = %x21-7E
315 * obs-text = %x80-FF
316 *
317 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
318 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
319 * "obs-fold" is volontary forgotten because haproxy remove this.
320 */
321 memset(http_encode_map, 0, sizeof(http_encode_map));
322 for (i = 0x00; i <= 0x08; i++)
323 FD_SET(i, http_encode_map);
324 for (i = 0x0a; i <= 0x1f; i++)
325 FD_SET(i, http_encode_map);
326 FD_SET(0x7f, http_encode_map);
327
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200328 /* memory allocations */
329 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100330 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100331}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332
Willy Tarreau53b6c742006-12-17 13:37:46 +0100333/*
334 * We have 26 list of methods (1 per first letter), each of which can have
335 * up to 3 entries (2 valid, 1 null).
336 */
337struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100338 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100339 int len;
340 const char text[8];
341};
342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100343const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100344 ['C' - 'A'] = {
345 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
346 },
347 ['D' - 'A'] = {
348 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
349 },
350 ['G' - 'A'] = {
351 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
352 },
353 ['H' - 'A'] = {
354 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
355 },
Christopher Fauleta591ef22015-07-31 14:26:57 +0200356 ['O' - 'A'] = {
357 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
358 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100359 ['P' - 'A'] = {
360 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
361 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
362 },
363 ['T' - 'A'] = {
364 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
365 },
366 /* rest is empty like this :
Willy Tarreaubcd03362015-09-03 17:15:21 +0200367 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100368 */
369};
370
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100371const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100372 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
373 [HTTP_METH_GET] = { "GET", 3 },
374 [HTTP_METH_HEAD] = { "HEAD", 4 },
375 [HTTP_METH_POST] = { "POST", 4 },
376 [HTTP_METH_PUT] = { "PUT", 3 },
377 [HTTP_METH_DELETE] = { "DELETE", 6 },
378 [HTTP_METH_TRACE] = { "TRACE", 5 },
379 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
380};
381
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100382/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200383 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100384 * RFC2616 for those chars.
385 */
386
387const char http_is_spht[256] = {
388 [' '] = 1, ['\t'] = 1,
389};
390
391const char http_is_crlf[256] = {
392 ['\r'] = 1, ['\n'] = 1,
393};
394
395const char http_is_lws[256] = {
396 [' '] = 1, ['\t'] = 1,
397 ['\r'] = 1, ['\n'] = 1,
398};
399
400const char http_is_sep[256] = {
401 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
402 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
403 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
404 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
405 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
406};
407
408const char http_is_ctl[256] = {
409 [0 ... 31] = 1,
410 [127] = 1,
411};
412
413/*
414 * A token is any ASCII char that is neither a separator nor a CTL char.
415 * Do not overwrite values in assignment since gcc-2.95 will not handle
416 * them correctly. Instead, define every non-CTL char's status.
417 */
418const char http_is_token[256] = {
419 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
420 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
421 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
422 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
423 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
424 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
425 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
426 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
427 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
428 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
429 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
430 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
431 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
432 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
433 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
434 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
435 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
436 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
437 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
438 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
439 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
440 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
441 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
442 ['|'] = 1, ['}'] = 0, ['~'] = 1,
443};
444
445
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100446/*
447 * An http ver_token is any ASCII which can be found in an HTTP version,
448 * which includes 'H', 'T', 'P', '/', '.' and any digit.
449 */
450const char http_is_ver_token[256] = {
451 ['.'] = 1, ['/'] = 1,
452 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
453 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
454 ['H'] = 1, ['P'] = 1, ['T'] = 1,
455};
456
457
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100458/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100459 * Adds a header and its CRLF at the tail of the message's buffer, just before
460 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100461 * The header is also automatically added to the index <hdr_idx>, and the end
462 * of headers is automatically adjusted. The number of bytes added is returned
463 * on success, otherwise <0 is returned indicating an error.
464 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100465int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100466{
467 int bytes, len;
468
469 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200470 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100471 if (!bytes)
472 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100473 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100474 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
475}
476
477/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100478 * Adds a header and its CRLF at the tail of the message's buffer, just before
479 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100480 * the buffer is only opened and the space reserved, but nothing is copied.
481 * The header is also automatically added to the index <hdr_idx>, and the end
482 * of headers is automatically adjusted. The number of bytes added is returned
483 * on success, otherwise <0 is returned indicating an error.
484 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100485int http_header_add_tail2(struct http_msg *msg,
486 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100487{
488 int bytes;
489
Willy Tarreau9b28e032012-10-12 23:49:43 +0200490 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100491 if (!bytes)
492 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100493 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100494 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
495}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200496
497/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100498 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
499 * If so, returns the position of the first non-space character relative to
500 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
501 * to return a pointer to the place after the first space. Returns 0 if the
502 * header name does not match. Checks are case-insensitive.
503 */
504int http_header_match2(const char *hdr, const char *end,
505 const char *name, int len)
506{
507 const char *val;
508
509 if (hdr + len >= end)
510 return 0;
511 if (hdr[len] != ':')
512 return 0;
513 if (strncasecmp(hdr, name, len) != 0)
514 return 0;
515 val = hdr + len + 1;
516 while (val < end && HTTP_IS_SPHT(*val))
517 val++;
518 if ((val >= end) && (len + 2 <= end - hdr))
519 return len + 2; /* we may replace starting from second space */
520 return val - hdr;
521}
522
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200523/* Find the first or next occurrence of header <name> in message buffer <sol>
524 * using headers index <idx>, and return it in the <ctx> structure. This
525 * structure holds everything necessary to use the header and find next
526 * occurrence. If its <idx> member is 0, the header is searched from the
527 * beginning. Otherwise, the next occurrence is returned. The function returns
528 * 1 when it finds a value, and 0 when there is no more. It is very similar to
529 * http_find_header2() except that it is designed to work with full-line headers
530 * whose comma is not a delimiter but is part of the syntax. As a special case,
531 * if ctx->val is NULL when searching for a new values of a header, the current
532 * header is rescanned. This allows rescanning after a header deletion.
533 */
534int http_find_full_header2(const char *name, int len,
535 char *sol, struct hdr_idx *idx,
536 struct hdr_ctx *ctx)
537{
538 char *eol, *sov;
539 int cur_idx, old_idx;
540
541 cur_idx = ctx->idx;
542 if (cur_idx) {
543 /* We have previously returned a header, let's search another one */
544 sol = ctx->line;
545 eol = sol + idx->v[cur_idx].len;
546 goto next_hdr;
547 }
548
549 /* first request for this header */
550 sol += hdr_idx_first_pos(idx);
551 old_idx = 0;
552 cur_idx = hdr_idx_first_idx(idx);
553 while (cur_idx) {
554 eol = sol + idx->v[cur_idx].len;
555
556 if (len == 0) {
557 /* No argument was passed, we want any header.
558 * To achieve this, we simply build a fake request. */
559 while (sol + len < eol && sol[len] != ':')
560 len++;
561 name = sol;
562 }
563
564 if ((len < eol - sol) &&
565 (sol[len] == ':') &&
566 (strncasecmp(sol, name, len) == 0)) {
567 ctx->del = len;
568 sov = sol + len + 1;
569 while (sov < eol && http_is_lws[(unsigned char)*sov])
570 sov++;
571
572 ctx->line = sol;
573 ctx->prev = old_idx;
574 ctx->idx = cur_idx;
575 ctx->val = sov - sol;
576 ctx->tws = 0;
577 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
578 eol--;
579 ctx->tws++;
580 }
581 ctx->vlen = eol - sov;
582 return 1;
583 }
584 next_hdr:
585 sol = eol + idx->v[cur_idx].cr + 1;
586 old_idx = cur_idx;
587 cur_idx = idx->v[cur_idx].next;
588 }
589 return 0;
590}
591
Willy Tarreau68085d82010-01-18 14:54:04 +0100592/* Find the end of the header value contained between <s> and <e>. See RFC2616,
593 * par 2.2 for more information. Note that it requires a valid header to return
594 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200595 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100596char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200597{
598 int quoted, qdpair;
599
600 quoted = qdpair = 0;
601 for (; s < e; s++) {
602 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200603 else if (quoted) {
604 if (*s == '\\') qdpair = 1;
605 else if (*s == '"') quoted = 0;
606 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200607 else if (*s == '"') quoted = 1;
608 else if (*s == ',') return s;
609 }
610 return s;
611}
612
613/* Find the first or next occurrence of header <name> in message buffer <sol>
614 * using headers index <idx>, and return it in the <ctx> structure. This
615 * structure holds everything necessary to use the header and find next
616 * occurrence. If its <idx> member is 0, the header is searched from the
617 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100618 * 1 when it finds a value, and 0 when there is no more. It is designed to work
619 * with headers defined as comma-separated lists. As a special case, if ctx->val
620 * is NULL when searching for a new values of a header, the current header is
621 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200622 */
623int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100624 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200625 struct hdr_ctx *ctx)
626{
Willy Tarreau68085d82010-01-18 14:54:04 +0100627 char *eol, *sov;
628 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200629
Willy Tarreau68085d82010-01-18 14:54:04 +0100630 cur_idx = ctx->idx;
631 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200632 /* We have previously returned a value, let's search
633 * another one on the same line.
634 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200635 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200636 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100637 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200638 eol = sol + idx->v[cur_idx].len;
639
640 if (sov >= eol)
641 /* no more values in this header */
642 goto next_hdr;
643
Willy Tarreau68085d82010-01-18 14:54:04 +0100644 /* values remaining for this header, skip the comma but save it
645 * for later use (eg: for header deletion).
646 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200647 sov++;
648 while (sov < eol && http_is_lws[(unsigned char)*sov])
649 sov++;
650
651 goto return_hdr;
652 }
653
654 /* first request for this header */
655 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100656 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200657 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200658 while (cur_idx) {
659 eol = sol + idx->v[cur_idx].len;
660
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200661 if (len == 0) {
662 /* No argument was passed, we want any header.
663 * To achieve this, we simply build a fake request. */
664 while (sol + len < eol && sol[len] != ':')
665 len++;
666 name = sol;
667 }
668
Willy Tarreau33a7e692007-06-10 19:45:56 +0200669 if ((len < eol - sol) &&
670 (sol[len] == ':') &&
671 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100672 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200673 sov = sol + len + 1;
674 while (sov < eol && http_is_lws[(unsigned char)*sov])
675 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100676
Willy Tarreau33a7e692007-06-10 19:45:56 +0200677 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100678 ctx->prev = old_idx;
679 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200680 ctx->idx = cur_idx;
681 ctx->val = sov - sol;
682
683 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200684 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200685 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200686 eol--;
687 ctx->tws++;
688 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200689 ctx->vlen = eol - sov;
690 return 1;
691 }
692 next_hdr:
693 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100694 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200695 cur_idx = idx->v[cur_idx].next;
696 }
697 return 0;
698}
699
700int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100701 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200702 struct hdr_ctx *ctx)
703{
704 return http_find_header2(name, strlen(name), sol, idx, ctx);
705}
706
Willy Tarreau68085d82010-01-18 14:54:04 +0100707/* Remove one value of a header. This only works on a <ctx> returned by one of
708 * the http_find_header functions. The value is removed, as well as surrounding
709 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100710 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100711 * message <msg>. The new index is returned. If it is zero, it means there is
712 * no more header, so any processing may stop. The ctx is always left in a form
713 * that can be handled by http_find_header2() to find next occurrence.
714 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100715int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100716{
717 int cur_idx = ctx->idx;
718 char *sol = ctx->line;
719 struct hdr_idx_elem *hdr;
720 int delta, skip_comma;
721
722 if (!cur_idx)
723 return 0;
724
725 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200726 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100727 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200728 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100729 http_msg_move_end(msg, delta);
730 idx->used--;
731 hdr->len = 0; /* unused entry */
732 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100733 if (idx->tail == ctx->idx)
734 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100735 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau14acc652015-01-07 17:23:50 +0100736 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100737 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200738 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100739 return ctx->idx;
740 }
741
742 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200743 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
744 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100745 */
746
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200747 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200748 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200749 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100750 NULL, 0);
751 hdr->len += delta;
752 http_msg_move_end(msg, delta);
753 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200754 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100755 return ctx->idx;
756}
757
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100758/* This function handles a server error at the stream interface level. The
759 * stream interface is assumed to be already in a closed state. An optional
760 * message is copied into the input buffer, and an HTTP status code stored.
761 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100762 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200763 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200764static void http_server_error(struct session *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100765 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200766{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200767 channel_auto_read(si->ob);
768 channel_abort(si->ob);
769 channel_auto_close(si->ob);
770 channel_erase(si->ob);
771 channel_auto_close(si->ib);
772 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100773 if (status > 0 && msg) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200774 s->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200775 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200776 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200777 if (!(s->flags & SN_ERR_MASK))
778 s->flags |= err;
779 if (!(s->flags & SN_FINST_MASK))
780 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200781}
782
Willy Tarreau80587432006-12-24 17:47:20 +0100783/* This function returns the appropriate error location for the given session
784 * and message.
785 */
786
Willy Tarreau783f2582012-09-04 12:19:04 +0200787struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100788{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200789 if (s->be->errmsg[msgnum].str)
790 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100791 else if (s->fe->errmsg[msgnum].str)
792 return &s->fe->errmsg[msgnum];
793 else
794 return &http_err_chunks[msgnum];
795}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796
Willy Tarreau53b6c742006-12-17 13:37:46 +0100797/*
Willy Tarreaubcd03362015-09-03 17:15:21 +0200798 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
799 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100800 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100801enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100802{
803 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100804 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100805
806 m = ((unsigned)*str - 'A');
807
808 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100809 for (h = http_methods[m]; h->len > 0; h++) {
810 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100811 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100812 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100813 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100814 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100815 }
Willy Tarreaubcd03362015-09-03 17:15:21 +0200816 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100817}
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);
Willy Tarreau2b9b25b2016-03-13 08:17:02 +01001405 len = p - h;
1406 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001407 return 0;
1408
Willy Tarreau2b9b25b2016-03-13 08:17:02 +01001409 chunk_initlen(&auth_method, h, 0, len);
1410 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001411
1412 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1413
1414 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001415 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001416
1417 if (len < 0)
1418 return 0;
1419
1420
1421 get_http_auth_buff[len] = '\0';
1422
1423 p = strchr(get_http_auth_buff, ':');
1424
1425 if (!p)
1426 return 0;
1427
1428 txn->auth.user = get_http_auth_buff;
1429 *p = '\0';
1430 txn->auth.pass = p+1;
1431
1432 txn->auth.method = HTTP_AUTH_BASIC;
1433 return 1;
1434 }
1435
1436 return 0;
1437}
1438
Willy Tarreau58f10d72006-12-04 02:26:12 +01001439
Willy Tarreau8973c702007-01-21 23:58:29 +01001440/*
1441 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001442 * depending on the initial msg->msg_state. The caller is responsible for
1443 * ensuring that the message does not wrap. The function can be preempted
1444 * everywhere when data are missing and recalled at the exact same location
1445 * with no information loss. The message may even be realigned between two
1446 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001447 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001448 * fields. Note that msg->sol will be initialized after completing the first
1449 * state, so that none of the msg pointers has to be initialized prior to the
1450 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001451 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001452void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453{
Willy Tarreau3770f232013-12-07 00:01:53 +01001454 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001456 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001457
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001458 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001459 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001460 ptr = buf->p + msg->next;
1461 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001462
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 if (unlikely(ptr >= end))
1464 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001465
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001467 /*
1468 * First, states that are specific to the response only.
1469 * We check them first so that request and headers are
1470 * closer to each other (accessed more often).
1471 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001472 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001473 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001474 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001475 /* we have a start of message, but we have to check
1476 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001477 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001478 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001479 if (unlikely(ptr != buf->p)) {
1480 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001481 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001482 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001483 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001484 }
Willy Tarreau26927362012-05-18 23:22:52 +02001485 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001486 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001487 hdr_idx_init(idx);
1488 state = HTTP_MSG_RPVER;
1489 goto http_msg_rpver;
1490 }
1491
1492 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1493 goto http_msg_invalid;
1494
1495 if (unlikely(*ptr == '\n'))
1496 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1497 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1498 /* stop here */
1499
Willy Tarreau8973c702007-01-21 23:58:29 +01001500 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001501 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001502 EXPECT_LF_HERE(ptr, http_msg_invalid);
1503 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1504 /* stop here */
1505
Willy Tarreau8973c702007-01-21 23:58:29 +01001506 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001507 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001508 case HTTP_MSG_RPVER_SP:
1509 case HTTP_MSG_RPCODE:
1510 case HTTP_MSG_RPCODE_SP:
1511 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001512 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001513 state, ptr, end,
1514 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001515 if (unlikely(!ptr))
1516 return;
1517
1518 /* we have a full response and we know that we have either a CR
1519 * or an LF at <ptr>.
1520 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001521 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1522
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001523 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001524 if (likely(*ptr == '\r'))
1525 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1526 goto http_msg_rpline_end;
1527
Willy Tarreau8973c702007-01-21 23:58:29 +01001528 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001529 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001530 /* msg->sol must point to the first of CR or LF. */
1531 EXPECT_LF_HERE(ptr, http_msg_invalid);
1532 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1533 /* stop here */
1534
1535 /*
1536 * Second, states that are specific to the request only
1537 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001539 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001541 /* we have a start of message, but we have to check
1542 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001543 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001544 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001545 if (likely(ptr != buf->p)) {
1546 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001547 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001548 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001549 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 }
Willy Tarreau26927362012-05-18 23:22:52 +02001551 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001552 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001553 state = HTTP_MSG_RQMETH;
1554 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001555 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001556
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1558 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 if (unlikely(*ptr == '\n'))
1561 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1562 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001563 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001564
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001566 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 EXPECT_LF_HERE(ptr, http_msg_invalid);
1568 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001569 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001572 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001573 case HTTP_MSG_RQMETH_SP:
1574 case HTTP_MSG_RQURI:
1575 case HTTP_MSG_RQURI_SP:
1576 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001577 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001578 state, ptr, end,
1579 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 if (unlikely(!ptr))
1581 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001582
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 /* we have a full request and we know that we have either a CR
1584 * or an LF at <ptr>.
1585 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001587
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001588 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 if (likely(*ptr == '\r'))
1590 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001591 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001592
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001594 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001595 /* check for HTTP/0.9 request : no version information available.
1596 * msg->sol must point to the first of CR or LF.
1597 */
1598 if (unlikely(msg->sl.rq.v_l == 0))
1599 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001600
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 EXPECT_LF_HERE(ptr, http_msg_invalid);
1602 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001603 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001604
Willy Tarreau8973c702007-01-21 23:58:29 +01001605 /*
1606 * Common states below
1607 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001608 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001609 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001610 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 if (likely(!HTTP_IS_CRLF(*ptr))) {
1612 goto http_msg_hdr_name;
1613 }
1614
1615 if (likely(*ptr == '\r'))
1616 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1617 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001618
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001619 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001620 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 /* assumes msg->sol points to the first char */
1622 if (likely(HTTP_IS_TOKEN(*ptr)))
1623 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001624
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001625 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001627
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001628 if (likely(msg->err_pos < -1) || *ptr == '\n')
1629 goto http_msg_invalid;
1630
1631 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001632 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001633
1634 /* and we still accept this non-token character */
1635 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001636
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001638 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001639 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 if (likely(HTTP_IS_SPHT(*ptr)))
1641 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001642
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001644 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001645
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001646 if (likely(!HTTP_IS_CRLF(*ptr))) {
1647 goto http_msg_hdr_val;
1648 }
1649
1650 if (likely(*ptr == '\r'))
1651 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1652 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001653
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001655 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 EXPECT_LF_HERE(ptr, http_msg_invalid);
1657 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001658
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001659 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001660 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001661 if (likely(HTTP_IS_SPHT(*ptr))) {
1662 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001663 for (; buf->p + msg->sov < ptr; msg->sov++)
1664 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001665 goto http_msg_hdr_l1_sp;
1666 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001667 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001668 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 goto http_msg_complete_header;
1670
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001672 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001673 /* assumes msg->sol points to the first char, and msg->sov
1674 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 */
1676 if (likely(!HTTP_IS_CRLF(*ptr)))
1677 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001678
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001679 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001680 /* Note: we could also copy eol into ->eoh so that we have the
1681 * real header end in case it ends with lots of LWS, but is this
1682 * really needed ?
1683 */
1684 if (likely(*ptr == '\r'))
1685 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1686 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001687
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001688 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001689 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 EXPECT_LF_HERE(ptr, http_msg_invalid);
1691 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001692
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001693 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001694 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001695 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1696 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001697 for (; buf->p + msg->eol < ptr; msg->eol++)
1698 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001699 goto http_msg_hdr_val;
1700 }
1701 http_msg_complete_header:
1702 /*
1703 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001704 * Assumes msg->sol points to the first char, msg->sov points
1705 * to the first character of the value and msg->eol to the
1706 * first CR or LF so we know how the line ends. We insert last
1707 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001709 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710 idx, idx->tail) < 0))
1711 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001712
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001713 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001714 if (likely(!HTTP_IS_CRLF(*ptr))) {
1715 goto http_msg_hdr_name;
1716 }
1717
1718 if (likely(*ptr == '\r'))
1719 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1720 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001721
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001723 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001724 /* Assumes msg->sol points to the first of either CR or LF.
1725 * Sets ->sov and ->next to the total header length, ->eoh to
1726 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1727 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 EXPECT_LF_HERE(ptr, http_msg_invalid);
1729 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001730 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001731 msg->eoh = msg->sol;
1732 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001733 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001734 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001735 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001736
1737 case HTTP_MSG_ERROR:
1738 /* this may only happen if we call http_msg_analyser() twice with an error */
1739 break;
1740
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001741 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001742#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001743 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1744 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001745#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001746 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001747 }
1748 http_msg_ood:
1749 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001750 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001751 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001753
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 http_msg_invalid:
1755 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001756 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001757 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 return;
1759}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001760
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001761/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1762 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1763 * nothing is done and 1 is returned.
1764 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001765static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001766{
1767 int delta;
1768 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001769 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001770
1771 if (msg->sl.rq.v_l != 0)
1772 return 1;
1773
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001774 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1775 if (txn->meth != HTTP_METH_GET)
1776 return 0;
1777
Willy Tarreau9b28e032012-10-12 23:49:43 +02001778 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001779 delta = 0;
1780
1781 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001782 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1783 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001784 }
1785 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001786 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001787 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001788 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001789 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001790 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001791 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001792 NULL, NULL);
1793 if (unlikely(!cur_end))
1794 return 0;
1795
1796 /* we have a full HTTP/1.0 request now and we know that
1797 * we have either a CR or an LF at <ptr>.
1798 */
1799 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1800 return 1;
1801}
1802
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001803/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001804 * and "keep-alive" values. If we already know that some headers may safely
1805 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001806 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1807 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001808 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001809 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1810 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1811 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001812 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001813 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001814void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001815{
Willy Tarreau5b154472009-12-21 20:11:07 +01001816 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001817 const char *hdr_val = "Connection";
1818 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001819
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001820 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001821 return;
1822
Willy Tarreau88d349d2010-01-25 12:15:43 +01001823 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1824 hdr_val = "Proxy-Connection";
1825 hdr_len = 16;
1826 }
1827
Willy Tarreau5b154472009-12-21 20:11:07 +01001828 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001829 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001830 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001831 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1832 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001833 if (to_del & 2)
1834 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001835 else
1836 txn->flags |= TX_CON_KAL_SET;
1837 }
1838 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1839 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001840 if (to_del & 1)
1841 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001842 else
1843 txn->flags |= TX_CON_CLO_SET;
1844 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001845 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1846 txn->flags |= TX_HDR_CONN_UPG;
1847 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001848 }
1849
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001850 txn->flags |= TX_HDR_CONN_PRS;
1851 return;
1852}
Willy Tarreau5b154472009-12-21 20:11:07 +01001853
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001854/* Apply desired changes on the Connection: header. Values may be removed and/or
1855 * added depending on the <wanted> flags, which are exclusively composed of
1856 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1857 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1858 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001859void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001860{
1861 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001862 const char *hdr_val = "Connection";
1863 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001864
1865 ctx.idx = 0;
1866
Willy Tarreau88d349d2010-01-25 12:15:43 +01001867
1868 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1869 hdr_val = "Proxy-Connection";
1870 hdr_len = 16;
1871 }
1872
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001873 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001874 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001875 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1876 if (wanted & TX_CON_KAL_SET)
1877 txn->flags |= TX_CON_KAL_SET;
1878 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001879 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001880 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001881 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1882 if (wanted & TX_CON_CLO_SET)
1883 txn->flags |= TX_CON_CLO_SET;
1884 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001885 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001886 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001887 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001888
1889 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1890 return;
1891
1892 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1893 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001894 hdr_val = "Connection: close";
1895 hdr_len = 17;
1896 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1897 hdr_val = "Proxy-Connection: close";
1898 hdr_len = 23;
1899 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001900 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001901 }
1902
1903 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1904 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001905 hdr_val = "Connection: keep-alive";
1906 hdr_len = 22;
1907 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1908 hdr_val = "Proxy-Connection: keep-alive";
1909 hdr_len = 28;
1910 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001911 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001912 }
1913 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001914}
1915
Willy Tarreauc24715e2014-04-17 15:21:20 +02001916/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1917 * the first byte of data after the chunk size, so that we know we can forward
1918 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1919 * the chunk size. That way it is always possible to differentiate between the
1920 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001921 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001922 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001923 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001924static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001925{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001926 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001927 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001928 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001929 const char *end = buf->data + buf->size;
1930 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001931 unsigned int chunk = 0;
1932
1933 /* The chunk size is in the following form, though we are only
1934 * interested in the size and CRLF :
1935 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1936 */
1937 while (1) {
1938 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001939 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001940 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001941 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001942 if (c < 0) /* not a hex digit anymore */
1943 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001944 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001945 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001946 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001947 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001948 chunk = (chunk << 4) + c;
1949 }
1950
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001952 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001953 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001954
1955 while (http_is_spht[(unsigned char)*ptr]) {
1956 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001957 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001958 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001959 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001960 }
1961
Willy Tarreaud98cf932009-12-27 22:54:55 +01001962 /* Up to there, we know that at least one byte is present at *ptr. Check
1963 * for the end of chunk size.
1964 */
1965 while (1) {
1966 if (likely(HTTP_IS_CRLF(*ptr))) {
1967 /* we now have a CR or an LF at ptr */
1968 if (likely(*ptr == '\r')) {
1969 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001970 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001971 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001972 return 0;
1973 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001974
Willy Tarreaud98cf932009-12-27 22:54:55 +01001975 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001976 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001977 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001978 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001979 /* done */
1980 break;
1981 }
1982 else if (*ptr == ';') {
1983 /* chunk extension, ends at next CRLF */
1984 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001985 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001986 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001987 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001988
1989 while (!HTTP_IS_CRLF(*ptr)) {
1990 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001991 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001992 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001993 return 0;
1994 }
1995 /* we have a CRLF now, loop above */
1996 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001997 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001998 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001999 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002000 }
2001
Willy Tarreaud98cf932009-12-27 22:54:55 +01002002 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002003 * which may or may not be present. We save that into ->next,
2004 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002005 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002006 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002007 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002008 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002009 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002010 msg->chunk_len = chunk;
2011 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002012 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002013 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002014 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002015 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002016 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002017}
2018
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002019/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002020 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002021 * the trailers is found, it is automatically scheduled to be forwarded,
2022 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2023 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002024 * except maybe msg->next if it could parse some lines, and returns zero.
2025 * If a parse error is encountered, the function returns < 0 and does not
2026 * change anything except maybe msg->next. Note that the message must
2027 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002028 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002029 * forwarding, and that msg->next exactly matches the length of trailers
2030 * already parsed and not forwarded. It is also important to note that this
2031 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002032 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002033static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002034{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002035 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002036
Willy Tarreaua458b672012-03-05 11:17:50 +01002037 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002038 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002039 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002040 const char *ptr = b_ptr(buf, msg->next);
2041 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002042 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002043
2044 /* scan current line and stop at LF or CRLF */
2045 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002046 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002047 return 0;
2048
2049 if (*ptr == '\n') {
2050 if (!p1)
2051 p1 = ptr;
2052 p2 = ptr;
2053 break;
2054 }
2055
2056 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002057 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002058 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002059 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002060 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002061 p1 = ptr;
2062 }
2063
2064 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002065 if (ptr >= buf->data + buf->size)
2066 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002067 }
2068
2069 /* after LF; point to beginning of next line */
2070 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002071 if (p2 >= buf->data + buf->size)
2072 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002073
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002074 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002075 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002076 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002077
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002078 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002079 /* LF/CRLF at beginning of line => end of trailers at p2.
2080 * Everything was scheduled for forwarding, there's nothing
2081 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002082 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002083 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002084 msg->msg_state = HTTP_MSG_DONE;
2085 return 1;
2086 }
2087 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002088 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002089 }
2090}
2091
Willy Tarreauc24715e2014-04-17 15:21:20 +02002092/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2093 * or a possible LF alone at the end of a chunk. It automatically adjusts
2094 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002095 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002096 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2097 * not enough data are available, the function does not change anything and
2098 * returns zero. If a parse error is encountered, the function returns < 0 and
2099 * does not change anything. Note: this function is designed to parse wrapped
2100 * CRLF at the end of the buffer.
2101 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002102static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002103{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002104 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002105 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002106 int bytes;
2107
2108 /* NB: we'll check data availabilty at the end. It's not a
2109 * problem because whatever we match first will be checked
2110 * against the correct length.
2111 */
2112 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002113 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002114 if (*ptr == '\r') {
2115 bytes++;
2116 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002117 if (ptr >= buf->data + buf->size)
2118 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002119 }
2120
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002121 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002122 return 0;
2123
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002124 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002125 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002126 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002127 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002128
2129 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002130 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002131 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002132 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002133 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002134 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2135 return 1;
2136}
Willy Tarreau5b154472009-12-21 20:11:07 +01002137
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002138/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2139 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2140 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2141 * Unparsable qvalues return 1000 as "q=1.000".
2142 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002143int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002144{
2145 int q = 1000;
2146
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002147 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002148 goto out;
2149 q = (*qvalue++ - '0') * 1000;
2150
2151 if (*qvalue++ != '.')
2152 goto out;
2153
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002154 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002155 goto out;
2156 q += (*qvalue++ - '0') * 100;
2157
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002158 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002159 goto out;
2160 q += (*qvalue++ - '0') * 10;
2161
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002162 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002163 goto out;
2164 q += (*qvalue++ - '0') * 1;
2165 out:
2166 if (q > 1000)
2167 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002168 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002169 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002170 return q;
2171}
William Lallemand82fe75c2012-10-23 10:25:10 +02002172
2173/*
2174 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002175 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002176int select_compression_request_header(struct session *s, struct buffer *req)
2177{
2178 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002179 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002180 struct hdr_ctx ctx;
2181 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002182 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002183
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002184 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2185 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002186 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2187 */
2188 ctx.idx = 0;
2189 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2190 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002191 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2192 (ctx.vlen < 31 ||
2193 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2194 ctx.line[ctx.val + 30] < '6' ||
2195 (ctx.line[ctx.val + 30] == '6' &&
2196 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2197 s->comp_algo = NULL;
2198 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002199 }
2200
William Lallemand82fe75c2012-10-23 10:25:10 +02002201 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002202 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 +01002203 int best_q = 0;
2204
William Lallemand82fe75c2012-10-23 10:25:10 +02002205 ctx.idx = 0;
2206 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002207 const char *qval;
2208 int q;
2209 int toklen;
2210
2211 /* try to isolate the token from the optional q-value */
2212 toklen = 0;
2213 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2214 toklen++;
2215
2216 qval = ctx.line + ctx.val + toklen;
2217 while (1) {
2218 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2219 qval++;
2220
2221 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2222 qval = NULL;
2223 break;
2224 }
2225 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002226
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002227 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2228 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002229
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002230 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2231 qval = NULL;
2232 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002233 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002234 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2235 break;
2236
2237 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2238 qval++;
2239 }
2240
2241 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002242 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002243
2244 if (q <= best_q)
2245 continue;
2246
2247 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2248 if (*(ctx.line + ctx.val) == '*' ||
2249 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2250 s->comp_algo = comp_algo;
2251 best_q = q;
2252 break;
2253 }
2254 }
2255 }
2256 }
2257
2258 /* remove all occurrences of the header when "compression offload" is set */
2259 if (s->comp_algo) {
2260 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2261 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2262 ctx.idx = 0;
2263 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2264 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002265 }
2266 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002267 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002268 }
2269
2270 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002271 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2272 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002273 if (comp_algo->add_data == identity_add_data) {
2274 s->comp_algo = comp_algo;
2275 return 1;
2276 }
2277 }
2278 }
2279
2280 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002281 return 0;
2282}
2283
2284/*
2285 * Selects a comression algorithm depending of the server response.
2286 */
2287int select_compression_response_header(struct session *s, struct buffer *res)
2288{
2289 struct http_txn *txn = &s->txn;
2290 struct http_msg *msg = &txn->rsp;
2291 struct hdr_ctx ctx;
2292 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002293
2294 /* no common compression algorithm was found in request header */
2295 if (s->comp_algo == NULL)
2296 goto fail;
2297
2298 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002299 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002300 goto fail;
2301
William Lallemandd3002612012-11-26 14:34:47 +01002302 /* 200 only */
2303 if (txn->status != 200)
2304 goto fail;
2305
William Lallemand82fe75c2012-10-23 10:25:10 +02002306 /* Content-Length is null */
2307 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2308 goto fail;
2309
2310 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002311 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002312 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2313 goto fail;
2314
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002315 /* no compression when Cache-Control: no-transform is present in the message */
2316 ctx.idx = 0;
2317 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2318 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2319 goto fail;
2320 }
2321
William Lallemand82fe75c2012-10-23 10:25:10 +02002322 comp_type = NULL;
2323
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002324 /* we don't want to compress multipart content-types, nor content-types that are
2325 * not listed in the "compression type" directive if any. If no content-type was
2326 * found but configuration requires one, we don't compress either. Backend has
2327 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002328 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002329 ctx.idx = 0;
2330 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2331 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2332 goto fail;
2333
2334 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2335 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002336 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002337 if (ctx.vlen >= comp_type->name_len &&
2338 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002339 /* this Content-Type should be compressed */
2340 break;
2341 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002342 /* this Content-Type should not be compressed */
2343 if (comp_type == NULL)
2344 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002345 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002346 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002347 else { /* no content-type header */
2348 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2349 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002350 }
2351
William Lallemandd85f9172012-11-09 17:05:39 +01002352 /* limit compression rate */
2353 if (global.comp_rate_lim > 0)
2354 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2355 goto fail;
2356
William Lallemand072a2bf2012-11-20 17:01:01 +01002357 /* limit cpu usage */
2358 if (idle_pct < compress_min_idle)
2359 goto fail;
2360
William Lallemand4c49fae2012-11-07 15:00:23 +01002361 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002362 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002363 goto fail;
2364
William Lallemandec3e3892012-11-12 17:02:18 +01002365 s->flags |= SN_COMP_READY;
2366
William Lallemand82fe75c2012-10-23 10:25:10 +02002367 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002368 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002369 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2370 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2371
2372 /* add Transfer-Encoding header */
2373 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2374 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2375
2376 /*
2377 * Add Content-Encoding header when it's not identity encoding.
2378 * RFC 2616 : Identity encoding: This content-coding is used only in the
2379 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2380 * header.
2381 */
2382 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002383 trash.len = 18;
2384 memcpy(trash.str, "Content-Encoding: ", trash.len);
2385 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2386 trash.len += s->comp_algo->name_len;
2387 trash.str[trash.len] = '\0';
2388 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002389 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002390 return 1;
2391
2392fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002393 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002394 return 0;
2395}
2396
Willy Tarreau2e47a3a2014-09-30 18:44:22 +02002397void http_adjust_conn_mode(struct session *s, struct http_txn *txn, struct http_msg *msg)
2398{
2399 int tmp = TX_CON_WANT_KAL;
2400
2401 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2402 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
2403 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2404 tmp = TX_CON_WANT_TUN;
2405
2406 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2407 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2408 tmp = TX_CON_WANT_TUN;
2409 }
2410
2411 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
2412 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2413 /* option httpclose + server_close => forceclose */
2414 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2415 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2416 tmp = TX_CON_WANT_CLO;
2417 else
2418 tmp = TX_CON_WANT_SCL;
2419 }
2420
2421 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
2422 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2423 tmp = TX_CON_WANT_CLO;
2424
2425 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2426 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2427
2428 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2429 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2430 /* parse the Connection header and possibly clean it */
2431 int to_del = 0;
2432 if ((msg->flags & HTTP_MSGF_VER_11) ||
2433 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2434 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
2435 to_del |= 2; /* remove "keep-alive" */
2436 if (!(msg->flags & HTTP_MSGF_VER_11))
2437 to_del |= 1; /* remove "close" */
2438 http_parse_connection_header(txn, msg, to_del);
2439 }
2440
2441 /* check if client or config asks for explicit close in KAL/SCL */
2442 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2443 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2444 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2445 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2446 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
2447 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
2448 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2449}
William Lallemand82fe75c2012-10-23 10:25:10 +02002450
Willy Tarreaud787e662009-07-07 10:14:51 +02002451/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2452 * processing can continue on next analysers, or zero if it either needs more
2453 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2454 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2455 * when it has nothing left to do, and may remove any analyser when it wants to
2456 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002457 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002458int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002459{
Willy Tarreau59234e92008-11-30 23:51:27 +01002460 /*
2461 * We will parse the partial (or complete) lines.
2462 * We will check the request syntax, and also join multi-line
2463 * headers. An index of all the lines will be elaborated while
2464 * parsing.
2465 *
2466 * For the parsing, we use a 28 states FSM.
2467 *
2468 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002469 * req->buf->p = beginning of request
2470 * req->buf->p + msg->eoh = end of processed headers / start of current one
2471 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002472 * msg->eol = end of current header or line (LF or CRLF)
2473 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002474 *
2475 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002476 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002477 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2478 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002479 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002480
Willy Tarreau59234e92008-11-30 23:51:27 +01002481 int cur_idx;
2482 struct http_txn *txn = &s->txn;
2483 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002484 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002485
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002486 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002487 now_ms, __FUNCTION__,
2488 s,
2489 req,
2490 req->rex, req->wex,
2491 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002492 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002493 req->analysers);
2494
Willy Tarreau52a0c602009-08-16 22:45:38 +02002495 /* we're speaking HTTP here, so let's speak HTTP to the client */
2496 s->srv_error = http_return_srv_error;
2497
Willy Tarreau83e3af02009-12-28 17:39:57 +01002498 /* There's a protected area at the end of the buffer for rewriting
2499 * purposes. We don't want to start to parse the request if the
2500 * protected area is affected, because we may have to move processed
2501 * data later, which is much more complicated.
2502 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002503 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002504 if (txn->flags & TX_NOT_FIRST) {
2505 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002506 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002507 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002508 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002509 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002510 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002511 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002512 return 0;
2513 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002514 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2515 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2516 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002517 }
2518
Willy Tarreau065e8332010-01-08 00:30:20 +01002519 /* Note that we have the same problem with the response ; we
2520 * may want to send a redirect, error or anything which requires
2521 * some spare space. So we'll ensure that we have at least
2522 * maxrewrite bytes available in the response buffer before
2523 * processing that one. This will only affect pipelined
2524 * keep-alive requests.
2525 */
2526 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002527 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002528 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2529 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2530 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002531 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002532 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002533 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002534 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002535 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002536 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002537 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002538 return 0;
2539 }
2540 }
2541
Willy Tarreau9b28e032012-10-12 23:49:43 +02002542 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002543 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002544 }
2545
Willy Tarreau59234e92008-11-30 23:51:27 +01002546 /* 1: we might have to print this header in debug mode */
2547 if (unlikely((global.mode & MODE_DEBUG) &&
2548 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau8767b132014-10-21 19:36:09 +02002549 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002551
Willy Tarreau9b28e032012-10-12 23:49:43 +02002552 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002553 /* this is a bit complex : in case of error on the request line,
2554 * we know that rq.l is still zero, so we display only the part
2555 * up to the end of the line (truncated by debug_hdr).
2556 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002557 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002558 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002559
Willy Tarreau59234e92008-11-30 23:51:27 +01002560 sol += hdr_idx_first_pos(&txn->hdr_idx);
2561 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002562
Willy Tarreau59234e92008-11-30 23:51:27 +01002563 while (cur_idx) {
2564 eol = sol + txn->hdr_idx.v[cur_idx].len;
2565 debug_hdr("clihdr", s, sol, eol);
2566 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2567 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002568 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002569 }
2570
Willy Tarreau58f10d72006-12-04 02:26:12 +01002571
Willy Tarreau59234e92008-11-30 23:51:27 +01002572 /*
2573 * Now we quickly check if we have found a full valid request.
2574 * If not so, we check the FD and buffer states before leaving.
2575 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002576 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002577 * requests are checked first. When waiting for a second request
2578 * on a keep-alive session, if we encounter and error, close, t/o,
2579 * we note the error in the session flags but don't set any state.
2580 * Since the error will be noted there, it will not be counted by
2581 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002582 * Last, we may increase some tracked counters' http request errors on
2583 * the cases that are deliberately the client's fault. For instance,
2584 * a timeout or connection reset is not counted as an error. However
2585 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002587
Willy Tarreau655dce92009-11-08 13:10:58 +01002588 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002589 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002590 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002591 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002592 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002593 session_inc_http_req_ctr(s);
2594 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002595 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002596 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002597 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002598
Willy Tarreau59234e92008-11-30 23:51:27 +01002599 /* 1: Since we are in header mode, if there's no space
2600 * left for headers, we won't be able to free more
2601 * later, so the session will never terminate. We
2602 * must terminate it now.
2603 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002604 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002605 /* FIXME: check if URI is set and return Status
2606 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002607 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002608 session_inc_http_req_ctr(s);
2609 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002610 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002611 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002612 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002613 goto return_bad_req;
2614 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002615
Willy Tarreau59234e92008-11-30 23:51:27 +01002616 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002617 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002618 if (!(s->flags & SN_ERR_MASK))
2619 s->flags |= SN_ERR_CLICL;
2620
Willy Tarreaufcffa692010-01-10 14:21:19 +01002621 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002622 goto failed_keep_alive;
2623
Willy Tarreau1c3a6122015-05-01 15:37:53 +02002624 if (s->fe->options & PR_O_IGNORE_PRB)
2625 goto failed_keep_alive;
2626
Willy Tarreau59234e92008-11-30 23:51:27 +01002627 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002628 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002629 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002630 session_inc_http_err_ctr(s);
2631 }
2632
Willy Tarreaudc979f22012-12-04 10:39:01 +01002633 txn->status = 400;
2634 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002635 msg->msg_state = HTTP_MSG_ERROR;
2636 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002637
Willy Tarreauda7ff642010-06-23 11:44:09 +02002638 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002639 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002640 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002641 if (s->listener->counters)
2642 s->listener->counters->failed_req++;
2643
Willy Tarreau59234e92008-11-30 23:51:27 +01002644 if (!(s->flags & SN_FINST_MASK))
2645 s->flags |= SN_FINST_R;
2646 return 0;
2647 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002648
Willy Tarreau59234e92008-11-30 23:51:27 +01002649 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002650 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002651 if (!(s->flags & SN_ERR_MASK))
2652 s->flags |= SN_ERR_CLITO;
2653
Willy Tarreaufcffa692010-01-10 14:21:19 +01002654 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002655 goto failed_keep_alive;
2656
Willy Tarreau1c3a6122015-05-01 15:37:53 +02002657 if (s->fe->options & PR_O_IGNORE_PRB)
2658 goto failed_keep_alive;
2659
Willy Tarreau59234e92008-11-30 23:51:27 +01002660 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002661 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002662 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002663 session_inc_http_err_ctr(s);
2664 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002665 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002666 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002667 msg->msg_state = HTTP_MSG_ERROR;
2668 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002669
Willy Tarreauda7ff642010-06-23 11:44:09 +02002670 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002671 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002672 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002673 if (s->listener->counters)
2674 s->listener->counters->failed_req++;
2675
Willy Tarreau59234e92008-11-30 23:51:27 +01002676 if (!(s->flags & SN_FINST_MASK))
2677 s->flags |= SN_FINST_R;
2678 return 0;
2679 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002680
Willy Tarreau59234e92008-11-30 23:51:27 +01002681 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002682 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002683 if (!(s->flags & SN_ERR_MASK))
2684 s->flags |= SN_ERR_CLICL;
2685
Willy Tarreaufcffa692010-01-10 14:21:19 +01002686 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002687 goto failed_keep_alive;
2688
Willy Tarreau1c3a6122015-05-01 15:37:53 +02002689 if (s->fe->options & PR_O_IGNORE_PRB)
2690 goto failed_keep_alive;
2691
Willy Tarreau4076a152009-04-02 15:18:36 +02002692 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002693 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002694 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002695 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002696 msg->msg_state = HTTP_MSG_ERROR;
2697 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002698
Willy Tarreauda7ff642010-06-23 11:44:09 +02002699 session_inc_http_err_ctr(s);
2700 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002701 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002702 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002703 if (s->listener->counters)
2704 s->listener->counters->failed_req++;
2705
Willy Tarreau59234e92008-11-30 23:51:27 +01002706 if (!(s->flags & SN_FINST_MASK))
2707 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002708 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002709 }
2710
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002711 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002712 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2713 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002714#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002715 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 +01002716 /* We need more data, we have to re-enable quick-ack in case we
2717 * previously disabled it, otherwise we might cause the client
2718 * to delay next data.
2719 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002720 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002721 }
2722#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002723
Willy Tarreaufcffa692010-01-10 14:21:19 +01002724 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2725 /* If the client starts to talk, let's fall back to
2726 * request timeout processing.
2727 */
2728 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002729 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002730 }
2731
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002733 if (!tick_isset(req->analyse_exp)) {
2734 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2735 (txn->flags & TX_WAIT_NEXT_RQ) &&
2736 tick_isset(s->be->timeout.httpka))
2737 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2738 else
2739 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2740 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002741
Willy Tarreau59234e92008-11-30 23:51:27 +01002742 /* we're not ready yet */
2743 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002744
2745 failed_keep_alive:
2746 /* Here we process low-level errors for keep-alive requests. In
2747 * short, if the request is not the first one and it experiences
2748 * a timeout, read error or shutdown, we just silently close so
2749 * that the client can try again.
2750 */
2751 txn->status = 0;
2752 msg->msg_state = HTTP_MSG_RQBEFORE;
2753 req->analysers = 0;
2754 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002755 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002756 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002757 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002758 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002759 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002760
Willy Tarreaud787e662009-07-07 10:14:51 +02002761 /* OK now we have a complete HTTP request with indexed headers. Let's
2762 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002763 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002764 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002765 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002766 * byte after the last LF. msg->sov points to the first byte of data.
2767 * msg->eol cannot be trusted because it may have been left uninitialized
2768 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002769 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002770
Willy Tarreauda7ff642010-06-23 11:44:09 +02002771 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002772 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2773
Willy Tarreaub16a5742010-01-10 14:46:16 +01002774 if (txn->flags & TX_WAIT_NEXT_RQ) {
2775 /* kill the pending keep-alive timeout */
2776 txn->flags &= ~TX_WAIT_NEXT_RQ;
2777 req->analyse_exp = TICK_ETERNITY;
2778 }
2779
2780
Willy Tarreaud787e662009-07-07 10:14:51 +02002781 /* Maybe we found in invalid header name while we were configured not
2782 * to block on that, so we have to capture it now.
2783 */
2784 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002785 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002786
Willy Tarreau59234e92008-11-30 23:51:27 +01002787 /*
2788 * 1: identify the method
2789 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002790 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002791
2792 /* we can make use of server redirect on GET and HEAD */
2793 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2794 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002795
Willy Tarreau59234e92008-11-30 23:51:27 +01002796 /*
2797 * 2: check if the URI matches the monitor_uri.
2798 * We have to do this for every request which gets in, because
2799 * the monitor-uri is defined by the frontend.
2800 */
2801 if (unlikely((s->fe->monitor_uri_len != 0) &&
2802 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002803 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002804 s->fe->monitor_uri,
2805 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002806 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002807 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002808 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002809 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002810
Willy Tarreau59234e92008-11-30 23:51:27 +01002811 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002812 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002813
Willy Tarreau59234e92008-11-30 23:51:27 +01002814 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002815 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002816 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002817
Willy Tarreau59234e92008-11-30 23:51:27 +01002818 ret = acl_pass(ret);
2819 if (cond->pol == ACL_COND_UNLESS)
2820 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002821
Willy Tarreau59234e92008-11-30 23:51:27 +01002822 if (ret) {
2823 /* we fail this request, let's return 503 service unavail */
2824 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002825 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
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;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002829 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002830 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002831
Willy Tarreau59234e92008-11-30 23:51:27 +01002832 /* nothing to fail, let's reply normaly */
2833 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002834 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002835 if (!(s->flags & SN_ERR_MASK))
2836 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002837 goto return_prx_cond;
2838 }
2839
2840 /*
2841 * 3: Maybe we have to copy the original REQURI for the logs ?
2842 * Note: we cannot log anymore if the request has been
2843 * classified as invalid.
2844 */
2845 if (unlikely(s->logs.logwait & LW_REQ)) {
2846 /* we have a complete HTTP request that we must log */
2847 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2848 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002849
Willy Tarreau59234e92008-11-30 23:51:27 +01002850 if (urilen >= REQURI_LEN)
2851 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002852 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002853 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002854
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002855 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002856 s->do_log(s);
2857 } else {
2858 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002859 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002860 }
Willy Tarreau06619262006-12-17 08:37:22 +01002861
Willy Tarreau59234e92008-11-30 23:51:27 +01002862 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002863 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002864 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002865
Willy Tarreau55645552015-05-01 13:26:00 +02002866 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2867 * exactly one digit "." one digit. This check may be disabled using
2868 * option accept-invalid-http-request.
2869 */
2870 if (!(s->fe->options2 & PR_O2_REQBUG_OK)) {
2871 if (msg->sl.rq.v_l != 8) {
2872 msg->err_pos = msg->sl.rq.v;
2873 goto return_bad_req;
2874 }
2875
2876 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2877 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2878 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2879 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2880 msg->err_pos = msg->sl.rq.v + 4;
2881 goto return_bad_req;
2882 }
2883 }
2884
Willy Tarreau5b154472009-12-21 20:11:07 +01002885 /* ... and check if the request is HTTP/1.1 or above */
2886 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002887 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2888 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2889 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002890 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002891
2892 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002893 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 +01002894
Willy Tarreau88d349d2010-01-25 12:15:43 +01002895 /* if the frontend has "option http-use-proxy-header", we'll check if
2896 * we have what looks like a proxied connection instead of a connection,
2897 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2898 * Note that this is *not* RFC-compliant, however browsers and proxies
2899 * happen to do that despite being non-standard :-(
2900 * We consider that a request not beginning with either '/' or '*' is
2901 * a proxied connection, which covers both "scheme://location" and
2902 * CONNECT ip:port.
2903 */
2904 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002905 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002906 txn->flags |= TX_USE_PX_CONN;
2907
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002908 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002909 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002910
Willy Tarreau59234e92008-11-30 23:51:27 +01002911 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002912 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002913 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002914 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002915
Willy Tarreau4db603d2015-05-01 10:05:17 +02002916 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2917 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002918 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002919 * The length of a message body is determined by one of the following
2920 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002921 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002922 * 1. Any response to a HEAD request and any response with a 1xx
2923 * (Informational), 204 (No Content), or 304 (Not Modified) status
2924 * code is always terminated by the first empty line after the
2925 * header fields, regardless of the header fields present in the
2926 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002927 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002928 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2929 * the connection will become a tunnel immediately after the empty
2930 * line that concludes the header fields. A client MUST ignore any
2931 * Content-Length or Transfer-Encoding header fields received in
2932 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002933 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002934 * 3. If a Transfer-Encoding header field is present and the chunked
2935 * transfer coding (Section 4.1) is the final encoding, the message
2936 * body length is determined by reading and decoding the chunked
2937 * data until the transfer coding indicates the data is complete.
2938 *
2939 * If a Transfer-Encoding header field is present in a response and
2940 * the chunked transfer coding is not the final encoding, the
2941 * message body length is determined by reading the connection until
2942 * it is closed by the server. If a Transfer-Encoding header field
2943 * is present in a request and the chunked transfer coding is not
2944 * the final encoding, the message body length cannot be determined
2945 * reliably; the server MUST respond with the 400 (Bad Request)
2946 * status code and then close the connection.
2947 *
2948 * If a message is received with both a Transfer-Encoding and a
2949 * Content-Length header field, the Transfer-Encoding overrides the
2950 * Content-Length. Such a message might indicate an attempt to
2951 * perform request smuggling (Section 9.5) or response splitting
2952 * (Section 9.4) and ought to be handled as an error. A sender MUST
2953 * remove the received Content-Length field prior to forwarding such
2954 * a message downstream.
2955 *
2956 * 4. If a message is received without Transfer-Encoding and with
2957 * either multiple Content-Length header fields having differing
2958 * field-values or a single Content-Length header field having an
2959 * invalid value, then the message framing is invalid and the
2960 * recipient MUST treat it as an unrecoverable error. If this is a
2961 * request message, the server MUST respond with a 400 (Bad Request)
2962 * status code and then close the connection. If this is a response
2963 * message received by a proxy, the proxy MUST close the connection
2964 * to the server, discard the received response, and send a 502 (Bad
2965 * Gateway) response to the client. If this is a response message
2966 * received by a user agent, the user agent MUST close the
2967 * connection to the server and discard the received response.
2968 *
2969 * 5. If a valid Content-Length header field is present without
2970 * Transfer-Encoding, its decimal value defines the expected message
2971 * body length in octets. If the sender closes the connection or
2972 * the recipient times out before the indicated number of octets are
2973 * received, the recipient MUST consider the message to be
2974 * incomplete and close the connection.
2975 *
2976 * 6. If this is a request message and none of the above are true, then
2977 * the message body length is zero (no message body is present).
2978 *
2979 * 7. Otherwise, this is a response message without a declared message
2980 * body length, so the message body length is determined by the
2981 * number of octets received prior to the server closing the
2982 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002983 */
2984
Willy Tarreau32b47f42009-10-18 20:55:02 +02002985 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002986 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaue77bc172015-05-01 10:06:30 +02002987 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002988 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002989 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2990 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau49efa262015-05-01 10:09:49 +02002991 /* chunked not last, return badreq */
2992 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002993 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002994 }
2995
Willy Tarreaudfa3d922015-04-30 10:57:51 +02002996 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002997 ctx.idx = 0;
Willy Tarreaudfa3d922015-04-30 10:57:51 +02002998 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2999 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3000 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3001 }
Willy Tarreau49efa262015-05-01 10:09:49 +02003002 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003003 signed long long cl;
3004
Willy Tarreauad14f752011-09-02 20:33:27 +02003005 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003006 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003007 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003008 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003009
Willy Tarreauad14f752011-09-02 20:33:27 +02003010 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003011 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003012 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003013 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003014
Willy Tarreauad14f752011-09-02 20:33:27 +02003015 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003016 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003017 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003018 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003019
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003020 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003021 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003022 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003023 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003024
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003025 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003026 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003027 }
3028
Willy Tarreau49efa262015-05-01 10:09:49 +02003029 /* even bodyless requests have a known length */
3030 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003031
Willy Tarreau179085c2014-04-28 16:48:56 +02003032 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3033 * only change if both the request and the config reference something else.
3034 * Option httpclose by itself sets tunnel mode where headers are mangled.
3035 * However, if another mode is set, it will affect it (eg: server-close/
3036 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3037 * if FE and BE have the same settings (common). The method consists in
3038 * checking if options changed between the two calls (implying that either
3039 * one is non-null, or one of them is non-null and we are there for the first
3040 * time.
3041 */
3042 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau2e47a3a2014-09-30 18:44:22 +02003043 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
3044 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003045
Willy Tarreaud787e662009-07-07 10:14:51 +02003046 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003047 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003048 req->analyse_exp = TICK_ETERNITY;
3049 return 1;
3050
3051 return_bad_req:
3052 /* We centralize bad requests processing here */
3053 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3054 /* we detected a parsing error. We want to archive this request
3055 * in the dedicated proxy area for later troubleshooting.
3056 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003057 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003058 }
3059
3060 txn->req.msg_state = HTTP_MSG_ERROR;
3061 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003062 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003063
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003064 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003065 if (s->listener->counters)
3066 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003067
3068 return_prx_cond:
3069 if (!(s->flags & SN_ERR_MASK))
3070 s->flags |= SN_ERR_PRXCOND;
3071 if (!(s->flags & SN_FINST_MASK))
3072 s->flags |= SN_FINST_R;
3073
3074 req->analysers = 0;
3075 req->analyse_exp = TICK_ETERNITY;
3076 return 0;
3077}
3078
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003079
Willy Tarreau347a35d2013-11-22 17:51:09 +01003080/* This function prepares an applet to handle the stats. It can deal with the
3081 * "100-continue" expectation, check that admin rules are met for POST requests,
3082 * and program a response message if something was unexpected. It cannot fail
3083 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003084 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003085 * s->target which is supposed to already point to the stats applet. The caller
3086 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003087 */
3088int http_handle_stats(struct session *s, struct channel *req)
3089{
3090 struct stats_admin_rule *stats_admin_rule;
3091 struct stream_interface *si = s->rep->prod;
3092 struct http_txn *txn = &s->txn;
3093 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003094 struct uri_auth *uri_auth = s->be->uri_auth;
3095 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003096 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003097
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003098 appctx = si_appctx(si);
3099 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3100 appctx->st1 = appctx->st2 = 0;
3101 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3102 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003103 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3104 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003105
3106 uri = msg->chn->buf->p + msg->sl.rq.u;
3107 lookup = uri + uri_auth->uri_len;
3108
3109 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3110 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003111 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003112 break;
3113 }
3114 }
3115
3116 if (uri_auth->refresh) {
3117 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3118 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003119 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003120 break;
3121 }
3122 }
3123 }
3124
3125 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3126 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003127 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003128 break;
3129 }
3130 }
3131
3132 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3133 if (memcmp(h, ";st=", 4) == 0) {
3134 int i;
3135 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003136 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003137 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3138 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003139 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003140 break;
3141 }
3142 }
3143 break;
3144 }
3145 }
3146
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003147 appctx->ctx.stats.scope_str = 0;
3148 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003149 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3150 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3151 int itx = 0;
3152 const char *h2;
3153 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3154 const char *err;
3155
3156 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3157 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003158 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003159 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3160 itx++;
3161 h++;
3162 }
3163
3164 if (itx > STAT_SCOPE_TXT_MAXLEN)
3165 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003166 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003167
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003168 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003169 memcpy(scope_txt, h2, itx);
3170 scope_txt[itx] = '\0';
3171 err = invalid_char(scope_txt);
3172 if (err) {
3173 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003174 appctx->ctx.stats.scope_str = 0;
3175 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003176 }
3177 break;
3178 }
3179 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003180
3181 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003182 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003183 int ret = 1;
3184
3185 if (stats_admin_rule->cond) {
3186 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3187 ret = acl_pass(ret);
3188 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3189 ret = !ret;
3190 }
3191
3192 if (ret) {
3193 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003194 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003195 break;
3196 }
3197 }
3198
3199 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003200 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003201 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003202 /* we'll need the request body, possibly after sending 100-continue */
3203 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003204 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003205 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003206 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003207 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3208 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003209 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003210 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003211 else {
3212 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003213 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003214 }
3215
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003216 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003217 return 1;
3218}
3219
Lukas Tribus67db8df2013-06-23 17:37:13 +02003220/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3221 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3222 */
3223static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3224{
3225#ifdef IP_TOS
3226 if (from.ss_family == AF_INET)
3227 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3228#endif
3229#ifdef IPV6_TCLASS
3230 if (from.ss_family == AF_INET6) {
3231 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3232 /* v4-mapped addresses need IP_TOS */
3233 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3234 else
3235 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3236 }
3237#endif
3238}
3239
Sasha Pachev218f0642014-06-16 12:05:59 -06003240static int http_transform_header(struct session* s, struct http_msg *msg, const char* name, uint name_len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003241 char* buf, struct hdr_idx* idx, struct list *fmt, struct my_regex *re,
Sasha Pachev218f0642014-06-16 12:05:59 -06003242 struct hdr_ctx* ctx, int action)
3243{
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003244 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3245 struct hdr_idx *idx, struct hdr_ctx *ctx);
3246 struct chunk *replace = get_trash_chunk();
3247 struct chunk *output = get_trash_chunk();
3248
3249 replace->len = build_logline(s, replace->str, replace->size, fmt);
3250 if (replace->len >= replace->size - 1)
3251 return -1;
3252
Sasha Pachev218f0642014-06-16 12:05:59 -06003253 ctx->idx = 0;
3254
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003255 /* Choose the header browsing function. */
3256 switch (action) {
3257 case HTTP_REQ_ACT_REPLACE_VAL:
3258 case HTTP_RES_ACT_REPLACE_VAL:
3259 http_find_hdr_func = http_find_header2;
3260 break;
3261 case HTTP_REQ_ACT_REPLACE_HDR:
3262 case HTTP_RES_ACT_REPLACE_HDR:
3263 http_find_hdr_func = http_find_full_header2;
3264 break;
3265 default: /* impossible */
3266 return -1;
3267 }
3268
3269 while (http_find_hdr_func(name, name_len, buf, idx, ctx)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06003270 struct hdr_idx_elem *hdr = idx->v + ctx->idx;
3271 int delta;
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003272 char *val = ctx->line + ctx->val;
3273 char* val_end = val + ctx->vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003274
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003275 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch))
3276 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003277
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003278 output->len = exp_replace(output->str, output->size, val, replace->str, pmatch);
3279 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003280 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003281
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003282 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003283
3284 hdr->len += delta;
3285 http_msg_move_end(msg, delta);
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003286
3287 /* Adjust the length of the current value of the index. */
3288 ctx->vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003289 }
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 ctx.idx = 0;
3393 /* remove all occurrences of the header */
3394 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3395 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3396 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003397 }
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003398 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003399
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003400 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003401 case HTTP_REQ_ACT_ADD_HDR:
3402 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3403 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3404 trash.len = rule->arg.hdr_add.name_len;
3405 trash.str[trash.len++] = ':';
3406 trash.str[trash.len++] = ' ';
3407 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003408
3409 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3410 /* remove all occurrences of the header */
3411 ctx.idx = 0;
3412 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3413 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3414 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3415 }
3416 }
3417
Willy Tarreau96257ec2012-12-27 10:46:37 +01003418 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3419 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003420
3421 case HTTP_REQ_ACT_DEL_ACL:
3422 case HTTP_REQ_ACT_DEL_MAP: {
3423 struct pat_ref *ref;
3424 char *key;
3425 int len;
3426
3427 /* collect reference */
3428 ref = pat_ref_lookup(rule->arg.map.ref);
3429 if (!ref)
3430 continue;
3431
3432 /* collect key */
3433 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3434 key = trash.str;
3435 key[len] = '\0';
3436
3437 /* perform update */
3438 /* returned code: 1=ok, 0=ko */
3439 pat_ref_delete(ref, key);
3440
3441 break;
3442 }
3443
3444 case HTTP_REQ_ACT_ADD_ACL: {
3445 struct pat_ref *ref;
3446 char *key;
3447 struct chunk *trash_key;
3448 int len;
3449
3450 trash_key = get_trash_chunk();
3451
3452 /* collect reference */
3453 ref = pat_ref_lookup(rule->arg.map.ref);
3454 if (!ref)
3455 continue;
3456
3457 /* collect key */
3458 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3459 key = trash_key->str;
3460 key[len] = '\0';
3461
3462 /* perform update */
3463 /* add entry only if it does not already exist */
3464 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003465 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003466
3467 break;
3468 }
3469
3470 case HTTP_REQ_ACT_SET_MAP: {
3471 struct pat_ref *ref;
3472 char *key, *value;
3473 struct chunk *trash_key, *trash_value;
3474 int len;
3475
3476 trash_key = get_trash_chunk();
3477 trash_value = get_trash_chunk();
3478
3479 /* collect reference */
3480 ref = pat_ref_lookup(rule->arg.map.ref);
3481 if (!ref)
3482 continue;
3483
3484 /* collect key */
3485 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3486 key = trash_key->str;
3487 key[len] = '\0';
3488
3489 /* collect value */
3490 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3491 value = trash_value->str;
3492 value[len] = '\0';
3493
3494 /* perform update */
3495 if (pat_ref_find_elt(ref, key) != NULL)
3496 /* update entry if it exists */
3497 pat_ref_set(ref, key, value, NULL);
3498 else
3499 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003500 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003501
3502 break;
3503 }
William Lallemand73025dd2014-04-24 14:38:37 +02003504
3505 case HTTP_REQ_ACT_CUSTOM_CONT:
3506 rule->action_ptr(rule, px, s, txn);
3507 break;
3508
3509 case HTTP_REQ_ACT_CUSTOM_STOP:
3510 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003511 return HTTP_RULE_RES_DONE;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003512 }
3513 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003514
3515 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003516 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003517}
3518
Willy Tarreau71241ab2012-12-27 11:30:54 +01003519
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003520/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3521 * transaction <txn>. Returns the first rule that prevents further processing
3522 * of the response (deny, ...) or NULL if it executed all rules or stopped
3523 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3524 * rule.
3525 */
3526static struct http_res_rule *
3527http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3528{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003529 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003530 struct http_res_rule *rule;
3531 struct hdr_ctx ctx;
3532
3533 list_for_each_entry(rule, rules, list) {
3534 if (rule->action >= HTTP_RES_ACT_MAX)
3535 continue;
3536
3537 /* check optional condition */
3538 if (rule->cond) {
3539 int ret;
3540
3541 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3542 ret = acl_pass(ret);
3543
3544 if (rule->cond->pol == ACL_COND_UNLESS)
3545 ret = !ret;
3546
3547 if (!ret) /* condition not matched */
3548 continue;
3549 }
3550
3551
3552 switch (rule->action) {
3553 case HTTP_RES_ACT_ALLOW:
3554 return NULL; /* "allow" rules are OK */
3555
3556 case HTTP_RES_ACT_DENY:
3557 txn->flags |= TX_SVDENY;
3558 return rule;
3559
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003560 case HTTP_RES_ACT_SET_NICE:
3561 s->task->nice = rule->arg.nice;
3562 break;
3563
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003564 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003565 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003566 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003567 break;
3568
Willy Tarreau51347ed2013-06-11 19:34:13 +02003569 case HTTP_RES_ACT_SET_MARK:
3570#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003571 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003572 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003573#endif
3574 break;
3575
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003576 case HTTP_RES_ACT_SET_LOGL:
3577 s->logs.level = rule->arg.loglevel;
3578 break;
3579
Sasha Pachev218f0642014-06-16 12:05:59 -06003580 case HTTP_RES_ACT_REPLACE_HDR:
3581 case HTTP_RES_ACT_REPLACE_VAL:
3582 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3583 txn->rsp.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003584 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003585 return NULL; /* note: we should report an error here */
3586 break;
3587
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003588 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003589 ctx.idx = 0;
3590 /* remove all occurrences of the header */
3591 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3592 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3593 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3594 }
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003595 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003596
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003597 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003598 case HTTP_RES_ACT_ADD_HDR:
3599 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3600 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3601 trash.len = rule->arg.hdr_add.name_len;
3602 trash.str[trash.len++] = ':';
3603 trash.str[trash.len++] = ' ';
3604 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003605
3606 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3607 /* remove all occurrences of the header */
3608 ctx.idx = 0;
3609 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3610 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3611 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3612 }
3613 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003614 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3615 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003616
3617 case HTTP_RES_ACT_DEL_ACL:
3618 case HTTP_RES_ACT_DEL_MAP: {
3619 struct pat_ref *ref;
3620 char *key;
3621 int len;
3622
3623 /* collect reference */
3624 ref = pat_ref_lookup(rule->arg.map.ref);
3625 if (!ref)
3626 continue;
3627
3628 /* collect key */
3629 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3630 key = trash.str;
3631 key[len] = '\0';
3632
3633 /* perform update */
3634 /* returned code: 1=ok, 0=ko */
3635 pat_ref_delete(ref, key);
3636
3637 break;
3638 }
3639
3640 case HTTP_RES_ACT_ADD_ACL: {
3641 struct pat_ref *ref;
3642 char *key;
3643 struct chunk *trash_key;
3644 int len;
3645
3646 trash_key = get_trash_chunk();
3647
3648 /* collect reference */
3649 ref = pat_ref_lookup(rule->arg.map.ref);
3650 if (!ref)
3651 continue;
3652
3653 /* collect key */
3654 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3655 key = trash_key->str;
3656 key[len] = '\0';
3657
3658 /* perform update */
3659 /* check if the entry already exists */
3660 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003661 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003662
3663 break;
3664 }
3665
3666 case HTTP_RES_ACT_SET_MAP: {
3667 struct pat_ref *ref;
3668 char *key, *value;
3669 struct chunk *trash_key, *trash_value;
3670 int len;
3671
3672 trash_key = get_trash_chunk();
3673 trash_value = get_trash_chunk();
3674
3675 /* collect reference */
3676 ref = pat_ref_lookup(rule->arg.map.ref);
3677 if (!ref)
3678 continue;
3679
3680 /* collect key */
3681 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3682 key = trash_key->str;
3683 key[len] = '\0';
3684
3685 /* collect value */
3686 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3687 value = trash_value->str;
3688 value[len] = '\0';
3689
3690 /* perform update */
3691 if (pat_ref_find_elt(ref, key) != NULL)
3692 /* update entry if it exists */
3693 pat_ref_set(ref, key, value, NULL);
3694 else
3695 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003696 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003697
3698 break;
3699 }
William Lallemand73025dd2014-04-24 14:38:37 +02003700
3701 case HTTP_RES_ACT_CUSTOM_CONT:
3702 rule->action_ptr(rule, px, s, txn);
3703 break;
3704
3705 case HTTP_RES_ACT_CUSTOM_STOP:
3706 rule->action_ptr(rule, px, s, txn);
3707 return rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003708 }
3709 }
3710
3711 /* we reached the end of the rules, nothing to report */
3712 return NULL;
3713}
3714
3715
Willy Tarreau71241ab2012-12-27 11:30:54 +01003716/* Perform an HTTP redirect based on the information in <rule>. The function
3717 * returns non-zero on success, or zero in case of a, irrecoverable error such
3718 * as too large a request to build a valid response.
3719 */
3720static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3721{
3722 struct http_msg *msg = &txn->req;
3723 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003724 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003725
3726 /* build redirect message */
3727 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003728 case 308:
3729 msg_fmt = HTTP_308;
3730 break;
3731 case 307:
3732 msg_fmt = HTTP_307;
3733 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003734 case 303:
3735 msg_fmt = HTTP_303;
3736 break;
3737 case 301:
3738 msg_fmt = HTTP_301;
3739 break;
3740 case 302:
3741 default:
3742 msg_fmt = HTTP_302;
3743 break;
3744 }
3745
3746 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3747 return 0;
3748
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003749 location = trash.str + trash.len;
3750
Willy Tarreau71241ab2012-12-27 11:30:54 +01003751 switch(rule->type) {
3752 case REDIRECT_TYPE_SCHEME: {
3753 const char *path;
3754 const char *host;
3755 struct hdr_ctx ctx;
3756 int pathlen;
3757 int hostlen;
3758
3759 host = "";
3760 hostlen = 0;
3761 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003762 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003763 host = ctx.line + ctx.val;
3764 hostlen = ctx.vlen;
3765 }
3766
3767 path = http_get_path(txn);
3768 /* build message using path */
3769 if (path) {
3770 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3771 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3772 int qs = 0;
3773 while (qs < pathlen) {
3774 if (path[qs] == '?') {
3775 pathlen = qs;
3776 break;
3777 }
3778 qs++;
3779 }
3780 }
3781 } else {
3782 path = "/";
3783 pathlen = 1;
3784 }
3785
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003786 if (rule->rdr_str) { /* this is an old "redirect" rule */
3787 /* check if we can add scheme + "://" + host + path */
3788 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3789 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003790
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003791 /* add scheme */
3792 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3793 trash.len += rule->rdr_len;
3794 }
3795 else {
3796 /* add scheme with executing log format */
3797 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003798
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003799 /* check if we can add scheme + "://" + host + path */
3800 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3801 return 0;
3802 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003803 /* add "://" */
3804 memcpy(trash.str + trash.len, "://", 3);
3805 trash.len += 3;
3806
3807 /* add host */
3808 memcpy(trash.str + trash.len, host, hostlen);
3809 trash.len += hostlen;
3810
3811 /* add path */
3812 memcpy(trash.str + trash.len, path, pathlen);
3813 trash.len += pathlen;
3814
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003815 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003816 if (trash.len && trash.str[trash.len - 1] != '/' &&
3817 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3818 if (trash.len > trash.size - 5)
3819 return 0;
3820 trash.str[trash.len] = '/';
3821 trash.len++;
3822 }
3823
3824 break;
3825 }
3826 case REDIRECT_TYPE_PREFIX: {
3827 const char *path;
3828 int pathlen;
3829
3830 path = http_get_path(txn);
3831 /* build message using path */
3832 if (path) {
3833 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3834 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3835 int qs = 0;
3836 while (qs < pathlen) {
3837 if (path[qs] == '?') {
3838 pathlen = qs;
3839 break;
3840 }
3841 qs++;
3842 }
3843 }
3844 } else {
3845 path = "/";
3846 pathlen = 1;
3847 }
3848
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003849 if (rule->rdr_str) { /* this is an old "redirect" rule */
3850 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3851 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003852
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003853 /* add prefix. Note that if prefix == "/", we don't want to
3854 * add anything, otherwise it makes it hard for the user to
3855 * configure a self-redirection.
3856 */
3857 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3858 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3859 trash.len += rule->rdr_len;
3860 }
3861 }
3862 else {
3863 /* add prefix with executing log format */
3864 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3865
3866 /* Check length */
3867 if (trash.len + pathlen > trash.size - 4)
3868 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003869 }
3870
3871 /* add path */
3872 memcpy(trash.str + trash.len, path, pathlen);
3873 trash.len += pathlen;
3874
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003875 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003876 if (trash.len && trash.str[trash.len - 1] != '/' &&
3877 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3878 if (trash.len > trash.size - 5)
3879 return 0;
3880 trash.str[trash.len] = '/';
3881 trash.len++;
3882 }
3883
3884 break;
3885 }
3886 case REDIRECT_TYPE_LOCATION:
3887 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003888 if (rule->rdr_str) { /* this is an old "redirect" rule */
3889 if (trash.len + rule->rdr_len > trash.size - 4)
3890 return 0;
3891
3892 /* add location */
3893 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3894 trash.len += rule->rdr_len;
3895 }
3896 else {
3897 /* add location with executing log format */
3898 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003899
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003900 /* Check left length */
3901 if (trash.len > trash.size - 4)
3902 return 0;
3903 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003904 break;
3905 }
3906
3907 if (rule->cookie_len) {
3908 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3909 trash.len += 14;
3910 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3911 trash.len += rule->cookie_len;
3912 memcpy(trash.str + trash.len, "\r\n", 2);
3913 trash.len += 2;
3914 }
3915
3916 /* add end of headers and the keep-alive/close status.
3917 * We may choose to set keep-alive if the Location begins
3918 * with a slash, because the client will come back to the
3919 * same server.
3920 */
3921 txn->status = rule->code;
3922 /* let's log the request time */
3923 s->logs.tv_request = now;
3924
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003925 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003926 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3927 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3928 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3929 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3930 /* keep-alive possible */
3931 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3932 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3933 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3934 trash.len += 30;
3935 } else {
3936 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3937 trash.len += 24;
3938 }
3939 }
3940 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3941 trash.len += 4;
3942 bo_inject(txn->rsp.chn, trash.str, trash.len);
3943 /* "eat" the request */
3944 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02003945 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003946 msg->sov = 0;
3947 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3948 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3949 txn->req.msg_state = HTTP_MSG_CLOSED;
3950 txn->rsp.msg_state = HTTP_MSG_DONE;
3951 } else {
3952 /* keep-alive not possible */
3953 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3954 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3955 trash.len += 29;
3956 } else {
3957 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3958 trash.len += 23;
3959 }
3960 stream_int_retnclose(txn->req.chn->prod, &trash);
3961 txn->req.chn->analysers = 0;
3962 }
3963
3964 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003965 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003966 if (!(s->flags & SN_FINST_MASK))
3967 s->flags |= SN_FINST_R;
3968
3969 return 1;
3970}
3971
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003972/* This stream analyser runs all HTTP request processing which is common to
3973 * frontends and backends, which means blocking ACLs, filters, connection-close,
3974 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003975 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003976 * either needs more data or wants to immediately abort the request (eg: deny,
3977 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003978 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003979int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003980{
Willy Tarreaud787e662009-07-07 10:14:51 +02003981 struct http_txn *txn = &s->txn;
3982 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003983 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003984 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003985 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02003986
Willy Tarreau655dce92009-11-08 13:10:58 +01003987 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003988 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003989 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003990 return 0;
3991 }
3992
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003993 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 +02003994 now_ms, __FUNCTION__,
3995 s,
3996 req,
3997 req->rex, req->wex,
3998 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003999 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02004000 req->analysers);
4001
Willy Tarreau65410832014-04-28 21:25:43 +02004002 /* just in case we have some per-backend tracking */
4003 session_inc_be_http_req_ctr(s);
4004
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004005 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004006 if (!LIST_ISEMPTY(&px->http_req_rules)) {
4007 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01004008
Willy Tarreau0b748332014-04-29 00:13:29 +02004009 switch (verdict) {
4010 case HTTP_RULE_RES_CONT:
4011 case HTTP_RULE_RES_STOP: /* nothing to do */
4012 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004013
Willy Tarreau0b748332014-04-29 00:13:29 +02004014 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4015 if (txn->flags & TX_CLTARPIT)
4016 goto tarpit;
4017 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004018
Willy Tarreau0b748332014-04-29 00:13:29 +02004019 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4020 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004021
Willy Tarreau0b748332014-04-29 00:13:29 +02004022 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004023 goto done;
4024
Willy Tarreau0b748332014-04-29 00:13:29 +02004025 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4026 goto return_bad_req;
4027 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004028 }
4029
Willy Tarreau52542592014-04-28 18:33:26 +02004030 /* OK at this stage, we know that the request was accepted according to
4031 * the http-request rules, we can check for the stats. Note that the
4032 * URI is detected *before* the req* rules in order not to be affected
4033 * by a possible reqrep, while they are processed *after* so that a
4034 * reqdeny can still block them. This clearly needs to change in 1.6!
4035 */
4036 if (stats_check_uri(s->rep->prod, txn, px)) {
4037 s->target = &http_stats_applet.obj_type;
4038 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
4039 txn->status = 500;
4040 s->logs.tv_request = now;
4041 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004042
Willy Tarreau52542592014-04-28 18:33:26 +02004043 if (!(s->flags & SN_ERR_MASK))
4044 s->flags |= SN_ERR_RESOURCE;
4045 goto return_prx_cond;
4046 }
4047
4048 /* parse the whole stats request and extract the relevant information */
4049 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004050 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4051 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004052
Willy Tarreau0b748332014-04-29 00:13:29 +02004053 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4054 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004055
Willy Tarreau0b748332014-04-29 00:13:29 +02004056 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4057 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004058 }
4059
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004060 /* evaluate the req* rules except reqadd */
4061 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004062 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004063 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004064
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004065 if (txn->flags & TX_CLDENY)
4066 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004067
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004068 if (txn->flags & TX_CLTARPIT)
4069 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004070 }
Willy Tarreau06619262006-12-17 08:37:22 +01004071
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004072 /* add request headers from the rule sets in the same order */
4073 list_for_each_entry(wl, &px->req_add, list) {
4074 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004075 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004076 ret = acl_pass(ret);
4077 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4078 ret = !ret;
4079 if (!ret)
4080 continue;
4081 }
4082
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004083 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004084 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004085 }
4086
Willy Tarreau52542592014-04-28 18:33:26 +02004087
4088 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004089 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004090 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004091 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
4092 s->fe->fe_counters.intercepted_req++;
4093
4094 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
4095 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
4096 if (!(s->flags & SN_FINST_MASK))
4097 s->flags |= SN_FINST_R;
4098
Willy Tarreau70730dd2014-04-24 18:06:27 +02004099 /* we may want to compress the stats page */
4100 if (s->fe->comp || s->be->comp)
4101 select_compression_request_header(s, req->buf);
4102
4103 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreaude8ca962014-11-20 22:23:10 +01004104 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004105 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004106 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004107
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004108 /* check whether we have some ACLs set to redirect this request */
4109 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004110 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004111 int ret;
4112
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004113 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004114 ret = acl_pass(ret);
4115 if (rule->cond->pol == ACL_COND_UNLESS)
4116 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004117 if (!ret)
4118 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004119 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004120 if (!http_apply_redirect_rule(rule, s, txn))
4121 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004122 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004123 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004124
Willy Tarreau2be39392010-01-03 17:24:51 +01004125 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4126 * If this happens, then the data will not come immediately, so we must
4127 * send all what we have without waiting. Note that due to the small gain
4128 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004129 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004130 * itself once used.
4131 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004132 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004133
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004134 done: /* done with this analyser, continue with next ones that the calling
4135 * points will have set, if any.
4136 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004137 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004138 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4139 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004140 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004141
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004142 tarpit:
4143 /* When a connection is tarpitted, we use the tarpit timeout,
4144 * which may be the same as the connect timeout if unspecified.
4145 * If unset, then set it to zero because we really want it to
4146 * eventually expire. We build the tarpit as an analyser.
4147 */
4148 channel_erase(s->req);
4149
4150 /* wipe the request out so that we can drop the connection early
4151 * if the client closes first.
4152 */
4153 channel_dont_connect(req);
4154 req->analysers = 0; /* remove switching rules etc... */
4155 req->analysers |= AN_REQ_HTTP_TARPIT;
4156 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4157 if (!req->analyse_exp)
4158 req->analyse_exp = tick_add(now_ms, 0);
4159 session_inc_http_err_ctr(s);
4160 s->fe->fe_counters.denied_req++;
4161 if (s->fe != s->be)
4162 s->be->be_counters.denied_req++;
4163 if (s->listener->counters)
4164 s->listener->counters->denied_req++;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004165 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004166
4167 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004168 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004169 txn->status = 403;
4170 s->logs.tv_request = now;
4171 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4172 session_inc_http_err_ctr(s);
4173 s->fe->fe_counters.denied_req++;
4174 if (s->fe != s->be)
4175 s->be->be_counters.denied_req++;
4176 if (s->listener->counters)
4177 s->listener->counters->denied_req++;
4178 goto return_prx_cond;
4179
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004180 return_bad_req:
4181 /* We centralize bad requests processing here */
4182 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4183 /* we detected a parsing error. We want to archive this request
4184 * in the dedicated proxy area for later troubleshooting.
4185 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004186 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004187 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004188
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004189 txn->req.msg_state = HTTP_MSG_ERROR;
4190 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004191 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004192
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004193 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004194 if (s->listener->counters)
4195 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004196
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004197 return_prx_cond:
4198 if (!(s->flags & SN_ERR_MASK))
4199 s->flags |= SN_ERR_PRXCOND;
4200 if (!(s->flags & SN_FINST_MASK))
4201 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004202
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004203 req->analysers = 0;
4204 req->analyse_exp = TICK_ETERNITY;
4205 return 0;
4206}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004207
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004208/* This function performs all the processing enabled for the current request.
4209 * It returns 1 if the processing can continue on next analysers, or zero if it
4210 * needs more data, encounters an error, or wants to immediately abort the
4211 * request. It relies on buffers flags, and updates s->req->analysers.
4212 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004213int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004214{
4215 struct http_txn *txn = &s->txn;
4216 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004217 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004218
Willy Tarreau655dce92009-11-08 13:10:58 +01004219 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004220 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004221 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004222 return 0;
4223 }
4224
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004225 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 +02004226 now_ms, __FUNCTION__,
4227 s,
4228 req,
4229 req->rex, req->wex,
4230 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004231 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004232 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004233
William Lallemand82fe75c2012-10-23 10:25:10 +02004234 if (s->fe->comp || s->be->comp)
4235 select_compression_request_header(s, req->buf);
4236
Willy Tarreau59234e92008-11-30 23:51:27 +01004237 /*
4238 * Right now, we know that we have processed the entire headers
4239 * and that unwanted requests have been filtered out. We can do
4240 * whatever we want with the remaining request. Also, now we
4241 * may have separate values for ->fe, ->be.
4242 */
Willy Tarreau06619262006-12-17 08:37:22 +01004243
Willy Tarreau59234e92008-11-30 23:51:27 +01004244 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004245 * If HTTP PROXY is set we simply get remote server address parsing
4246 * incoming request. Note that this requires that a connection is
4247 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004248 */
4249 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004250 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004251 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004252
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004253 /* Note that for now we don't reuse existing proxy connections */
4254 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004255 txn->req.msg_state = HTTP_MSG_ERROR;
4256 txn->status = 500;
4257 req->analysers = 0;
4258 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4259
4260 if (!(s->flags & SN_ERR_MASK))
4261 s->flags |= SN_ERR_RESOURCE;
4262 if (!(s->flags & SN_FINST_MASK))
4263 s->flags |= SN_FINST_R;
4264
4265 return 0;
4266 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004267
4268 path = http_get_path(txn);
4269 url2sa(req->buf->p + msg->sl.rq.u,
4270 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004271 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004272 /* if the path was found, we have to remove everything between
4273 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4274 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4275 * u_l characters by a single "/".
4276 */
4277 if (path) {
4278 char *cur_ptr = req->buf->p;
4279 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4280 int delta;
4281
4282 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4283 http_msg_move_end(&txn->req, delta);
4284 cur_end += delta;
4285 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4286 goto return_bad_req;
4287 }
4288 else {
4289 char *cur_ptr = req->buf->p;
4290 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4291 int delta;
4292
4293 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4294 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4295 http_msg_move_end(&txn->req, delta);
4296 cur_end += delta;
4297 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4298 goto return_bad_req;
4299 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004300 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004301
Willy Tarreau59234e92008-11-30 23:51:27 +01004302 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004303 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004304 * Note that doing so might move headers in the request, but
4305 * the fields will stay coherent and the URI will not move.
4306 * This should only be performed in the backend.
4307 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004308 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004309 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4310 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004311
Willy Tarreau59234e92008-11-30 23:51:27 +01004312 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004313 * 8: the appsession cookie was looked up very early in 1.2,
4314 * so let's do the same now.
4315 */
4316
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004317 /* It needs to look into the URI unless persistence must be ignored */
4318 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004319 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 +01004320 }
4321
William Lallemanda73203e2012-03-12 12:48:57 +01004322 /* add unique-id if "header-unique-id" is specified */
4323
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004324 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4325 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4326 goto return_bad_req;
4327 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004328 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004329 }
William Lallemanda73203e2012-03-12 12:48:57 +01004330
4331 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004332 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4333 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004334 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004335 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004336 goto return_bad_req;
4337 }
4338
Cyril Bontéb21570a2009-11-29 20:04:48 +01004339 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004340 * 9: add X-Forwarded-For if either the frontend or the backend
4341 * asks for it.
4342 */
4343 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004344 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004345 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004346 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4347 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004348 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004349 /* The header is set to be added only if none is present
4350 * and we found it, so don't do anything.
4351 */
4352 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004353 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004354 /* Add an X-Forwarded-For header unless the source IP is
4355 * in the 'except' network range.
4356 */
4357 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004358 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004359 != s->fe->except_net.s_addr) &&
4360 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004361 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004362 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004363 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004364 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004365 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004366
4367 /* Note: we rely on the backend to get the header name to be used for
4368 * x-forwarded-for, because the header is really meant for the backends.
4369 * However, if the backend did not specify any option, we have to rely
4370 * on the frontend's header name.
4371 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004372 if (s->be->fwdfor_hdr_len) {
4373 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004374 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004375 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004376 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004377 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004378 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004379 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 +01004380
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004381 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004382 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004383 }
4384 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004385 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004386 /* FIXME: for the sake of completeness, we should also support
4387 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004388 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004389 int len;
4390 char pn[INET6_ADDRSTRLEN];
4391 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004392 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004393 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004394
Willy Tarreau59234e92008-11-30 23:51:27 +01004395 /* Note: we rely on the backend to get the header name to be used for
4396 * x-forwarded-for, because the header is really meant for the backends.
4397 * However, if the backend did not specify any option, we have to rely
4398 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004399 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004400 if (s->be->fwdfor_hdr_len) {
4401 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004402 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004403 } else {
4404 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004405 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004406 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004407 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004408
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004409 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004410 goto return_bad_req;
4411 }
4412 }
4413
4414 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004415 * 10: add X-Original-To if either the frontend or the backend
4416 * asks for it.
4417 */
4418 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4419
4420 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004421 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004422 /* Add an X-Original-To header unless the destination IP is
4423 * in the 'except' network range.
4424 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004425 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004426
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004427 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004428 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004429 (((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 +02004430 != s->fe->except_to.s_addr) &&
4431 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004432 (((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 +02004433 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004434 int len;
4435 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004436 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004437
4438 /* Note: we rely on the backend to get the header name to be used for
4439 * x-original-to, because the header is really meant for the backends.
4440 * However, if the backend did not specify any option, we have to rely
4441 * on the frontend's header name.
4442 */
4443 if (s->be->orgto_hdr_len) {
4444 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004445 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004446 } else {
4447 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004448 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004449 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004450 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 +02004451
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004452 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004453 goto return_bad_req;
4454 }
4455 }
4456 }
4457
Willy Tarreau50fc7772012-11-11 22:19:57 +01004458 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4459 * If an "Upgrade" token is found, the header is left untouched in order not to have
4460 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4461 * "Upgrade" is present in the Connection header.
4462 */
4463 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4464 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004465 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4466 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004467 unsigned int want_flags = 0;
4468
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004469 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004470 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004471 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4472 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004473 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004474 want_flags |= TX_CON_CLO_SET;
4475 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004476 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004477 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4478 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004479 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004480 want_flags |= TX_CON_KAL_SET;
4481 }
4482
4483 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004484 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004485 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004486
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004487
Willy Tarreau522d6c02009-12-06 18:49:18 +01004488 /* If we have no server assigned yet and we're balancing on url_param
4489 * with a POST request, we may be interested in checking the body for
4490 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004491 */
4492 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4493 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004494 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004495 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004496 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004497 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004498
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004499 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004500 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004501#ifdef TCP_QUICKACK
4502 /* We expect some data from the client. Unless we know for sure
4503 * we already have a full request, we have to re-enable quick-ack
4504 * in case we previously disabled it, otherwise we might cause
4505 * the client to delay further data.
4506 */
4507 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004508 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004509 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004510 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004511 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004512#endif
4513 }
Willy Tarreau03945942009-12-22 16:50:27 +01004514
Willy Tarreau59234e92008-11-30 23:51:27 +01004515 /*************************************************************
4516 * OK, that's finished for the headers. We have done what we *
4517 * could. Let's switch to the DATA state. *
4518 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004519 req->analyse_exp = TICK_ETERNITY;
4520 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004521
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004522 /* if the server closes the connection, we want to immediately react
4523 * and close the socket to save packets and syscalls.
4524 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004525 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4526 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004527
Willy Tarreau59234e92008-11-30 23:51:27 +01004528 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004529 /* OK let's go on with the BODY now */
4530 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004531
Willy Tarreau59234e92008-11-30 23:51:27 +01004532 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004533 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004534 /* we detected a parsing error. We want to archive this request
4535 * in the dedicated proxy area for later troubleshooting.
4536 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004537 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004538 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004539
Willy Tarreau59234e92008-11-30 23:51:27 +01004540 txn->req.msg_state = HTTP_MSG_ERROR;
4541 txn->status = 400;
4542 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004543 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004544
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004545 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004546 if (s->listener->counters)
4547 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004548
Willy Tarreau59234e92008-11-30 23:51:27 +01004549 if (!(s->flags & SN_ERR_MASK))
4550 s->flags |= SN_ERR_PRXCOND;
4551 if (!(s->flags & SN_FINST_MASK))
4552 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004553 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004554}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004555
Willy Tarreau60b85b02008-11-30 23:28:40 +01004556/* This function is an analyser which processes the HTTP tarpit. It always
4557 * returns zero, at the beginning because it prevents any other processing
4558 * from occurring, and at the end because it terminates the request.
4559 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004560int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004561{
4562 struct http_txn *txn = &s->txn;
4563
4564 /* This connection is being tarpitted. The CLIENT side has
4565 * already set the connect expiration date to the right
4566 * timeout. We just have to check that the client is still
4567 * there and that the timeout has not expired.
4568 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004569 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004570 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004571 !tick_is_expired(req->analyse_exp, now_ms))
4572 return 0;
4573
4574 /* We will set the queue timer to the time spent, just for
4575 * logging purposes. We fake a 500 server error, so that the
4576 * attacker will not suspect his connection has been tarpitted.
4577 * It will not cause trouble to the logs because we can exclude
4578 * the tarpitted connections by filtering on the 'PT' status flags.
4579 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004580 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4581
4582 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004583 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004584 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004585
4586 req->analysers = 0;
4587 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004588
Willy Tarreau60b85b02008-11-30 23:28:40 +01004589 if (!(s->flags & SN_ERR_MASK))
4590 s->flags |= SN_ERR_PRXCOND;
4591 if (!(s->flags & SN_FINST_MASK))
4592 s->flags |= SN_FINST_T;
4593 return 0;
4594}
4595
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004596/* This function is an analyser which waits for the HTTP request body. It waits
4597 * for either the buffer to be full, or the full advertised contents to have
4598 * reached the buffer. It must only be called after the standard HTTP request
4599 * processing has occurred, because it expects the request to be parsed and will
4600 * look for the Expect header. It may send a 100-Continue interim response. It
4601 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4602 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4603 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004604 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004605int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004606{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004607 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004608 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004609
4610 /* We have to parse the HTTP request body to find any required data.
4611 * "balance url_param check_post" should have been the only way to get
4612 * into this. We were brought here after HTTP header analysis, so all
4613 * related structures are ready.
4614 */
4615
Willy Tarreau890988f2014-04-10 11:59:33 +02004616 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4617 /* This is the first call */
4618 if (msg->msg_state < HTTP_MSG_BODY)
4619 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004620
Willy Tarreau890988f2014-04-10 11:59:33 +02004621 if (msg->msg_state < HTTP_MSG_100_SENT) {
4622 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4623 * send an HTTP/1.1 100 Continue intermediate response.
4624 */
4625 if (msg->flags & HTTP_MSGF_VER_11) {
4626 struct hdr_ctx ctx;
4627 ctx.idx = 0;
4628 /* Expect is allowed in 1.1, look for it */
4629 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4630 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4631 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4632 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004633 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004634 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004635 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004636
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004637 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004638 * req->buf->p still points to the beginning of the message. We
4639 * must save the body in msg->next because it survives buffer
4640 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004641 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004642 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004643
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004644 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004645 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4646 else
4647 msg->msg_state = HTTP_MSG_DATA;
4648 }
4649
Willy Tarreau890988f2014-04-10 11:59:33 +02004650 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4651 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreau5b7f9242015-05-01 23:05:14 +02004652 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004653 goto missing_data;
4654
4655 /* OK we have everything we need now */
4656 goto http_end;
4657 }
4658
4659 /* OK here we're parsing a chunked-encoded message */
4660
Willy Tarreau522d6c02009-12-06 18:49:18 +01004661 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004662 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004663 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004664 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004665 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004666 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004667
Willy Tarreau115acb92009-12-26 13:56:06 +01004668 if (!ret)
4669 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004670 else if (ret < 0) {
4671 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004672 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004673 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004674 }
4675
Willy Tarreaud98cf932009-12-27 22:54:55 +01004676 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau5b7f9242015-05-01 23:05:14 +02004677 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4678 * for at least a whole chunk or the whole content length bytes after
4679 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004680 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004681 if (msg->msg_state == HTTP_MSG_TRAILERS)
4682 goto http_end;
4683
Willy Tarreau5b7f9242015-05-01 23:05:14 +02004684 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004685 goto http_end;
4686
4687 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004688 /* we get here if we need to wait for more data. If the buffer is full,
4689 * we have the maximum we can expect.
4690 */
4691 if (buffer_full(req->buf, global.tune.maxrewrite))
4692 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004693
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004694 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004695 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004696 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004697
4698 if (!(s->flags & SN_ERR_MASK))
4699 s->flags |= SN_ERR_CLITO;
4700 if (!(s->flags & SN_FINST_MASK))
4701 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004702 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004703 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004704
4705 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004706 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004707 /* Not enough data. We'll re-use the http-request
4708 * timeout here. Ideally, we should set the timeout
4709 * relative to the accept() date. We just set the
4710 * request timeout once at the beginning of the
4711 * request.
4712 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004713 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004714 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004715 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004716 return 0;
4717 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004718
4719 http_end:
4720 /* The situation will not evolve, so let's give up on the analysis. */
4721 s->logs.tv_request = now; /* update the request timer to reflect full request */
4722 req->analysers &= ~an_bit;
4723 req->analyse_exp = TICK_ETERNITY;
4724 return 1;
4725
4726 return_bad_req: /* let's centralize all bad requests */
4727 txn->req.msg_state = HTTP_MSG_ERROR;
4728 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004729 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004730
Willy Tarreau79ebac62010-06-07 13:47:49 +02004731 if (!(s->flags & SN_ERR_MASK))
4732 s->flags |= SN_ERR_PRXCOND;
4733 if (!(s->flags & SN_FINST_MASK))
4734 s->flags |= SN_FINST_R;
4735
Willy Tarreau522d6c02009-12-06 18:49:18 +01004736 return_err_msg:
4737 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004738 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004739 if (s->listener->counters)
4740 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004741 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004742}
4743
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004744/* send a server's name with an outgoing request over an established connection.
4745 * Note: this function is designed to be called once the request has been scheduled
4746 * for being forwarded. This is the reason why it rewinds the buffer before
4747 * proceeding.
4748 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004749int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004750
4751 struct hdr_ctx ctx;
4752
Mark Lamourinec2247f02012-01-04 13:02:01 -05004753 char *hdr_name = be->server_id_hdr_name;
4754 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004755 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004756 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004757 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004758
William Lallemandd9e90662012-01-30 17:27:17 +01004759 ctx.idx = 0;
4760
Willy Tarreau211cdec2014-04-17 20:18:08 +02004761 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004762 if (old_o) {
4763 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004764 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004765 txn->req.next += old_o;
4766 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004767 }
4768
Willy Tarreau9b28e032012-10-12 23:49:43 +02004769 old_i = chn->buf->i;
4770 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 -05004771 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004772 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004773 }
4774
4775 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004776 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004777 memcpy(hdr_val, hdr_name, hdr_name_len);
4778 hdr_val += hdr_name_len;
4779 *hdr_val++ = ':';
4780 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004781 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4782 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004783
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004784 if (old_o) {
4785 /* If this was a forwarded request, we must readjust the amount of
4786 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004787 * variations. Note that the current state is >= HTTP_MSG_BODY,
4788 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004789 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004790 old_o += chn->buf->i - old_i;
4791 b_adv(chn->buf, old_o);
4792 txn->req.next -= old_o;
4793 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004794 }
4795
Mark Lamourinec2247f02012-01-04 13:02:01 -05004796 return 0;
4797}
4798
Willy Tarreau610ecce2010-01-04 21:15:02 +01004799/* Terminate current transaction and prepare a new one. This is very tricky
4800 * right now but it works.
4801 */
4802void http_end_txn_clean_session(struct session *s)
4803{
Willy Tarreau068621e2013-12-23 15:11:25 +01004804 int prev_status = s->txn.status;
4805
Willy Tarreau610ecce2010-01-04 21:15:02 +01004806 /* FIXME: We need a more portable way of releasing a backend's and a
4807 * server's connections. We need a safer way to reinitialize buffer
4808 * flags. We also need a more accurate method for computing per-request
4809 * data.
4810 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004811
Willy Tarreau4213a112013-12-15 10:25:42 +01004812 /* unless we're doing keep-alive, we want to quickly close the connection
4813 * to the server.
4814 */
4815 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4816 !si_conn_ready(s->req->cons)) {
4817 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4818 si_shutr(s->req->cons);
4819 si_shutw(s->req->cons);
4820 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004821
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004822 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004823 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004824 if (unlikely(s->srv_conn))
4825 sess_change_server(s, NULL);
4826 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004827
4828 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4829 session_process_counters(s);
4830
4831 if (s->txn.status) {
4832 int n;
4833
4834 n = s->txn.status / 100;
4835 if (n < 1 || n > 5)
4836 n = 0;
4837
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004838 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004839 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004840 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004841 s->fe->fe_counters.p.http.comp_rsp++;
4842 }
Willy Tarreau24657792010-02-26 10:30:28 +01004843 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004844 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004845 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004846 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004847 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004848 s->be->be_counters.p.http.comp_rsp++;
4849 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004850 }
4851
4852 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004853 s->logs.bytes_in -= s->req->buf->i;
4854 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004855
4856 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004857 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004858 !(s->flags & SN_MONITOR) &&
4859 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4860 s->do_log(s);
4861 }
4862
Willy Tarreauc177ea72014-06-25 15:36:04 +02004863 /* stop tracking content-based counters */
4864 session_stop_content_counters(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004865 session_update_time_stats(s);
4866
Willy Tarreau610ecce2010-01-04 21:15:02 +01004867 s->logs.accept_date = date; /* user-visible date for logging */
4868 s->logs.tv_accept = now; /* corrected date for internal use */
4869 tv_zero(&s->logs.tv_request);
4870 s->logs.t_queue = -1;
4871 s->logs.t_connect = -1;
4872 s->logs.t_data = -1;
4873 s->logs.t_close = 0;
4874 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4875 s->logs.srv_queue_size = 0; /* we will get this number soon */
4876
Willy Tarreau9b28e032012-10-12 23:49:43 +02004877 s->logs.bytes_in = s->req->total = s->req->buf->i;
4878 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004879
4880 if (s->pend_pos)
4881 pendconn_free(s->pend_pos);
4882
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004883 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004884 if (s->flags & SN_CURR_SESS) {
4885 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004886 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004887 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004888 if (may_dequeue_tasks(objt_server(s->target), s->be))
4889 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004890 }
4891
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004892 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004893
Willy Tarreau4213a112013-12-15 10:25:42 +01004894 /* only release our endpoint if we don't intend to reuse the
4895 * connection.
4896 */
4897 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4898 !si_conn_ready(s->req->cons)) {
4899 si_release_endpoint(s->req->cons);
4900 }
4901
Willy Tarreau610ecce2010-01-04 21:15:02 +01004902 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004903 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004904 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004905 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004906 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreaub4d05092014-09-01 20:35:55 +02004907 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);
4908 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 +02004909 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 +01004910 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Cyril Bonté17f77072014-10-22 22:30:13 +02004911 s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004912
Willy Tarreau610ecce2010-01-04 21:15:02 +01004913 s->txn.meth = 0;
4914 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004915 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004916
4917 if (prev_status == 401 || prev_status == 407) {
4918 /* In HTTP keep-alive mode, if we receive a 401, we still have
4919 * a chance of being able to send the visitor again to the same
4920 * server over the same connection. This is required by some
4921 * broken protocols such as NTLM, and anyway whenever there is
4922 * an opportunity for sending the challenge to the proper place,
4923 * it's better to do it (at least it helps with debugging).
4924 */
4925 s->txn.flags |= TX_PREFER_LAST;
4926 }
4927
Willy Tarreauee55dc02010-06-01 10:56:34 +02004928 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004929 s->req->cons->flags |= SI_FL_INDEP_STR;
4930
Willy Tarreau96e31212011-05-30 18:10:30 +02004931 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004932 s->req->flags |= CF_NEVER_WAIT;
4933 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004934 }
4935
Willy Tarreau610ecce2010-01-04 21:15:02 +01004936 /* if the request buffer is not empty, it means we're
4937 * about to process another request, so send pending
4938 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004939 * Just don't do this if the buffer is close to be full,
4940 * because the request will wait for it to flush a little
4941 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004942 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004943 if (s->req->buf->i) {
4944 if (s->rep->buf->o &&
4945 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4946 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004947 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004948 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004949
Willy Tarreau3de8e7a2015-11-25 20:11:11 +01004950 /* we're removing the analysers, we MUST re-enable events detection.
4951 * We don't enable close on the response channel since it's either
4952 * already closed, or in keep-alive with an idle connection handler.
4953 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004954 channel_auto_read(s->req);
4955 channel_auto_close(s->req);
4956 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004957
Willy Tarreau27375622013-12-17 00:00:28 +01004958 /* we're in keep-alive with an idle connection, monitor it */
4959 si_idle_conn(s->req->cons);
4960
Willy Tarreau342b11c2010-11-24 16:22:09 +01004961 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004962 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004963}
4964
4965
4966/* This function updates the request state machine according to the response
4967 * state machine and buffer flags. It returns 1 if it changes anything (flag
4968 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4969 * it is only used to find when a request/response couple is complete. Both
4970 * this function and its equivalent should loop until both return zero. It
4971 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4972 */
4973int http_sync_req_state(struct session *s)
4974{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004975 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004976 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004977 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004978 unsigned int old_state = txn->req.msg_state;
4979
Willy Tarreau610ecce2010-01-04 21:15:02 +01004980 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4981 return 0;
4982
4983 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004984 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004985 * We can shut the read side unless we want to abort_on_close,
4986 * or we have a POST request. The issue with POST requests is
4987 * that some browsers still send a CRLF after the request, and
4988 * this CRLF must be read so that it does not remain in the kernel
4989 * buffers, otherwise a close could cause an RST on some systems
4990 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004991 * Note that if we're using keep-alive on the client side, we'd
4992 * rather poll now and keep the polling enabled for the whole
4993 * session's life than enabling/disabling it between each
4994 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004995 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004996 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4997 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4998 !(s->be->options & PR_O_ABRT_CLOSE) &&
4999 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005000 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005001
Willy Tarreau40f151a2012-12-20 12:10:09 +01005002 /* if the server closes the connection, we want to immediately react
5003 * and close the socket to save packets and syscalls.
5004 */
5005 chn->cons->flags |= SI_FL_NOHALF;
5006
Willy Tarreauc0d56132015-11-18 11:59:55 +01005007 /* In any case we've finished parsing the request so we must
5008 * disable Nagle when sending data because 1) we're not going
5009 * to shut this side, and 2) the server is waiting for us to
5010 * send pending data.
5011 */
5012 chn->flags |= CF_NEVER_WAIT;
5013
Willy Tarreau610ecce2010-01-04 21:15:02 +01005014 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5015 goto wait_other_side;
5016
5017 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5018 /* The server has not finished to respond, so we
5019 * don't want to move in order not to upset it.
5020 */
5021 goto wait_other_side;
5022 }
5023
5024 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5025 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005026 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005027 txn->req.msg_state = HTTP_MSG_TUNNEL;
5028 goto wait_other_side;
5029 }
5030
5031 /* When we get here, it means that both the request and the
5032 * response have finished receiving. Depending on the connection
5033 * mode, we'll have to wait for the last bytes to leave in either
5034 * direction, and sometimes for a close to be effective.
5035 */
5036
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005037 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5038 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005039 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5040 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005041 }
5042 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5043 /* Option forceclose is set, or either side wants to close,
5044 * let's enforce it now that we're not expecting any new
5045 * data to come. The caller knows the session is complete
5046 * once both states are CLOSED.
5047 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005048 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5049 channel_shutr_now(chn);
5050 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005051 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005052 }
5053 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005054 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5055 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005056 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005057 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5058 channel_auto_read(chn);
5059 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005060 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005061 }
5062
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005063 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005064 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005065 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005066
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005067 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005068 txn->req.msg_state = HTTP_MSG_CLOSING;
5069 goto http_msg_closing;
5070 }
5071 else {
5072 txn->req.msg_state = HTTP_MSG_CLOSED;
5073 goto http_msg_closed;
5074 }
5075 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005076 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005077 }
5078
5079 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5080 http_msg_closing:
5081 /* nothing else to forward, just waiting for the output buffer
5082 * to be empty and for the shutw_now to take effect.
5083 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005084 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005085 txn->req.msg_state = HTTP_MSG_CLOSED;
5086 goto http_msg_closed;
5087 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005088 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005089 txn->req.msg_state = HTTP_MSG_ERROR;
5090 goto wait_other_side;
5091 }
5092 }
5093
5094 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5095 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005096 /* see above in MSG_DONE why we only do this in these states */
5097 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5098 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5099 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005100 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005101 goto wait_other_side;
5102 }
5103
5104 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005105 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005106}
5107
5108
5109/* This function updates the response state machine according to the request
5110 * state machine and buffer flags. It returns 1 if it changes anything (flag
5111 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5112 * it is only used to find when a request/response couple is complete. Both
5113 * this function and its equivalent should loop until both return zero. It
5114 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5115 */
5116int http_sync_res_state(struct session *s)
5117{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005118 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005119 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005120 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005121 unsigned int old_state = txn->rsp.msg_state;
5122
Willy Tarreau610ecce2010-01-04 21:15:02 +01005123 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5124 return 0;
5125
5126 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5127 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005128 * still monitor the server connection for a possible close
5129 * while the request is being uploaded, so we don't disable
5130 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005131 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005132 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005133
5134 if (txn->req.msg_state == HTTP_MSG_ERROR)
5135 goto wait_other_side;
5136
5137 if (txn->req.msg_state < HTTP_MSG_DONE) {
5138 /* The client seems to still be sending data, probably
5139 * because we got an error response during an upload.
5140 * We have the choice of either breaking the connection
5141 * or letting it pass through. Let's do the later.
5142 */
5143 goto wait_other_side;
5144 }
5145
5146 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5147 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005148 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005149 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005150 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005151 goto wait_other_side;
5152 }
5153
5154 /* When we get here, it means that both the request and the
5155 * response have finished receiving. Depending on the connection
5156 * mode, we'll have to wait for the last bytes to leave in either
5157 * direction, and sometimes for a close to be effective.
5158 */
5159
5160 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5161 /* Server-close mode : shut read and wait for the request
5162 * side to close its output buffer. The caller will detect
5163 * when we're in DONE and the other is in CLOSED and will
5164 * catch that for the final cleanup.
5165 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005166 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5167 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005168 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005169 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5170 /* Option forceclose is set, or either side wants to close,
5171 * let's enforce it now that we're not expecting any new
5172 * data to come. The caller knows the session is complete
5173 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005174 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005175 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5176 channel_shutr_now(chn);
5177 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005178 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005179 }
5180 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005181 /* The last possible modes are keep-alive and tunnel. Tunnel will
5182 * need to forward remaining data. Keep-alive will need to monitor
5183 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005184 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005185 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005186 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005187 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5188 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005189 }
5190
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005191 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005192 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005193 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005194 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5195 goto http_msg_closing;
5196 }
5197 else {
5198 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5199 goto http_msg_closed;
5200 }
5201 }
5202 goto wait_other_side;
5203 }
5204
5205 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5206 http_msg_closing:
5207 /* nothing else to forward, just waiting for the output buffer
5208 * to be empty and for the shutw_now to take effect.
5209 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005210 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005211 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5212 goto http_msg_closed;
5213 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005214 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005215 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005216 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005217 if (objt_server(s->target))
5218 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005219 goto wait_other_side;
5220 }
5221 }
5222
5223 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5224 http_msg_closed:
5225 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005226 bi_erase(chn);
5227 channel_auto_close(chn);
5228 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005229 goto wait_other_side;
5230 }
5231
5232 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005233 /* We force the response to leave immediately if we're waiting for the
5234 * other side, since there is no pending shutdown to push it out.
5235 */
5236 if (!channel_is_empty(chn))
5237 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005238 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005239}
5240
5241
5242/* Resync the request and response state machines. Return 1 if either state
5243 * changes.
5244 */
5245int http_resync_states(struct session *s)
5246{
5247 struct http_txn *txn = &s->txn;
5248 int old_req_state = txn->req.msg_state;
5249 int old_res_state = txn->rsp.msg_state;
5250
Willy Tarreau610ecce2010-01-04 21:15:02 +01005251 http_sync_req_state(s);
5252 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005253 if (!http_sync_res_state(s))
5254 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005255 if (!http_sync_req_state(s))
5256 break;
5257 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005258
Willy Tarreau610ecce2010-01-04 21:15:02 +01005259 /* OK, both state machines agree on a compatible state.
5260 * There are a few cases we're interested in :
5261 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5262 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5263 * directions, so let's simply disable both analysers.
5264 * - HTTP_MSG_CLOSED on the response only means we must abort the
5265 * request.
5266 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5267 * with server-close mode means we've completed one request and we
5268 * must re-initialize the server connection.
5269 */
5270
5271 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5272 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5273 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5274 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5275 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005276 channel_auto_close(s->req);
5277 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005278 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005279 channel_auto_close(s->rep);
5280 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005281 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005282 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5283 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005284 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005285 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005286 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005287 channel_auto_close(s->rep);
5288 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01005289 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005290 channel_abort(s->req);
5291 channel_auto_close(s->req);
5292 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005293 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005294 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005295 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5296 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005297 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005298 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5299 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5300 /* server-close/keep-alive: terminate this transaction,
5301 * possibly killing the server connection and reinitialize
5302 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005303 */
5304 http_end_txn_clean_session(s);
5305 }
5306
Willy Tarreau610ecce2010-01-04 21:15:02 +01005307 return txn->req.msg_state != old_req_state ||
5308 txn->rsp.msg_state != old_res_state;
5309}
5310
Willy Tarreaud98cf932009-12-27 22:54:55 +01005311/* This function is an analyser which forwards request body (including chunk
5312 * sizes if any). It is called as soon as we must forward, even if we forward
5313 * zero byte. The only situation where it must not be called is when we're in
5314 * tunnel mode and we want to forward till the close. It's used both to forward
5315 * remaining data and to resync after end of body. It expects the msg_state to
5316 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5317 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005318 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005319 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005320 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005321int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005322{
5323 struct http_txn *txn = &s->txn;
5324 struct http_msg *msg = &s->txn.req;
5325
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005326 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5327 return 0;
5328
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005329 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005330 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005331 /* Output closed while we were sending data. We must abort and
5332 * wake the other side up.
5333 */
5334 msg->msg_state = HTTP_MSG_ERROR;
5335 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005336 return 1;
5337 }
5338
Willy Tarreaud98cf932009-12-27 22:54:55 +01005339 /* Note that we don't have to send 100-continue back because we don't
5340 * need the data to complete our job, and it's up to the server to
5341 * decide whether to return 100, 417 or anything else in return of
5342 * an "Expect: 100-continue" header.
5343 */
5344
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005345 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005346 /* we have msg->sov which points to the first byte of message
5347 * body, and req->buf.p still points to the beginning of the
5348 * message. We forward the headers now, as we don't need them
5349 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005350 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005351 b_adv(req->buf, msg->sov);
5352 msg->next -= msg->sov;
5353 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005354
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005355 /* The previous analysers guarantee that the state is somewhere
5356 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5357 * msg->next are always correct.
5358 */
5359 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5360 if (msg->flags & HTTP_MSGF_TE_CHNK)
5361 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5362 else
5363 msg->msg_state = HTTP_MSG_DATA;
5364 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005365 }
5366
Willy Tarreau7ba23542014-04-17 21:50:00 +02005367 /* Some post-connect processing might want us to refrain from starting to
5368 * forward data. Currently, the only reason for this is "balance url_param"
5369 * whichs need to parse/process the request after we've enabled forwarding.
5370 */
5371 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5372 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5373 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005374 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005375 goto missing_data;
5376 }
5377 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5378 }
5379
Willy Tarreau80a92c02014-03-12 10:41:13 +01005380 /* in most states, we should abort in case of early close */
5381 channel_auto_close(req);
5382
Willy Tarreauefdf0942014-04-24 20:08:57 +02005383 if (req->to_forward) {
5384 /* We can't process the buffer's contents yet */
5385 req->flags |= CF_WAKE_WRITE;
5386 goto missing_data;
5387 }
5388
Willy Tarreaud98cf932009-12-27 22:54:55 +01005389 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005390 if (msg->msg_state == HTTP_MSG_DATA) {
5391 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005392 /* we may have some pending data starting at req->buf->p */
5393 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005394 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005395 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005396 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005397 msg->next += msg->chunk_len;
5398 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005399
5400 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005401 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005402 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005403 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005404 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005405 }
5406 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005407 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005408 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005409 * TRAILERS state.
5410 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005411 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005412
Willy Tarreau54d23df2012-10-25 19:04:45 +02005413 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005414 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005415 else if (ret < 0) {
5416 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005417 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005418 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005419 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005420 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005421 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005422 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005423 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005424 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005425 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005426
5427 if (ret == 0)
5428 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005429 else if (ret < 0) {
5430 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005431 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005432 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005433 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005434 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005435 /* we're in MSG_CHUNK_SIZE now */
5436 }
5437 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005438 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005439
5440 if (ret == 0)
5441 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005442 else if (ret < 0) {
5443 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005444 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005445 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005446 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005447 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005448 /* we're in HTTP_MSG_DONE now */
5449 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005450 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005451 int old_state = msg->msg_state;
5452
Willy Tarreau610ecce2010-01-04 21:15:02 +01005453 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005454
5455 /* we may have some pending data starting at req->buf->p
5456 * such as last chunk of data or trailers.
5457 */
5458 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005459 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005460 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005461 msg->next = 0;
5462
Willy Tarreau294e4672015-05-11 18:30:33 +02005463 /* we don't want to forward closes on DONE except in
5464 * tunnel mode.
5465 */
5466 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005467 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005468 if (http_resync_states(s)) {
5469 /* some state changes occurred, maybe the analyser
5470 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005471 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005472 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005473 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005474 /* request errors are most likely due to
5475 * the server aborting the transfer.
5476 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005477 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005478 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005479 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005480 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005481 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005482 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005483 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005484 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005485
5486 /* If "option abortonclose" is set on the backend, we
5487 * want to monitor the client's connection and forward
5488 * any shutdown notification to the server, which will
5489 * decide whether to close or to go on processing the
Willy Tarreau294e4672015-05-11 18:30:33 +02005490 * request. We only do that in tunnel mode, and not in
5491 * other modes since it can be abused to exhaust source
5492 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005493 */
5494 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005495 channel_auto_read(req);
Willy Tarreau294e4672015-05-11 18:30:33 +02005496 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5497 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5498 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005499 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005500 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005501 else if (s->txn.meth == HTTP_METH_POST) {
5502 /* POST requests may require to read extra CRLF
5503 * sent by broken browsers and which could cause
5504 * an RST to be sent upon close on some systems
5505 * (eg: Linux).
5506 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005507 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005508 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005509
Willy Tarreau610ecce2010-01-04 21:15:02 +01005510 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005511 }
5512 }
5513
Willy Tarreaud98cf932009-12-27 22:54:55 +01005514 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005515 /* we may have some pending data starting at req->buf->p */
5516 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005517 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005518 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5519
Willy Tarreaubed410e2014-04-22 08:19:34 +02005520 msg->next = 0;
5521 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5522
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005523 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005524 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005525 if (!(s->flags & SN_ERR_MASK))
5526 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005527 if (!(s->flags & SN_FINST_MASK)) {
5528 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5529 s->flags |= SN_FINST_H;
5530 else
5531 s->flags |= SN_FINST_D;
5532 }
5533
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005534 s->fe->fe_counters.cli_aborts++;
5535 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005536 if (objt_server(s->target))
5537 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005538
5539 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005540 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005541
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005542 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005543 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005544 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005545
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005546 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005547 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005548 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005549 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005550 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005551
Willy Tarreau5c620922011-05-11 19:56:11 +02005552 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005553 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005554 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005555 * modes are already handled by the stream sock layer. We must not do
5556 * this in content-length mode because it could present the MSG_MORE
5557 * flag with the last block of forwarded data, which would cause an
5558 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005559 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005560 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005561 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005562
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005563 return 0;
5564
Willy Tarreaud98cf932009-12-27 22:54:55 +01005565 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005566 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005567 if (s->listener->counters)
5568 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005569
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005570 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005571 /* we may have some pending data starting at req->buf->p */
5572 b_adv(req->buf, msg->next);
5573 msg->next = 0;
5574
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005575 txn->req.msg_state = HTTP_MSG_ERROR;
5576 if (txn->status) {
5577 /* Note: we don't send any error if some data were already sent */
5578 stream_int_retnclose(req->prod, NULL);
5579 } else {
5580 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005581 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005582 }
5583 req->analysers = 0;
5584 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005585
5586 if (!(s->flags & SN_ERR_MASK))
5587 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005588 if (!(s->flags & SN_FINST_MASK)) {
5589 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5590 s->flags |= SN_FINST_H;
5591 else
5592 s->flags |= SN_FINST_D;
5593 }
5594 return 0;
5595
5596 aborted_xfer:
5597 txn->req.msg_state = HTTP_MSG_ERROR;
5598 if (txn->status) {
5599 /* Note: we don't send any error if some data were already sent */
5600 stream_int_retnclose(req->prod, NULL);
5601 } else {
5602 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005603 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005604 }
5605 req->analysers = 0;
5606 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5607
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005608 s->fe->fe_counters.srv_aborts++;
5609 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005610 if (objt_server(s->target))
5611 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005612
5613 if (!(s->flags & SN_ERR_MASK))
5614 s->flags |= SN_ERR_SRVCL;
5615 if (!(s->flags & SN_FINST_MASK)) {
5616 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5617 s->flags |= SN_FINST_H;
5618 else
5619 s->flags |= SN_FINST_D;
5620 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005621 return 0;
5622}
5623
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005624/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5625 * processing can continue on next analysers, or zero if it either needs more
5626 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5627 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5628 * when it has nothing left to do, and may remove any analyser when it wants to
5629 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005630 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005631int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005632{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005633 struct http_txn *txn = &s->txn;
5634 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005635 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005636 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005637 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005638 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005639
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005640 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 +02005641 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005642 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005643 rep,
5644 rep->rex, rep->wex,
5645 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005646 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005647 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005648
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005649 /*
5650 * Now parse the partial (or complete) lines.
5651 * We will check the response syntax, and also join multi-line
5652 * headers. An index of all the lines will be elaborated while
5653 * parsing.
5654 *
5655 * For the parsing, we use a 28 states FSM.
5656 *
5657 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005658 * rep->buf->p = beginning of response
5659 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5660 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005661 * msg->eol = end of current header or line (LF or CRLF)
5662 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005663 */
5664
Willy Tarreau628c40c2014-04-24 19:11:26 +02005665 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005666 /* There's a protected area at the end of the buffer for rewriting
5667 * purposes. We don't want to start to parse the request if the
5668 * protected area is affected, because we may have to move processed
5669 * data later, which is much more complicated.
5670 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005671 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005672 if (unlikely(!channel_reserved(rep))) {
5673 /* some data has still not left the buffer, wake us once that's done */
5674 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5675 goto abort_response;
5676 channel_dont_close(rep);
5677 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005678 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005679 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005680 }
5681
Willy Tarreau379357a2013-06-08 12:55:46 +02005682 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5683 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5684 buffer_slow_realign(rep->buf);
5685
Willy Tarreau9b28e032012-10-12 23:49:43 +02005686 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005687 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005688 }
5689
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005690 /* 1: we might have to print this header in debug mode */
5691 if (unlikely((global.mode & MODE_DEBUG) &&
5692 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau8767b132014-10-21 19:36:09 +02005693 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005694 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005695
Willy Tarreau9b28e032012-10-12 23:49:43 +02005696 sol = rep->buf->p;
5697 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005698 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005699
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005700 sol += hdr_idx_first_pos(&txn->hdr_idx);
5701 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005702
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005703 while (cur_idx) {
5704 eol = sol + txn->hdr_idx.v[cur_idx].len;
5705 debug_hdr("srvhdr", s, sol, eol);
5706 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5707 cur_idx = txn->hdr_idx.v[cur_idx].next;
5708 }
5709 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005710
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005711 /*
5712 * Now we quickly check if we have found a full valid response.
5713 * If not so, we check the FD and buffer states before leaving.
5714 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005715 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005716 * responses are checked first.
5717 *
5718 * Depending on whether the client is still there or not, we
5719 * may send an error response back or not. Note that normally
5720 * we should only check for HTTP status there, and check I/O
5721 * errors somewhere else.
5722 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005723
Willy Tarreau655dce92009-11-08 13:10:58 +01005724 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005725 /* Invalid response */
5726 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5727 /* we detected a parsing error. We want to archive this response
5728 * in the dedicated proxy area for later troubleshooting.
5729 */
5730 hdr_response_bad:
5731 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005732 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005733
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005734 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005735 if (objt_server(s->target)) {
5736 objt_server(s->target)->counters.failed_resp++;
5737 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005738 }
Willy Tarreau64648412010-03-05 10:41:54 +01005739 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005740 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005741 rep->analysers = 0;
5742 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005743 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005744 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005745 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005746
5747 if (!(s->flags & SN_ERR_MASK))
5748 s->flags |= SN_ERR_PRXCOND;
5749 if (!(s->flags & SN_FINST_MASK))
5750 s->flags |= SN_FINST_H;
5751
5752 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005753 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005754
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005755 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005756 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005757 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005758 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005759 goto hdr_response_bad;
5760 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005761
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005762 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005763 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005764 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005765 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005766 else if (txn->flags & TX_NOT_FIRST)
5767 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005768
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005769 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005770 if (objt_server(s->target)) {
5771 objt_server(s->target)->counters.failed_resp++;
5772 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005773 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005774
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005775 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005776 rep->analysers = 0;
5777 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005778 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005779 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005780 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005781
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005782 if (!(s->flags & SN_ERR_MASK))
5783 s->flags |= SN_ERR_SRVCL;
5784 if (!(s->flags & SN_FINST_MASK))
5785 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005786 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005787 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005788
Willy Tarreau2a4f5112014-06-23 15:22:31 +02005789 /* read timeout : return a 504 to the client. */
5790 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005791 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005792 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005793
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005794 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005795 if (objt_server(s->target)) {
5796 objt_server(s->target)->counters.failed_resp++;
5797 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005798 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005799
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005800 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005801 rep->analysers = 0;
5802 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005803 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005804 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005805 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005806
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005807 if (!(s->flags & SN_ERR_MASK))
5808 s->flags |= SN_ERR_SRVTO;
5809 if (!(s->flags & SN_FINST_MASK))
5810 s->flags |= SN_FINST_H;
5811 return 0;
5812 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005813
Willy Tarreauf003d372012-11-26 13:35:37 +01005814 /* client abort with an abortonclose */
5815 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5816 s->fe->fe_counters.cli_aborts++;
5817 s->be->be_counters.cli_aborts++;
5818 if (objt_server(s->target))
5819 objt_server(s->target)->counters.cli_aborts++;
5820
5821 rep->analysers = 0;
5822 channel_auto_close(rep);
5823
5824 txn->status = 400;
5825 bi_erase(rep);
5826 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5827
5828 if (!(s->flags & SN_ERR_MASK))
5829 s->flags |= SN_ERR_CLICL;
5830 if (!(s->flags & SN_FINST_MASK))
5831 s->flags |= SN_FINST_H;
5832
5833 /* process_session() will take care of the error */
5834 return 0;
5835 }
5836
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005837 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005838 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005839 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005840 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005841 else if (txn->flags & TX_NOT_FIRST)
5842 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005843
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005844 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005845 if (objt_server(s->target)) {
5846 objt_server(s->target)->counters.failed_resp++;
5847 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005848 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005849
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005850 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005851 rep->analysers = 0;
5852 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005853 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005854 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005855 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005856
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005857 if (!(s->flags & SN_ERR_MASK))
5858 s->flags |= SN_ERR_SRVCL;
5859 if (!(s->flags & SN_FINST_MASK))
5860 s->flags |= SN_FINST_H;
5861 return 0;
5862 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005863
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005864 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005865 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005866 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005867 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005868 else if (txn->flags & TX_NOT_FIRST)
5869 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005870
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005871 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005872 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005873 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005874
5875 if (!(s->flags & SN_ERR_MASK))
5876 s->flags |= SN_ERR_CLICL;
5877 if (!(s->flags & SN_FINST_MASK))
5878 s->flags |= SN_FINST_H;
5879
5880 /* process_session() will take care of the error */
5881 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005882 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005883
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005884 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005885 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005886 return 0;
5887 }
5888
5889 /* More interesting part now : we know that we have a complete
5890 * response which at least looks like HTTP. We have an indicator
5891 * of each header's length, so we can parse them quickly.
5892 */
5893
5894 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005895 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005896
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005897 /*
5898 * 1: get the status code
5899 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005900 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005901 if (n < 1 || n > 5)
5902 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005903 /* when the client triggers a 4xx from the server, it's most often due
5904 * to a missing object or permission. These events should be tracked
5905 * because if they happen often, it may indicate a brute force or a
5906 * vulnerability scan.
5907 */
5908 if (n == 4)
5909 session_inc_http_err_ctr(s);
5910
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005911 if (objt_server(s->target))
5912 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005913
Willy Tarreau55645552015-05-01 13:26:00 +02005914 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5915 * exactly one digit "." one digit. This check may be disabled using
5916 * option accept-invalid-http-response.
5917 */
5918 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5919 if (msg->sl.st.v_l != 8) {
5920 msg->err_pos = 0;
5921 goto hdr_response_bad;
5922 }
5923
5924 if (rep->buf->p[4] != '/' ||
5925 !isdigit((unsigned char)rep->buf->p[5]) ||
5926 rep->buf->p[6] != '.' ||
5927 !isdigit((unsigned char)rep->buf->p[7])) {
5928 msg->err_pos = 4;
5929 goto hdr_response_bad;
5930 }
5931 }
5932
Willy Tarreau5b154472009-12-21 20:11:07 +01005933 /* check if the response is HTTP/1.1 or above */
5934 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005935 ((rep->buf->p[5] > '1') ||
5936 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005937 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005938
5939 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005940 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 +01005941
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005942 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005943 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005944
Willy Tarreau9b28e032012-10-12 23:49:43 +02005945 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005946
Willy Tarreau39650402010-03-15 19:44:39 +01005947 /* Adjust server's health based on status code. Note: status codes 501
5948 * and 505 are triggered on demand by client request, so we must not
5949 * count them as server failures.
5950 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005951 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005952 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005953 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005954 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005955 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005956 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005957
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005958 /*
5959 * 2: check for cacheability.
5960 */
5961
5962 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005963 case 100:
5964 /*
5965 * We may be facing a 100-continue response, in which case this
5966 * is not the right response, and we're waiting for the next one.
5967 * Let's allow this response to go to the client and wait for the
5968 * next one.
5969 */
5970 hdr_idx_init(&txn->hdr_idx);
5971 msg->next -= channel_forward(rep, msg->next);
5972 msg->msg_state = HTTP_MSG_RPBEFORE;
5973 txn->status = 0;
5974 s->logs.t_data = -1; /* was not a response yet */
5975 goto next_one;
5976
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005977 case 200:
5978 case 203:
5979 case 206:
5980 case 300:
5981 case 301:
5982 case 410:
5983 /* RFC2616 @13.4:
5984 * "A response received with a status code of
5985 * 200, 203, 206, 300, 301 or 410 MAY be stored
5986 * by a cache (...) unless a cache-control
5987 * directive prohibits caching."
5988 *
5989 * RFC2616 @9.5: POST method :
5990 * "Responses to this method are not cacheable,
5991 * unless the response includes appropriate
5992 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005993 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005994 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005995 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005996 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5997 break;
5998 default:
5999 break;
6000 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006001
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006002 /*
6003 * 3: we may need to capture headers
6004 */
6005 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01006006 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006007 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006008 txn->rsp.cap, s->fe->rsp_cap);
6009
Willy Tarreau4db603d2015-05-01 10:05:17 +02006010 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6011 * by RFC7230#3.3.3 :
6012 *
6013 * The length of a message body is determined by one of the following
6014 * (in order of precedence):
6015 *
6016 * 1. Any response to a HEAD request and any response with a 1xx
6017 * (Informational), 204 (No Content), or 304 (Not Modified) status
6018 * code is always terminated by the first empty line after the
6019 * header fields, regardless of the header fields present in the
6020 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006021 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006022 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6023 * the connection will become a tunnel immediately after the empty
6024 * line that concludes the header fields. A client MUST ignore any
6025 * Content-Length or Transfer-Encoding header fields received in
6026 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006027 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006028 * 3. If a Transfer-Encoding header field is present and the chunked
6029 * transfer coding (Section 4.1) is the final encoding, the message
6030 * body length is determined by reading and decoding the chunked
6031 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006032 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006033 * If a Transfer-Encoding header field is present in a response and
6034 * the chunked transfer coding is not the final encoding, the
6035 * message body length is determined by reading the connection until
6036 * it is closed by the server. If a Transfer-Encoding header field
6037 * is present in a request and the chunked transfer coding is not
6038 * the final encoding, the message body length cannot be determined
6039 * reliably; the server MUST respond with the 400 (Bad Request)
6040 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006041 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006042 * If a message is received with both a Transfer-Encoding and a
6043 * Content-Length header field, the Transfer-Encoding overrides the
6044 * Content-Length. Such a message might indicate an attempt to
6045 * perform request smuggling (Section 9.5) or response splitting
6046 * (Section 9.4) and ought to be handled as an error. A sender MUST
6047 * remove the received Content-Length field prior to forwarding such
6048 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006049 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006050 * 4. If a message is received without Transfer-Encoding and with
6051 * either multiple Content-Length header fields having differing
6052 * field-values or a single Content-Length header field having an
6053 * invalid value, then the message framing is invalid and the
6054 * recipient MUST treat it as an unrecoverable error. If this is a
6055 * request message, the server MUST respond with a 400 (Bad Request)
6056 * status code and then close the connection. If this is a response
6057 * message received by a proxy, the proxy MUST close the connection
6058 * to the server, discard the received response, and send a 502 (Bad
6059 * Gateway) response to the client. If this is a response message
6060 * received by a user agent, the user agent MUST close the
6061 * connection to the server and discard the received response.
6062 *
6063 * 5. If a valid Content-Length header field is present without
6064 * Transfer-Encoding, its decimal value defines the expected message
6065 * body length in octets. If the sender closes the connection or
6066 * the recipient times out before the indicated number of octets are
6067 * received, the recipient MUST consider the message to be
6068 * incomplete and close the connection.
6069 *
6070 * 6. If this is a request message and none of the above are true, then
6071 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006072 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006073 * 7. Otherwise, this is a response message without a declared message
6074 * body length, so the message body length is determined by the
6075 * number of octets received prior to the server closing the
6076 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006077 */
6078
6079 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006080 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006081 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006082 * FIXME: should we parse anyway and return an error on chunked encoding ?
6083 */
6084 if (txn->meth == HTTP_METH_HEAD ||
6085 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006086 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006087 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006088 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006089 goto skip_content_length;
6090 }
6091
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006092 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006093 ctx.idx = 0;
Willy Tarreaue77bc172015-05-01 10:06:30 +02006094 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006095 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006096 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6097 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006098 /* bad transfer-encoding (chunked followed by something else) */
6099 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006100 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006101 break;
6102 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006103 }
6104
Willy Tarreaudfa3d922015-04-30 10:57:51 +02006105 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006106 ctx.idx = 0;
Willy Tarreau660418d2015-05-01 10:25:45 +02006107 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreaudfa3d922015-04-30 10:57:51 +02006108 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6109 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6110 }
Willy Tarreau660418d2015-05-01 10:25:45 +02006111 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006112 signed long long cl;
6113
Willy Tarreauad14f752011-09-02 20:33:27 +02006114 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006115 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006116 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006117 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006118
Willy Tarreauad14f752011-09-02 20:33:27 +02006119 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006120 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006121 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006122 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006123
Willy Tarreauad14f752011-09-02 20:33:27 +02006124 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006125 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006126 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006127 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006128
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006129 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006130 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006131 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006132 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006133
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006134 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006135 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006136 }
6137
William Lallemand82fe75c2012-10-23 10:25:10 +02006138 if (s->fe->comp || s->be->comp)
6139 select_compression_response_header(s, rep->buf);
6140
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006141skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006142 /* Now we have to check if we need to modify the Connection header.
6143 * This is more difficult on the response than it is on the request,
6144 * because we can have two different HTTP versions and we don't know
6145 * how the client will interprete a response. For instance, let's say
6146 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6147 * HTTP/1.1 response without any header. Maybe it will bound itself to
6148 * HTTP/1.0 because it only knows about it, and will consider the lack
6149 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6150 * the lack of header as a keep-alive. Thus we will use two flags
6151 * indicating how a request MAY be understood by the client. In case
6152 * of multiple possibilities, we'll fix the header to be explicit. If
6153 * ambiguous cases such as both close and keepalive are seen, then we
6154 * will fall back to explicit close. Note that we won't take risks with
6155 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006156 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006157 */
6158
Willy Tarreaudc008c52010-02-01 16:20:08 +01006159 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6160 txn->status == 101)) {
6161 /* Either we've established an explicit tunnel, or we're
6162 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006163 * to understand the next protocols. We have to switch to tunnel
6164 * mode, so that we transfer the request and responses then let
6165 * this protocol pass unmodified. When we later implement specific
6166 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006167 * header which contains information about that protocol for
6168 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006169 */
6170 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6171 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006172 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6173 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006174 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6175 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006176 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006177
Willy Tarreau70dffda2014-01-30 03:07:23 +01006178 /* this situation happens when combining pretend-keepalive with httpclose. */
6179 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006180 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6181 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006182 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6183
Willy Tarreau60466522010-01-18 19:08:45 +01006184 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006185 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006186 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6187 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006188
Willy Tarreau60466522010-01-18 19:08:45 +01006189 /* now adjust header transformations depending on current state */
6190 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6191 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6192 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006193 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006194 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006195 }
Willy Tarreau60466522010-01-18 19:08:45 +01006196 else { /* SCL / KAL */
6197 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006198 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006199 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006200 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006201
Willy Tarreau60466522010-01-18 19:08:45 +01006202 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006203 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006204
Willy Tarreau60466522010-01-18 19:08:45 +01006205 /* Some keep-alive responses are converted to Server-close if
6206 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006207 */
Willy Tarreau60466522010-01-18 19:08:45 +01006208 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6209 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006210 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006211 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006212 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006213 }
6214
Willy Tarreau7959a552013-09-23 16:44:27 +02006215 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006216 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006217
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006218 /* end of job, return OK */
6219 rep->analysers &= ~an_bit;
6220 rep->analyse_exp = TICK_ETERNITY;
6221 channel_auto_close(rep);
6222 return 1;
6223
6224 abort_keep_alive:
6225 /* A keep-alive request to the server failed on a network error.
6226 * The client is required to retry. We need to close without returning
6227 * any other information so that the client retries.
6228 */
6229 txn->status = 0;
6230 rep->analysers = 0;
6231 s->req->analysers = 0;
6232 channel_auto_close(rep);
6233 s->logs.logwait = 0;
6234 s->logs.level = 0;
6235 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
6236 bi_erase(rep);
6237 stream_int_retnclose(rep->cons, NULL);
6238 return 0;
6239}
6240
6241/* This function performs all the processing enabled for the current response.
6242 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6243 * and updates s->rep->analysers. It might make sense to explode it into several
6244 * other functions. It works like process_request (see indications above).
6245 */
6246int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6247{
6248 struct http_txn *txn = &s->txn;
6249 struct http_msg *msg = &txn->rsp;
6250 struct proxy *cur_proxy;
6251 struct cond_wordlist *wl;
6252 struct http_res_rule *http_res_last_rule = NULL;
6253
6254 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6255 now_ms, __FUNCTION__,
6256 s,
6257 rep,
6258 rep->rex, rep->wex,
6259 rep->flags,
6260 rep->buf->i,
6261 rep->analysers);
6262
6263 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6264 return 0;
6265
6266 rep->analysers &= ~an_bit;
6267 rep->analyse_exp = TICK_ETERNITY;
6268
Willy Tarreau70730dd2014-04-24 18:06:27 +02006269 /* The stats applet needs to adjust the Connection header but we don't
6270 * apply any filter there.
6271 */
6272 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6273 goto skip_filters;
6274
Willy Tarreau58975672014-04-24 21:13:57 +02006275 /*
6276 * We will have to evaluate the filters.
6277 * As opposed to version 1.2, now they will be evaluated in the
6278 * filters order and not in the header order. This means that
6279 * each filter has to be validated among all headers.
6280 *
6281 * Filters are tried with ->be first, then with ->fe if it is
6282 * different from ->be.
6283 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006284
Willy Tarreau58975672014-04-24 21:13:57 +02006285 cur_proxy = s->be;
6286 while (1) {
6287 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006288
Willy Tarreau58975672014-04-24 21:13:57 +02006289 /* evaluate http-response rules */
6290 if (!http_res_last_rule)
6291 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 +02006292
Willy Tarreau58975672014-04-24 21:13:57 +02006293 /* try headers filters */
6294 if (rule_set->rsp_exp != NULL) {
6295 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6296 return_bad_resp:
6297 if (objt_server(s->target)) {
6298 objt_server(s->target)->counters.failed_resp++;
6299 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006300 }
Willy Tarreau58975672014-04-24 21:13:57 +02006301 s->be->be_counters.failed_resp++;
6302 return_srv_prx_502:
6303 rep->analysers = 0;
6304 txn->status = 502;
6305 s->logs.t_data = -1; /* was not a valid response */
6306 rep->prod->flags |= SI_FL_NOLINGER;
6307 bi_erase(rep);
6308 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6309 if (!(s->flags & SN_ERR_MASK))
6310 s->flags |= SN_ERR_PRXCOND;
6311 if (!(s->flags & SN_FINST_MASK))
6312 s->flags |= SN_FINST_H;
6313 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006314 }
Willy Tarreau58975672014-04-24 21:13:57 +02006315 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006316
Willy Tarreau58975672014-04-24 21:13:57 +02006317 /* has the response been denied ? */
6318 if (txn->flags & TX_SVDENY) {
6319 if (objt_server(s->target))
6320 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006321
Willy Tarreau58975672014-04-24 21:13:57 +02006322 s->be->be_counters.denied_resp++;
6323 s->fe->fe_counters.denied_resp++;
6324 if (s->listener->counters)
6325 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006326
Willy Tarreau58975672014-04-24 21:13:57 +02006327 goto return_srv_prx_502;
6328 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006329
Willy Tarreau58975672014-04-24 21:13:57 +02006330 /* add response headers from the rule sets in the same order */
6331 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006332 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006333 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006334 if (wl->cond) {
6335 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6336 ret = acl_pass(ret);
6337 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6338 ret = !ret;
6339 if (!ret)
6340 continue;
6341 }
6342 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6343 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006344 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006345
Willy Tarreau58975672014-04-24 21:13:57 +02006346 /* check whether we're already working on the frontend */
6347 if (cur_proxy == s->fe)
6348 break;
6349 cur_proxy = s->fe;
6350 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006351
Willy Tarreau58975672014-04-24 21:13:57 +02006352 /* OK that's all we can do for 1xx responses */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006353 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006354 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006355
Willy Tarreau58975672014-04-24 21:13:57 +02006356 /*
6357 * Now check for a server cookie.
6358 */
6359 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6360 (s->be->options & PR_O_CHK_CACHE))
6361 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006362
Willy Tarreau58975672014-04-24 21:13:57 +02006363 /*
6364 * Check for cache-control or pragma headers if required.
6365 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006366 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 +02006367 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006368
Willy Tarreau58975672014-04-24 21:13:57 +02006369 /*
6370 * Add server cookie in the response if needed
6371 */
6372 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6373 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6374 (!(s->flags & SN_DIRECT) ||
6375 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6376 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6377 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6378 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6379 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6380 !(s->flags & SN_IGNORE_PRST)) {
6381 /* the server is known, it's not the one the client requested, or the
6382 * cookie's last seen date needs to be refreshed. We have to
6383 * insert a set-cookie here, except if we want to insert only on POST
6384 * requests and this one isn't. Note that servers which don't have cookies
6385 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006386 */
Willy Tarreau58975672014-04-24 21:13:57 +02006387 if (!objt_server(s->target)->cookie) {
6388 chunk_printf(&trash,
6389 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6390 s->be->cookie_name);
6391 }
6392 else {
6393 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006394
Willy Tarreau58975672014-04-24 21:13:57 +02006395 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6396 /* emit last_date, which is mandatory */
6397 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6398 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6399 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006400
Willy Tarreau58975672014-04-24 21:13:57 +02006401 if (s->be->cookie_maxlife) {
6402 /* emit first_date, which is either the original one or
6403 * the current date.
6404 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006405 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006406 s30tob64(txn->cookie_first_date ?
6407 txn->cookie_first_date >> 2 :
6408 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006409 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006410 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006411 }
Willy Tarreau58975672014-04-24 21:13:57 +02006412 chunk_appendf(&trash, "; path=/");
6413 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006414
Willy Tarreau58975672014-04-24 21:13:57 +02006415 if (s->be->cookie_domain)
6416 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006417
Willy Tarreau58975672014-04-24 21:13:57 +02006418 if (s->be->ck_opts & PR_CK_HTTPONLY)
6419 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006420
Willy Tarreau58975672014-04-24 21:13:57 +02006421 if (s->be->ck_opts & PR_CK_SECURE)
6422 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006423
Willy Tarreau58975672014-04-24 21:13:57 +02006424 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6425 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006426
Willy Tarreau58975672014-04-24 21:13:57 +02006427 txn->flags &= ~TX_SCK_MASK;
6428 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6429 /* the server did not change, only the date was updated */
6430 txn->flags |= TX_SCK_UPDATED;
6431 else
6432 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006433
Willy Tarreau58975672014-04-24 21:13:57 +02006434 /* Here, we will tell an eventual cache on the client side that we don't
6435 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6436 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6437 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006438 */
Willy Tarreau58975672014-04-24 21:13:57 +02006439 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006440
Willy Tarreau58975672014-04-24 21:13:57 +02006441 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006442
Willy Tarreau58975672014-04-24 21:13:57 +02006443 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6444 "Cache-control: private", 22) < 0))
6445 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006446 }
Willy Tarreau58975672014-04-24 21:13:57 +02006447 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006448
Willy Tarreau58975672014-04-24 21:13:57 +02006449 /*
6450 * Check if result will be cacheable with a cookie.
6451 * We'll block the response if security checks have caught
6452 * nasty things such as a cacheable cookie.
6453 */
6454 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6455 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6456 (s->be->options & PR_O_CHK_CACHE)) {
6457 /* we're in presence of a cacheable response containing
6458 * a set-cookie header. We'll block it as requested by
6459 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006460 */
Willy Tarreau58975672014-04-24 21:13:57 +02006461 if (objt_server(s->target))
6462 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006463
Willy Tarreau58975672014-04-24 21:13:57 +02006464 s->be->be_counters.denied_resp++;
6465 s->fe->fe_counters.denied_resp++;
6466 if (s->listener->counters)
6467 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006468
Willy Tarreau58975672014-04-24 21:13:57 +02006469 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6470 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6471 send_log(s->be, LOG_ALERT,
6472 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6473 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6474 goto return_srv_prx_502;
6475 }
Willy Tarreau03945942009-12-22 16:50:27 +01006476
Willy Tarreau70730dd2014-04-24 18:06:27 +02006477 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006478 /*
6479 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6480 * If an "Upgrade" token is found, the header is left untouched in order
6481 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006482 * if anything but "Upgrade" is present in the Connection header. We don't
6483 * want to touch any 101 response either since it's switching to another
6484 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006485 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006486 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006487 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6488 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6489 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6490 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006491
Willy Tarreau58975672014-04-24 21:13:57 +02006492 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6493 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6494 /* we want a keep-alive response here. Keep-alive header
6495 * required if either side is not 1.1.
6496 */
6497 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6498 want_flags |= TX_CON_KAL_SET;
6499 }
6500 else {
6501 /* we want a close response here. Close header required if
6502 * the server is 1.1, regardless of the client.
6503 */
6504 if (msg->flags & HTTP_MSGF_VER_11)
6505 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006506 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006507
Willy Tarreau58975672014-04-24 21:13:57 +02006508 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6509 http_change_connection_header(txn, msg, want_flags);
6510 }
6511
6512 skip_header_mangling:
6513 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6514 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6515 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006516
Willy Tarreau58975672014-04-24 21:13:57 +02006517 /* if the user wants to log as soon as possible, without counting
6518 * bytes from the server, then this is the right moment. We have
6519 * to temporarily assign bytes_out to log what we currently have.
6520 */
6521 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6522 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6523 s->logs.bytes_out = txn->rsp.eoh;
6524 s->do_log(s);
6525 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006526 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006527 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006528}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006529
Willy Tarreaud98cf932009-12-27 22:54:55 +01006530/* This function is an analyser which forwards response body (including chunk
6531 * sizes if any). It is called as soon as we must forward, even if we forward
6532 * zero byte. The only situation where it must not be called is when we're in
6533 * tunnel mode and we want to forward till the close. It's used both to forward
6534 * remaining data and to resync after end of body. It expects the msg_state to
6535 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6536 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006537 *
6538 * It is capable of compressing response data both in content-length mode and
6539 * in chunked mode. The state machines follows different flows depending on
6540 * whether content-length and chunked modes are used, since there are no
6541 * trailers in content-length :
6542 *
6543 * chk-mode cl-mode
6544 * ,----- BODY -----.
6545 * / \
6546 * V size > 0 V chk-mode
6547 * .--> SIZE -------------> DATA -------------> CRLF
6548 * | | size == 0 | last byte |
6549 * | v final crlf v inspected |
6550 * | TRAILERS -----------> DONE |
6551 * | |
6552 * `----------------------------------------------'
6553 *
6554 * Compression only happens in the DATA state, and must be flushed in final
6555 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6556 * is performed at once on final states for all bytes parsed, or when leaving
6557 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006558 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006559int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006560{
6561 struct http_txn *txn = &s->txn;
6562 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006563 static struct buffer *tmpbuf = NULL;
6564 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006565 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006566
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006567 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6568 return 0;
6569
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006570 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006571 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006572 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006573 /* Output closed while we were sending data. We must abort and
6574 * wake the other side up.
6575 */
6576 msg->msg_state = HTTP_MSG_ERROR;
6577 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006578 return 1;
6579 }
6580
Willy Tarreau4fe41902010-06-07 22:27:41 +02006581 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006582 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006583
Willy Tarreau5bebcd02014-07-10 19:06:10 +02006584 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006585 /* we have msg->sov which points to the first byte of message
6586 * body, and res->buf.p still points to the beginning of the
6587 * message. We forward the headers now, as we don't need them
6588 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006589 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006590 b_adv(res->buf, msg->sov);
6591 msg->next -= msg->sov;
6592 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006593
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006594 /* The previous analysers guarantee that the state is somewhere
6595 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6596 * msg->next are always correct.
6597 */
6598 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6599 if (msg->flags & HTTP_MSGF_TE_CHNK)
6600 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6601 else
6602 msg->msg_state = HTTP_MSG_DATA;
6603 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006604 }
6605
Willy Tarreauefdf0942014-04-24 20:08:57 +02006606 if (res->to_forward) {
6607 /* We can't process the buffer's contents yet */
6608 res->flags |= CF_WAKE_WRITE;
6609 goto missing_data;
6610 }
6611
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006612 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6613 /* We need a compression buffer in the DATA state to put the
6614 * output of compressed data, and in CRLF state to let the
6615 * TRAILERS state finish the job of removing the trailing CRLF.
6616 */
6617 if (unlikely(tmpbuf == NULL)) {
6618 /* this is the first time we need the compression buffer */
6619 tmpbuf = pool_alloc2(pool2_buffer);
6620 if (tmpbuf == NULL)
6621 goto aborted_xfer; /* no memory */
6622 }
6623
6624 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006625 if (ret < 0) {
6626 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006627 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006628 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006629 compressing = 1;
6630 }
6631
Willy Tarreaud98cf932009-12-27 22:54:55 +01006632 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006633 switch (msg->msg_state - HTTP_MSG_DATA) {
6634 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006635 /* we may have some pending data starting at res->buf->p */
6636 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006637 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006638 if (ret < 0)
6639 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006640
Willy Tarreaud5a67832014-04-21 10:54:27 +02006641 if (msg->chunk_len) {
6642 /* input empty or output full */
6643 if (res->buf->i > msg->next)
6644 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006645 goto missing_data;
6646 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006647 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006648 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006649 if (msg->chunk_len > res->buf->i - msg->next) {
6650 /* output full */
6651 res->flags |= CF_WAKE_WRITE;
6652 goto missing_data;
6653 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006654 msg->next += msg->chunk_len;
6655 msg->chunk_len = 0;
6656 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006657
6658 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006659 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006660 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006661 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006662 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006663 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006664 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006665 /* fall through for HTTP_MSG_CHUNK_CRLF */
6666
6667 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6668 /* we want the CRLF after the data */
6669
6670 ret = http_skip_chunk_crlf(msg);
6671 if (ret == 0)
6672 goto missing_data;
6673 else if (ret < 0) {
6674 if (msg->err_pos >= 0)
6675 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6676 goto return_bad_res;
6677 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006678 /* we're in MSG_CHUNK_SIZE now, fall through */
6679
6680 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006681 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006682 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006683 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006684 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006685
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006686 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006687 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006688 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006689 else if (ret < 0) {
6690 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006691 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006692 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006693 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006694 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006695 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006696
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006697 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006698 if (unlikely(compressing)) {
6699 /* we need to flush output contents before syncing FSMs */
6700 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6701 compressing = 0;
6702 }
6703
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006704 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006705 if (ret == 0)
6706 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006707 else if (ret < 0) {
6708 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006709 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006710 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006711 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006712 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006713
6714 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006715 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006716 if (unlikely(compressing)) {
6717 /* we need to flush output contents before syncing FSMs */
6718 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6719 compressing = 0;
6720 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006721
Willy Tarreauc623c172014-04-18 09:53:50 +02006722 /* we may have some pending data starting at res->buf->p
6723 * such as a last chunk of data or trailers.
6724 */
6725 b_adv(res->buf, msg->next);
6726 msg->next = 0;
6727
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006728 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006729 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006730 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6731 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006732 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006733
Willy Tarreau610ecce2010-01-04 21:15:02 +01006734 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006735 /* some state changes occurred, maybe the analyser
6736 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006737 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006738 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006739 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006740 /* response errors are most likely due to
6741 * the client aborting the transfer.
6742 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006743 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006744 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006745 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006746 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006747 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006748 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006749 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006750 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006751 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006752 }
6753 }
6754
Willy Tarreaud98cf932009-12-27 22:54:55 +01006755 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006756 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006757 if (unlikely(compressing)) {
6758 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006759 compressing = 0;
6760 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006761
Willy Tarreauc623c172014-04-18 09:53:50 +02006762 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6763 b_adv(res->buf, msg->next);
6764 msg->next = 0;
6765 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6766 }
6767
Willy Tarreauf003d372012-11-26 13:35:37 +01006768 if (res->flags & CF_SHUTW)
6769 goto aborted_xfer;
6770
6771 /* stop waiting for data if the input is closed before the end. If the
6772 * client side was already closed, it means that the client has aborted,
6773 * so we don't want to count this as a server abort. Otherwise it's a
6774 * server abort.
6775 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006776 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006777 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006778 goto aborted_xfer;
Christopher Fauletbe801672015-06-19 09:00:58 +02006779 /* If we have some pending data, we continue the processing */
6780 if (!buffer_pending(res->buf)) {
6781 if (!(s->flags & SN_ERR_MASK))
6782 s->flags |= SN_ERR_SRVCL;
6783 s->be->be_counters.srv_aborts++;
6784 if (objt_server(s->target))
6785 objt_server(s->target)->counters.srv_aborts++;
6786 goto return_bad_res_stats_ok;
6787 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006788 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006789
Willy Tarreau40dba092010-03-04 18:14:51 +01006790 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006791 if (!s->req->analysers)
6792 goto return_bad_res;
6793
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006794 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006795 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006796 * Similarly, with keep-alive on the client side, we don't want to forward a
6797 * close.
6798 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006799 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006800 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6801 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006802 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006803
Willy Tarreau5c620922011-05-11 19:56:11 +02006804 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006805 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006806 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006807 * modes are already handled by the stream sock layer. We must not do
6808 * this in content-length mode because it could present the MSG_MORE
6809 * flag with the last block of forwarded data, which would cause an
6810 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006811 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006812 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006813 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006814
Willy Tarreaud98cf932009-12-27 22:54:55 +01006815 /* the session handler will take care of timeouts and errors */
6816 return 0;
6817
Willy Tarreau40dba092010-03-04 18:14:51 +01006818 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006819 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006820 if (objt_server(s->target))
6821 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006822
6823 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006824 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006825 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006826 compressing = 0;
6827 }
6828
Willy Tarreauc623c172014-04-18 09:53:50 +02006829 /* we may have some pending data starting at res->buf->p */
6830 if (s->comp_algo == NULL) {
6831 b_adv(res->buf, msg->next);
6832 msg->next = 0;
6833 }
6834
Willy Tarreaud98cf932009-12-27 22:54:55 +01006835 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006836 /* don't send any error message as we're in the body */
6837 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006838 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006839 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006840 if (objt_server(s->target))
6841 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006842
6843 if (!(s->flags & SN_ERR_MASK))
6844 s->flags |= SN_ERR_PRXCOND;
6845 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006846 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006847 return 0;
6848
6849 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006850 if (unlikely(compressing)) {
6851 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6852 compressing = 0;
6853 }
6854
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006855 txn->rsp.msg_state = HTTP_MSG_ERROR;
6856 /* don't send any error message as we're in the body */
6857 stream_int_retnclose(res->cons, NULL);
6858 res->analysers = 0;
6859 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6860
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006861 s->fe->fe_counters.cli_aborts++;
6862 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006863 if (objt_server(s->target))
6864 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006865
6866 if (!(s->flags & SN_ERR_MASK))
6867 s->flags |= SN_ERR_CLICL;
6868 if (!(s->flags & SN_FINST_MASK))
6869 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006870 return 0;
6871}
6872
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006873/* Iterate the same filter through all request headers.
6874 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006875 * Since it can manage the switch to another backend, it updates the per-proxy
6876 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006877 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006878int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006879{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006880 char *cur_ptr, *cur_end, *cur_next;
6881 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006882 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006883 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006884 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006885
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006886 last_hdr = 0;
6887
Willy Tarreau9b28e032012-10-12 23:49:43 +02006888 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006889 old_idx = 0;
6890
6891 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006892 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006893 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006894 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006895 (exp->action == ACT_ALLOW ||
6896 exp->action == ACT_DENY ||
6897 exp->action == ACT_TARPIT))
6898 return 0;
6899
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006900 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006901 if (!cur_idx)
6902 break;
6903
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006904 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006905 cur_ptr = cur_next;
6906 cur_end = cur_ptr + cur_hdr->len;
6907 cur_next = cur_end + cur_hdr->cr + 1;
6908
6909 /* Now we have one header between cur_ptr and cur_end,
6910 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006911 */
6912
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02006913 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006914 switch (exp->action) {
6915 case ACT_SETBE:
6916 /* It is not possible to jump a second time.
6917 * FIXME: should we return an HTTP/500 here so that
6918 * the admin knows there's a problem ?
6919 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006920 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006921 break;
6922
6923 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006924 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006925 last_hdr = 1;
6926 break;
6927
6928 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006929 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006930 last_hdr = 1;
6931 break;
6932
6933 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006934 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006935 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006936 break;
6937
6938 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006939 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006940 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006941 break;
6942
6943 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006944 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6945 if (trash.len < 0)
6946 return -1;
6947
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006948 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006949 /* FIXME: if the user adds a newline in the replacement, the
6950 * index will not be recalculated for now, and the new line
6951 * will not be counted as a new header.
6952 */
6953
6954 cur_end += delta;
6955 cur_next += delta;
6956 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006957 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006958 break;
6959
6960 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006961 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006962 cur_next += delta;
6963
Willy Tarreaufa355d42009-11-29 18:12:29 +01006964 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006965 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6966 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006967 cur_hdr->len = 0;
6968 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006969 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006970 break;
6971
6972 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006973 }
6974
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006975 /* keep the link from this header to next one in case of later
6976 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006977 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006978 old_idx = cur_idx;
6979 }
6980 return 0;
6981}
6982
6983
6984/* Apply the filter to the request line.
6985 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6986 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006987 * Since it can manage the switch to another backend, it updates the per-proxy
6988 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006989 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006990int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006991{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006992 char *cur_ptr, *cur_end;
6993 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006994 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006995 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006996
Willy Tarreau3d300592007-03-18 18:34:41 +01006997 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006998 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006999 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007000 (exp->action == ACT_ALLOW ||
7001 exp->action == ACT_DENY ||
7002 exp->action == ACT_TARPIT))
7003 return 0;
7004 else if (exp->action == ACT_REMOVE)
7005 return 0;
7006
7007 done = 0;
7008
Willy Tarreau9b28e032012-10-12 23:49:43 +02007009 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007010 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007011
7012 /* Now we have the request line between cur_ptr and cur_end */
7013
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007014 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007015 switch (exp->action) {
7016 case ACT_SETBE:
7017 /* It is not possible to jump a second time.
7018 * FIXME: should we return an HTTP/500 here so that
7019 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007020 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007021 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007022 break;
7023
7024 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007025 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007026 done = 1;
7027 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007028
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007029 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007030 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007031 done = 1;
7032 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007033
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007034 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007035 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007036 done = 1;
7037 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007038
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007039 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007040 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007041 done = 1;
7042 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007043
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007044 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007045 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7046 if (trash.len < 0)
7047 return -1;
7048
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007049 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007050 /* FIXME: if the user adds a newline in the replacement, the
7051 * index will not be recalculated for now, and the new line
7052 * will not be counted as a new header.
7053 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007054
Willy Tarreaufa355d42009-11-29 18:12:29 +01007055 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007056 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007057 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007058 HTTP_MSG_RQMETH,
7059 cur_ptr, cur_end + 1,
7060 NULL, NULL);
7061 if (unlikely(!cur_end))
7062 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007063
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007064 /* we have a full request and we know that we have either a CR
7065 * or an LF at <ptr>.
7066 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007067 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7068 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007069 /* there is no point trying this regex on headers */
7070 return 1;
7071 }
7072 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007073 return done;
7074}
Willy Tarreau97de6242006-12-27 17:18:38 +01007075
Willy Tarreau58f10d72006-12-04 02:26:12 +01007076
Willy Tarreau58f10d72006-12-04 02:26:12 +01007077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007078/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01007079 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007080 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007081 * unparsable request. Since it can manage the switch to another backend, it
7082 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007083 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007084int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007085{
Willy Tarreau6c123b12010-01-28 20:22:06 +01007086 struct http_txn *txn = &s->txn;
7087 struct hdr_exp *exp;
7088
7089 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007090 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007091
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007092 /*
7093 * The interleaving of transformations and verdicts
7094 * makes it difficult to decide to continue or stop
7095 * the evaluation.
7096 */
7097
Willy Tarreau6c123b12010-01-28 20:22:06 +01007098 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7099 break;
7100
Willy Tarreau3d300592007-03-18 18:34:41 +01007101 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007102 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007103 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007104 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007105
7106 /* if this filter had a condition, evaluate it now and skip to
7107 * next filter if the condition does not match.
7108 */
7109 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007110 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007111 ret = acl_pass(ret);
7112 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7113 ret = !ret;
7114
7115 if (!ret)
7116 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007117 }
7118
7119 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007120 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007121 if (unlikely(ret < 0))
7122 return -1;
7123
7124 if (likely(ret == 0)) {
7125 /* The filter did not match the request, it can be
7126 * iterated through all headers.
7127 */
Willy Tarreaua256fae2015-01-30 20:58:58 +01007128 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7129 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007130 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007131 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007132 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007133}
7134
7135
Willy Tarreaua15645d2007-03-18 16:22:39 +01007136
Willy Tarreau58f10d72006-12-04 02:26:12 +01007137/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007138 * Try to retrieve the server associated to the appsession.
7139 * If the server is found, it's assigned to the session.
7140 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007141void manage_client_side_appsession(struct session *s, const char *buf, int len) {
7142 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007143 appsess *asession = NULL;
7144 char *sessid_temp = NULL;
7145
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007146 if (len > s->be->appsession_len) {
7147 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007148 }
7149
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007150 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007151 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007152 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007153 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007154 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007155 }
7156
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007157 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007158 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007159 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007160 return;
7161 }
7162
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007163 memcpy(txn->sessid, buf, len);
7164 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007165 }
7166
7167 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7168 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007169 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007170 return;
7171 }
7172
Cyril Bontéb21570a2009-11-29 20:04:48 +01007173 memcpy(sessid_temp, buf, len);
7174 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007175
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007176 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007177 /* free previously allocated memory */
7178 pool_free2(apools.sessid, sessid_temp);
7179
7180 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007181 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7182 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007183 asession->request_count++;
7184
7185 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007186 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007187
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007188 while (srv) {
7189 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007190 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007191 (s->be->options & PR_O_PERSIST) ||
7192 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007193 /* we found the server and it's usable */
7194 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007195 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007196 s->flags |= SN_DIRECT | SN_ASSIGNED;
7197 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007198
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007199 break;
7200 } else {
7201 txn->flags &= ~TX_CK_MASK;
7202 txn->flags |= TX_CK_DOWN;
7203 }
7204 }
7205 srv = srv->next;
7206 }
7207 }
7208 }
7209}
7210
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007211/* Find the end of a cookie value contained between <s> and <e>. It works the
7212 * same way as with headers above except that the semi-colon also ends a token.
7213 * See RFC2965 for more information. Note that it requires a valid header to
7214 * return a valid result.
7215 */
7216char *find_cookie_value_end(char *s, const char *e)
7217{
7218 int quoted, qdpair;
7219
7220 quoted = qdpair = 0;
7221 for (; s < e; s++) {
7222 if (qdpair) qdpair = 0;
7223 else if (quoted) {
7224 if (*s == '\\') qdpair = 1;
7225 else if (*s == '"') quoted = 0;
7226 }
7227 else if (*s == '"') quoted = 1;
7228 else if (*s == ',' || *s == ';') return s;
7229 }
7230 return s;
7231}
7232
7233/* Delete a value in a header between delimiters <from> and <next> in buffer
7234 * <buf>. The number of characters displaced is returned, and the pointer to
7235 * the first delimiter is updated if required. The function tries as much as
7236 * possible to respect the following principles :
7237 * - replace <from> delimiter by the <next> one unless <from> points to a
7238 * colon, in which case <next> is simply removed
7239 * - set exactly one space character after the new first delimiter, unless
7240 * there are not enough characters in the block being moved to do so.
7241 * - remove unneeded spaces before the previous delimiter and after the new
7242 * one.
7243 *
7244 * It is the caller's responsibility to ensure that :
7245 * - <from> points to a valid delimiter or the colon ;
7246 * - <next> points to a valid delimiter or the final CR/LF ;
7247 * - there are non-space chars before <from> ;
7248 * - there is a CR/LF at or after <next>.
7249 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007250int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007251{
7252 char *prev = *from;
7253
7254 if (*prev == ':') {
7255 /* We're removing the first value, preserve the colon and add a
7256 * space if possible.
7257 */
7258 if (!http_is_crlf[(unsigned char)*next])
7259 next++;
7260 prev++;
7261 if (prev < next)
7262 *prev++ = ' ';
7263
7264 while (http_is_spht[(unsigned char)*next])
7265 next++;
7266 } else {
7267 /* Remove useless spaces before the old delimiter. */
7268 while (http_is_spht[(unsigned char)*(prev-1)])
7269 prev--;
7270 *from = prev;
7271
7272 /* copy the delimiter and if possible a space if we're
7273 * not at the end of the line.
7274 */
7275 if (!http_is_crlf[(unsigned char)*next]) {
7276 *prev++ = *next++;
7277 if (prev + 1 < next)
7278 *prev++ = ' ';
7279 while (http_is_spht[(unsigned char)*next])
7280 next++;
7281 }
7282 }
7283 return buffer_replace2(buf, prev, next, NULL, 0);
7284}
7285
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007286/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007287 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007288 * desirable to call it only when needed. This code is quite complex because
7289 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7290 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007291 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007292void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007293{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007294 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007295 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007296 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007297 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7298 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007299
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007300 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007301 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007302 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007303
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007304 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007305 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007306 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007307
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007308 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007309 hdr_beg = hdr_next;
7310 hdr_end = hdr_beg + cur_hdr->len;
7311 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007312
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007313 /* We have one full header between hdr_beg and hdr_end, and the
7314 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007315 * "Cookie:" headers.
7316 */
7317
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007318 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007319 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007320 old_idx = cur_idx;
7321 continue;
7322 }
7323
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007324 del_from = NULL; /* nothing to be deleted */
7325 preserve_hdr = 0; /* assume we may kill the whole header */
7326
Willy Tarreau58f10d72006-12-04 02:26:12 +01007327 /* Now look for cookies. Conforming to RFC2109, we have to support
7328 * attributes whose name begin with a '$', and associate them with
7329 * the right cookie, if we want to delete this cookie.
7330 * So there are 3 cases for each cookie read :
7331 * 1) it's a special attribute, beginning with a '$' : ignore it.
7332 * 2) it's a server id cookie that we *MAY* want to delete : save
7333 * some pointers on it (last semi-colon, beginning of cookie...)
7334 * 3) it's an application cookie : we *MAY* have to delete a previous
7335 * "special" cookie.
7336 * At the end of loop, if a "special" cookie remains, we may have to
7337 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007338 * *MUST* delete it.
7339 *
7340 * Note: RFC2965 is unclear about the processing of spaces around
7341 * the equal sign in the ATTR=VALUE form. A careful inspection of
7342 * the RFC explicitly allows spaces before it, and not within the
7343 * tokens (attrs or values). An inspection of RFC2109 allows that
7344 * too but section 10.1.3 lets one think that spaces may be allowed
7345 * after the equal sign too, resulting in some (rare) buggy
7346 * implementations trying to do that. So let's do what servers do.
7347 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7348 * allowed quoted strings in values, with any possible character
7349 * after a backslash, including control chars and delimitors, which
7350 * causes parsing to become ambiguous. Browsers also allow spaces
7351 * within values even without quotes.
7352 *
7353 * We have to keep multiple pointers in order to support cookie
7354 * removal at the beginning, middle or end of header without
7355 * corrupting the header. All of these headers are valid :
7356 *
7357 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7358 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7359 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7360 * | | | | | | | | |
7361 * | | | | | | | | hdr_end <--+
7362 * | | | | | | | +--> next
7363 * | | | | | | +----> val_end
7364 * | | | | | +-----------> val_beg
7365 * | | | | +--------------> equal
7366 * | | | +----------------> att_end
7367 * | | +---------------------> att_beg
7368 * | +--------------------------> prev
7369 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007370 */
7371
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007372 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7373 /* Iterate through all cookies on this line */
7374
7375 /* find att_beg */
7376 att_beg = prev + 1;
7377 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7378 att_beg++;
7379
7380 /* find att_end : this is the first character after the last non
7381 * space before the equal. It may be equal to hdr_end.
7382 */
7383 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007384
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007385 while (equal < hdr_end) {
7386 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007387 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007388 if (http_is_spht[(unsigned char)*equal++])
7389 continue;
7390 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007391 }
7392
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007393 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7394 * is between <att_beg> and <equal>, both may be identical.
7395 */
7396
7397 /* look for end of cookie if there is an equal sign */
7398 if (equal < hdr_end && *equal == '=') {
7399 /* look for the beginning of the value */
7400 val_beg = equal + 1;
7401 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7402 val_beg++;
7403
7404 /* find the end of the value, respecting quotes */
7405 next = find_cookie_value_end(val_beg, hdr_end);
7406
7407 /* make val_end point to the first white space or delimitor after the value */
7408 val_end = next;
7409 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7410 val_end--;
7411 } else {
7412 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007413 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007414
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007415 /* We have nothing to do with attributes beginning with '$'. However,
7416 * they will automatically be removed if a header before them is removed,
7417 * since they're supposed to be linked together.
7418 */
7419 if (*att_beg == '$')
7420 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007421
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007422 /* Ignore cookies with no equal sign */
7423 if (equal == next) {
7424 /* This is not our cookie, so we must preserve it. But if we already
7425 * scheduled another cookie for removal, we cannot remove the
7426 * complete header, but we can remove the previous block itself.
7427 */
7428 preserve_hdr = 1;
7429 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007430 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007431 val_end += delta;
7432 next += delta;
7433 hdr_end += delta;
7434 hdr_next += delta;
7435 cur_hdr->len += delta;
7436 http_msg_move_end(&txn->req, delta);
7437 prev = del_from;
7438 del_from = NULL;
7439 }
7440 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007441 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007442
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007443 /* if there are spaces around the equal sign, we need to
7444 * strip them otherwise we'll get trouble for cookie captures,
7445 * or even for rewrites. Since this happens extremely rarely,
7446 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007447 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007448 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7449 int stripped_before = 0;
7450 int stripped_after = 0;
7451
7452 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007453 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007454 equal += stripped_before;
7455 val_beg += stripped_before;
7456 }
7457
7458 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007459 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007460 val_beg += stripped_after;
7461 stripped_before += stripped_after;
7462 }
7463
7464 val_end += stripped_before;
7465 next += stripped_before;
7466 hdr_end += stripped_before;
7467 hdr_next += stripped_before;
7468 cur_hdr->len += stripped_before;
7469 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007470 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007471 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007472
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007473 /* First, let's see if we want to capture this cookie. We check
7474 * that we don't already have a client side cookie, because we
7475 * can only capture one. Also as an optimisation, we ignore
7476 * cookies shorter than the declared name.
7477 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007478 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7479 (val_end - att_beg >= s->fe->capture_namelen) &&
7480 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007481 int log_len = val_end - att_beg;
7482
7483 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7484 Alert("HTTP logging : out of memory.\n");
7485 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007486 if (log_len > s->fe->capture_len)
7487 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007488 memcpy(txn->cli_cookie, att_beg, log_len);
7489 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007490 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007491 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007492
Willy Tarreaubca99692010-10-06 19:25:55 +02007493 /* Persistence cookies in passive, rewrite or insert mode have the
7494 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007495 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007496 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007497 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007498 * For cookies in prefix mode, the form is :
7499 *
7500 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007501 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007502 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7503 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7504 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007505 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007506
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007507 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7508 * have the server ID between val_beg and delim, and the original cookie between
7509 * delim+1 and val_end. Otherwise, delim==val_end :
7510 *
7511 * Cookie: NAME=SRV; # in all but prefix modes
7512 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7513 * | || || | |+-> next
7514 * | || || | +--> val_end
7515 * | || || +---------> delim
7516 * | || |+------------> val_beg
7517 * | || +-------------> att_end = equal
7518 * | |+-----------------> att_beg
7519 * | +------------------> prev
7520 * +-------------------------> hdr_beg
7521 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007522
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007523 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007524 for (delim = val_beg; delim < val_end; delim++)
7525 if (*delim == COOKIE_DELIM)
7526 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007527 } else {
7528 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007529 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007530 /* Now check if the cookie contains a date field, which would
7531 * appear after a vertical bar ('|') just after the server name
7532 * and before the delimiter.
7533 */
7534 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7535 if (vbar1) {
7536 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007537 * right is the last seen date. It is a base64 encoded
7538 * 30-bit value representing the UNIX date since the
7539 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007540 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007541 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007542 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007543 if (val_end - vbar1 >= 5) {
7544 val = b64tos30(vbar1);
7545 if (val > 0)
7546 txn->cookie_last_date = val << 2;
7547 }
7548 /* look for a second vertical bar */
7549 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7550 if (vbar1 && (val_end - vbar1 > 5)) {
7551 val = b64tos30(vbar1 + 1);
7552 if (val > 0)
7553 txn->cookie_first_date = val << 2;
7554 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007555 }
7556 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007557
Willy Tarreauf64d1412010-10-07 20:06:11 +02007558 /* if the cookie has an expiration date and the proxy wants to check
7559 * it, then we do that now. We first check if the cookie is too old,
7560 * then only if it has expired. We detect strict overflow because the
7561 * time resolution here is not great (4 seconds). Cookies with dates
7562 * in the future are ignored if their offset is beyond one day. This
7563 * allows an admin to fix timezone issues without expiring everyone
7564 * and at the same time avoids keeping unwanted side effects for too
7565 * long.
7566 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007567 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7568 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007569 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007570 txn->flags &= ~TX_CK_MASK;
7571 txn->flags |= TX_CK_OLD;
7572 delim = val_beg; // let's pretend we have not found the cookie
7573 txn->cookie_first_date = 0;
7574 txn->cookie_last_date = 0;
7575 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007576 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7577 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007578 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007579 txn->flags &= ~TX_CK_MASK;
7580 txn->flags |= TX_CK_EXPIRED;
7581 delim = val_beg; // let's pretend we have not found the cookie
7582 txn->cookie_first_date = 0;
7583 txn->cookie_last_date = 0;
7584 }
7585
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007586 /* Here, we'll look for the first running server which supports the cookie.
7587 * This allows to share a same cookie between several servers, for example
7588 * to dedicate backup servers to specific servers only.
7589 * However, to prevent clients from sticking to cookie-less backup server
7590 * when they have incidentely learned an empty cookie, we simply ignore
7591 * empty cookies and mark them as invalid.
7592 * The same behaviour is applied when persistence must be ignored.
7593 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007594 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007595 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007596
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007597 while (srv) {
7598 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7599 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007600 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007601 (s->be->options & PR_O_PERSIST) ||
7602 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007603 /* we found the server and we can use it */
7604 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007605 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007606 s->flags |= SN_DIRECT | SN_ASSIGNED;
7607 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007608 break;
7609 } else {
7610 /* we found a server, but it's down,
7611 * mark it as such and go on in case
7612 * another one is available.
7613 */
7614 txn->flags &= ~TX_CK_MASK;
7615 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007616 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007617 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007618 srv = srv->next;
7619 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007620
Willy Tarreauf64d1412010-10-07 20:06:11 +02007621 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007622 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007623 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007624 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007625 txn->flags |= TX_CK_UNUSED;
7626 else
7627 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007628 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007629
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007630 /* depending on the cookie mode, we may have to either :
7631 * - delete the complete cookie if we're in insert+indirect mode, so that
7632 * the server never sees it ;
7633 * - remove the server id from the cookie value, and tag the cookie as an
7634 * application cookie so that it does not get accidentely removed later,
7635 * if we're in cookie prefix mode
7636 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007637 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007638 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007639
Willy Tarreau9b28e032012-10-12 23:49:43 +02007640 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007641 val_end += delta;
7642 next += delta;
7643 hdr_end += delta;
7644 hdr_next += delta;
7645 cur_hdr->len += delta;
7646 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007647
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007648 del_from = NULL;
7649 preserve_hdr = 1; /* we want to keep this cookie */
7650 }
7651 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007652 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007653 del_from = prev;
7654 }
7655 } else {
7656 /* This is not our cookie, so we must preserve it. But if we already
7657 * scheduled another cookie for removal, we cannot remove the
7658 * complete header, but we can remove the previous block itself.
7659 */
7660 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007661
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007662 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007663 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007664 if (att_beg >= del_from)
7665 att_beg += delta;
7666 if (att_end >= del_from)
7667 att_end += delta;
7668 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007669 val_end += delta;
7670 next += delta;
7671 hdr_end += delta;
7672 hdr_next += delta;
7673 cur_hdr->len += delta;
7674 http_msg_move_end(&txn->req, delta);
7675 prev = del_from;
7676 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007677 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007678 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007679
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007680 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007681 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007682 int cmp_len, value_len;
7683 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007684
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007685 if (s->be->options2 & PR_O2_AS_PFX) {
7686 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7687 value_begin = att_beg + s->be->appsession_name_len;
7688 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007689 } else {
7690 cmp_len = att_end - att_beg;
7691 value_begin = val_beg;
7692 value_len = val_end - val_beg;
7693 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007694
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007695 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007696 if (cmp_len == s->be->appsession_name_len &&
7697 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7698 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007699 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007700 }
7701
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007702 /* continue with next cookie on this header line */
7703 att_beg = next;
7704 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007705
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007706 /* There are no more cookies on this line.
7707 * We may still have one (or several) marked for deletion at the
7708 * end of the line. We must do this now in two ways :
7709 * - if some cookies must be preserved, we only delete from the
7710 * mark to the end of line ;
7711 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007712 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007713 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007714 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007715 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007716 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007717 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007718 cur_hdr->len += delta;
7719 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007720 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007721
7722 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007723 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7724 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007725 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007726 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007727 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007728 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007729 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007730 }
7731
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007732 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007733 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007734 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007735}
7736
7737
Willy Tarreaua15645d2007-03-18 16:22:39 +01007738/* Iterate the same filter through all response headers contained in <rtr>.
7739 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7740 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007741int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007742{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007743 char *cur_ptr, *cur_end, *cur_next;
7744 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007745 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007746 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007747 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007748
7749 last_hdr = 0;
7750
Willy Tarreau9b28e032012-10-12 23:49:43 +02007751 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007752 old_idx = 0;
7753
7754 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007755 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007756 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007757 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007758 (exp->action == ACT_ALLOW ||
7759 exp->action == ACT_DENY))
7760 return 0;
7761
7762 cur_idx = txn->hdr_idx.v[old_idx].next;
7763 if (!cur_idx)
7764 break;
7765
7766 cur_hdr = &txn->hdr_idx.v[cur_idx];
7767 cur_ptr = cur_next;
7768 cur_end = cur_ptr + cur_hdr->len;
7769 cur_next = cur_end + cur_hdr->cr + 1;
7770
7771 /* Now we have one header between cur_ptr and cur_end,
7772 * and the next header starts at cur_next.
7773 */
7774
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007775 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007776 switch (exp->action) {
7777 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007778 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007779 last_hdr = 1;
7780 break;
7781
7782 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007783 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007784 last_hdr = 1;
7785 break;
7786
7787 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007788 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7789 if (trash.len < 0)
7790 return -1;
7791
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007792 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007793 /* FIXME: if the user adds a newline in the replacement, the
7794 * index will not be recalculated for now, and the new line
7795 * will not be counted as a new header.
7796 */
7797
7798 cur_end += delta;
7799 cur_next += delta;
7800 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007801 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007802 break;
7803
7804 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007805 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007806 cur_next += delta;
7807
Willy Tarreaufa355d42009-11-29 18:12:29 +01007808 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007809 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7810 txn->hdr_idx.used--;
7811 cur_hdr->len = 0;
7812 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007813 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007814 break;
7815
7816 }
7817 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007818
7819 /* keep the link from this header to next one in case of later
7820 * removal of next header.
7821 */
7822 old_idx = cur_idx;
7823 }
7824 return 0;
7825}
7826
7827
7828/* Apply the filter to the status line in the response buffer <rtr>.
7829 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7830 * or -1 if a replacement resulted in an invalid status line.
7831 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007832int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007833{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007834 char *cur_ptr, *cur_end;
7835 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007836 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007837 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007838
7839
Willy Tarreau3d300592007-03-18 18:34:41 +01007840 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007841 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007842 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007843 (exp->action == ACT_ALLOW ||
7844 exp->action == ACT_DENY))
7845 return 0;
7846 else if (exp->action == ACT_REMOVE)
7847 return 0;
7848
7849 done = 0;
7850
Willy Tarreau9b28e032012-10-12 23:49:43 +02007851 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007852 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007853
7854 /* Now we have the status line between cur_ptr and cur_end */
7855
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007856 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007857 switch (exp->action) {
7858 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007859 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007860 done = 1;
7861 break;
7862
7863 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007864 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007865 done = 1;
7866 break;
7867
7868 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007869 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7870 if (trash.len < 0)
7871 return -1;
7872
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007873 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007874 /* FIXME: if the user adds a newline in the replacement, the
7875 * index will not be recalculated for now, and the new line
7876 * will not be counted as a new header.
7877 */
7878
Willy Tarreaufa355d42009-11-29 18:12:29 +01007879 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007880 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007881 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007882 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007883 cur_ptr, cur_end + 1,
7884 NULL, NULL);
7885 if (unlikely(!cur_end))
7886 return -1;
7887
7888 /* we have a full respnse and we know that we have either a CR
7889 * or an LF at <ptr>.
7890 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007891 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007892 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007893 /* there is no point trying this regex on headers */
7894 return 1;
7895 }
7896 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007897 return done;
7898}
7899
7900
7901
7902/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007903 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007904 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7905 * unparsable response.
7906 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007907int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007908{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007909 struct http_txn *txn = &s->txn;
7910 struct hdr_exp *exp;
7911
7912 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007913 int ret;
7914
7915 /*
7916 * The interleaving of transformations and verdicts
7917 * makes it difficult to decide to continue or stop
7918 * the evaluation.
7919 */
7920
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007921 if (txn->flags & TX_SVDENY)
7922 break;
7923
Willy Tarreau3d300592007-03-18 18:34:41 +01007924 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007925 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7926 exp->action == ACT_PASS)) {
7927 exp = exp->next;
7928 continue;
7929 }
7930
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007931 /* if this filter had a condition, evaluate it now and skip to
7932 * next filter if the condition does not match.
7933 */
7934 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007935 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007936 ret = acl_pass(ret);
7937 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7938 ret = !ret;
7939 if (!ret)
7940 continue;
7941 }
7942
Willy Tarreaua15645d2007-03-18 16:22:39 +01007943 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007944 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007945 if (unlikely(ret < 0))
7946 return -1;
7947
7948 if (likely(ret == 0)) {
7949 /* The filter did not match the response, it can be
7950 * iterated through all headers.
7951 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007952 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7953 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007954 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007955 }
7956 return 0;
7957}
7958
7959
Willy Tarreaua15645d2007-03-18 16:22:39 +01007960/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007961 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007962 * desirable to call it only when needed. This function is also used when we
7963 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007964 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007965void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007966{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007967 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007968 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007969 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007970 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007971 char *hdr_beg, *hdr_end, *hdr_next;
7972 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007973
Willy Tarreaua15645d2007-03-18 16:22:39 +01007974 /* Iterate through the headers.
7975 * we start with the start line.
7976 */
7977 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007978 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007979
7980 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7981 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007982 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007983
7984 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007985 hdr_beg = hdr_next;
7986 hdr_end = hdr_beg + cur_hdr->len;
7987 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007988
Willy Tarreau24581ba2010-08-31 22:39:35 +02007989 /* We have one full header between hdr_beg and hdr_end, and the
7990 * next header starts at hdr_next. We're only interested in
7991 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007992 */
7993
Willy Tarreau24581ba2010-08-31 22:39:35 +02007994 is_cookie2 = 0;
7995 prev = hdr_beg + 10;
7996 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007997 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007998 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7999 if (!val) {
8000 old_idx = cur_idx;
8001 continue;
8002 }
8003 is_cookie2 = 1;
8004 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008005 }
8006
Willy Tarreau24581ba2010-08-31 22:39:35 +02008007 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8008 * <prev> points to the colon.
8009 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008010 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008011
Willy Tarreau24581ba2010-08-31 22:39:35 +02008012 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8013 * check-cache is enabled) and we are not interested in checking
8014 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008015 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008016 if (s->be->cookie_name == NULL &&
8017 s->be->appsession_name == NULL &&
8018 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008019 return;
8020
Willy Tarreau24581ba2010-08-31 22:39:35 +02008021 /* OK so now we know we have to process this response cookie.
8022 * The format of the Set-Cookie header is slightly different
8023 * from the format of the Cookie header in that it does not
8024 * support the comma as a cookie delimiter (thus the header
8025 * cannot be folded) because the Expires attribute described in
8026 * the original Netscape's spec may contain an unquoted date
8027 * with a comma inside. We have to live with this because
8028 * many browsers don't support Max-Age and some browsers don't
8029 * support quoted strings. However the Set-Cookie2 header is
8030 * clean.
8031 *
8032 * We have to keep multiple pointers in order to support cookie
8033 * removal at the beginning, middle or end of header without
8034 * corrupting the header (in case of set-cookie2). A special
8035 * pointer, <scav> points to the beginning of the set-cookie-av
8036 * fields after the first semi-colon. The <next> pointer points
8037 * either to the end of line (set-cookie) or next unquoted comma
8038 * (set-cookie2). All of these headers are valid :
8039 *
8040 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8041 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8042 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8043 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8044 * | | | | | | | | | |
8045 * | | | | | | | | +-> next hdr_end <--+
8046 * | | | | | | | +------------> scav
8047 * | | | | | | +--------------> val_end
8048 * | | | | | +--------------------> val_beg
8049 * | | | | +----------------------> equal
8050 * | | | +------------------------> att_end
8051 * | | +----------------------------> att_beg
8052 * | +------------------------------> prev
8053 * +-----------------------------------------> hdr_beg
8054 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008055
Willy Tarreau24581ba2010-08-31 22:39:35 +02008056 for (; prev < hdr_end; prev = next) {
8057 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008058
Willy Tarreau24581ba2010-08-31 22:39:35 +02008059 /* find att_beg */
8060 att_beg = prev + 1;
8061 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8062 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008063
Willy Tarreau24581ba2010-08-31 22:39:35 +02008064 /* find att_end : this is the first character after the last non
8065 * space before the equal. It may be equal to hdr_end.
8066 */
8067 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008068
Willy Tarreau24581ba2010-08-31 22:39:35 +02008069 while (equal < hdr_end) {
8070 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8071 break;
8072 if (http_is_spht[(unsigned char)*equal++])
8073 continue;
8074 att_end = equal;
8075 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008076
Willy Tarreau24581ba2010-08-31 22:39:35 +02008077 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8078 * is between <att_beg> and <equal>, both may be identical.
8079 */
8080
8081 /* look for end of cookie if there is an equal sign */
8082 if (equal < hdr_end && *equal == '=') {
8083 /* look for the beginning of the value */
8084 val_beg = equal + 1;
8085 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8086 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008087
Willy Tarreau24581ba2010-08-31 22:39:35 +02008088 /* find the end of the value, respecting quotes */
8089 next = find_cookie_value_end(val_beg, hdr_end);
8090
8091 /* make val_end point to the first white space or delimitor after the value */
8092 val_end = next;
8093 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8094 val_end--;
8095 } else {
8096 /* <equal> points to next comma, semi-colon or EOL */
8097 val_beg = val_end = next = equal;
8098 }
8099
8100 if (next < hdr_end) {
8101 /* Set-Cookie2 supports multiple cookies, and <next> points to
8102 * a colon or semi-colon before the end. So skip all attr-value
8103 * pairs and look for the next comma. For Set-Cookie, since
8104 * commas are permitted in values, skip to the end.
8105 */
8106 if (is_cookie2)
8107 next = find_hdr_value_end(next, hdr_end);
8108 else
8109 next = hdr_end;
8110 }
8111
8112 /* Now everything is as on the diagram above */
8113
8114 /* Ignore cookies with no equal sign */
8115 if (equal == val_end)
8116 continue;
8117
8118 /* If there are spaces around the equal sign, we need to
8119 * strip them otherwise we'll get trouble for cookie captures,
8120 * or even for rewrites. Since this happens extremely rarely,
8121 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008122 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008123 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8124 int stripped_before = 0;
8125 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008126
Willy Tarreau24581ba2010-08-31 22:39:35 +02008127 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008128 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008129 equal += stripped_before;
8130 val_beg += stripped_before;
8131 }
8132
8133 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008134 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008135 val_beg += stripped_after;
8136 stripped_before += stripped_after;
8137 }
8138
8139 val_end += stripped_before;
8140 next += stripped_before;
8141 hdr_end += stripped_before;
8142 hdr_next += stripped_before;
8143 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008144 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008145 }
8146
8147 /* First, let's see if we want to capture this cookie. We check
8148 * that we don't already have a server side cookie, because we
8149 * can only capture one. Also as an optimisation, we ignore
8150 * cookies shorter than the declared name.
8151 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008152 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008153 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008154 (val_end - att_beg >= s->fe->capture_namelen) &&
8155 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008156 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008157 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008158 Alert("HTTP logging : out of memory.\n");
8159 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008160 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008161 if (log_len > s->fe->capture_len)
8162 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008163 memcpy(txn->srv_cookie, att_beg, log_len);
8164 txn->srv_cookie[log_len] = 0;
8165 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008166 }
8167
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008168 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008169 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008170 if (!(s->flags & SN_IGNORE_PRST) &&
8171 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8172 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008173 /* assume passive cookie by default */
8174 txn->flags &= ~TX_SCK_MASK;
8175 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008176
8177 /* If the cookie is in insert mode on a known server, we'll delete
8178 * this occurrence because we'll insert another one later.
8179 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008180 * a direct access.
8181 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008182 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008183 /* The "preserve" flag was set, we don't want to touch the
8184 * server's cookie.
8185 */
8186 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008187 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8188 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008189 /* this cookie must be deleted */
8190 if (*prev == ':' && next == hdr_end) {
8191 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008192 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008193 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8194 txn->hdr_idx.used--;
8195 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008196 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008197 hdr_next += delta;
8198 http_msg_move_end(&txn->rsp, delta);
8199 /* note: while both invalid now, <next> and <hdr_end>
8200 * are still equal, so the for() will stop as expected.
8201 */
8202 } else {
8203 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008204 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008205 next = prev;
8206 hdr_end += delta;
8207 hdr_next += delta;
8208 cur_hdr->len += delta;
8209 http_msg_move_end(&txn->rsp, delta);
8210 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008211 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008212 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008213 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008214 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008215 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008216 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008217 * with this server since we know it.
8218 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008219 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008220 next += delta;
8221 hdr_end += delta;
8222 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008223 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008224 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008225
Willy Tarreauf1348312010-10-07 15:54:11 +02008226 txn->flags &= ~TX_SCK_MASK;
8227 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008228 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008229 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008230 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008231 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008232 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008233 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008234 next += delta;
8235 hdr_end += delta;
8236 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008237 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008238 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008239
Willy Tarreau827aee92011-03-10 16:55:02 +01008240 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008241 txn->flags &= ~TX_SCK_MASK;
8242 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008243 }
8244 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008245 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008246 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008247 int cmp_len, value_len;
8248 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008249
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008250 if (s->be->options2 & PR_O2_AS_PFX) {
8251 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8252 value_begin = att_beg + s->be->appsession_name_len;
8253 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008254 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008255 cmp_len = att_end - att_beg;
8256 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008257 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008258 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008259
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008260 if ((cmp_len == s->be->appsession_name_len) &&
8261 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008262 /* free a possibly previously allocated memory */
8263 pool_free2(apools.sessid, txn->sessid);
8264
Cyril Bontéb21570a2009-11-29 20:04:48 +01008265 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008266 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008267 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008268 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008269 return;
8270 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008271 memcpy(txn->sessid, value_begin, value_len);
8272 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008273 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008274 }
8275 /* that's done for this cookie, check the next one on the same
8276 * line when next != hdr_end (only if is_cookie2).
8277 */
8278 }
8279 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008280 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008281 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008282
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008283 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008284 appsess *asession = NULL;
8285 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008286 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008287 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008288 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008289 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8290 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008291 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008292 return;
8293 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008294 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8295
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008296 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8297 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008298 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8299 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008300 return;
8301 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008302 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8303 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008304
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008305 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008306 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008307 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008308 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8309 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008310 return;
8311 }
8312 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008313 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008314
8315 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008316 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008317 }
8318
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008319 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008320 asession->request_count++;
8321 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008322}
8323
8324
Willy Tarreaua15645d2007-03-18 16:22:39 +01008325/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008326 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008327 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008328void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008329{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008330 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008331 char *p1, *p2;
8332
8333 char *cur_ptr, *cur_end, *cur_next;
8334 int cur_idx;
8335
Willy Tarreau5df51872007-11-25 16:20:08 +01008336 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008337 return;
8338
8339 /* Iterate through the headers.
8340 * we start with the start line.
8341 */
8342 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008343 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008344
8345 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8346 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008347 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008348
8349 cur_hdr = &txn->hdr_idx.v[cur_idx];
8350 cur_ptr = cur_next;
8351 cur_end = cur_ptr + cur_hdr->len;
8352 cur_next = cur_end + cur_hdr->cr + 1;
8353
8354 /* We have one full header between cur_ptr and cur_end, and the
8355 * next header starts at cur_next. We're only interested in
8356 * "Cookie:" headers.
8357 */
8358
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008359 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8360 if (val) {
8361 if ((cur_end - (cur_ptr + val) >= 8) &&
8362 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8363 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8364 return;
8365 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008366 }
8367
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008368 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8369 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008370 continue;
8371
8372 /* OK, right now we know we have a cache-control header at cur_ptr */
8373
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008374 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008375
8376 if (p1 >= cur_end) /* no more info */
8377 continue;
8378
8379 /* p1 is at the beginning of the value */
8380 p2 = p1;
8381
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008382 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008383 p2++;
8384
8385 /* we have a complete value between p1 and p2 */
8386 if (p2 < cur_end && *p2 == '=') {
8387 /* we have something of the form no-cache="set-cookie" */
8388 if ((cur_end - p1 >= 21) &&
8389 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8390 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008391 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008392 continue;
8393 }
8394
8395 /* OK, so we know that either p2 points to the end of string or to a comma */
8396 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008397 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008398 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8399 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8400 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008401 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008402 return;
8403 }
8404
8405 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008406 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008407 continue;
8408 }
8409 }
8410}
8411
8412
Willy Tarreau58f10d72006-12-04 02:26:12 +01008413/*
8414 * Try to retrieve a known appsession in the URI, then the associated server.
8415 * If the server is found, it's assigned to the session.
8416 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008417void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008418{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008419 char *end_params, *first_param, *cur_param, *next_param;
8420 char separator;
8421 int value_len;
8422
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008423 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008424
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008425 if (s->be->appsession_name == NULL ||
8426 (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 +01008427 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008428 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008429
Cyril Bontéb21570a2009-11-29 20:04:48 +01008430 first_param = NULL;
8431 switch (mode) {
8432 case PR_O2_AS_M_PP:
8433 first_param = memchr(begin, ';', len);
8434 break;
8435 case PR_O2_AS_M_QS:
8436 first_param = memchr(begin, '?', len);
8437 break;
8438 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008439
Cyril Bontéb21570a2009-11-29 20:04:48 +01008440 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008441 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008442 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008443
Cyril Bontéb21570a2009-11-29 20:04:48 +01008444 switch (mode) {
8445 case PR_O2_AS_M_PP:
8446 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8447 end_params = (char *) begin + len;
8448 }
8449 separator = ';';
8450 break;
8451 case PR_O2_AS_M_QS:
8452 end_params = (char *) begin + len;
8453 separator = '&';
8454 break;
8455 default:
8456 /* unknown mode, shouldn't happen */
8457 return;
8458 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008459
Cyril Bontéb21570a2009-11-29 20:04:48 +01008460 cur_param = next_param = end_params;
8461 while (cur_param > first_param) {
8462 cur_param--;
8463 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8464 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008465 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8466 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8467 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008468 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008469 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8470 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008471 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008472 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008473 }
8474 break;
8475 }
8476 next_param = cur_param;
8477 }
8478 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008479#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008480 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008481 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008482#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008483}
8484
Willy Tarreaub2513902006-12-17 14:52:38 +01008485/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008486 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008487 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008488 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008489 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008490 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008491 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008492 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008493 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008494int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008495{
8496 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008497 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008498 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008499
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008500 if (!uri_auth)
8501 return 0;
8502
Cyril Bonté70be45d2010-10-12 00:14:35 +02008503 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008504 return 0;
8505
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008506 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008507 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008508 return 0;
8509
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008510 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008511 return 0;
8512
Willy Tarreaub2513902006-12-17 14:52:38 +01008513 return 1;
8514}
8515
Willy Tarreau4076a152009-04-02 15:18:36 +02008516/*
8517 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008518 * By default it tries to report the error position as msg->err_pos. However if
8519 * this one is not set, it will then report msg->next, which is the last known
8520 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008521 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008522 */
8523void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008524 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008525 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008526{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008527 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008528 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008529
Willy Tarreau9b28e032012-10-12 23:49:43 +02008530 es->len = MIN(chn->buf->i, sizeof(es->buf));
8531 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008532 len1 = MIN(len1, es->len);
8533 len2 = es->len - len1; /* remaining data if buffer wraps */
8534
Willy Tarreau9b28e032012-10-12 23:49:43 +02008535 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008536 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008537 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008538
Willy Tarreau4076a152009-04-02 15:18:36 +02008539 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008540 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008541 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008542 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008543
Willy Tarreau4076a152009-04-02 15:18:36 +02008544 es->when = date; // user-visible date
8545 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008546 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008547 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008548 if (objt_conn(s->req->prod->end))
8549 es->src = __objt_conn(s->req->prod->end)->addr.from;
8550 else
8551 memset(&es->src, 0, sizeof(es->src));
8552
Willy Tarreau078272e2010-12-12 12:46:33 +01008553 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008554 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008555 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008556 es->s_flags = s->flags;
8557 es->t_flags = s->txn.flags;
8558 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008559 es->b_out = chn->buf->o;
8560 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008561 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008562 es->m_clen = msg->chunk_len;
8563 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008564}
Willy Tarreaub2513902006-12-17 14:52:38 +01008565
Willy Tarreau294c4732011-12-16 21:35:50 +01008566/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8567 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8568 * performed over the whole headers. Otherwise it must contain a valid header
8569 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8570 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8571 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8572 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008573 * -1. The value fetch stops at commas, so this function is suited for use with
8574 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008575 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008576 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008577unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008578 struct hdr_idx *idx, int occ,
8579 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008580{
Willy Tarreau294c4732011-12-16 21:35:50 +01008581 struct hdr_ctx local_ctx;
8582 char *ptr_hist[MAX_HDR_HISTORY];
8583 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008584 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008585 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008586
Willy Tarreau294c4732011-12-16 21:35:50 +01008587 if (!ctx) {
8588 local_ctx.idx = 0;
8589 ctx = &local_ctx;
8590 }
8591
Willy Tarreaubce70882009-09-07 11:51:47 +02008592 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008593 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008594 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008595 occ--;
8596 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008597 *vptr = ctx->line + ctx->val;
8598 *vlen = ctx->vlen;
8599 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008600 }
8601 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008602 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008603 }
8604
8605 /* negative occurrence, we scan all the list then walk back */
8606 if (-occ > MAX_HDR_HISTORY)
8607 return 0;
8608
Willy Tarreau294c4732011-12-16 21:35:50 +01008609 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008610 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008611 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8612 len_hist[hist_ptr] = ctx->vlen;
8613 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008614 hist_ptr = 0;
8615 found++;
8616 }
8617 if (-occ > found)
8618 return 0;
8619 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008620 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8621 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8622 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008623 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008624 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008625 if (hist_ptr >= MAX_HDR_HISTORY)
8626 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008627 *vptr = ptr_hist[hist_ptr];
8628 *vlen = len_hist[hist_ptr];
8629 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008630}
8631
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008632/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8633 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8634 * performed over the whole headers. Otherwise it must contain a valid header
8635 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8636 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8637 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8638 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8639 * -1. This function differs from http_get_hdr() in that it only returns full
8640 * line header values and does not stop at commas.
8641 * The return value is 0 if nothing was found, or non-zero otherwise.
8642 */
8643unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8644 struct hdr_idx *idx, int occ,
8645 struct hdr_ctx *ctx, char **vptr, int *vlen)
8646{
8647 struct hdr_ctx local_ctx;
8648 char *ptr_hist[MAX_HDR_HISTORY];
8649 int len_hist[MAX_HDR_HISTORY];
8650 unsigned int hist_ptr;
8651 int found;
8652
8653 if (!ctx) {
8654 local_ctx.idx = 0;
8655 ctx = &local_ctx;
8656 }
8657
8658 if (occ >= 0) {
8659 /* search from the beginning */
8660 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8661 occ--;
8662 if (occ <= 0) {
8663 *vptr = ctx->line + ctx->val;
8664 *vlen = ctx->vlen;
8665 return 1;
8666 }
8667 }
8668 return 0;
8669 }
8670
8671 /* negative occurrence, we scan all the list then walk back */
8672 if (-occ > MAX_HDR_HISTORY)
8673 return 0;
8674
8675 found = hist_ptr = 0;
8676 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8677 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8678 len_hist[hist_ptr] = ctx->vlen;
8679 if (++hist_ptr >= MAX_HDR_HISTORY)
8680 hist_ptr = 0;
8681 found++;
8682 }
8683 if (-occ > found)
8684 return 0;
8685 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8686 * find occurrence -occ, so we have to check [hist_ptr+occ].
8687 */
8688 hist_ptr += occ;
8689 if (hist_ptr >= MAX_HDR_HISTORY)
8690 hist_ptr -= MAX_HDR_HISTORY;
8691 *vptr = ptr_hist[hist_ptr];
8692 *vlen = len_hist[hist_ptr];
8693 return 1;
8694}
8695
Willy Tarreaubaaee002006-06-26 02:48:02 +02008696/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008697 * Print a debug line with a header. Always stop at the first CR or LF char,
8698 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8699 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008700 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008701void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008702{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008703 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008704 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008705 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008706 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8707 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 +02008708
8709 for (max = 0; start + max < end; max++)
8710 if (start[max] == '\r' || start[max] == '\n')
8711 break;
8712
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008713 UBOUND(max, trash.size - trash.len - 3);
8714 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8715 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008716 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008717}
8718
Willy Tarreau0937bc42009-12-22 15:03:09 +01008719/*
8720 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8721 * the required fields are properly allocated and that we only need to (re)init
8722 * them. This should be used before processing any new request.
8723 */
8724void http_init_txn(struct session *s)
8725{
8726 struct http_txn *txn = &s->txn;
8727 struct proxy *fe = s->fe;
8728
8729 txn->flags = 0;
8730 txn->status = -1;
8731
Willy Tarreauf64d1412010-10-07 20:06:11 +02008732 txn->cookie_first_date = 0;
8733 txn->cookie_last_date = 0;
8734
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008735 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008736 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008737 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008738 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008739 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008740 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008741 txn->req.chunk_len = 0LL;
8742 txn->req.body_len = 0LL;
8743 txn->rsp.chunk_len = 0LL;
8744 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008745 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8746 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008747 txn->req.chn = s->req;
8748 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008749
8750 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008751
8752 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8753 if (fe->options2 & PR_O2_REQBUG_OK)
8754 txn->req.err_pos = -1; /* let buggy requests pass */
8755
Willy Tarreau46023632010-01-07 22:51:47 +01008756 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008757 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8758
Willy Tarreau46023632010-01-07 22:51:47 +01008759 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008760 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8761
8762 if (txn->hdr_idx.v)
8763 hdr_idx_init(&txn->hdr_idx);
8764}
8765
8766/* to be used at the end of a transaction */
8767void http_end_txn(struct session *s)
8768{
8769 struct http_txn *txn = &s->txn;
8770
Willy Tarreau75195602014-03-11 15:48:55 +01008771 /* release any possible compression context */
8772 if (s->flags & SN_COMP_READY)
8773 s->comp_algo->end(&s->comp_ctx);
8774 s->comp_algo = NULL;
8775 s->flags &= ~SN_COMP_READY;
8776
Willy Tarreau0937bc42009-12-22 15:03:09 +01008777 /* these ones will have been dynamically allocated */
8778 pool_free2(pool2_requri, txn->uri);
8779 pool_free2(pool2_capture, txn->cli_cookie);
8780 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008781 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008782 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008783
William Lallemanda73203e2012-03-12 12:48:57 +01008784 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008785 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008786 txn->uri = NULL;
8787 txn->srv_cookie = NULL;
8788 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008789
8790 if (txn->req.cap) {
8791 struct cap_hdr *h;
8792 for (h = s->fe->req_cap; h; h = h->next)
8793 pool_free2(h->pool, txn->req.cap[h->index]);
8794 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8795 }
8796
8797 if (txn->rsp.cap) {
8798 struct cap_hdr *h;
8799 for (h = s->fe->rsp_cap; h; h = h->next)
8800 pool_free2(h->pool, txn->rsp.cap[h->index]);
8801 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8802 }
8803
Willy Tarreau0937bc42009-12-22 15:03:09 +01008804}
8805
8806/* to be used at the end of a transaction to prepare a new one */
8807void http_reset_txn(struct session *s)
8808{
8809 http_end_txn(s);
8810 http_init_txn(s);
8811
8812 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008813 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008814 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008815 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008816 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008817 /* re-init store persistence */
8818 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008819 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008820
Willy Tarreau0937bc42009-12-22 15:03:09 +01008821 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008822
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008823 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008824
Willy Tarreau739cfba2010-01-25 23:11:14 +01008825 /* We must trim any excess data from the response buffer, because we
8826 * may have blocked an invalid response from a server that we don't
8827 * want to accidentely forward once we disable the analysers, nor do
8828 * we want those data to come along with next response. A typical
8829 * example of such data would be from a buggy server responding to
8830 * a HEAD with some data, or sending more than the advertised
8831 * content-length.
8832 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008833 if (unlikely(s->rep->buf->i))
8834 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008835
Willy Tarreau0937bc42009-12-22 15:03:09 +01008836 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008837 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008838
Willy Tarreaud04e8582010-05-31 12:31:35 +02008839 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008840 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008841
8842 s->req->rex = TICK_ETERNITY;
8843 s->req->wex = TICK_ETERNITY;
8844 s->req->analyse_exp = TICK_ETERNITY;
8845 s->rep->rex = TICK_ETERNITY;
8846 s->rep->wex = TICK_ETERNITY;
8847 s->rep->analyse_exp = TICK_ETERNITY;
8848}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008849
Sasha Pachev218f0642014-06-16 12:05:59 -06008850void free_http_res_rules(struct list *r)
8851{
8852 struct http_res_rule *tr, *pr;
8853
8854 list_for_each_entry_safe(pr, tr, r, list) {
8855 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008856 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008857 free(pr);
8858 }
8859}
8860
8861void free_http_req_rules(struct list *r)
8862{
Willy Tarreauff011f22011-01-06 17:51:27 +01008863 struct http_req_rule *tr, *pr;
8864
8865 list_for_each_entry_safe(pr, tr, r, list) {
8866 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008867 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008868 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008869
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008870 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008871 free(pr);
8872 }
8873}
8874
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008875/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008876struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8877{
8878 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008879 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008880 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008881 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008882
8883 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8884 if (!rule) {
8885 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008886 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008887 }
8888
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008889 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008890 rule->action = HTTP_REQ_ACT_ALLOW;
8891 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008892 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008893 rule->action = HTTP_REQ_ACT_DENY;
8894 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008895 } else if (!strcmp(args[0], "tarpit")) {
8896 rule->action = HTTP_REQ_ACT_TARPIT;
8897 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008898 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008899 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008900 cur_arg = 1;
8901
8902 while(*args[cur_arg]) {
8903 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008904 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008905 cur_arg+=2;
8906 continue;
8907 } else
8908 break;
8909 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008910 } else if (!strcmp(args[0], "set-nice")) {
8911 rule->action = HTTP_REQ_ACT_SET_NICE;
8912 cur_arg = 1;
8913
8914 if (!*args[cur_arg] ||
8915 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8916 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8917 file, linenum, args[0]);
8918 goto out_err;
8919 }
8920 rule->arg.nice = atoi(args[cur_arg]);
8921 if (rule->arg.nice < -1024)
8922 rule->arg.nice = -1024;
8923 else if (rule->arg.nice > 1024)
8924 rule->arg.nice = 1024;
8925 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008926 } else if (!strcmp(args[0], "set-tos")) {
8927#ifdef IP_TOS
8928 char *err;
8929 rule->action = HTTP_REQ_ACT_SET_TOS;
8930 cur_arg = 1;
8931
8932 if (!*args[cur_arg] ||
8933 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8934 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8935 file, linenum, args[0]);
8936 goto out_err;
8937 }
8938
8939 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8940 if (err && *err != '\0') {
8941 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8942 file, linenum, err, args[0]);
8943 goto out_err;
8944 }
8945 cur_arg++;
8946#else
8947 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8948 goto out_err;
8949#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008950 } else if (!strcmp(args[0], "set-mark")) {
8951#ifdef SO_MARK
8952 char *err;
8953 rule->action = HTTP_REQ_ACT_SET_MARK;
8954 cur_arg = 1;
8955
8956 if (!*args[cur_arg] ||
8957 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8958 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8959 file, linenum, args[0]);
8960 goto out_err;
8961 }
8962
8963 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8964 if (err && *err != '\0') {
8965 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8966 file, linenum, err, args[0]);
8967 goto out_err;
8968 }
8969 cur_arg++;
8970 global.last_checks |= LSTCHK_NETADM;
8971#else
8972 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8973 goto out_err;
8974#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008975 } else if (!strcmp(args[0], "set-log-level")) {
8976 rule->action = HTTP_REQ_ACT_SET_LOGL;
8977 cur_arg = 1;
8978
8979 if (!*args[cur_arg] ||
8980 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8981 bad_log_level:
8982 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8983 file, linenum, args[0]);
8984 goto out_err;
8985 }
8986 if (strcmp(args[cur_arg], "silent") == 0)
8987 rule->arg.loglevel = -1;
8988 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8989 goto bad_log_level;
8990 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008991 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8992 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8993 cur_arg = 1;
8994
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008995 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8996 (*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 +01008997 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8998 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008999 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009000 }
9001
9002 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9003 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9004 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009005
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009006 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009007 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009008 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9009 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009010 free(proxy->conf.lfs_file);
9011 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9012 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009013 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009014 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9015 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009016 cur_arg = 1;
9017
9018 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann8f249f42014-06-24 11:10:00 +02009019 (*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 -06009020 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9021 file, linenum, args[0]);
9022 goto out_err;
9023 }
9024
9025 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9026 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9027 LIST_INIT(&rule->arg.hdr_add.fmt);
9028
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009029 error = NULL;
9030 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9031 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9032 args[cur_arg + 1], error);
9033 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009034 goto out_err;
9035 }
9036
9037 proxy->conf.args.ctx = ARGC_HRQ;
9038 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9039 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9040 file, linenum);
9041
9042 free(proxy->conf.lfs_file);
9043 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9044 proxy->conf.lfs_line = proxy->conf.args.line;
9045 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009046 } else if (strcmp(args[0], "del-header") == 0) {
9047 rule->action = HTTP_REQ_ACT_DEL_HDR;
9048 cur_arg = 1;
9049
9050 if (!*args[cur_arg] ||
9051 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9052 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9053 file, linenum, args[0]);
9054 goto out_err;
9055 }
9056
9057 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9058 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9059
9060 proxy->conf.args.ctx = ARGC_HRQ;
9061 free(proxy->conf.lfs_file);
9062 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9063 proxy->conf.lfs_line = proxy->conf.args.line;
9064 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009065 } else if (strcmp(args[0], "redirect") == 0) {
9066 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009067 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009068
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009069 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009070 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9071 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9072 goto out_err;
9073 }
9074
9075 /* this redirect rule might already contain a parsed condition which
9076 * we'll pass to the http-request rule.
9077 */
9078 rule->action = HTTP_REQ_ACT_REDIR;
9079 rule->arg.redir = redir;
9080 rule->cond = redir->cond;
9081 redir->cond = NULL;
9082 cur_arg = 2;
9083 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009084 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9085 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9086 rule->action = HTTP_REQ_ACT_ADD_ACL;
9087 /*
9088 * '+ 8' for 'add-acl('
9089 * '- 9' for 'add-acl(' + trailing ')'
9090 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009091 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009092
9093 cur_arg = 1;
9094
9095 if (!*args[cur_arg] ||
9096 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9097 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9098 file, linenum, args[0]);
9099 goto out_err;
9100 }
9101
9102 LIST_INIT(&rule->arg.map.key);
9103 proxy->conf.args.ctx = ARGC_HRQ;
9104 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9105 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9106 file, linenum);
9107 free(proxy->conf.lfs_file);
9108 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9109 proxy->conf.lfs_line = proxy->conf.args.line;
9110 cur_arg += 1;
9111 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9112 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9113 rule->action = HTTP_REQ_ACT_DEL_ACL;
9114 /*
9115 * '+ 8' for 'del-acl('
9116 * '- 9' for 'del-acl(' + trailing ')'
9117 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009118 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009119
9120 cur_arg = 1;
9121
9122 if (!*args[cur_arg] ||
9123 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9124 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9125 file, linenum, args[0]);
9126 goto out_err;
9127 }
9128
9129 LIST_INIT(&rule->arg.map.key);
9130 proxy->conf.args.ctx = ARGC_HRQ;
9131 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9132 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9133 file, linenum);
9134 free(proxy->conf.lfs_file);
9135 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9136 proxy->conf.lfs_line = proxy->conf.args.line;
9137 cur_arg += 1;
9138 } else if (strncmp(args[0], "del-map", 7) == 0) {
9139 /* http-request del-map(<reference (map name)>) <key pattern> */
9140 rule->action = HTTP_REQ_ACT_DEL_MAP;
9141 /*
9142 * '+ 8' for 'del-map('
9143 * '- 9' for 'del-map(' + trailing ')'
9144 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009145 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009146
9147 cur_arg = 1;
9148
9149 if (!*args[cur_arg] ||
9150 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9151 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9152 file, linenum, args[0]);
9153 goto out_err;
9154 }
9155
9156 LIST_INIT(&rule->arg.map.key);
9157 proxy->conf.args.ctx = ARGC_HRQ;
9158 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9159 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9160 file, linenum);
9161 free(proxy->conf.lfs_file);
9162 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9163 proxy->conf.lfs_line = proxy->conf.args.line;
9164 cur_arg += 1;
9165 } else if (strncmp(args[0], "set-map", 7) == 0) {
9166 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9167 rule->action = HTTP_REQ_ACT_SET_MAP;
9168 /*
9169 * '+ 8' for 'set-map('
9170 * '- 9' for 'set-map(' + trailing ')'
9171 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009172 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009173
9174 cur_arg = 1;
9175
9176 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9177 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9178 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9179 file, linenum, args[0]);
9180 goto out_err;
9181 }
9182
9183 LIST_INIT(&rule->arg.map.key);
9184 LIST_INIT(&rule->arg.map.value);
9185 proxy->conf.args.ctx = ARGC_HRQ;
9186
9187 /* key pattern */
9188 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9189 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9190 file, linenum);
9191
9192 /* value pattern */
9193 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9194 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9195 file, linenum);
9196 free(proxy->conf.lfs_file);
9197 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9198 proxy->conf.lfs_line = proxy->conf.args.line;
9199
9200 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009201 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9202 char *errmsg = NULL;
9203 cur_arg = 1;
9204 /* try in the module list */
9205 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9206 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9207 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9208 free(errmsg);
9209 goto out_err;
9210 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009211 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009212 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 +01009213 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009214 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009215 }
9216
9217 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9218 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009219 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009220
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009221 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9222 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9223 file, linenum, args[0], errmsg);
9224 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009225 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009226 }
9227 rule->cond = cond;
9228 }
9229 else if (*args[cur_arg]) {
9230 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9231 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9232 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009233 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009234 }
9235
9236 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009237 out_err:
9238 free(rule);
9239 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009240}
9241
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009242/* parse an "http-respose" rule */
9243struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9244{
9245 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009246 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009247 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009248 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009249
9250 rule = calloc(1, sizeof(*rule));
9251 if (!rule) {
9252 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9253 goto out_err;
9254 }
9255
9256 if (!strcmp(args[0], "allow")) {
9257 rule->action = HTTP_RES_ACT_ALLOW;
9258 cur_arg = 1;
9259 } else if (!strcmp(args[0], "deny")) {
9260 rule->action = HTTP_RES_ACT_DENY;
9261 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009262 } else if (!strcmp(args[0], "set-nice")) {
9263 rule->action = HTTP_RES_ACT_SET_NICE;
9264 cur_arg = 1;
9265
9266 if (!*args[cur_arg] ||
9267 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9268 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9269 file, linenum, args[0]);
9270 goto out_err;
9271 }
9272 rule->arg.nice = atoi(args[cur_arg]);
9273 if (rule->arg.nice < -1024)
9274 rule->arg.nice = -1024;
9275 else if (rule->arg.nice > 1024)
9276 rule->arg.nice = 1024;
9277 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009278 } else if (!strcmp(args[0], "set-tos")) {
9279#ifdef IP_TOS
9280 char *err;
9281 rule->action = HTTP_RES_ACT_SET_TOS;
9282 cur_arg = 1;
9283
9284 if (!*args[cur_arg] ||
9285 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9286 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9287 file, linenum, args[0]);
9288 goto out_err;
9289 }
9290
9291 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9292 if (err && *err != '\0') {
9293 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9294 file, linenum, err, args[0]);
9295 goto out_err;
9296 }
9297 cur_arg++;
9298#else
9299 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9300 goto out_err;
9301#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009302 } else if (!strcmp(args[0], "set-mark")) {
9303#ifdef SO_MARK
9304 char *err;
9305 rule->action = HTTP_RES_ACT_SET_MARK;
9306 cur_arg = 1;
9307
9308 if (!*args[cur_arg] ||
9309 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9310 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9311 file, linenum, args[0]);
9312 goto out_err;
9313 }
9314
9315 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9316 if (err && *err != '\0') {
9317 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9318 file, linenum, err, args[0]);
9319 goto out_err;
9320 }
9321 cur_arg++;
9322 global.last_checks |= LSTCHK_NETADM;
9323#else
9324 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9325 goto out_err;
9326#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009327 } else if (!strcmp(args[0], "set-log-level")) {
9328 rule->action = HTTP_RES_ACT_SET_LOGL;
9329 cur_arg = 1;
9330
9331 if (!*args[cur_arg] ||
9332 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9333 bad_log_level:
9334 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9335 file, linenum, args[0]);
9336 goto out_err;
9337 }
9338 if (strcmp(args[cur_arg], "silent") == 0)
9339 rule->arg.loglevel = -1;
9340 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9341 goto bad_log_level;
9342 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009343 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9344 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9345 cur_arg = 1;
9346
9347 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9348 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9349 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9350 file, linenum, args[0]);
9351 goto out_err;
9352 }
9353
9354 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9355 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9356 LIST_INIT(&rule->arg.hdr_add.fmt);
9357
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009358 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009359 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009360 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9361 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009362 free(proxy->conf.lfs_file);
9363 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9364 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009365 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009366 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009367 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009368 cur_arg = 1;
9369
9370 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmanna772b942014-08-08 17:29:06 +02009371 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9372 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009373 file, linenum, args[0]);
9374 goto out_err;
9375 }
9376
9377 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9378 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9379 LIST_INIT(&rule->arg.hdr_add.fmt);
9380
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009381 error = NULL;
9382 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9383 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9384 args[cur_arg + 1], error);
9385 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009386 goto out_err;
9387 }
9388
9389 proxy->conf.args.ctx = ARGC_HRQ;
9390 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9391 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9392 file, linenum);
9393
9394 free(proxy->conf.lfs_file);
9395 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9396 proxy->conf.lfs_line = proxy->conf.args.line;
9397 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009398 } else if (strcmp(args[0], "del-header") == 0) {
9399 rule->action = HTTP_RES_ACT_DEL_HDR;
9400 cur_arg = 1;
9401
9402 if (!*args[cur_arg] ||
9403 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9404 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9405 file, linenum, args[0]);
9406 goto out_err;
9407 }
9408
9409 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9410 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9411
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009412 proxy->conf.args.ctx = ARGC_HRS;
9413 free(proxy->conf.lfs_file);
9414 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9415 proxy->conf.lfs_line = proxy->conf.args.line;
9416 cur_arg += 1;
9417 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9418 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9419 rule->action = HTTP_RES_ACT_ADD_ACL;
9420 /*
9421 * '+ 8' for 'add-acl('
9422 * '- 9' for 'add-acl(' + trailing ')'
9423 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009424 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009425
9426 cur_arg = 1;
9427
9428 if (!*args[cur_arg] ||
9429 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9430 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9431 file, linenum, args[0]);
9432 goto out_err;
9433 }
9434
9435 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009436 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009437 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9438 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9439 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009440 free(proxy->conf.lfs_file);
9441 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9442 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009443
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009444 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009445 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9446 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9447 rule->action = HTTP_RES_ACT_DEL_ACL;
9448 /*
9449 * '+ 8' for 'del-acl('
9450 * '- 9' for 'del-acl(' + trailing ')'
9451 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009452 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009453
9454 cur_arg = 1;
9455
9456 if (!*args[cur_arg] ||
9457 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9458 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9459 file, linenum, args[0]);
9460 goto out_err;
9461 }
9462
9463 LIST_INIT(&rule->arg.map.key);
9464 proxy->conf.args.ctx = ARGC_HRS;
9465 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9466 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9467 file, linenum);
9468 free(proxy->conf.lfs_file);
9469 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9470 proxy->conf.lfs_line = proxy->conf.args.line;
9471 cur_arg += 1;
9472 } else if (strncmp(args[0], "del-map", 7) == 0) {
9473 /* http-response del-map(<reference (map name)>) <key pattern> */
9474 rule->action = HTTP_RES_ACT_DEL_MAP;
9475 /*
9476 * '+ 8' for 'del-map('
9477 * '- 9' for 'del-map(' + trailing ')'
9478 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009479 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009480
9481 cur_arg = 1;
9482
9483 if (!*args[cur_arg] ||
9484 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9485 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9486 file, linenum, args[0]);
9487 goto out_err;
9488 }
9489
9490 LIST_INIT(&rule->arg.map.key);
9491 proxy->conf.args.ctx = ARGC_HRS;
9492 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9493 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9494 file, linenum);
9495 free(proxy->conf.lfs_file);
9496 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9497 proxy->conf.lfs_line = proxy->conf.args.line;
9498 cur_arg += 1;
9499 } else if (strncmp(args[0], "set-map", 7) == 0) {
9500 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9501 rule->action = HTTP_RES_ACT_SET_MAP;
9502 /*
9503 * '+ 8' for 'set-map('
9504 * '- 9' for 'set-map(' + trailing ')'
9505 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009506 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009507
9508 cur_arg = 1;
9509
9510 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9511 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9512 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9513 file, linenum, args[0]);
9514 goto out_err;
9515 }
9516
9517 LIST_INIT(&rule->arg.map.key);
9518 LIST_INIT(&rule->arg.map.value);
9519
9520 proxy->conf.args.ctx = ARGC_HRS;
9521
9522 /* key pattern */
9523 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9524 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9525 file, linenum);
9526
9527 /* value pattern */
9528 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9529 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9530 file, linenum);
9531
9532 free(proxy->conf.lfs_file);
9533 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9534 proxy->conf.lfs_line = proxy->conf.args.line;
9535
9536 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009537 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9538 char *errmsg = NULL;
9539 cur_arg = 1;
9540 /* try in the module list */
9541 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9542 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9543 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9544 free(errmsg);
9545 goto out_err;
9546 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009547 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009548 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 +02009549 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9550 goto out_err;
9551 }
9552
9553 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9554 struct acl_cond *cond;
9555 char *errmsg = NULL;
9556
9557 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9558 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9559 file, linenum, args[0], errmsg);
9560 free(errmsg);
9561 goto out_err;
9562 }
9563 rule->cond = cond;
9564 }
9565 else if (*args[cur_arg]) {
9566 Alert("parsing [%s:%d]: 'http-response %s' expects"
9567 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9568 file, linenum, args[0], args[cur_arg]);
9569 goto out_err;
9570 }
9571
9572 return rule;
9573 out_err:
9574 free(rule);
9575 return NULL;
9576}
9577
Willy Tarreau4baae242012-12-27 12:00:31 +01009578/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009579 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9580 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009581 */
9582struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009583 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009584{
9585 struct redirect_rule *rule;
9586 int cur_arg;
9587 int type = REDIRECT_TYPE_NONE;
9588 int code = 302;
9589 const char *destination = NULL;
9590 const char *cookie = NULL;
9591 int cookie_set = 0;
9592 unsigned int flags = REDIRECT_FLAG_NONE;
9593 struct acl_cond *cond = NULL;
9594
9595 cur_arg = 0;
9596 while (*(args[cur_arg])) {
9597 if (strcmp(args[cur_arg], "location") == 0) {
9598 if (!*args[cur_arg + 1])
9599 goto missing_arg;
9600
9601 type = REDIRECT_TYPE_LOCATION;
9602 cur_arg++;
9603 destination = args[cur_arg];
9604 }
9605 else if (strcmp(args[cur_arg], "prefix") == 0) {
9606 if (!*args[cur_arg + 1])
9607 goto missing_arg;
9608
9609 type = REDIRECT_TYPE_PREFIX;
9610 cur_arg++;
9611 destination = args[cur_arg];
9612 }
9613 else if (strcmp(args[cur_arg], "scheme") == 0) {
9614 if (!*args[cur_arg + 1])
9615 goto missing_arg;
9616
9617 type = REDIRECT_TYPE_SCHEME;
9618 cur_arg++;
9619 destination = args[cur_arg];
9620 }
9621 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9622 if (!*args[cur_arg + 1])
9623 goto missing_arg;
9624
9625 cur_arg++;
9626 cookie = args[cur_arg];
9627 cookie_set = 1;
9628 }
9629 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9630 if (!*args[cur_arg + 1])
9631 goto missing_arg;
9632
9633 cur_arg++;
9634 cookie = args[cur_arg];
9635 cookie_set = 0;
9636 }
9637 else if (strcmp(args[cur_arg], "code") == 0) {
9638 if (!*args[cur_arg + 1])
9639 goto missing_arg;
9640
9641 cur_arg++;
9642 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009643 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009644 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009645 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009646 args[cur_arg - 1], args[cur_arg]);
9647 return NULL;
9648 }
9649 }
9650 else if (!strcmp(args[cur_arg],"drop-query")) {
9651 flags |= REDIRECT_FLAG_DROP_QS;
9652 }
9653 else if (!strcmp(args[cur_arg],"append-slash")) {
9654 flags |= REDIRECT_FLAG_APPEND_SLASH;
9655 }
9656 else if (strcmp(args[cur_arg], "if") == 0 ||
9657 strcmp(args[cur_arg], "unless") == 0) {
9658 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9659 if (!cond) {
9660 memprintf(errmsg, "error in condition: %s", *errmsg);
9661 return NULL;
9662 }
9663 break;
9664 }
9665 else {
9666 memprintf(errmsg,
9667 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9668 args[cur_arg]);
9669 return NULL;
9670 }
9671 cur_arg++;
9672 }
9673
9674 if (type == REDIRECT_TYPE_NONE) {
9675 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9676 return NULL;
9677 }
9678
9679 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9680 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009681 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009682
9683 if (!use_fmt) {
9684 /* old-style static redirect rule */
9685 rule->rdr_str = strdup(destination);
9686 rule->rdr_len = strlen(destination);
9687 }
9688 else {
9689 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009690
9691 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9692 * if prefix == "/", we don't want to add anything, otherwise it
9693 * makes it hard for the user to configure a self-redirection.
9694 */
Godbach543b9782014-12-18 15:44:58 +08009695 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009696 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009697 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009698 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9699 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009700 free(curproxy->conf.lfs_file);
9701 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9702 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009703 }
9704 }
9705
Willy Tarreau4baae242012-12-27 12:00:31 +01009706 if (cookie) {
9707 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9708 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9709 */
9710 rule->cookie_len = strlen(cookie);
9711 if (cookie_set) {
9712 rule->cookie_str = malloc(rule->cookie_len + 10);
9713 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9714 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9715 rule->cookie_len += 9;
9716 } else {
9717 rule->cookie_str = malloc(rule->cookie_len + 21);
9718 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9719 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9720 rule->cookie_len += 20;
9721 }
9722 }
9723 rule->type = type;
9724 rule->code = code;
9725 rule->flags = flags;
9726 LIST_INIT(&rule->list);
9727 return rule;
9728
9729 missing_arg:
9730 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9731 return NULL;
9732}
9733
Willy Tarreau8797c062007-05-07 00:55:35 +02009734/************************************************************************/
9735/* The code below is dedicated to ACL parsing and matching */
9736/************************************************************************/
9737
9738
Willy Tarreau14174bc2012-04-16 14:34:04 +02009739/* This function ensures that the prerequisites for an L7 fetch are ready,
9740 * which means that a request or response is ready. If some data is missing,
9741 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009742 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9743 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009744 *
9745 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009746 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9747 * decide whether or not an HTTP message is present ;
9748 * 0 if the requested data cannot be fetched or if it is certain that
9749 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009750 * 1 if an HTTP message is ready
9751 */
9752static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009753smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009754 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009755{
9756 struct http_txn *txn = l7;
9757 struct http_msg *msg = &txn->req;
9758
9759 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9760 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9761 */
9762
9763 if (unlikely(!s || !txn))
9764 return 0;
9765
9766 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009767 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009768
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009769 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009770 if (unlikely(!s->req))
9771 return 0;
9772
Willy Tarreauaae75e32013-03-29 12:31:49 +01009773 /* If the buffer does not leave enough free space at the end,
9774 * we must first realign it.
9775 */
9776 if (s->req->buf->p > s->req->buf->data &&
9777 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9778 buffer_slow_realign(s->req->buf);
9779
Willy Tarreau14174bc2012-04-16 14:34:04 +02009780 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009781 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009782 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009783
9784 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009785 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009786 http_msg_analyzer(msg, &txn->hdr_idx);
9787
9788 /* Still no valid request ? */
9789 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009790 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009791 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009792 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009793 }
9794 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009795 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009796 return 0;
9797 }
9798
9799 /* OK we just got a valid HTTP request. We have some minor
9800 * preparation to perform so that further checks can rely
9801 * on HTTP tests.
9802 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009803
9804 /* If the request was parsed but was too large, we must absolutely
9805 * return an error so that it is not processed. At the moment this
9806 * cannot happen, but if the parsers are to change in the future,
9807 * we want this check to be maintained.
9808 */
9809 if (unlikely(s->req->buf->i + s->req->buf->p >
9810 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9811 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009812 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009813 return 1;
9814 }
9815
Willy Tarreau9b28e032012-10-12 23:49:43 +02009816 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009817 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9818 s->flags |= SN_REDIRECTABLE;
9819
Willy Tarreau506d0502013-07-06 13:29:24 +02009820 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9821 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009822 }
9823
Willy Tarreau506d0502013-07-06 13:29:24 +02009824 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009825 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009826 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009827
9828 /* otherwise everything's ready for the request */
9829 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009830 else {
9831 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009832 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9833 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009834 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009835 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009836 }
9837
9838 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009839 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009840 return 1;
9841}
Willy Tarreau8797c062007-05-07 00:55:35 +02009842
Willy Tarreaua4ba9db2014-06-25 16:56:41 +02009843/* Note: these functinos *do* modify the sample. Even in case of success, at
9844 * least the type and uint value are modified.
9845 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009846#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009847 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 +02009848
Willy Tarreau24e32d82012-04-23 23:55:44 +02009849#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009850 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 +02009851
Willy Tarreau8797c062007-05-07 00:55:35 +02009852
9853/* 1. Check on METHOD
9854 * We use the pre-parsed method if it is known, and store its number as an
9855 * integer. If it is unknown, we use the pointer and the length.
9856 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009857static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009858{
9859 int len, meth;
9860
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009861 len = strlen(text);
9862 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009863
9864 pattern->val.i = meth;
9865 if (meth == HTTP_METH_OTHER) {
Willy Tarreau71196f32014-08-29 15:15:50 +02009866 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009867 pattern->len = len;
9868 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009869 else {
9870 pattern->ptr.str = NULL;
9871 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009872 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009873 return 1;
9874}
9875
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009876/* This function fetches the method of current HTTP request and stores
9877 * it in the global pattern struct as a chunk. There are two possibilities :
9878 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9879 * in <len> and <ptr> is NULL ;
9880 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9881 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009882 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009883 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009884static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009885smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009886 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009887{
9888 int meth;
9889 struct http_txn *txn = l7;
9890
Willy Tarreau24e32d82012-04-23 23:55:44 +02009891 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009892
Willy Tarreau8797c062007-05-07 00:55:35 +02009893 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009894 smp->type = SMP_T_METH;
9895 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009896 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009897 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9898 /* ensure the indexes are not affected */
9899 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009900 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009901 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9902 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009903 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009904 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009905 return 1;
9906}
9907
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009908/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009909static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009910{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009911 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009912 struct pattern_list *lst;
9913 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009914
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009915 list_for_each_entry(lst, &expr->patterns, list) {
9916 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009917
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009918 /* well-known method */
9919 if (pattern->val.i != HTTP_METH_OTHER) {
9920 if (smp->data.meth.meth == pattern->val.i)
9921 return pattern;
9922 else
9923 continue;
9924 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009925
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009926 /* Other method, we must compare the strings */
9927 if (pattern->len != smp->data.meth.str.len)
9928 continue;
9929
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009930 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreauc62a0c62014-08-28 20:42:57 +02009931 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
9932 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009933 return pattern;
9934 }
9935 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009936}
9937
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009938static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009939smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009940 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009941{
9942 struct http_txn *txn = l7;
9943 char *ptr;
9944 int len;
9945
Willy Tarreauc0239e02012-04-16 14:42:55 +02009946 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009947
Willy Tarreau8797c062007-05-07 00:55:35 +02009948 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009949 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009950
9951 while ((len-- > 0) && (*ptr++ != '/'));
9952 if (len <= 0)
9953 return 0;
9954
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009955 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009956 smp->data.str.str = ptr;
9957 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009958
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009959 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009960 return 1;
9961}
9962
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009963static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009964smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009965 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009966{
9967 struct http_txn *txn = l7;
9968 char *ptr;
9969 int len;
9970
Willy Tarreauc0239e02012-04-16 14:42:55 +02009971 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009972
Willy Tarreauf26b2522012-12-14 08:33:14 +01009973 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9974 return 0;
9975
Willy Tarreau8797c062007-05-07 00:55:35 +02009976 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009977 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009978
9979 while ((len-- > 0) && (*ptr++ != '/'));
9980 if (len <= 0)
9981 return 0;
9982
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009983 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009984 smp->data.str.str = ptr;
9985 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009986
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009987 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009988 return 1;
9989}
9990
9991/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009992static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009993smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009994 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009995{
9996 struct http_txn *txn = l7;
9997 char *ptr;
9998 int len;
9999
Willy Tarreauc0239e02012-04-16 14:42:55 +020010000 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010001
Willy Tarreauf26b2522012-12-14 08:33:14 +010010002 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10003 return 0;
10004
Willy Tarreau8797c062007-05-07 00:55:35 +020010005 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010006 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010007
Willy Tarreauf853c462012-04-23 18:53:56 +020010008 smp->type = SMP_T_UINT;
10009 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010010 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010011 return 1;
10012}
10013
10014/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010015static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010016smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010017 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +020010018{
10019 struct http_txn *txn = l7;
10020
Willy Tarreauc0239e02012-04-16 14:42:55 +020010021 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010022
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010023 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010024 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010025 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010026 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010027 return 1;
10028}
10029
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010030static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010031smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010032 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010033{
10034 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010035 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010036
Willy Tarreauc0239e02012-04-16 14:42:55 +020010037 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010038
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010039 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 +020010040 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010041 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010042
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010043 smp->type = SMP_T_IPV4;
10044 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010045 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010046 return 1;
10047}
10048
10049static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010050smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010051 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010052{
10053 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010054 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010055
Willy Tarreauc0239e02012-04-16 14:42:55 +020010056 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010057
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010058 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 +020010059 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10060 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010061
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010062 smp->type = SMP_T_UINT;
10063 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010064 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010065 return 1;
10066}
10067
Willy Tarreau185b5c42012-04-26 15:11:51 +020010068/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10069 * Accepts an optional argument of type string containing the header field name,
10070 * and an optional argument of type signed or unsigned integer to request an
10071 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010072 * headers are considered from the first one. It does not stop on commas and
10073 * returns full lines instead (useful for User-Agent or Date for example).
10074 */
10075static int
10076smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010077 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010078{
10079 struct http_txn *txn = l7;
10080 struct hdr_idx *idx = &txn->hdr_idx;
10081 struct hdr_ctx *ctx = smp->ctx.a[0];
10082 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10083 int occ = 0;
10084 const char *name_str = NULL;
10085 int name_len = 0;
10086
10087 if (!ctx) {
10088 /* first call */
10089 ctx = &static_hdr_ctx;
10090 ctx->idx = 0;
10091 smp->ctx.a[0] = ctx;
10092 }
10093
10094 if (args) {
10095 if (args[0].type != ARGT_STR)
10096 return 0;
10097 name_str = args[0].data.str.str;
10098 name_len = args[0].data.str.len;
10099
10100 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10101 occ = args[1].data.uint;
10102 }
10103
10104 CHECK_HTTP_MESSAGE_FIRST();
10105
10106 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10107 /* search for header from the beginning */
10108 ctx->idx = 0;
10109
10110 if (!occ && !(opt & SMP_OPT_ITERATE))
10111 /* no explicit occurrence and single fetch => last header by default */
10112 occ = -1;
10113
10114 if (!occ)
10115 /* prepare to report multiple occurrences for ACL fetches */
10116 smp->flags |= SMP_F_NOT_LAST;
10117
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010118 smp->type = SMP_T_STR;
10119 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010120 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10121 return 1;
10122
10123 smp->flags &= ~SMP_F_NOT_LAST;
10124 return 0;
10125}
10126
10127/* 6. Check on HTTP header count. The number of occurrences is returned.
10128 * Accepts exactly 1 argument of type string. It does not stop on commas and
10129 * returns full lines instead (useful for User-Agent or Date for example).
10130 */
10131static int
10132smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010133 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010134{
10135 struct http_txn *txn = l7;
10136 struct hdr_idx *idx = &txn->hdr_idx;
10137 struct hdr_ctx ctx;
10138 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10139 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010140 const char *name = NULL;
10141 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010142
Willy Tarreau29437342015-04-01 19:16:09 +020010143 if (args && args->type == ARGT_STR) {
10144 name = args->data.str.str;
10145 len = args->data.str.len;
10146 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010147
10148 CHECK_HTTP_MESSAGE_FIRST();
10149
10150 ctx.idx = 0;
10151 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010152 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010153 cnt++;
10154
10155 smp->type = SMP_T_UINT;
10156 smp->data.uint = cnt;
10157 smp->flags = SMP_F_VOL_HDR;
10158 return 1;
10159}
10160
10161/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10162 * Accepts an optional argument of type string containing the header field name,
10163 * and an optional argument of type signed or unsigned integer to request an
10164 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010165 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010166 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010167static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010168smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010169 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010170{
10171 struct http_txn *txn = l7;
10172 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010173 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010174 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010175 int occ = 0;
10176 const char *name_str = NULL;
10177 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010178
Willy Tarreaua890d072013-04-02 12:01:06 +020010179 if (!ctx) {
10180 /* first call */
10181 ctx = &static_hdr_ctx;
10182 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010183 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010184 }
10185
Willy Tarreau185b5c42012-04-26 15:11:51 +020010186 if (args) {
10187 if (args[0].type != ARGT_STR)
10188 return 0;
10189 name_str = args[0].data.str.str;
10190 name_len = args[0].data.str.len;
10191
10192 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10193 occ = args[1].data.uint;
10194 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010195
Willy Tarreaue333ec92012-04-16 16:26:40 +020010196 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010197
Willy Tarreau185b5c42012-04-26 15:11:51 +020010198 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010199 /* search for header from the beginning */
10200 ctx->idx = 0;
10201
Willy Tarreau185b5c42012-04-26 15:11:51 +020010202 if (!occ && !(opt & SMP_OPT_ITERATE))
10203 /* no explicit occurrence and single fetch => last header by default */
10204 occ = -1;
10205
10206 if (!occ)
10207 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010208 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010209
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010210 smp->type = SMP_T_STR;
10211 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010212 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 +020010213 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010214
Willy Tarreau37406352012-04-23 16:16:37 +020010215 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010216 return 0;
10217}
10218
Willy Tarreauc11416f2007-06-17 16:58:38 +020010219/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010220 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010221 */
10222static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010223smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010224 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010225{
10226 struct http_txn *txn = l7;
10227 struct hdr_idx *idx = &txn->hdr_idx;
10228 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010229 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010230 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010231 const char *name = NULL;
10232 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010233
Willy Tarreau29437342015-04-01 19:16:09 +020010234 if (args && args->type == ARGT_STR) {
10235 name = args->data.str.str;
10236 len = args->data.str.len;
10237 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010238
Willy Tarreaue333ec92012-04-16 16:26:40 +020010239 CHECK_HTTP_MESSAGE_FIRST();
10240
Willy Tarreau33a7e692007-06-10 19:45:56 +020010241 ctx.idx = 0;
10242 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010243 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010244 cnt++;
10245
Willy Tarreauf853c462012-04-23 18:53:56 +020010246 smp->type = SMP_T_UINT;
10247 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010248 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010249 return 1;
10250}
10251
Willy Tarreau185b5c42012-04-26 15:11:51 +020010252/* Fetch an HTTP header's integer value. The integer value is returned. It
10253 * takes a mandatory argument of type string and an optional one of type int
10254 * to designate a specific occurrence. It returns an unsigned integer, which
10255 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010256 */
10257static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010258smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010259 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010260{
Willy Tarreauef38c392013-07-22 16:29:32 +020010261 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010262
Willy Tarreauf853c462012-04-23 18:53:56 +020010263 if (ret > 0) {
10264 smp->type = SMP_T_UINT;
10265 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10266 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010267
Willy Tarreaud53e2422012-04-16 17:21:11 +020010268 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010269}
10270
Cyril Bonté69fa9922012-10-25 00:01:06 +020010271/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10272 * and an optional one of type int to designate a specific occurrence.
10273 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010274 */
10275static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010276smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010277 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +020010278{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010279 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010280
Willy Tarreauef38c392013-07-22 16:29:32 +020010281 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010282 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10283 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010284 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010285 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010286 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010287 if (smp->data.str.len < temp->size - 1) {
10288 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10289 temp->str[smp->data.str.len] = '\0';
10290 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10291 smp->type = SMP_T_IPV6;
10292 break;
10293 }
10294 }
10295 }
10296
Willy Tarreaud53e2422012-04-16 17:21:11 +020010297 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010298 if (!(smp->flags & SMP_F_NOT_LAST))
10299 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010300 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010301 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010302}
10303
Willy Tarreau737b0c12007-06-10 21:28:46 +020010304/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10305 * the first '/' after the possible hostname, and ends before the possible '?'.
10306 */
10307static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010308smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010309 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010310{
10311 struct http_txn *txn = l7;
10312 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010313
Willy Tarreauc0239e02012-04-16 14:42:55 +020010314 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010315
Willy Tarreau9b28e032012-10-12 23:49:43 +020010316 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010317 ptr = http_get_path(txn);
10318 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010319 return 0;
10320
10321 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010322 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010323 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010324
10325 while (ptr < end && *ptr != '?')
10326 ptr++;
10327
Willy Tarreauf853c462012-04-23 18:53:56 +020010328 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010329 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010330 return 1;
10331}
10332
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010333/* This produces a concatenation of the first occurrence of the Host header
10334 * followed by the path component if it begins with a slash ('/'). This means
10335 * that '*' will not be added, resulting in exactly the first Host entry.
10336 * If no Host header is found, then the path is returned as-is. The returned
10337 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010338 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010339 */
10340static int
10341smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010342 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010343{
10344 struct http_txn *txn = l7;
10345 char *ptr, *end, *beg;
10346 struct hdr_ctx ctx;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010347 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010348
10349 CHECK_HTTP_MESSAGE_FIRST();
10350
10351 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010352 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +020010353 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010354
10355 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010356 temp = get_trash_chunk();
10357 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010358 smp->type = SMP_T_STR;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010359 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010360 smp->data.str.len = ctx.vlen;
10361
10362 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010363 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010364 beg = http_get_path(txn);
10365 if (!beg)
10366 beg = end;
10367
10368 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10369
10370 if (beg < ptr && *beg == '/') {
10371 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10372 smp->data.str.len += ptr - beg;
10373 }
10374
10375 smp->flags = SMP_F_VOL_1ST;
10376 return 1;
10377}
10378
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010379/* This produces a 32-bit hash of the concatenation of the first occurrence of
10380 * the Host header followed by the path component if it begins with a slash ('/').
10381 * This means that '*' will not be added, resulting in exactly the first Host
10382 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010383 * is hashed using the path hash followed by a full avalanche hash and provides a
10384 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010385 * high-traffic sites without having to store whole paths.
10386 */
10387static int
10388smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010389 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010390{
10391 struct http_txn *txn = l7;
10392 struct hdr_ctx ctx;
10393 unsigned int hash = 0;
10394 char *ptr, *beg, *end;
10395 int len;
10396
10397 CHECK_HTTP_MESSAGE_FIRST();
10398
10399 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010400 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010401 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10402 ptr = ctx.line + ctx.val;
10403 len = ctx.vlen;
10404 while (len--)
10405 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10406 }
10407
10408 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010409 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010410 beg = http_get_path(txn);
10411 if (!beg)
10412 beg = end;
10413
10414 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10415
10416 if (beg < ptr && *beg == '/') {
10417 while (beg < ptr)
10418 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10419 }
10420 hash = full_hash(hash);
10421
10422 smp->type = SMP_T_UINT;
10423 smp->data.uint = hash;
10424 smp->flags = SMP_F_VOL_1ST;
10425 return 1;
10426}
10427
Willy Tarreau4a550602012-12-09 14:53:32 +010010428/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010429 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10430 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10431 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010432 * that in environments where IPv6 is insignificant, truncating the output to
10433 * 8 bytes would still work.
10434 */
10435static int
10436smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010437 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +010010438{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010439 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010440 struct connection *cli_conn = objt_conn(l4->si[0].end);
10441
10442 if (!cli_conn)
10443 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010444
Willy Tarreauef38c392013-07-22 16:29:32 +020010445 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +010010446 return 0;
10447
Willy Tarreau47ca5452012-12-23 20:22:19 +010010448 temp = get_trash_chunk();
Willy Tarreau0dff81c2014-07-15 21:34:06 +020010449 *(unsigned int *)temp->str = htonl(smp->data.uint);
10450 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010451
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010452 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010453 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010454 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010455 temp->len += 4;
10456 break;
10457 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010458 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010459 temp->len += 16;
10460 break;
10461 default:
10462 return 0;
10463 }
10464
10465 smp->data.str = *temp;
10466 smp->type = SMP_T_BIN;
10467 return 1;
10468}
10469
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010470static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010471smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010472 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010473{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010474 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10475 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10476 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010477
Willy Tarreau24e32d82012-04-23 23:55:44 +020010478 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010479
Willy Tarreauf853c462012-04-23 18:53:56 +020010480 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010481 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010482 return 1;
10483}
10484
Willy Tarreau7f18e522010-10-22 20:04:13 +020010485/* return a valid test if the current request is the first one on the connection */
10486static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010487smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010488 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010489{
10490 if (!s)
10491 return 0;
10492
Willy Tarreauf853c462012-04-23 18:53:56 +020010493 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010494 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010495 return 1;
10496}
10497
Willy Tarreau34db1082012-04-19 17:16:54 +020010498/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010499static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010500smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010501 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010502{
10503
Willy Tarreau24e32d82012-04-23 23:55:44 +020010504 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010505 return 0;
10506
Willy Tarreauc0239e02012-04-16 14:42:55 +020010507 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010508
Willy Tarreauc0239e02012-04-16 14:42:55 +020010509 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010510 return 0;
10511
Willy Tarreauf853c462012-04-23 18:53:56 +020010512 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010513 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010514 return 1;
10515}
Willy Tarreau8797c062007-05-07 00:55:35 +020010516
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010517/* Accepts exactly 1 argument of type userlist */
10518static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010519smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010520 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010521{
10522
10523 if (!args || args->type != ARGT_USR)
10524 return 0;
10525
10526 CHECK_HTTP_MESSAGE_FIRST();
10527
10528 if (!get_http_auth(l4))
10529 return 0;
10530
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010531 /* if the user does not belong to the userlist or has a wrong password,
10532 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010533 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010534 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010535 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10536 return 0;
10537
10538 /* pat_match_auth() will need the user list */
10539 smp->ctx.a[0] = args->data.usr;
10540
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010541 smp->type = SMP_T_STR;
10542 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010543 smp->data.str.str = l4->txn.auth.user;
10544 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010545
10546 return 1;
10547}
10548
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010549/* Try to find the next occurrence of a cookie name in a cookie header value.
10550 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10551 * the cookie value is returned into *value and *value_l, and the function
10552 * returns a pointer to the next pointer to search from if the value was found.
10553 * Otherwise if the cookie was not found, NULL is returned and neither value
10554 * nor value_l are touched. The input <hdr> string should first point to the
10555 * header's value, and the <hdr_end> pointer must point to the first character
10556 * not part of the value. <list> must be non-zero if value may represent a list
10557 * of values (cookie headers). This makes it faster to abort parsing when no
10558 * list is expected.
10559 */
10560static char *
10561extract_cookie_value(char *hdr, const char *hdr_end,
10562 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010563 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010564{
10565 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10566 char *next;
10567
10568 /* we search at least a cookie name followed by an equal, and more
10569 * generally something like this :
10570 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10571 */
10572 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10573 /* Iterate through all cookies on this line */
10574
10575 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10576 att_beg++;
10577
10578 /* find att_end : this is the first character after the last non
10579 * space before the equal. It may be equal to hdr_end.
10580 */
10581 equal = att_end = att_beg;
10582
10583 while (equal < hdr_end) {
10584 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10585 break;
10586 if (http_is_spht[(unsigned char)*equal++])
10587 continue;
10588 att_end = equal;
10589 }
10590
10591 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10592 * is between <att_beg> and <equal>, both may be identical.
10593 */
10594
10595 /* look for end of cookie if there is an equal sign */
10596 if (equal < hdr_end && *equal == '=') {
10597 /* look for the beginning of the value */
10598 val_beg = equal + 1;
10599 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10600 val_beg++;
10601
10602 /* find the end of the value, respecting quotes */
10603 next = find_cookie_value_end(val_beg, hdr_end);
10604
10605 /* make val_end point to the first white space or delimitor after the value */
10606 val_end = next;
10607 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10608 val_end--;
10609 } else {
10610 val_beg = val_end = next = equal;
10611 }
10612
10613 /* We have nothing to do with attributes beginning with '$'. However,
10614 * they will automatically be removed if a header before them is removed,
10615 * since they're supposed to be linked together.
10616 */
10617 if (*att_beg == '$')
10618 continue;
10619
10620 /* Ignore cookies with no equal sign */
10621 if (equal == next)
10622 continue;
10623
10624 /* Now we have the cookie name between att_beg and att_end, and
10625 * its value between val_beg and val_end.
10626 */
10627
10628 if (att_end - att_beg == cookie_name_l &&
10629 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10630 /* let's return this value and indicate where to go on from */
10631 *value = val_beg;
10632 *value_l = val_end - val_beg;
10633 return next + 1;
10634 }
10635
10636 /* Set-Cookie headers only have the name in the first attr=value part */
10637 if (!list)
10638 break;
10639 }
10640
10641 return NULL;
10642}
10643
William Lallemanda43ba4e2014-01-28 18:14:25 +010010644/* Fetch a captured HTTP request header. The index is the position of
10645 * the "capture" option in the configuration file
10646 */
10647static int
10648smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10649 const struct arg *args, struct sample *smp, const char *kw)
10650{
10651 struct proxy *fe = l4->fe;
10652 struct http_txn *txn = l7;
10653 int idx;
10654
10655 if (!args || args->type != ARGT_UINT)
10656 return 0;
10657
10658 idx = args->data.uint;
10659
10660 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10661 return 0;
10662
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010663 smp->type = SMP_T_STR;
10664 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010665 smp->data.str.str = txn->req.cap[idx];
10666 smp->data.str.len = strlen(txn->req.cap[idx]);
10667
10668 return 1;
10669}
10670
10671/* Fetch a captured HTTP response header. The index is the position of
10672 * the "capture" option in the configuration file
10673 */
10674static int
10675smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10676 const struct arg *args, struct sample *smp, const char *kw)
10677{
10678 struct proxy *fe = l4->fe;
10679 struct http_txn *txn = l7;
10680 int idx;
10681
10682 if (!args || args->type != ARGT_UINT)
10683 return 0;
10684
10685 idx = args->data.uint;
10686
10687 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10688 return 0;
10689
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010690 smp->type = SMP_T_STR;
10691 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010692 smp->data.str.str = txn->rsp.cap[idx];
10693 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10694
10695 return 1;
10696}
10697
William Lallemand65ad6e12014-01-31 15:08:02 +010010698/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10699static int
10700smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10701 const struct arg *args, struct sample *smp, const char *kw)
10702{
10703 struct chunk *temp;
10704 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010705 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010706
10707 if (!txn->uri)
10708 return 0;
10709
William Lallemand96a77852014-02-05 00:30:02 +010010710 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010711
William Lallemand96a77852014-02-05 00:30:02 +010010712 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10713 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010714
William Lallemand96a77852014-02-05 00:30:02 +010010715 temp = get_trash_chunk();
10716 temp->str = txn->uri;
10717 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010718 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010719 smp->type = SMP_T_STR;
10720 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010721
10722 return 1;
10723
10724}
10725
10726/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10727static int
10728smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10729 const struct arg *args, struct sample *smp, const char *kw)
10730{
10731 struct chunk *temp;
10732 struct http_txn *txn = l7;
10733 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010734
10735 if (!txn->uri)
10736 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010737
William Lallemand65ad6e12014-01-31 15:08:02 +010010738 ptr = txn->uri;
10739
10740 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10741 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010742
William Lallemand65ad6e12014-01-31 15:08:02 +010010743 if (!*ptr)
10744 return 0;
10745
10746 ptr++; /* skip the space */
10747
10748 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010749 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010750 if (!ptr)
10751 return 0;
10752 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10753 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010754
10755 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010756 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010757 smp->type = SMP_T_STR;
10758 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010759
10760 return 1;
10761}
10762
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010763/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10764 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10765 */
10766static int
10767smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10768 const struct arg *args, struct sample *smp, const char *kw)
10769{
10770 struct http_txn *txn = l7;
10771
10772 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10773 return 0;
10774
10775 if (txn->req.flags & HTTP_MSGF_VER_11)
10776 smp->data.str.str = "HTTP/1.1";
10777 else
10778 smp->data.str.str = "HTTP/1.0";
10779
10780 smp->data.str.len = 8;
10781 smp->type = SMP_T_STR;
10782 smp->flags = SMP_F_CONST;
10783 return 1;
10784
10785}
10786
10787/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10788 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10789 */
10790static int
10791smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10792 const struct arg *args, struct sample *smp, const char *kw)
10793{
10794 struct http_txn *txn = l7;
10795
10796 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10797 return 0;
10798
10799 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10800 smp->data.str.str = "HTTP/1.1";
10801 else
10802 smp->data.str.str = "HTTP/1.0";
10803
10804 smp->data.str.len = 8;
10805 smp->type = SMP_T_STR;
10806 smp->flags = SMP_F_CONST;
10807 return 1;
10808
10809}
10810
William Lallemand65ad6e12014-01-31 15:08:02 +010010811
Willy Tarreaue333ec92012-04-16 16:26:40 +020010812/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010813 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010814 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010815 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010816 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010817 * Accepts exactly 1 argument of type string. If the input options indicate
10818 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010819 * The returned sample is of type CSTR. Can be used to parse cookies in other
10820 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010821 */
William Lallemand07c8b242014-05-02 17:11:07 +020010822int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010823 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010824{
10825 struct http_txn *txn = l7;
10826 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010827 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010828 const struct http_msg *msg;
10829 const char *hdr_name;
10830 int hdr_name_len;
10831 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010832 int occ = 0;
10833 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010834
Willy Tarreau24e32d82012-04-23 23:55:44 +020010835 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010836 return 0;
10837
Willy Tarreaua890d072013-04-02 12:01:06 +020010838 if (!ctx) {
10839 /* first call */
10840 ctx = &static_hdr_ctx;
10841 ctx->idx = 0;
10842 smp->ctx.a[2] = ctx;
10843 }
10844
Willy Tarreaue333ec92012-04-16 16:26:40 +020010845 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010846
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010847 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010848 msg = &txn->req;
10849 hdr_name = "Cookie";
10850 hdr_name_len = 6;
10851 } else {
10852 msg = &txn->rsp;
10853 hdr_name = "Set-Cookie";
10854 hdr_name_len = 10;
10855 }
10856
Willy Tarreau28376d62012-04-26 21:26:10 +020010857 if (!occ && !(opt & SMP_OPT_ITERATE))
10858 /* no explicit occurrence and single fetch => last cookie by default */
10859 occ = -1;
10860
10861 /* OK so basically here, either we want only one value and it's the
10862 * last one, or we want to iterate over all of them and we fetch the
10863 * next one.
10864 */
10865
Willy Tarreau9b28e032012-10-12 23:49:43 +020010866 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010867 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010868 /* search for the header from the beginning, we must first initialize
10869 * the search parameters.
10870 */
Willy Tarreau37406352012-04-23 16:16:37 +020010871 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010872 ctx->idx = 0;
10873 }
10874
Willy Tarreau28376d62012-04-26 21:26:10 +020010875 smp->flags |= SMP_F_VOL_HDR;
10876
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010877 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010878 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10879 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010880 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10881 goto out;
10882
Willy Tarreau24e32d82012-04-23 23:55:44 +020010883 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010884 continue;
10885
Willy Tarreau37406352012-04-23 16:16:37 +020010886 smp->ctx.a[0] = ctx->line + ctx->val;
10887 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010888 }
10889
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010890 smp->type = SMP_T_STR;
10891 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010892 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010893 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010894 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010895 &smp->data.str.str,
10896 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010897 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010898 found = 1;
10899 if (occ >= 0) {
10900 /* one value was returned into smp->data.str.{str,len} */
10901 smp->flags |= SMP_F_NOT_LAST;
10902 return 1;
10903 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010904 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010905 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010906 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010907 /* all cookie headers and values were scanned. If we're looking for the
10908 * last occurrence, we may return it now.
10909 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010910 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010911 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010912 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010913}
10914
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010915/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010916 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010917 * multiple cookies may be parsed on the same line. The returned sample is of
10918 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010919 */
10920static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010921smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010922 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010923{
10924 struct http_txn *txn = l7;
10925 struct hdr_idx *idx = &txn->hdr_idx;
10926 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010927 const struct http_msg *msg;
10928 const char *hdr_name;
10929 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010930 int cnt;
10931 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010932 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010933
Willy Tarreau24e32d82012-04-23 23:55:44 +020010934 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010935 return 0;
10936
Willy Tarreaue333ec92012-04-16 16:26:40 +020010937 CHECK_HTTP_MESSAGE_FIRST();
10938
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010939 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010940 msg = &txn->req;
10941 hdr_name = "Cookie";
10942 hdr_name_len = 6;
10943 } else {
10944 msg = &txn->rsp;
10945 hdr_name = "Set-Cookie";
10946 hdr_name_len = 10;
10947 }
10948
Willy Tarreau9b28e032012-10-12 23:49:43 +020010949 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010950 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010951 ctx.idx = 0;
10952 cnt = 0;
10953
10954 while (1) {
10955 /* Note: val_beg == NULL every time we need to fetch a new header */
10956 if (!val_beg) {
10957 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10958 break;
10959
Willy Tarreau24e32d82012-04-23 23:55:44 +020010960 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010961 continue;
10962
10963 val_beg = ctx.line + ctx.val;
10964 val_end = val_beg + ctx.vlen;
10965 }
10966
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010967 smp->type = SMP_T_STR;
10968 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010969 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010970 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010971 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010972 &smp->data.str.str,
10973 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010974 cnt++;
10975 }
10976 }
10977
Willy Tarreaub169eba2013-12-16 15:14:43 +010010978 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010979 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010980 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010981 return 1;
10982}
10983
Willy Tarreau51539362012-05-08 12:46:28 +020010984/* Fetch an cookie's integer value. The integer value is returned. It
10985 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10986 */
10987static int
10988smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010989 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010990{
Willy Tarreauef38c392013-07-22 16:29:32 +020010991 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010992
10993 if (ret > 0) {
10994 smp->type = SMP_T_UINT;
10995 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10996 }
10997
10998 return ret;
10999}
11000
Willy Tarreau8797c062007-05-07 00:55:35 +020011001/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011002/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011003/************************************************************************/
11004
David Cournapeau16023ee2010-12-23 20:55:41 +090011005/*
11006 * Given a path string and its length, find the position of beginning of the
11007 * query string. Returns NULL if no query string is found in the path.
11008 *
11009 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11010 *
11011 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11012 */
bedis4c75cca2012-10-05 08:38:24 +020011013static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011014{
11015 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011016
bedis4c75cca2012-10-05 08:38:24 +020011017 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011018 return p ? p + 1 : NULL;
11019}
11020
bedis4c75cca2012-10-05 08:38:24 +020011021static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011022{
bedis4c75cca2012-10-05 08:38:24 +020011023 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011024}
11025
11026/*
11027 * Given a url parameter, find the starting position of the first occurence,
11028 * or NULL if the parameter is not found.
11029 *
11030 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11031 * the function will return query_string+8.
11032 */
11033static char*
11034find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011035 char* url_param_name, size_t url_param_name_l,
11036 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011037{
11038 char *pos, *last;
11039
11040 pos = query_string;
11041 last = query_string + query_string_l - url_param_name_l - 1;
11042
11043 while (pos <= last) {
11044 if (pos[url_param_name_l] == '=') {
11045 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11046 return pos;
11047 pos += url_param_name_l + 1;
11048 }
bedis4c75cca2012-10-05 08:38:24 +020011049 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011050 pos++;
11051 pos++;
11052 }
11053 return NULL;
11054}
11055
11056/*
Cyril Bonté0f836e12015-11-26 21:39:56 +010011057 * Given a url parameter name and a query string, find the next value.
11058 * An empty url_param_name matches the first available parameter.
11059 * If the parameter is found, 1 is returned and *value / *value_l are updated
11060 * to respectively provide a pointer to the value and its length.
11061 * Otherwise, 0 is returned and value/value_l are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011062 */
11063static int
11064find_url_param_value(char* path, size_t path_l,
11065 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011066 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011067{
11068 char *query_string, *qs_end;
11069 char *arg_start;
11070 char *value_start, *value_end;
11071
bedis4c75cca2012-10-05 08:38:24 +020011072 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011073 if (!query_string)
11074 return 0;
11075
11076 qs_end = path + path_l;
11077 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011078 url_param_name, url_param_name_l,
11079 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011080 if (!arg_start)
11081 return 0;
11082
11083 value_start = arg_start + url_param_name_l + 1;
11084 value_end = value_start;
11085
bedis4c75cca2012-10-05 08:38:24 +020011086 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011087 value_end++;
11088
11089 *value = value_start;
11090 *value_l = value_end - value_start;
Cyril Bonté0f836e12015-11-26 21:39:56 +010011091 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011092}
11093
11094static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011095smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011096 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090011097{
bedis4c75cca2012-10-05 08:38:24 +020011098 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090011099 struct http_txn *txn = l7;
11100 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011101
bedis4c75cca2012-10-05 08:38:24 +020011102 if (!args || args[0].type != ARGT_STR ||
11103 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011104 return 0;
11105
11106 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011107
bedis4c75cca2012-10-05 08:38:24 +020011108 if (args[1].type)
11109 delim = *args[1].data.str.str;
11110
Willy Tarreau9b28e032012-10-12 23:49:43 +020011111 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011112 args->data.str.str, args->data.str.len,
11113 &smp->data.str.str, &smp->data.str.len,
11114 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011115 return 0;
11116
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011117 smp->type = SMP_T_STR;
11118 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011119 return 1;
11120}
11121
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011122/* Return the signed integer value for the specified url parameter (see url_param
11123 * above).
11124 */
11125static int
11126smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011127 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011128{
Willy Tarreauef38c392013-07-22 16:29:32 +020011129 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011130
11131 if (ret > 0) {
11132 smp->type = SMP_T_UINT;
11133 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11134 }
11135
11136 return ret;
11137}
11138
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011139/* This produces a 32-bit hash of the concatenation of the first occurrence of
11140 * the Host header followed by the path component if it begins with a slash ('/').
11141 * This means that '*' will not be added, resulting in exactly the first Host
11142 * entry. If no Host header is found, then the path is used. The resulting value
11143 * is hashed using the url hash followed by a full avalanche hash and provides a
11144 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11145 * high-traffic sites without having to store whole paths.
11146 * this differs from the base32 functions in that it includes the url parameters
11147 * as well as the path
11148 */
11149static int
11150smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011151 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011152{
11153 struct http_txn *txn = l7;
11154 struct hdr_ctx ctx;
11155 unsigned int hash = 0;
11156 char *ptr, *beg, *end;
11157 int len;
11158
11159 CHECK_HTTP_MESSAGE_FIRST();
11160
11161 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011162 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011163 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11164 ptr = ctx.line + ctx.val;
11165 len = ctx.vlen;
11166 while (len--)
11167 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11168 }
11169
11170 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011171 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 +000011172 beg = http_get_path(txn);
11173 if (!beg)
11174 beg = end;
11175
11176 for (ptr = beg; ptr < end ; ptr++);
11177
11178 if (beg < ptr && *beg == '/') {
11179 while (beg < ptr)
11180 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11181 }
11182 hash = full_hash(hash);
11183
11184 smp->type = SMP_T_UINT;
11185 smp->data.uint = hash;
11186 smp->flags = SMP_F_VOL_1ST;
11187 return 1;
11188}
11189
11190/* This concatenates the source address with the 32-bit hash of the Host and
11191 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11192 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11193 * on the source address length. The URL hash is stored before the address so
11194 * that in environments where IPv6 is insignificant, truncating the output to
11195 * 8 bytes would still work.
11196 */
11197static int
11198smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011199 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011200{
11201 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011202 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011203
Willy Tarreaue155ec22013-11-18 18:33:22 +010011204 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011205 return 0;
11206
11207 temp = get_trash_chunk();
11208 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11209 temp->len += sizeof(smp->data.uint);
11210
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011211 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011212 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011213 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011214 temp->len += 4;
11215 break;
11216 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011217 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011218 temp->len += 16;
11219 break;
11220 default:
11221 return 0;
11222 }
11223
11224 smp->data.str = *temp;
11225 smp->type = SMP_T_BIN;
11226 return 1;
11227}
11228
Willy Tarreau185b5c42012-04-26 15:11:51 +020011229/* This function is used to validate the arguments passed to any "hdr" fetch
11230 * keyword. These keywords support an optional positive or negative occurrence
11231 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11232 * is assumed that the types are already the correct ones. Returns 0 on error,
11233 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11234 * error message in case of error, that the caller is responsible for freeing.
11235 * The initial location must either be freeable or NULL.
11236 */
11237static int val_hdr(struct arg *arg, char **err_msg)
11238{
11239 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011240 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011241 return 0;
11242 }
11243 return 1;
11244}
11245
Willy Tarreau276fae92013-07-25 14:36:01 +020011246/* takes an UINT value on input supposed to represent the time since EPOCH,
11247 * adds an optional offset found in args[0] and emits a string representing
11248 * the date in RFC-1123/5322 format.
11249 */
11250static int sample_conv_http_date(const struct arg *args, struct sample *smp)
11251{
Cyril Bontéf980b362016-01-22 19:40:28 +010011252 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011253 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11254 struct chunk *temp;
11255 struct tm *tm;
11256 time_t curr_date = smp->data.uint;
11257
11258 /* add offset */
11259 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11260 curr_date += args[0].data.sint;
11261
11262 tm = gmtime(&curr_date);
Thierry FOURNIER95558722015-07-08 00:15:20 +020011263 if (!tm)
11264 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011265
11266 temp = get_trash_chunk();
11267 temp->len = snprintf(temp->str, temp->size - temp->len,
11268 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11269 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11270 tm->tm_hour, tm->tm_min, tm->tm_sec);
11271
11272 smp->data.str = *temp;
11273 smp->type = SMP_T_STR;
11274 return 1;
11275}
11276
Thierry FOURNIERad903512014-04-11 17:51:01 +020011277/* Match language range with language tag. RFC2616 14.4:
11278 *
11279 * A language-range matches a language-tag if it exactly equals
11280 * the tag, or if it exactly equals a prefix of the tag such
11281 * that the first tag character following the prefix is "-".
11282 *
11283 * Return 1 if the strings match, else return 0.
11284 */
11285static inline int language_range_match(const char *range, int range_len,
11286 const char *tag, int tag_len)
11287{
11288 const char *end = range + range_len;
11289 const char *tend = tag + tag_len;
11290 while (range < end) {
11291 if (*range == '-' && tag == tend)
11292 return 1;
11293 if (*range != *tag || tag == tend)
11294 return 0;
11295 range++;
11296 tag++;
11297 }
11298 /* Return true only if the last char of the tag is matched. */
11299 return tag == tend;
11300}
11301
11302/* Arguments: The list of expected value, the number of parts returned and the separator */
11303static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
11304{
11305 const char *al = smp->data.str.str;
11306 const char *end = al + smp->data.str.len;
11307 const char *token;
11308 int toklen;
11309 int qvalue;
11310 const char *str;
11311 const char *w;
11312 int best_q = 0;
11313
11314 /* Set the constant to the sample, because the output of the
11315 * function will be peek in the constant configuration string.
11316 */
11317 smp->flags |= SMP_F_CONST;
11318 smp->data.str.size = 0;
11319 smp->data.str.str = "";
11320 smp->data.str.len = 0;
11321
11322 /* Parse the accept language */
11323 while (1) {
11324
11325 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011326 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011327 al++;
11328 if (al >= end)
11329 break;
11330
11331 /* Start of the fisrt word. */
11332 token = al;
11333
11334 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011335 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011336 al++;
11337 if (al == token)
11338 goto expect_comma;
11339
11340 /* Length of the token. */
11341 toklen = al - token;
11342 qvalue = 1000;
11343
11344 /* Check if the token exists in the list. If the token not exists,
11345 * jump to the next token.
11346 */
11347 str = args[0].data.str.str;
11348 w = str;
11349 while (1) {
11350 if (*str == ';' || *str == '\0') {
11351 if (language_range_match(token, toklen, w, str-w))
11352 goto look_for_q;
11353 if (*str == '\0')
11354 goto expect_comma;
11355 w = str + 1;
11356 }
11357 str++;
11358 }
11359 goto expect_comma;
11360
11361look_for_q:
11362
11363 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011364 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011365 al++;
11366 if (al >= end)
11367 goto process_value;
11368
11369 /* If ',' is found, process the result */
11370 if (*al == ',')
11371 goto process_value;
11372
11373 /* If the character is different from ';', look
11374 * for the end of the header part in best effort.
11375 */
11376 if (*al != ';')
11377 goto expect_comma;
11378
11379 /* Assumes that the char is ';', now expect "q=". */
11380 al++;
11381
11382 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011383 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011384 al++;
11385 if (al >= end)
11386 goto process_value;
11387
11388 /* Expect 'q'. If no 'q', continue in best effort */
11389 if (*al != 'q')
11390 goto process_value;
11391 al++;
11392
11393 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011394 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011395 al++;
11396 if (al >= end)
11397 goto process_value;
11398
11399 /* Expect '='. If no '=', continue in best effort */
11400 if (*al != '=')
11401 goto process_value;
11402 al++;
11403
11404 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011405 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011406 al++;
11407 if (al >= end)
11408 goto process_value;
11409
11410 /* Parse the q value. */
11411 qvalue = parse_qvalue(al, &al);
11412
11413process_value:
11414
11415 /* If the new q value is the best q value, then store the associated
11416 * language in the response. If qvalue is the biggest value (1000),
11417 * break the process.
11418 */
11419 if (qvalue > best_q) {
11420 smp->data.str.str = (char *)w;
11421 smp->data.str.len = str - w;
11422 if (qvalue >= 1000)
11423 break;
11424 best_q = qvalue;
11425 }
11426
11427expect_comma:
11428
11429 /* Expect comma or end. If the end is detected, quit the loop. */
11430 while (al < end && *al != ',')
11431 al++;
11432 if (al >= end)
11433 break;
11434
11435 /* Comma is found, jump it and restart the analyzer. */
11436 al++;
11437 }
11438
11439 /* Set default value if required. */
11440 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11441 smp->data.str.str = args[1].data.str.str;
11442 smp->data.str.len = args[1].data.str.len;
11443 }
11444
11445 /* Return true only if a matching language was found. */
11446 return smp->data.str.len != 0;
11447}
11448
William Lallemand73025dd2014-04-24 14:38:37 +020011449/*
11450 * Return the struct http_req_action_kw associated to a keyword.
11451 */
11452struct http_req_action_kw *action_http_req_custom(const char *kw)
11453{
11454 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11455 struct http_req_action_kw_list *kw_list;
11456 int i;
11457
11458 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11459 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11460 if (!strcmp(kw, kw_list->kw[i].kw))
11461 return &kw_list->kw[i];
11462 }
11463 }
11464 }
11465 return NULL;
11466}
11467
11468/*
11469 * Return the struct http_res_action_kw associated to a keyword.
11470 */
11471struct http_res_action_kw *action_http_res_custom(const char *kw)
11472{
11473 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11474 struct http_res_action_kw_list *kw_list;
11475 int i;
11476
11477 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11478 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11479 if (!strcmp(kw, kw_list->kw[i].kw))
11480 return &kw_list->kw[i];
11481 }
11482 }
11483 }
11484 return NULL;
11485}
11486
Willy Tarreau4a568972010-05-12 08:08:50 +020011487/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011488/* All supported ACL keywords must be declared here. */
11489/************************************************************************/
11490
11491/* Note: must not be declared <const> as its list will be overwritten.
11492 * Please take care of keeping this list alphabetically sorted.
11493 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011494static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011495 { "base", "base", PAT_MATCH_STR },
11496 { "base_beg", "base", PAT_MATCH_BEG },
11497 { "base_dir", "base", PAT_MATCH_DIR },
11498 { "base_dom", "base", PAT_MATCH_DOM },
11499 { "base_end", "base", PAT_MATCH_END },
11500 { "base_len", "base", PAT_MATCH_LEN },
11501 { "base_reg", "base", PAT_MATCH_REG },
11502 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011503
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011504 { "cook", "req.cook", PAT_MATCH_STR },
11505 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11506 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11507 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11508 { "cook_end", "req.cook", PAT_MATCH_END },
11509 { "cook_len", "req.cook", PAT_MATCH_LEN },
11510 { "cook_reg", "req.cook", PAT_MATCH_REG },
11511 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011512
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011513 { "hdr", "req.hdr", PAT_MATCH_STR },
11514 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11515 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11516 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11517 { "hdr_end", "req.hdr", PAT_MATCH_END },
11518 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11519 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11520 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011521
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011522 /* these two declarations uses strings with list storage (in place
11523 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11524 * and delete functions are relative to the list management. The parse
11525 * and match method are related to the corresponding fetch methods. This
11526 * is very particular ACL declaration mode.
11527 */
11528 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11529 { "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 +020011530
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011531 { "path", "path", PAT_MATCH_STR },
11532 { "path_beg", "path", PAT_MATCH_BEG },
11533 { "path_dir", "path", PAT_MATCH_DIR },
11534 { "path_dom", "path", PAT_MATCH_DOM },
11535 { "path_end", "path", PAT_MATCH_END },
11536 { "path_len", "path", PAT_MATCH_LEN },
11537 { "path_reg", "path", PAT_MATCH_REG },
11538 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011539
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011540 { "req_ver", "req.ver", PAT_MATCH_STR },
11541 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011542
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011543 { "scook", "res.cook", PAT_MATCH_STR },
11544 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11545 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11546 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11547 { "scook_end", "res.cook", PAT_MATCH_END },
11548 { "scook_len", "res.cook", PAT_MATCH_LEN },
11549 { "scook_reg", "res.cook", PAT_MATCH_REG },
11550 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011551
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011552 { "shdr", "res.hdr", PAT_MATCH_STR },
11553 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11554 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11555 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11556 { "shdr_end", "res.hdr", PAT_MATCH_END },
11557 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11558 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11559 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011560
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011561 { "url", "url", PAT_MATCH_STR },
11562 { "url_beg", "url", PAT_MATCH_BEG },
11563 { "url_dir", "url", PAT_MATCH_DIR },
11564 { "url_dom", "url", PAT_MATCH_DOM },
11565 { "url_end", "url", PAT_MATCH_END },
11566 { "url_len", "url", PAT_MATCH_LEN },
11567 { "url_reg", "url", PAT_MATCH_REG },
11568 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011569
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011570 { "urlp", "urlp", PAT_MATCH_STR },
11571 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11572 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11573 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11574 { "urlp_end", "urlp", PAT_MATCH_END },
11575 { "urlp_len", "urlp", PAT_MATCH_LEN },
11576 { "urlp_reg", "urlp", PAT_MATCH_REG },
11577 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011578
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011579 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011580}};
11581
11582/************************************************************************/
11583/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011584/************************************************************************/
11585/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011586static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011587 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011588 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11589 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11590
William Lallemanda43ba4e2014-01-28 18:14:25 +010011591 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011592 { "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 +020011593
11594 /* retrieve these captures from the HTTP logs */
11595 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11596 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11597 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11598
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011599 { "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 +020011600 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011601
Willy Tarreau409bcde2013-01-08 00:31:00 +010011602 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11603 * are only here to match the ACL's name, are request-only and are used
11604 * for ACL compatibility only.
11605 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011606 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11607 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011608 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11609 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11610
11611 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11612 * only here to match the ACL's name, are request-only and are used for
11613 * ACL compatibility only.
11614 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011615 { "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 +010011616 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11617 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11618 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11619
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011620 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011621 { "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 +010011622 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011623 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011624 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011625
11626 /* HTTP protocol on the request path */
11627 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011628 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011629
11630 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011631 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11632 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011633
11634 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011635 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11636 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011637
Willy Tarreau18ed2562013-01-14 15:56:36 +010011638 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011639 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011640 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11641 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11642
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011643 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011644 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011645 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011646 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11647 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11648 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11649
11650 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011651 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011652 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11653 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11654
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011655 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011656 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011657 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011658 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11659 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11660 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11661
Willy Tarreau409bcde2013-01-08 00:31:00 +010011662 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011663 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011664 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11665 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011666 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011667
11668 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011669 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011670 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11671 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11672 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11673
11674 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011675 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011676 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11677 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011678 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11679 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011680 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11681 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011682 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11683 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011684}};
11685
Willy Tarreau8797c062007-05-07 00:55:35 +020011686
Willy Tarreau276fae92013-07-25 14:36:01 +020011687/* Note: must not be declared <const> as its list will be overwritten */
11688static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011689 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11690 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011691 { NULL, NULL, 0, 0, 0 },
11692}};
11693
Willy Tarreau8797c062007-05-07 00:55:35 +020011694__attribute__((constructor))
11695static void __http_protocol_init(void)
11696{
11697 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011698 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011699 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020011700}
11701
11702
Willy Tarreau58f10d72006-12-04 02:26:12 +010011703/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011704 * Local variables:
11705 * c-indent-level: 8
11706 * c-basic-offset: 8
11707 * End:
11708 */