blob: 2622e2bfd68e9fa5871748b2f514a0b1f203c03e [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;
Nenad Merdanovicbb656432016-03-29 13:14:30 +02008685
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008686 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovicbb656432016-03-29 13:14:30 +02008687 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8688 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8689 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008690 */
Nenad Merdanovicbb656432016-03-29 13:14:30 +02008691 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008692 if (hist_ptr >= MAX_HDR_HISTORY)
8693 hist_ptr -= MAX_HDR_HISTORY;
8694 *vptr = ptr_hist[hist_ptr];
8695 *vlen = len_hist[hist_ptr];
8696 return 1;
8697}
8698
Willy Tarreaubaaee002006-06-26 02:48:02 +02008699/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008700 * Print a debug line with a header. Always stop at the first CR or LF char,
8701 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8702 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008703 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008704void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008705{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008706 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008707 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008708 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008709 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8710 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 +02008711
8712 for (max = 0; start + max < end; max++)
8713 if (start[max] == '\r' || start[max] == '\n')
8714 break;
8715
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008716 UBOUND(max, trash.size - trash.len - 3);
8717 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8718 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008719 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008720}
8721
Willy Tarreau0937bc42009-12-22 15:03:09 +01008722/*
8723 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8724 * the required fields are properly allocated and that we only need to (re)init
8725 * them. This should be used before processing any new request.
8726 */
8727void http_init_txn(struct session *s)
8728{
8729 struct http_txn *txn = &s->txn;
8730 struct proxy *fe = s->fe;
8731
8732 txn->flags = 0;
8733 txn->status = -1;
8734
Willy Tarreauf64d1412010-10-07 20:06:11 +02008735 txn->cookie_first_date = 0;
8736 txn->cookie_last_date = 0;
8737
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008738 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008739 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008740 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008741 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008742 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008743 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008744 txn->req.chunk_len = 0LL;
8745 txn->req.body_len = 0LL;
8746 txn->rsp.chunk_len = 0LL;
8747 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008748 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8749 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008750 txn->req.chn = s->req;
8751 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008752
8753 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008754
8755 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8756 if (fe->options2 & PR_O2_REQBUG_OK)
8757 txn->req.err_pos = -1; /* let buggy requests pass */
8758
Willy Tarreau46023632010-01-07 22:51:47 +01008759 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008760 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8761
Willy Tarreau46023632010-01-07 22:51:47 +01008762 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008763 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8764
8765 if (txn->hdr_idx.v)
8766 hdr_idx_init(&txn->hdr_idx);
8767}
8768
8769/* to be used at the end of a transaction */
8770void http_end_txn(struct session *s)
8771{
8772 struct http_txn *txn = &s->txn;
8773
Willy Tarreau75195602014-03-11 15:48:55 +01008774 /* release any possible compression context */
8775 if (s->flags & SN_COMP_READY)
8776 s->comp_algo->end(&s->comp_ctx);
8777 s->comp_algo = NULL;
8778 s->flags &= ~SN_COMP_READY;
8779
Willy Tarreau0937bc42009-12-22 15:03:09 +01008780 /* these ones will have been dynamically allocated */
8781 pool_free2(pool2_requri, txn->uri);
8782 pool_free2(pool2_capture, txn->cli_cookie);
8783 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008784 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008785 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008786
William Lallemanda73203e2012-03-12 12:48:57 +01008787 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008788 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008789 txn->uri = NULL;
8790 txn->srv_cookie = NULL;
8791 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008792
8793 if (txn->req.cap) {
8794 struct cap_hdr *h;
8795 for (h = s->fe->req_cap; h; h = h->next)
8796 pool_free2(h->pool, txn->req.cap[h->index]);
8797 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8798 }
8799
8800 if (txn->rsp.cap) {
8801 struct cap_hdr *h;
8802 for (h = s->fe->rsp_cap; h; h = h->next)
8803 pool_free2(h->pool, txn->rsp.cap[h->index]);
8804 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8805 }
8806
Willy Tarreau0937bc42009-12-22 15:03:09 +01008807}
8808
8809/* to be used at the end of a transaction to prepare a new one */
8810void http_reset_txn(struct session *s)
8811{
8812 http_end_txn(s);
8813 http_init_txn(s);
8814
8815 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008816 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008817 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008818 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008819 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008820 /* re-init store persistence */
8821 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008822 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008823
Willy Tarreau0937bc42009-12-22 15:03:09 +01008824 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008825
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008826 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008827
Willy Tarreau739cfba2010-01-25 23:11:14 +01008828 /* We must trim any excess data from the response buffer, because we
8829 * may have blocked an invalid response from a server that we don't
8830 * want to accidentely forward once we disable the analysers, nor do
8831 * we want those data to come along with next response. A typical
8832 * example of such data would be from a buggy server responding to
8833 * a HEAD with some data, or sending more than the advertised
8834 * content-length.
8835 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008836 if (unlikely(s->rep->buf->i))
8837 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008838
Willy Tarreau0937bc42009-12-22 15:03:09 +01008839 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008840 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008841
Willy Tarreaud04e8582010-05-31 12:31:35 +02008842 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008843 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008844
8845 s->req->rex = TICK_ETERNITY;
8846 s->req->wex = TICK_ETERNITY;
8847 s->req->analyse_exp = TICK_ETERNITY;
8848 s->rep->rex = TICK_ETERNITY;
8849 s->rep->wex = TICK_ETERNITY;
8850 s->rep->analyse_exp = TICK_ETERNITY;
8851}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008852
Sasha Pachev218f0642014-06-16 12:05:59 -06008853void free_http_res_rules(struct list *r)
8854{
8855 struct http_res_rule *tr, *pr;
8856
8857 list_for_each_entry_safe(pr, tr, r, list) {
8858 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008859 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008860 free(pr);
8861 }
8862}
8863
8864void free_http_req_rules(struct list *r)
8865{
Willy Tarreauff011f22011-01-06 17:51:27 +01008866 struct http_req_rule *tr, *pr;
8867
8868 list_for_each_entry_safe(pr, tr, r, list) {
8869 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008870 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008871 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008872
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008873 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008874 free(pr);
8875 }
8876}
8877
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008878/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008879struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8880{
8881 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008882 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008883 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008884 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008885
8886 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8887 if (!rule) {
8888 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008889 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008890 }
8891
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008892 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008893 rule->action = HTTP_REQ_ACT_ALLOW;
8894 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008895 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008896 rule->action = HTTP_REQ_ACT_DENY;
8897 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008898 } else if (!strcmp(args[0], "tarpit")) {
8899 rule->action = HTTP_REQ_ACT_TARPIT;
8900 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008901 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008902 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008903 cur_arg = 1;
8904
8905 while(*args[cur_arg]) {
8906 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008907 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008908 cur_arg+=2;
8909 continue;
8910 } else
8911 break;
8912 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008913 } else if (!strcmp(args[0], "set-nice")) {
8914 rule->action = HTTP_REQ_ACT_SET_NICE;
8915 cur_arg = 1;
8916
8917 if (!*args[cur_arg] ||
8918 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8919 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8920 file, linenum, args[0]);
8921 goto out_err;
8922 }
8923 rule->arg.nice = atoi(args[cur_arg]);
8924 if (rule->arg.nice < -1024)
8925 rule->arg.nice = -1024;
8926 else if (rule->arg.nice > 1024)
8927 rule->arg.nice = 1024;
8928 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008929 } else if (!strcmp(args[0], "set-tos")) {
8930#ifdef IP_TOS
8931 char *err;
8932 rule->action = HTTP_REQ_ACT_SET_TOS;
8933 cur_arg = 1;
8934
8935 if (!*args[cur_arg] ||
8936 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8937 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8938 file, linenum, args[0]);
8939 goto out_err;
8940 }
8941
8942 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8943 if (err && *err != '\0') {
8944 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8945 file, linenum, err, args[0]);
8946 goto out_err;
8947 }
8948 cur_arg++;
8949#else
8950 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8951 goto out_err;
8952#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008953 } else if (!strcmp(args[0], "set-mark")) {
8954#ifdef SO_MARK
8955 char *err;
8956 rule->action = HTTP_REQ_ACT_SET_MARK;
8957 cur_arg = 1;
8958
8959 if (!*args[cur_arg] ||
8960 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8961 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8962 file, linenum, args[0]);
8963 goto out_err;
8964 }
8965
8966 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8967 if (err && *err != '\0') {
8968 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8969 file, linenum, err, args[0]);
8970 goto out_err;
8971 }
8972 cur_arg++;
8973 global.last_checks |= LSTCHK_NETADM;
8974#else
8975 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8976 goto out_err;
8977#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008978 } else if (!strcmp(args[0], "set-log-level")) {
8979 rule->action = HTTP_REQ_ACT_SET_LOGL;
8980 cur_arg = 1;
8981
8982 if (!*args[cur_arg] ||
8983 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8984 bad_log_level:
8985 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8986 file, linenum, args[0]);
8987 goto out_err;
8988 }
8989 if (strcmp(args[cur_arg], "silent") == 0)
8990 rule->arg.loglevel = -1;
8991 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8992 goto bad_log_level;
8993 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008994 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8995 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8996 cur_arg = 1;
8997
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008998 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8999 (*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 +01009000 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9001 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009002 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009003 }
9004
9005 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9006 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9007 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009008
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009009 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009010 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009011 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9012 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009013 free(proxy->conf.lfs_file);
9014 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9015 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009016 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009017 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9018 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009019 cur_arg = 1;
9020
9021 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann8f249f42014-06-24 11:10:00 +02009022 (*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 -06009023 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9024 file, linenum, args[0]);
9025 goto out_err;
9026 }
9027
9028 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9029 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9030 LIST_INIT(&rule->arg.hdr_add.fmt);
9031
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009032 error = NULL;
9033 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9034 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9035 args[cur_arg + 1], error);
9036 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009037 goto out_err;
9038 }
9039
9040 proxy->conf.args.ctx = ARGC_HRQ;
9041 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9042 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9043 file, linenum);
9044
9045 free(proxy->conf.lfs_file);
9046 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9047 proxy->conf.lfs_line = proxy->conf.args.line;
9048 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009049 } else if (strcmp(args[0], "del-header") == 0) {
9050 rule->action = HTTP_REQ_ACT_DEL_HDR;
9051 cur_arg = 1;
9052
9053 if (!*args[cur_arg] ||
9054 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9055 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9056 file, linenum, args[0]);
9057 goto out_err;
9058 }
9059
9060 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9061 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9062
9063 proxy->conf.args.ctx = ARGC_HRQ;
9064 free(proxy->conf.lfs_file);
9065 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9066 proxy->conf.lfs_line = proxy->conf.args.line;
9067 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009068 } else if (strcmp(args[0], "redirect") == 0) {
9069 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009070 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009071
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009072 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009073 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9074 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9075 goto out_err;
9076 }
9077
9078 /* this redirect rule might already contain a parsed condition which
9079 * we'll pass to the http-request rule.
9080 */
9081 rule->action = HTTP_REQ_ACT_REDIR;
9082 rule->arg.redir = redir;
9083 rule->cond = redir->cond;
9084 redir->cond = NULL;
9085 cur_arg = 2;
9086 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009087 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9088 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9089 rule->action = HTTP_REQ_ACT_ADD_ACL;
9090 /*
9091 * '+ 8' for 'add-acl('
9092 * '- 9' for 'add-acl(' + trailing ')'
9093 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009094 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009095
9096 cur_arg = 1;
9097
9098 if (!*args[cur_arg] ||
9099 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9100 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9101 file, linenum, args[0]);
9102 goto out_err;
9103 }
9104
9105 LIST_INIT(&rule->arg.map.key);
9106 proxy->conf.args.ctx = ARGC_HRQ;
9107 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9108 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9109 file, linenum);
9110 free(proxy->conf.lfs_file);
9111 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9112 proxy->conf.lfs_line = proxy->conf.args.line;
9113 cur_arg += 1;
9114 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9115 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9116 rule->action = HTTP_REQ_ACT_DEL_ACL;
9117 /*
9118 * '+ 8' for 'del-acl('
9119 * '- 9' for 'del-acl(' + trailing ')'
9120 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009121 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009122
9123 cur_arg = 1;
9124
9125 if (!*args[cur_arg] ||
9126 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9127 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9128 file, linenum, args[0]);
9129 goto out_err;
9130 }
9131
9132 LIST_INIT(&rule->arg.map.key);
9133 proxy->conf.args.ctx = ARGC_HRQ;
9134 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9135 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9136 file, linenum);
9137 free(proxy->conf.lfs_file);
9138 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9139 proxy->conf.lfs_line = proxy->conf.args.line;
9140 cur_arg += 1;
9141 } else if (strncmp(args[0], "del-map", 7) == 0) {
9142 /* http-request del-map(<reference (map name)>) <key pattern> */
9143 rule->action = HTTP_REQ_ACT_DEL_MAP;
9144 /*
9145 * '+ 8' for 'del-map('
9146 * '- 9' for 'del-map(' + trailing ')'
9147 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009148 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009149
9150 cur_arg = 1;
9151
9152 if (!*args[cur_arg] ||
9153 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9154 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9155 file, linenum, args[0]);
9156 goto out_err;
9157 }
9158
9159 LIST_INIT(&rule->arg.map.key);
9160 proxy->conf.args.ctx = ARGC_HRQ;
9161 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9162 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9163 file, linenum);
9164 free(proxy->conf.lfs_file);
9165 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9166 proxy->conf.lfs_line = proxy->conf.args.line;
9167 cur_arg += 1;
9168 } else if (strncmp(args[0], "set-map", 7) == 0) {
9169 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9170 rule->action = HTTP_REQ_ACT_SET_MAP;
9171 /*
9172 * '+ 8' for 'set-map('
9173 * '- 9' for 'set-map(' + trailing ')'
9174 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009175 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009176
9177 cur_arg = 1;
9178
9179 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9180 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9181 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9182 file, linenum, args[0]);
9183 goto out_err;
9184 }
9185
9186 LIST_INIT(&rule->arg.map.key);
9187 LIST_INIT(&rule->arg.map.value);
9188 proxy->conf.args.ctx = ARGC_HRQ;
9189
9190 /* key pattern */
9191 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9192 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9193 file, linenum);
9194
9195 /* value pattern */
9196 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9197 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9198 file, linenum);
9199 free(proxy->conf.lfs_file);
9200 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9201 proxy->conf.lfs_line = proxy->conf.args.line;
9202
9203 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009204 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9205 char *errmsg = NULL;
9206 cur_arg = 1;
9207 /* try in the module list */
9208 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9209 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9210 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9211 free(errmsg);
9212 goto out_err;
9213 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009214 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009215 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 +01009216 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009217 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009218 }
9219
9220 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9221 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009222 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009223
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009224 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9225 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9226 file, linenum, args[0], errmsg);
9227 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009228 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009229 }
9230 rule->cond = cond;
9231 }
9232 else if (*args[cur_arg]) {
9233 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9234 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9235 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009236 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009237 }
9238
9239 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009240 out_err:
9241 free(rule);
9242 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009243}
9244
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009245/* parse an "http-respose" rule */
9246struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9247{
9248 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009249 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009250 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009251 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009252
9253 rule = calloc(1, sizeof(*rule));
9254 if (!rule) {
9255 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9256 goto out_err;
9257 }
9258
9259 if (!strcmp(args[0], "allow")) {
9260 rule->action = HTTP_RES_ACT_ALLOW;
9261 cur_arg = 1;
9262 } else if (!strcmp(args[0], "deny")) {
9263 rule->action = HTTP_RES_ACT_DENY;
9264 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009265 } else if (!strcmp(args[0], "set-nice")) {
9266 rule->action = HTTP_RES_ACT_SET_NICE;
9267 cur_arg = 1;
9268
9269 if (!*args[cur_arg] ||
9270 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9271 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9272 file, linenum, args[0]);
9273 goto out_err;
9274 }
9275 rule->arg.nice = atoi(args[cur_arg]);
9276 if (rule->arg.nice < -1024)
9277 rule->arg.nice = -1024;
9278 else if (rule->arg.nice > 1024)
9279 rule->arg.nice = 1024;
9280 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009281 } else if (!strcmp(args[0], "set-tos")) {
9282#ifdef IP_TOS
9283 char *err;
9284 rule->action = HTTP_RES_ACT_SET_TOS;
9285 cur_arg = 1;
9286
9287 if (!*args[cur_arg] ||
9288 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9289 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9290 file, linenum, args[0]);
9291 goto out_err;
9292 }
9293
9294 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9295 if (err && *err != '\0') {
9296 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9297 file, linenum, err, args[0]);
9298 goto out_err;
9299 }
9300 cur_arg++;
9301#else
9302 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9303 goto out_err;
9304#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009305 } else if (!strcmp(args[0], "set-mark")) {
9306#ifdef SO_MARK
9307 char *err;
9308 rule->action = HTTP_RES_ACT_SET_MARK;
9309 cur_arg = 1;
9310
9311 if (!*args[cur_arg] ||
9312 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9313 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9314 file, linenum, args[0]);
9315 goto out_err;
9316 }
9317
9318 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9319 if (err && *err != '\0') {
9320 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9321 file, linenum, err, args[0]);
9322 goto out_err;
9323 }
9324 cur_arg++;
9325 global.last_checks |= LSTCHK_NETADM;
9326#else
9327 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9328 goto out_err;
9329#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009330 } else if (!strcmp(args[0], "set-log-level")) {
9331 rule->action = HTTP_RES_ACT_SET_LOGL;
9332 cur_arg = 1;
9333
9334 if (!*args[cur_arg] ||
9335 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9336 bad_log_level:
9337 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9338 file, linenum, args[0]);
9339 goto out_err;
9340 }
9341 if (strcmp(args[cur_arg], "silent") == 0)
9342 rule->arg.loglevel = -1;
9343 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9344 goto bad_log_level;
9345 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009346 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9347 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9348 cur_arg = 1;
9349
9350 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9351 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9352 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9353 file, linenum, args[0]);
9354 goto out_err;
9355 }
9356
9357 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9358 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9359 LIST_INIT(&rule->arg.hdr_add.fmt);
9360
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009361 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009362 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009363 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9364 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009365 free(proxy->conf.lfs_file);
9366 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9367 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009368 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009369 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009370 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009371 cur_arg = 1;
9372
9373 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmanna772b942014-08-08 17:29:06 +02009374 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9375 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009376 file, linenum, args[0]);
9377 goto out_err;
9378 }
9379
9380 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9381 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9382 LIST_INIT(&rule->arg.hdr_add.fmt);
9383
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009384 error = NULL;
9385 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9386 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9387 args[cur_arg + 1], error);
9388 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009389 goto out_err;
9390 }
9391
9392 proxy->conf.args.ctx = ARGC_HRQ;
9393 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9394 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9395 file, linenum);
9396
9397 free(proxy->conf.lfs_file);
9398 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9399 proxy->conf.lfs_line = proxy->conf.args.line;
9400 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009401 } else if (strcmp(args[0], "del-header") == 0) {
9402 rule->action = HTTP_RES_ACT_DEL_HDR;
9403 cur_arg = 1;
9404
9405 if (!*args[cur_arg] ||
9406 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9407 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9408 file, linenum, args[0]);
9409 goto out_err;
9410 }
9411
9412 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9413 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9414
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009415 proxy->conf.args.ctx = ARGC_HRS;
9416 free(proxy->conf.lfs_file);
9417 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9418 proxy->conf.lfs_line = proxy->conf.args.line;
9419 cur_arg += 1;
9420 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9421 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9422 rule->action = HTTP_RES_ACT_ADD_ACL;
9423 /*
9424 * '+ 8' for 'add-acl('
9425 * '- 9' for 'add-acl(' + trailing ')'
9426 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009427 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009428
9429 cur_arg = 1;
9430
9431 if (!*args[cur_arg] ||
9432 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9433 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9434 file, linenum, args[0]);
9435 goto out_err;
9436 }
9437
9438 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009439 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009440 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9441 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9442 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009443 free(proxy->conf.lfs_file);
9444 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9445 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009446
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009447 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009448 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9449 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9450 rule->action = HTTP_RES_ACT_DEL_ACL;
9451 /*
9452 * '+ 8' for 'del-acl('
9453 * '- 9' for 'del-acl(' + trailing ')'
9454 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009455 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009456
9457 cur_arg = 1;
9458
9459 if (!*args[cur_arg] ||
9460 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9461 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9462 file, linenum, args[0]);
9463 goto out_err;
9464 }
9465
9466 LIST_INIT(&rule->arg.map.key);
9467 proxy->conf.args.ctx = ARGC_HRS;
9468 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9469 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9470 file, linenum);
9471 free(proxy->conf.lfs_file);
9472 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9473 proxy->conf.lfs_line = proxy->conf.args.line;
9474 cur_arg += 1;
9475 } else if (strncmp(args[0], "del-map", 7) == 0) {
9476 /* http-response del-map(<reference (map name)>) <key pattern> */
9477 rule->action = HTTP_RES_ACT_DEL_MAP;
9478 /*
9479 * '+ 8' for 'del-map('
9480 * '- 9' for 'del-map(' + trailing ')'
9481 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009482 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009483
9484 cur_arg = 1;
9485
9486 if (!*args[cur_arg] ||
9487 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9488 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9489 file, linenum, args[0]);
9490 goto out_err;
9491 }
9492
9493 LIST_INIT(&rule->arg.map.key);
9494 proxy->conf.args.ctx = ARGC_HRS;
9495 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9496 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9497 file, linenum);
9498 free(proxy->conf.lfs_file);
9499 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9500 proxy->conf.lfs_line = proxy->conf.args.line;
9501 cur_arg += 1;
9502 } else if (strncmp(args[0], "set-map", 7) == 0) {
9503 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9504 rule->action = HTTP_RES_ACT_SET_MAP;
9505 /*
9506 * '+ 8' for 'set-map('
9507 * '- 9' for 'set-map(' + trailing ')'
9508 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009509 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009510
9511 cur_arg = 1;
9512
9513 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9514 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9515 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9516 file, linenum, args[0]);
9517 goto out_err;
9518 }
9519
9520 LIST_INIT(&rule->arg.map.key);
9521 LIST_INIT(&rule->arg.map.value);
9522
9523 proxy->conf.args.ctx = ARGC_HRS;
9524
9525 /* key pattern */
9526 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9527 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9528 file, linenum);
9529
9530 /* value pattern */
9531 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9532 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9533 file, linenum);
9534
9535 free(proxy->conf.lfs_file);
9536 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9537 proxy->conf.lfs_line = proxy->conf.args.line;
9538
9539 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009540 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9541 char *errmsg = NULL;
9542 cur_arg = 1;
9543 /* try in the module list */
9544 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9545 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9546 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9547 free(errmsg);
9548 goto out_err;
9549 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009550 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009551 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 +02009552 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9553 goto out_err;
9554 }
9555
9556 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9557 struct acl_cond *cond;
9558 char *errmsg = NULL;
9559
9560 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9561 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9562 file, linenum, args[0], errmsg);
9563 free(errmsg);
9564 goto out_err;
9565 }
9566 rule->cond = cond;
9567 }
9568 else if (*args[cur_arg]) {
9569 Alert("parsing [%s:%d]: 'http-response %s' expects"
9570 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9571 file, linenum, args[0], args[cur_arg]);
9572 goto out_err;
9573 }
9574
9575 return rule;
9576 out_err:
9577 free(rule);
9578 return NULL;
9579}
9580
Willy Tarreau4baae242012-12-27 12:00:31 +01009581/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009582 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9583 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009584 */
9585struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009586 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009587{
9588 struct redirect_rule *rule;
9589 int cur_arg;
9590 int type = REDIRECT_TYPE_NONE;
9591 int code = 302;
9592 const char *destination = NULL;
9593 const char *cookie = NULL;
9594 int cookie_set = 0;
9595 unsigned int flags = REDIRECT_FLAG_NONE;
9596 struct acl_cond *cond = NULL;
9597
9598 cur_arg = 0;
9599 while (*(args[cur_arg])) {
9600 if (strcmp(args[cur_arg], "location") == 0) {
9601 if (!*args[cur_arg + 1])
9602 goto missing_arg;
9603
9604 type = REDIRECT_TYPE_LOCATION;
9605 cur_arg++;
9606 destination = args[cur_arg];
9607 }
9608 else if (strcmp(args[cur_arg], "prefix") == 0) {
9609 if (!*args[cur_arg + 1])
9610 goto missing_arg;
9611
9612 type = REDIRECT_TYPE_PREFIX;
9613 cur_arg++;
9614 destination = args[cur_arg];
9615 }
9616 else if (strcmp(args[cur_arg], "scheme") == 0) {
9617 if (!*args[cur_arg + 1])
9618 goto missing_arg;
9619
9620 type = REDIRECT_TYPE_SCHEME;
9621 cur_arg++;
9622 destination = args[cur_arg];
9623 }
9624 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9625 if (!*args[cur_arg + 1])
9626 goto missing_arg;
9627
9628 cur_arg++;
9629 cookie = args[cur_arg];
9630 cookie_set = 1;
9631 }
9632 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9633 if (!*args[cur_arg + 1])
9634 goto missing_arg;
9635
9636 cur_arg++;
9637 cookie = args[cur_arg];
9638 cookie_set = 0;
9639 }
9640 else if (strcmp(args[cur_arg], "code") == 0) {
9641 if (!*args[cur_arg + 1])
9642 goto missing_arg;
9643
9644 cur_arg++;
9645 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009646 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009647 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009648 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009649 args[cur_arg - 1], args[cur_arg]);
9650 return NULL;
9651 }
9652 }
9653 else if (!strcmp(args[cur_arg],"drop-query")) {
9654 flags |= REDIRECT_FLAG_DROP_QS;
9655 }
9656 else if (!strcmp(args[cur_arg],"append-slash")) {
9657 flags |= REDIRECT_FLAG_APPEND_SLASH;
9658 }
9659 else if (strcmp(args[cur_arg], "if") == 0 ||
9660 strcmp(args[cur_arg], "unless") == 0) {
9661 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9662 if (!cond) {
9663 memprintf(errmsg, "error in condition: %s", *errmsg);
9664 return NULL;
9665 }
9666 break;
9667 }
9668 else {
9669 memprintf(errmsg,
9670 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9671 args[cur_arg]);
9672 return NULL;
9673 }
9674 cur_arg++;
9675 }
9676
9677 if (type == REDIRECT_TYPE_NONE) {
9678 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9679 return NULL;
9680 }
9681
9682 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9683 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009684 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009685
9686 if (!use_fmt) {
9687 /* old-style static redirect rule */
9688 rule->rdr_str = strdup(destination);
9689 rule->rdr_len = strlen(destination);
9690 }
9691 else {
9692 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009693
9694 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9695 * if prefix == "/", we don't want to add anything, otherwise it
9696 * makes it hard for the user to configure a self-redirection.
9697 */
Godbach543b9782014-12-18 15:44:58 +08009698 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009699 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009700 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009701 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9702 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009703 free(curproxy->conf.lfs_file);
9704 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9705 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009706 }
9707 }
9708
Willy Tarreau4baae242012-12-27 12:00:31 +01009709 if (cookie) {
9710 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9711 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9712 */
9713 rule->cookie_len = strlen(cookie);
9714 if (cookie_set) {
9715 rule->cookie_str = malloc(rule->cookie_len + 10);
9716 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9717 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9718 rule->cookie_len += 9;
9719 } else {
9720 rule->cookie_str = malloc(rule->cookie_len + 21);
9721 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9722 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9723 rule->cookie_len += 20;
9724 }
9725 }
9726 rule->type = type;
9727 rule->code = code;
9728 rule->flags = flags;
9729 LIST_INIT(&rule->list);
9730 return rule;
9731
9732 missing_arg:
9733 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9734 return NULL;
9735}
9736
Willy Tarreau8797c062007-05-07 00:55:35 +02009737/************************************************************************/
9738/* The code below is dedicated to ACL parsing and matching */
9739/************************************************************************/
9740
9741
Willy Tarreau14174bc2012-04-16 14:34:04 +02009742/* This function ensures that the prerequisites for an L7 fetch are ready,
9743 * which means that a request or response is ready. If some data is missing,
9744 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009745 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9746 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009747 *
9748 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009749 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9750 * decide whether or not an HTTP message is present ;
9751 * 0 if the requested data cannot be fetched or if it is certain that
9752 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009753 * 1 if an HTTP message is ready
9754 */
9755static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009756smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009757 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009758{
9759 struct http_txn *txn = l7;
9760 struct http_msg *msg = &txn->req;
9761
9762 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9763 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9764 */
9765
9766 if (unlikely(!s || !txn))
9767 return 0;
9768
9769 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009770 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009771
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009772 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009773 if (unlikely(!s->req))
9774 return 0;
9775
Willy Tarreauaae75e32013-03-29 12:31:49 +01009776 /* If the buffer does not leave enough free space at the end,
9777 * we must first realign it.
9778 */
9779 if (s->req->buf->p > s->req->buf->data &&
9780 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9781 buffer_slow_realign(s->req->buf);
9782
Willy Tarreau14174bc2012-04-16 14:34:04 +02009783 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009784 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009785 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009786
9787 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009788 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009789 http_msg_analyzer(msg, &txn->hdr_idx);
9790
9791 /* Still no valid request ? */
9792 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009793 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009794 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009795 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009796 }
9797 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009798 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009799 return 0;
9800 }
9801
9802 /* OK we just got a valid HTTP request. We have some minor
9803 * preparation to perform so that further checks can rely
9804 * on HTTP tests.
9805 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009806
9807 /* If the request was parsed but was too large, we must absolutely
9808 * return an error so that it is not processed. At the moment this
9809 * cannot happen, but if the parsers are to change in the future,
9810 * we want this check to be maintained.
9811 */
9812 if (unlikely(s->req->buf->i + s->req->buf->p >
9813 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9814 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009815 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009816 return 1;
9817 }
9818
Willy Tarreau9b28e032012-10-12 23:49:43 +02009819 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009820 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9821 s->flags |= SN_REDIRECTABLE;
9822
Willy Tarreau506d0502013-07-06 13:29:24 +02009823 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9824 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009825 }
9826
Willy Tarreau506d0502013-07-06 13:29:24 +02009827 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009828 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009829 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009830
9831 /* otherwise everything's ready for the request */
9832 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009833 else {
9834 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009835 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9836 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009837 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009838 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009839 }
9840
9841 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009842 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009843 return 1;
9844}
Willy Tarreau8797c062007-05-07 00:55:35 +02009845
Willy Tarreaua4ba9db2014-06-25 16:56:41 +02009846/* Note: these functinos *do* modify the sample. Even in case of success, at
9847 * least the type and uint value are modified.
9848 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009849#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009850 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 +02009851
Willy Tarreau24e32d82012-04-23 23:55:44 +02009852#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009853 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 +02009854
Willy Tarreau8797c062007-05-07 00:55:35 +02009855
9856/* 1. Check on METHOD
9857 * We use the pre-parsed method if it is known, and store its number as an
9858 * integer. If it is unknown, we use the pointer and the length.
9859 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009860static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009861{
9862 int len, meth;
9863
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009864 len = strlen(text);
9865 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009866
9867 pattern->val.i = meth;
9868 if (meth == HTTP_METH_OTHER) {
Willy Tarreau71196f32014-08-29 15:15:50 +02009869 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009870 pattern->len = len;
9871 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009872 else {
9873 pattern->ptr.str = NULL;
9874 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009875 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009876 return 1;
9877}
9878
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009879/* This function fetches the method of current HTTP request and stores
9880 * it in the global pattern struct as a chunk. There are two possibilities :
9881 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9882 * in <len> and <ptr> is NULL ;
9883 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9884 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009885 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009886 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009887static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009888smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009889 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009890{
9891 int meth;
9892 struct http_txn *txn = l7;
9893
Willy Tarreau24e32d82012-04-23 23:55:44 +02009894 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009895
Willy Tarreau8797c062007-05-07 00:55:35 +02009896 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009897 smp->type = SMP_T_METH;
9898 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009899 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009900 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9901 /* ensure the indexes are not affected */
9902 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009903 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009904 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9905 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009906 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009907 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009908 return 1;
9909}
9910
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009911/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009912static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009913{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009914 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009915 struct pattern_list *lst;
9916 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009917
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009918 list_for_each_entry(lst, &expr->patterns, list) {
9919 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009920
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009921 /* well-known method */
9922 if (pattern->val.i != HTTP_METH_OTHER) {
9923 if (smp->data.meth.meth == pattern->val.i)
9924 return pattern;
9925 else
9926 continue;
9927 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009928
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009929 /* Other method, we must compare the strings */
9930 if (pattern->len != smp->data.meth.str.len)
9931 continue;
9932
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009933 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreauc62a0c62014-08-28 20:42:57 +02009934 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
9935 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009936 return pattern;
9937 }
9938 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009939}
9940
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009941static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009942smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009943 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009944{
9945 struct http_txn *txn = l7;
9946 char *ptr;
9947 int len;
9948
Willy Tarreauc0239e02012-04-16 14:42:55 +02009949 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009950
Willy Tarreau8797c062007-05-07 00:55:35 +02009951 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009952 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009953
9954 while ((len-- > 0) && (*ptr++ != '/'));
9955 if (len <= 0)
9956 return 0;
9957
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009958 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009959 smp->data.str.str = ptr;
9960 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009961
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009962 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009963 return 1;
9964}
9965
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009966static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009967smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009968 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009969{
9970 struct http_txn *txn = l7;
9971 char *ptr;
9972 int len;
9973
Willy Tarreauc0239e02012-04-16 14:42:55 +02009974 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009975
Willy Tarreauf26b2522012-12-14 08:33:14 +01009976 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9977 return 0;
9978
Willy Tarreau8797c062007-05-07 00:55:35 +02009979 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009980 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009981
9982 while ((len-- > 0) && (*ptr++ != '/'));
9983 if (len <= 0)
9984 return 0;
9985
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009986 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009987 smp->data.str.str = ptr;
9988 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009989
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009990 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009991 return 1;
9992}
9993
9994/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009995static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009996smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009997 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009998{
9999 struct http_txn *txn = l7;
10000 char *ptr;
10001 int len;
10002
Willy Tarreauc0239e02012-04-16 14:42:55 +020010003 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010004
Willy Tarreauf26b2522012-12-14 08:33:14 +010010005 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10006 return 0;
10007
Willy Tarreau8797c062007-05-07 00:55:35 +020010008 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010009 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010010
Willy Tarreauf853c462012-04-23 18:53:56 +020010011 smp->type = SMP_T_UINT;
10012 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010013 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010014 return 1;
10015}
10016
10017/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010018static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010019smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010020 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +020010021{
10022 struct http_txn *txn = l7;
10023
Willy Tarreauc0239e02012-04-16 14:42:55 +020010024 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010025
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010026 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010027 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010028 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010029 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010030 return 1;
10031}
10032
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010033static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010034smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010035 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010036{
10037 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010038 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010039
Willy Tarreauc0239e02012-04-16 14:42:55 +020010040 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010041
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010042 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 +020010043 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010044 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010045
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010046 smp->type = SMP_T_IPV4;
10047 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010048 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010049 return 1;
10050}
10051
10052static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010053smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010054 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010055{
10056 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010057 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010058
Willy Tarreauc0239e02012-04-16 14:42:55 +020010059 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010060
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010061 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 +020010062 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10063 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010064
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010065 smp->type = SMP_T_UINT;
10066 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010067 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010068 return 1;
10069}
10070
Willy Tarreau185b5c42012-04-26 15:11:51 +020010071/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10072 * Accepts an optional argument of type string containing the header field name,
10073 * and an optional argument of type signed or unsigned integer to request an
10074 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010075 * headers are considered from the first one. It does not stop on commas and
10076 * returns full lines instead (useful for User-Agent or Date for example).
10077 */
10078static int
10079smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010080 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010081{
10082 struct http_txn *txn = l7;
10083 struct hdr_idx *idx = &txn->hdr_idx;
10084 struct hdr_ctx *ctx = smp->ctx.a[0];
10085 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10086 int occ = 0;
10087 const char *name_str = NULL;
10088 int name_len = 0;
10089
10090 if (!ctx) {
10091 /* first call */
10092 ctx = &static_hdr_ctx;
10093 ctx->idx = 0;
10094 smp->ctx.a[0] = ctx;
10095 }
10096
10097 if (args) {
10098 if (args[0].type != ARGT_STR)
10099 return 0;
10100 name_str = args[0].data.str.str;
10101 name_len = args[0].data.str.len;
10102
10103 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10104 occ = args[1].data.uint;
10105 }
10106
10107 CHECK_HTTP_MESSAGE_FIRST();
10108
10109 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10110 /* search for header from the beginning */
10111 ctx->idx = 0;
10112
10113 if (!occ && !(opt & SMP_OPT_ITERATE))
10114 /* no explicit occurrence and single fetch => last header by default */
10115 occ = -1;
10116
10117 if (!occ)
10118 /* prepare to report multiple occurrences for ACL fetches */
10119 smp->flags |= SMP_F_NOT_LAST;
10120
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010121 smp->type = SMP_T_STR;
10122 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010123 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10124 return 1;
10125
10126 smp->flags &= ~SMP_F_NOT_LAST;
10127 return 0;
10128}
10129
10130/* 6. Check on HTTP header count. The number of occurrences is returned.
10131 * Accepts exactly 1 argument of type string. It does not stop on commas and
10132 * returns full lines instead (useful for User-Agent or Date for example).
10133 */
10134static int
10135smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010136 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010137{
10138 struct http_txn *txn = l7;
10139 struct hdr_idx *idx = &txn->hdr_idx;
10140 struct hdr_ctx ctx;
10141 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10142 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010143 const char *name = NULL;
10144 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010145
Willy Tarreau29437342015-04-01 19:16:09 +020010146 if (args && args->type == ARGT_STR) {
10147 name = args->data.str.str;
10148 len = args->data.str.len;
10149 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010150
10151 CHECK_HTTP_MESSAGE_FIRST();
10152
10153 ctx.idx = 0;
10154 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010155 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010156 cnt++;
10157
10158 smp->type = SMP_T_UINT;
10159 smp->data.uint = cnt;
10160 smp->flags = SMP_F_VOL_HDR;
10161 return 1;
10162}
10163
10164/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10165 * Accepts an optional argument of type string containing the header field name,
10166 * and an optional argument of type signed or unsigned integer to request an
10167 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010168 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010169 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010170static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010171smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010172 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010173{
10174 struct http_txn *txn = l7;
10175 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010176 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010177 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010178 int occ = 0;
10179 const char *name_str = NULL;
10180 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010181
Willy Tarreaua890d072013-04-02 12:01:06 +020010182 if (!ctx) {
10183 /* first call */
10184 ctx = &static_hdr_ctx;
10185 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010186 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010187 }
10188
Willy Tarreau185b5c42012-04-26 15:11:51 +020010189 if (args) {
10190 if (args[0].type != ARGT_STR)
10191 return 0;
10192 name_str = args[0].data.str.str;
10193 name_len = args[0].data.str.len;
10194
10195 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10196 occ = args[1].data.uint;
10197 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010198
Willy Tarreaue333ec92012-04-16 16:26:40 +020010199 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010200
Willy Tarreau185b5c42012-04-26 15:11:51 +020010201 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010202 /* search for header from the beginning */
10203 ctx->idx = 0;
10204
Willy Tarreau185b5c42012-04-26 15:11:51 +020010205 if (!occ && !(opt & SMP_OPT_ITERATE))
10206 /* no explicit occurrence and single fetch => last header by default */
10207 occ = -1;
10208
10209 if (!occ)
10210 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010211 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010212
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010213 smp->type = SMP_T_STR;
10214 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010215 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 +020010216 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010217
Willy Tarreau37406352012-04-23 16:16:37 +020010218 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010219 return 0;
10220}
10221
Willy Tarreauc11416f2007-06-17 16:58:38 +020010222/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010223 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010224 */
10225static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010226smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010227 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010228{
10229 struct http_txn *txn = l7;
10230 struct hdr_idx *idx = &txn->hdr_idx;
10231 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010232 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010233 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010234 const char *name = NULL;
10235 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010236
Willy Tarreau29437342015-04-01 19:16:09 +020010237 if (args && args->type == ARGT_STR) {
10238 name = args->data.str.str;
10239 len = args->data.str.len;
10240 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010241
Willy Tarreaue333ec92012-04-16 16:26:40 +020010242 CHECK_HTTP_MESSAGE_FIRST();
10243
Willy Tarreau33a7e692007-06-10 19:45:56 +020010244 ctx.idx = 0;
10245 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010246 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010247 cnt++;
10248
Willy Tarreauf853c462012-04-23 18:53:56 +020010249 smp->type = SMP_T_UINT;
10250 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010251 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010252 return 1;
10253}
10254
Willy Tarreau185b5c42012-04-26 15:11:51 +020010255/* Fetch an HTTP header's integer value. The integer value is returned. It
10256 * takes a mandatory argument of type string and an optional one of type int
10257 * to designate a specific occurrence. It returns an unsigned integer, which
10258 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010259 */
10260static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010261smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010262 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010263{
Willy Tarreauef38c392013-07-22 16:29:32 +020010264 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010265
Willy Tarreauf853c462012-04-23 18:53:56 +020010266 if (ret > 0) {
10267 smp->type = SMP_T_UINT;
10268 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10269 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010270
Willy Tarreaud53e2422012-04-16 17:21:11 +020010271 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010272}
10273
Cyril Bonté69fa9922012-10-25 00:01:06 +020010274/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10275 * and an optional one of type int to designate a specific occurrence.
10276 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010277 */
10278static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010279smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010280 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +020010281{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010282 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010283
Willy Tarreauef38c392013-07-22 16:29:32 +020010284 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010285 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10286 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010287 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010288 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010289 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010290 if (smp->data.str.len < temp->size - 1) {
10291 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10292 temp->str[smp->data.str.len] = '\0';
10293 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10294 smp->type = SMP_T_IPV6;
10295 break;
10296 }
10297 }
10298 }
10299
Willy Tarreaud53e2422012-04-16 17:21:11 +020010300 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010301 if (!(smp->flags & SMP_F_NOT_LAST))
10302 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010303 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010304 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010305}
10306
Willy Tarreau737b0c12007-06-10 21:28:46 +020010307/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10308 * the first '/' after the possible hostname, and ends before the possible '?'.
10309 */
10310static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010311smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010312 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010313{
10314 struct http_txn *txn = l7;
10315 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010316
Willy Tarreauc0239e02012-04-16 14:42:55 +020010317 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010318
Willy Tarreau9b28e032012-10-12 23:49:43 +020010319 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010320 ptr = http_get_path(txn);
10321 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010322 return 0;
10323
10324 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010325 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010326 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010327
10328 while (ptr < end && *ptr != '?')
10329 ptr++;
10330
Willy Tarreauf853c462012-04-23 18:53:56 +020010331 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010332 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010333 return 1;
10334}
10335
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010336/* This produces a concatenation of the first occurrence of the Host header
10337 * followed by the path component if it begins with a slash ('/'). This means
10338 * that '*' will not be added, resulting in exactly the first Host entry.
10339 * If no Host header is found, then the path is returned as-is. The returned
10340 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010341 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010342 */
10343static int
10344smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010345 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010346{
10347 struct http_txn *txn = l7;
10348 char *ptr, *end, *beg;
10349 struct hdr_ctx ctx;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010350 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010351
10352 CHECK_HTTP_MESSAGE_FIRST();
10353
10354 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010355 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +020010356 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010357
10358 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010359 temp = get_trash_chunk();
10360 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010361 smp->type = SMP_T_STR;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010362 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010363 smp->data.str.len = ctx.vlen;
10364
10365 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010366 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010367 beg = http_get_path(txn);
10368 if (!beg)
10369 beg = end;
10370
10371 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10372
10373 if (beg < ptr && *beg == '/') {
10374 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10375 smp->data.str.len += ptr - beg;
10376 }
10377
10378 smp->flags = SMP_F_VOL_1ST;
10379 return 1;
10380}
10381
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010382/* This produces a 32-bit hash of the concatenation of the first occurrence of
10383 * the Host header followed by the path component if it begins with a slash ('/').
10384 * This means that '*' will not be added, resulting in exactly the first Host
10385 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010386 * is hashed using the path hash followed by a full avalanche hash and provides a
10387 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010388 * high-traffic sites without having to store whole paths.
10389 */
10390static int
10391smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010392 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010393{
10394 struct http_txn *txn = l7;
10395 struct hdr_ctx ctx;
10396 unsigned int hash = 0;
10397 char *ptr, *beg, *end;
10398 int len;
10399
10400 CHECK_HTTP_MESSAGE_FIRST();
10401
10402 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010403 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010404 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10405 ptr = ctx.line + ctx.val;
10406 len = ctx.vlen;
10407 while (len--)
10408 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10409 }
10410
10411 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010412 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010413 beg = http_get_path(txn);
10414 if (!beg)
10415 beg = end;
10416
10417 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10418
10419 if (beg < ptr && *beg == '/') {
10420 while (beg < ptr)
10421 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10422 }
10423 hash = full_hash(hash);
10424
10425 smp->type = SMP_T_UINT;
10426 smp->data.uint = hash;
10427 smp->flags = SMP_F_VOL_1ST;
10428 return 1;
10429}
10430
Willy Tarreau4a550602012-12-09 14:53:32 +010010431/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010432 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10433 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10434 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010435 * that in environments where IPv6 is insignificant, truncating the output to
10436 * 8 bytes would still work.
10437 */
10438static int
10439smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010440 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +010010441{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010442 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010443 struct connection *cli_conn = objt_conn(l4->si[0].end);
10444
10445 if (!cli_conn)
10446 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010447
Willy Tarreauef38c392013-07-22 16:29:32 +020010448 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +010010449 return 0;
10450
Willy Tarreau47ca5452012-12-23 20:22:19 +010010451 temp = get_trash_chunk();
Willy Tarreau0dff81c2014-07-15 21:34:06 +020010452 *(unsigned int *)temp->str = htonl(smp->data.uint);
10453 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010454
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010455 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010456 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010457 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010458 temp->len += 4;
10459 break;
10460 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010461 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010462 temp->len += 16;
10463 break;
10464 default:
10465 return 0;
10466 }
10467
10468 smp->data.str = *temp;
10469 smp->type = SMP_T_BIN;
10470 return 1;
10471}
10472
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010473static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010474smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010475 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010476{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010477 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10478 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10479 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010480
Willy Tarreau24e32d82012-04-23 23:55:44 +020010481 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010482
Willy Tarreauf853c462012-04-23 18:53:56 +020010483 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010484 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010485 return 1;
10486}
10487
Willy Tarreau7f18e522010-10-22 20:04:13 +020010488/* return a valid test if the current request is the first one on the connection */
10489static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010490smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010491 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010492{
10493 if (!s)
10494 return 0;
10495
Willy Tarreauf853c462012-04-23 18:53:56 +020010496 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010497 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010498 return 1;
10499}
10500
Willy Tarreau34db1082012-04-19 17:16:54 +020010501/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010502static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010503smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010504 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010505{
10506
Willy Tarreau24e32d82012-04-23 23:55:44 +020010507 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010508 return 0;
10509
Willy Tarreauc0239e02012-04-16 14:42:55 +020010510 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010511
Willy Tarreauc0239e02012-04-16 14:42:55 +020010512 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010513 return 0;
10514
Willy Tarreauf853c462012-04-23 18:53:56 +020010515 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010516 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010517 return 1;
10518}
Willy Tarreau8797c062007-05-07 00:55:35 +020010519
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010520/* Accepts exactly 1 argument of type userlist */
10521static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010522smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010523 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010524{
10525
10526 if (!args || args->type != ARGT_USR)
10527 return 0;
10528
10529 CHECK_HTTP_MESSAGE_FIRST();
10530
10531 if (!get_http_auth(l4))
10532 return 0;
10533
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010534 /* if the user does not belong to the userlist or has a wrong password,
10535 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010536 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010537 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010538 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10539 return 0;
10540
10541 /* pat_match_auth() will need the user list */
10542 smp->ctx.a[0] = args->data.usr;
10543
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010544 smp->type = SMP_T_STR;
10545 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010546 smp->data.str.str = l4->txn.auth.user;
10547 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010548
10549 return 1;
10550}
10551
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010552/* Try to find the next occurrence of a cookie name in a cookie header value.
10553 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10554 * the cookie value is returned into *value and *value_l, and the function
10555 * returns a pointer to the next pointer to search from if the value was found.
10556 * Otherwise if the cookie was not found, NULL is returned and neither value
10557 * nor value_l are touched. The input <hdr> string should first point to the
10558 * header's value, and the <hdr_end> pointer must point to the first character
10559 * not part of the value. <list> must be non-zero if value may represent a list
10560 * of values (cookie headers). This makes it faster to abort parsing when no
10561 * list is expected.
10562 */
10563static char *
10564extract_cookie_value(char *hdr, const char *hdr_end,
10565 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010566 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010567{
10568 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10569 char *next;
10570
10571 /* we search at least a cookie name followed by an equal, and more
10572 * generally something like this :
10573 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10574 */
10575 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10576 /* Iterate through all cookies on this line */
10577
10578 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10579 att_beg++;
10580
10581 /* find att_end : this is the first character after the last non
10582 * space before the equal. It may be equal to hdr_end.
10583 */
10584 equal = att_end = att_beg;
10585
10586 while (equal < hdr_end) {
10587 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10588 break;
10589 if (http_is_spht[(unsigned char)*equal++])
10590 continue;
10591 att_end = equal;
10592 }
10593
10594 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10595 * is between <att_beg> and <equal>, both may be identical.
10596 */
10597
10598 /* look for end of cookie if there is an equal sign */
10599 if (equal < hdr_end && *equal == '=') {
10600 /* look for the beginning of the value */
10601 val_beg = equal + 1;
10602 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10603 val_beg++;
10604
10605 /* find the end of the value, respecting quotes */
10606 next = find_cookie_value_end(val_beg, hdr_end);
10607
10608 /* make val_end point to the first white space or delimitor after the value */
10609 val_end = next;
10610 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10611 val_end--;
10612 } else {
10613 val_beg = val_end = next = equal;
10614 }
10615
10616 /* We have nothing to do with attributes beginning with '$'. However,
10617 * they will automatically be removed if a header before them is removed,
10618 * since they're supposed to be linked together.
10619 */
10620 if (*att_beg == '$')
10621 continue;
10622
10623 /* Ignore cookies with no equal sign */
10624 if (equal == next)
10625 continue;
10626
10627 /* Now we have the cookie name between att_beg and att_end, and
10628 * its value between val_beg and val_end.
10629 */
10630
10631 if (att_end - att_beg == cookie_name_l &&
10632 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10633 /* let's return this value and indicate where to go on from */
10634 *value = val_beg;
10635 *value_l = val_end - val_beg;
10636 return next + 1;
10637 }
10638
10639 /* Set-Cookie headers only have the name in the first attr=value part */
10640 if (!list)
10641 break;
10642 }
10643
10644 return NULL;
10645}
10646
William Lallemanda43ba4e2014-01-28 18:14:25 +010010647/* Fetch a captured HTTP request header. The index is the position of
10648 * the "capture" option in the configuration file
10649 */
10650static int
10651smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10652 const struct arg *args, struct sample *smp, const char *kw)
10653{
10654 struct proxy *fe = l4->fe;
10655 struct http_txn *txn = l7;
10656 int idx;
10657
10658 if (!args || args->type != ARGT_UINT)
10659 return 0;
10660
10661 idx = args->data.uint;
10662
10663 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10664 return 0;
10665
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010666 smp->type = SMP_T_STR;
10667 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010668 smp->data.str.str = txn->req.cap[idx];
10669 smp->data.str.len = strlen(txn->req.cap[idx]);
10670
10671 return 1;
10672}
10673
10674/* Fetch a captured HTTP response header. The index is the position of
10675 * the "capture" option in the configuration file
10676 */
10677static int
10678smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10679 const struct arg *args, struct sample *smp, const char *kw)
10680{
10681 struct proxy *fe = l4->fe;
10682 struct http_txn *txn = l7;
10683 int idx;
10684
10685 if (!args || args->type != ARGT_UINT)
10686 return 0;
10687
10688 idx = args->data.uint;
10689
10690 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10691 return 0;
10692
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010693 smp->type = SMP_T_STR;
10694 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010695 smp->data.str.str = txn->rsp.cap[idx];
10696 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10697
10698 return 1;
10699}
10700
William Lallemand65ad6e12014-01-31 15:08:02 +010010701/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10702static int
10703smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10704 const struct arg *args, struct sample *smp, const char *kw)
10705{
10706 struct chunk *temp;
10707 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010708 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010709
10710 if (!txn->uri)
10711 return 0;
10712
William Lallemand96a77852014-02-05 00:30:02 +010010713 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010714
William Lallemand96a77852014-02-05 00:30:02 +010010715 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10716 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010717
William Lallemand96a77852014-02-05 00:30:02 +010010718 temp = get_trash_chunk();
10719 temp->str = txn->uri;
10720 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010721 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010722 smp->type = SMP_T_STR;
10723 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010724
10725 return 1;
10726
10727}
10728
10729/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10730static int
10731smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10732 const struct arg *args, struct sample *smp, const char *kw)
10733{
10734 struct chunk *temp;
10735 struct http_txn *txn = l7;
10736 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010737
10738 if (!txn->uri)
10739 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010740
William Lallemand65ad6e12014-01-31 15:08:02 +010010741 ptr = txn->uri;
10742
10743 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10744 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010745
William Lallemand65ad6e12014-01-31 15:08:02 +010010746 if (!*ptr)
10747 return 0;
10748
10749 ptr++; /* skip the space */
10750
10751 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010752 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010753 if (!ptr)
10754 return 0;
10755 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10756 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010757
10758 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010759 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010760 smp->type = SMP_T_STR;
10761 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010762
10763 return 1;
10764}
10765
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010766/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10767 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10768 */
10769static int
10770smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10771 const struct arg *args, struct sample *smp, const char *kw)
10772{
10773 struct http_txn *txn = l7;
10774
10775 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10776 return 0;
10777
10778 if (txn->req.flags & HTTP_MSGF_VER_11)
10779 smp->data.str.str = "HTTP/1.1";
10780 else
10781 smp->data.str.str = "HTTP/1.0";
10782
10783 smp->data.str.len = 8;
10784 smp->type = SMP_T_STR;
10785 smp->flags = SMP_F_CONST;
10786 return 1;
10787
10788}
10789
10790/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10791 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10792 */
10793static int
10794smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10795 const struct arg *args, struct sample *smp, const char *kw)
10796{
10797 struct http_txn *txn = l7;
10798
10799 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10800 return 0;
10801
10802 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10803 smp->data.str.str = "HTTP/1.1";
10804 else
10805 smp->data.str.str = "HTTP/1.0";
10806
10807 smp->data.str.len = 8;
10808 smp->type = SMP_T_STR;
10809 smp->flags = SMP_F_CONST;
10810 return 1;
10811
10812}
10813
William Lallemand65ad6e12014-01-31 15:08:02 +010010814
Willy Tarreaue333ec92012-04-16 16:26:40 +020010815/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010816 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010817 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010818 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010819 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010820 * Accepts exactly 1 argument of type string. If the input options indicate
10821 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010822 * The returned sample is of type CSTR. Can be used to parse cookies in other
10823 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010824 */
William Lallemand07c8b242014-05-02 17:11:07 +020010825int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010826 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010827{
10828 struct http_txn *txn = l7;
10829 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010830 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010831 const struct http_msg *msg;
10832 const char *hdr_name;
10833 int hdr_name_len;
10834 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010835 int occ = 0;
10836 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010837
Willy Tarreau24e32d82012-04-23 23:55:44 +020010838 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010839 return 0;
10840
Willy Tarreaua890d072013-04-02 12:01:06 +020010841 if (!ctx) {
10842 /* first call */
10843 ctx = &static_hdr_ctx;
10844 ctx->idx = 0;
10845 smp->ctx.a[2] = ctx;
10846 }
10847
Willy Tarreaue333ec92012-04-16 16:26:40 +020010848 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010849
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010850 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010851 msg = &txn->req;
10852 hdr_name = "Cookie";
10853 hdr_name_len = 6;
10854 } else {
10855 msg = &txn->rsp;
10856 hdr_name = "Set-Cookie";
10857 hdr_name_len = 10;
10858 }
10859
Willy Tarreau28376d62012-04-26 21:26:10 +020010860 if (!occ && !(opt & SMP_OPT_ITERATE))
10861 /* no explicit occurrence and single fetch => last cookie by default */
10862 occ = -1;
10863
10864 /* OK so basically here, either we want only one value and it's the
10865 * last one, or we want to iterate over all of them and we fetch the
10866 * next one.
10867 */
10868
Willy Tarreau9b28e032012-10-12 23:49:43 +020010869 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010870 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010871 /* search for the header from the beginning, we must first initialize
10872 * the search parameters.
10873 */
Willy Tarreau37406352012-04-23 16:16:37 +020010874 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010875 ctx->idx = 0;
10876 }
10877
Willy Tarreau28376d62012-04-26 21:26:10 +020010878 smp->flags |= SMP_F_VOL_HDR;
10879
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010880 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010881 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10882 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010883 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10884 goto out;
10885
Willy Tarreau24e32d82012-04-23 23:55:44 +020010886 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010887 continue;
10888
Willy Tarreau37406352012-04-23 16:16:37 +020010889 smp->ctx.a[0] = ctx->line + ctx->val;
10890 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010891 }
10892
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010893 smp->type = SMP_T_STR;
10894 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010895 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010896 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010897 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010898 &smp->data.str.str,
10899 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010900 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010901 found = 1;
10902 if (occ >= 0) {
10903 /* one value was returned into smp->data.str.{str,len} */
10904 smp->flags |= SMP_F_NOT_LAST;
10905 return 1;
10906 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010907 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010908 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010909 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010910 /* all cookie headers and values were scanned. If we're looking for the
10911 * last occurrence, we may return it now.
10912 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010913 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010914 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010915 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010916}
10917
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010918/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010919 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010920 * multiple cookies may be parsed on the same line. The returned sample is of
10921 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010922 */
10923static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010924smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010925 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010926{
10927 struct http_txn *txn = l7;
10928 struct hdr_idx *idx = &txn->hdr_idx;
10929 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010930 const struct http_msg *msg;
10931 const char *hdr_name;
10932 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010933 int cnt;
10934 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010935 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010936
Willy Tarreau24e32d82012-04-23 23:55:44 +020010937 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010938 return 0;
10939
Willy Tarreaue333ec92012-04-16 16:26:40 +020010940 CHECK_HTTP_MESSAGE_FIRST();
10941
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010942 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010943 msg = &txn->req;
10944 hdr_name = "Cookie";
10945 hdr_name_len = 6;
10946 } else {
10947 msg = &txn->rsp;
10948 hdr_name = "Set-Cookie";
10949 hdr_name_len = 10;
10950 }
10951
Willy Tarreau9b28e032012-10-12 23:49:43 +020010952 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010953 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010954 ctx.idx = 0;
10955 cnt = 0;
10956
10957 while (1) {
10958 /* Note: val_beg == NULL every time we need to fetch a new header */
10959 if (!val_beg) {
10960 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10961 break;
10962
Willy Tarreau24e32d82012-04-23 23:55:44 +020010963 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010964 continue;
10965
10966 val_beg = ctx.line + ctx.val;
10967 val_end = val_beg + ctx.vlen;
10968 }
10969
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010970 smp->type = SMP_T_STR;
10971 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010972 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010973 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010974 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010975 &smp->data.str.str,
10976 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010977 cnt++;
10978 }
10979 }
10980
Willy Tarreaub169eba2013-12-16 15:14:43 +010010981 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010982 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010983 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010984 return 1;
10985}
10986
Willy Tarreau51539362012-05-08 12:46:28 +020010987/* Fetch an cookie's integer value. The integer value is returned. It
10988 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10989 */
10990static int
10991smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010992 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010993{
Willy Tarreauef38c392013-07-22 16:29:32 +020010994 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010995
10996 if (ret > 0) {
10997 smp->type = SMP_T_UINT;
10998 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10999 }
11000
11001 return ret;
11002}
11003
Willy Tarreau8797c062007-05-07 00:55:35 +020011004/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011005/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011006/************************************************************************/
11007
David Cournapeau16023ee2010-12-23 20:55:41 +090011008/*
11009 * Given a path string and its length, find the position of beginning of the
11010 * query string. Returns NULL if no query string is found in the path.
11011 *
11012 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11013 *
11014 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11015 */
bedis4c75cca2012-10-05 08:38:24 +020011016static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011017{
11018 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011019
bedis4c75cca2012-10-05 08:38:24 +020011020 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011021 return p ? p + 1 : NULL;
11022}
11023
bedis4c75cca2012-10-05 08:38:24 +020011024static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011025{
bedis4c75cca2012-10-05 08:38:24 +020011026 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011027}
11028
11029/*
11030 * Given a url parameter, find the starting position of the first occurence,
11031 * or NULL if the parameter is not found.
11032 *
11033 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11034 * the function will return query_string+8.
11035 */
11036static char*
11037find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011038 char* url_param_name, size_t url_param_name_l,
11039 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011040{
11041 char *pos, *last;
11042
11043 pos = query_string;
11044 last = query_string + query_string_l - url_param_name_l - 1;
11045
11046 while (pos <= last) {
11047 if (pos[url_param_name_l] == '=') {
11048 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11049 return pos;
11050 pos += url_param_name_l + 1;
11051 }
bedis4c75cca2012-10-05 08:38:24 +020011052 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011053 pos++;
11054 pos++;
11055 }
11056 return NULL;
11057}
11058
11059/*
Cyril Bonté0f836e12015-11-26 21:39:56 +010011060 * Given a url parameter name and a query string, find the next value.
11061 * An empty url_param_name matches the first available parameter.
11062 * If the parameter is found, 1 is returned and *value / *value_l are updated
11063 * to respectively provide a pointer to the value and its length.
11064 * Otherwise, 0 is returned and value/value_l are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011065 */
11066static int
11067find_url_param_value(char* path, size_t path_l,
11068 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011069 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011070{
11071 char *query_string, *qs_end;
11072 char *arg_start;
11073 char *value_start, *value_end;
11074
bedis4c75cca2012-10-05 08:38:24 +020011075 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011076 if (!query_string)
11077 return 0;
11078
11079 qs_end = path + path_l;
11080 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011081 url_param_name, url_param_name_l,
11082 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011083 if (!arg_start)
11084 return 0;
11085
11086 value_start = arg_start + url_param_name_l + 1;
11087 value_end = value_start;
11088
bedis4c75cca2012-10-05 08:38:24 +020011089 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011090 value_end++;
11091
11092 *value = value_start;
11093 *value_l = value_end - value_start;
Cyril Bonté0f836e12015-11-26 21:39:56 +010011094 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011095}
11096
11097static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011098smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011099 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090011100{
bedis4c75cca2012-10-05 08:38:24 +020011101 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090011102 struct http_txn *txn = l7;
11103 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011104
bedis4c75cca2012-10-05 08:38:24 +020011105 if (!args || args[0].type != ARGT_STR ||
11106 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011107 return 0;
11108
11109 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011110
bedis4c75cca2012-10-05 08:38:24 +020011111 if (args[1].type)
11112 delim = *args[1].data.str.str;
11113
Willy Tarreau9b28e032012-10-12 23:49:43 +020011114 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011115 args->data.str.str, args->data.str.len,
11116 &smp->data.str.str, &smp->data.str.len,
11117 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011118 return 0;
11119
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011120 smp->type = SMP_T_STR;
11121 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011122 return 1;
11123}
11124
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011125/* Return the signed integer value for the specified url parameter (see url_param
11126 * above).
11127 */
11128static int
11129smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011130 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011131{
Willy Tarreauef38c392013-07-22 16:29:32 +020011132 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011133
11134 if (ret > 0) {
11135 smp->type = SMP_T_UINT;
11136 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11137 }
11138
11139 return ret;
11140}
11141
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011142/* This produces a 32-bit hash of the concatenation of the first occurrence of
11143 * the Host header followed by the path component if it begins with a slash ('/').
11144 * This means that '*' will not be added, resulting in exactly the first Host
11145 * entry. If no Host header is found, then the path is used. The resulting value
11146 * is hashed using the url hash followed by a full avalanche hash and provides a
11147 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11148 * high-traffic sites without having to store whole paths.
11149 * this differs from the base32 functions in that it includes the url parameters
11150 * as well as the path
11151 */
11152static int
11153smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011154 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011155{
11156 struct http_txn *txn = l7;
11157 struct hdr_ctx ctx;
11158 unsigned int hash = 0;
11159 char *ptr, *beg, *end;
11160 int len;
11161
11162 CHECK_HTTP_MESSAGE_FIRST();
11163
11164 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011165 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011166 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11167 ptr = ctx.line + ctx.val;
11168 len = ctx.vlen;
11169 while (len--)
11170 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11171 }
11172
11173 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011174 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 +000011175 beg = http_get_path(txn);
11176 if (!beg)
11177 beg = end;
11178
11179 for (ptr = beg; ptr < end ; ptr++);
11180
11181 if (beg < ptr && *beg == '/') {
11182 while (beg < ptr)
11183 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11184 }
11185 hash = full_hash(hash);
11186
11187 smp->type = SMP_T_UINT;
11188 smp->data.uint = hash;
11189 smp->flags = SMP_F_VOL_1ST;
11190 return 1;
11191}
11192
11193/* This concatenates the source address with the 32-bit hash of the Host and
11194 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11195 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11196 * on the source address length. The URL hash is stored before the address so
11197 * that in environments where IPv6 is insignificant, truncating the output to
11198 * 8 bytes would still work.
11199 */
11200static int
11201smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011202 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011203{
11204 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011205 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011206
Willy Tarreaue155ec22013-11-18 18:33:22 +010011207 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011208 return 0;
11209
11210 temp = get_trash_chunk();
11211 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11212 temp->len += sizeof(smp->data.uint);
11213
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011214 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011215 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011216 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011217 temp->len += 4;
11218 break;
11219 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011220 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011221 temp->len += 16;
11222 break;
11223 default:
11224 return 0;
11225 }
11226
11227 smp->data.str = *temp;
11228 smp->type = SMP_T_BIN;
11229 return 1;
11230}
11231
Willy Tarreau185b5c42012-04-26 15:11:51 +020011232/* This function is used to validate the arguments passed to any "hdr" fetch
11233 * keyword. These keywords support an optional positive or negative occurrence
11234 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11235 * is assumed that the types are already the correct ones. Returns 0 on error,
11236 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11237 * error message in case of error, that the caller is responsible for freeing.
11238 * The initial location must either be freeable or NULL.
11239 */
11240static int val_hdr(struct arg *arg, char **err_msg)
11241{
11242 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011243 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011244 return 0;
11245 }
11246 return 1;
11247}
11248
Willy Tarreau276fae92013-07-25 14:36:01 +020011249/* takes an UINT value on input supposed to represent the time since EPOCH,
11250 * adds an optional offset found in args[0] and emits a string representing
11251 * the date in RFC-1123/5322 format.
11252 */
11253static int sample_conv_http_date(const struct arg *args, struct sample *smp)
11254{
Cyril Bontéf980b362016-01-22 19:40:28 +010011255 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011256 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11257 struct chunk *temp;
11258 struct tm *tm;
11259 time_t curr_date = smp->data.uint;
11260
11261 /* add offset */
11262 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11263 curr_date += args[0].data.sint;
11264
11265 tm = gmtime(&curr_date);
Thierry FOURNIER95558722015-07-08 00:15:20 +020011266 if (!tm)
11267 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011268
11269 temp = get_trash_chunk();
11270 temp->len = snprintf(temp->str, temp->size - temp->len,
11271 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11272 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11273 tm->tm_hour, tm->tm_min, tm->tm_sec);
11274
11275 smp->data.str = *temp;
11276 smp->type = SMP_T_STR;
11277 return 1;
11278}
11279
Thierry FOURNIERad903512014-04-11 17:51:01 +020011280/* Match language range with language tag. RFC2616 14.4:
11281 *
11282 * A language-range matches a language-tag if it exactly equals
11283 * the tag, or if it exactly equals a prefix of the tag such
11284 * that the first tag character following the prefix is "-".
11285 *
11286 * Return 1 if the strings match, else return 0.
11287 */
11288static inline int language_range_match(const char *range, int range_len,
11289 const char *tag, int tag_len)
11290{
11291 const char *end = range + range_len;
11292 const char *tend = tag + tag_len;
11293 while (range < end) {
11294 if (*range == '-' && tag == tend)
11295 return 1;
11296 if (*range != *tag || tag == tend)
11297 return 0;
11298 range++;
11299 tag++;
11300 }
11301 /* Return true only if the last char of the tag is matched. */
11302 return tag == tend;
11303}
11304
11305/* Arguments: The list of expected value, the number of parts returned and the separator */
11306static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
11307{
11308 const char *al = smp->data.str.str;
11309 const char *end = al + smp->data.str.len;
11310 const char *token;
11311 int toklen;
11312 int qvalue;
11313 const char *str;
11314 const char *w;
11315 int best_q = 0;
11316
11317 /* Set the constant to the sample, because the output of the
11318 * function will be peek in the constant configuration string.
11319 */
11320 smp->flags |= SMP_F_CONST;
11321 smp->data.str.size = 0;
11322 smp->data.str.str = "";
11323 smp->data.str.len = 0;
11324
11325 /* Parse the accept language */
11326 while (1) {
11327
11328 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011329 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011330 al++;
11331 if (al >= end)
11332 break;
11333
11334 /* Start of the fisrt word. */
11335 token = al;
11336
11337 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011338 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011339 al++;
11340 if (al == token)
11341 goto expect_comma;
11342
11343 /* Length of the token. */
11344 toklen = al - token;
11345 qvalue = 1000;
11346
11347 /* Check if the token exists in the list. If the token not exists,
11348 * jump to the next token.
11349 */
11350 str = args[0].data.str.str;
11351 w = str;
11352 while (1) {
11353 if (*str == ';' || *str == '\0') {
11354 if (language_range_match(token, toklen, w, str-w))
11355 goto look_for_q;
11356 if (*str == '\0')
11357 goto expect_comma;
11358 w = str + 1;
11359 }
11360 str++;
11361 }
11362 goto expect_comma;
11363
11364look_for_q:
11365
11366 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011367 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011368 al++;
11369 if (al >= end)
11370 goto process_value;
11371
11372 /* If ',' is found, process the result */
11373 if (*al == ',')
11374 goto process_value;
11375
11376 /* If the character is different from ';', look
11377 * for the end of the header part in best effort.
11378 */
11379 if (*al != ';')
11380 goto expect_comma;
11381
11382 /* Assumes that the char is ';', now expect "q=". */
11383 al++;
11384
11385 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011386 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011387 al++;
11388 if (al >= end)
11389 goto process_value;
11390
11391 /* Expect 'q'. If no 'q', continue in best effort */
11392 if (*al != 'q')
11393 goto process_value;
11394 al++;
11395
11396 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011397 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011398 al++;
11399 if (al >= end)
11400 goto process_value;
11401
11402 /* Expect '='. If no '=', continue in best effort */
11403 if (*al != '=')
11404 goto process_value;
11405 al++;
11406
11407 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011408 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011409 al++;
11410 if (al >= end)
11411 goto process_value;
11412
11413 /* Parse the q value. */
11414 qvalue = parse_qvalue(al, &al);
11415
11416process_value:
11417
11418 /* If the new q value is the best q value, then store the associated
11419 * language in the response. If qvalue is the biggest value (1000),
11420 * break the process.
11421 */
11422 if (qvalue > best_q) {
11423 smp->data.str.str = (char *)w;
11424 smp->data.str.len = str - w;
11425 if (qvalue >= 1000)
11426 break;
11427 best_q = qvalue;
11428 }
11429
11430expect_comma:
11431
11432 /* Expect comma or end. If the end is detected, quit the loop. */
11433 while (al < end && *al != ',')
11434 al++;
11435 if (al >= end)
11436 break;
11437
11438 /* Comma is found, jump it and restart the analyzer. */
11439 al++;
11440 }
11441
11442 /* Set default value if required. */
11443 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11444 smp->data.str.str = args[1].data.str.str;
11445 smp->data.str.len = args[1].data.str.len;
11446 }
11447
11448 /* Return true only if a matching language was found. */
11449 return smp->data.str.len != 0;
11450}
11451
William Lallemand73025dd2014-04-24 14:38:37 +020011452/*
11453 * Return the struct http_req_action_kw associated to a keyword.
11454 */
11455struct http_req_action_kw *action_http_req_custom(const char *kw)
11456{
11457 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11458 struct http_req_action_kw_list *kw_list;
11459 int i;
11460
11461 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11462 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11463 if (!strcmp(kw, kw_list->kw[i].kw))
11464 return &kw_list->kw[i];
11465 }
11466 }
11467 }
11468 return NULL;
11469}
11470
11471/*
11472 * Return the struct http_res_action_kw associated to a keyword.
11473 */
11474struct http_res_action_kw *action_http_res_custom(const char *kw)
11475{
11476 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11477 struct http_res_action_kw_list *kw_list;
11478 int i;
11479
11480 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11481 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11482 if (!strcmp(kw, kw_list->kw[i].kw))
11483 return &kw_list->kw[i];
11484 }
11485 }
11486 }
11487 return NULL;
11488}
11489
Willy Tarreau4a568972010-05-12 08:08:50 +020011490/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011491/* All supported ACL keywords must be declared here. */
11492/************************************************************************/
11493
11494/* Note: must not be declared <const> as its list will be overwritten.
11495 * Please take care of keeping this list alphabetically sorted.
11496 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011497static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011498 { "base", "base", PAT_MATCH_STR },
11499 { "base_beg", "base", PAT_MATCH_BEG },
11500 { "base_dir", "base", PAT_MATCH_DIR },
11501 { "base_dom", "base", PAT_MATCH_DOM },
11502 { "base_end", "base", PAT_MATCH_END },
11503 { "base_len", "base", PAT_MATCH_LEN },
11504 { "base_reg", "base", PAT_MATCH_REG },
11505 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011506
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011507 { "cook", "req.cook", PAT_MATCH_STR },
11508 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11509 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11510 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11511 { "cook_end", "req.cook", PAT_MATCH_END },
11512 { "cook_len", "req.cook", PAT_MATCH_LEN },
11513 { "cook_reg", "req.cook", PAT_MATCH_REG },
11514 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011515
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011516 { "hdr", "req.hdr", PAT_MATCH_STR },
11517 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11518 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11519 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11520 { "hdr_end", "req.hdr", PAT_MATCH_END },
11521 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11522 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11523 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011524
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011525 /* these two declarations uses strings with list storage (in place
11526 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11527 * and delete functions are relative to the list management. The parse
11528 * and match method are related to the corresponding fetch methods. This
11529 * is very particular ACL declaration mode.
11530 */
11531 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11532 { "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 +020011533
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011534 { "path", "path", PAT_MATCH_STR },
11535 { "path_beg", "path", PAT_MATCH_BEG },
11536 { "path_dir", "path", PAT_MATCH_DIR },
11537 { "path_dom", "path", PAT_MATCH_DOM },
11538 { "path_end", "path", PAT_MATCH_END },
11539 { "path_len", "path", PAT_MATCH_LEN },
11540 { "path_reg", "path", PAT_MATCH_REG },
11541 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011542
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011543 { "req_ver", "req.ver", PAT_MATCH_STR },
11544 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011545
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011546 { "scook", "res.cook", PAT_MATCH_STR },
11547 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11548 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11549 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11550 { "scook_end", "res.cook", PAT_MATCH_END },
11551 { "scook_len", "res.cook", PAT_MATCH_LEN },
11552 { "scook_reg", "res.cook", PAT_MATCH_REG },
11553 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011554
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011555 { "shdr", "res.hdr", PAT_MATCH_STR },
11556 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11557 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11558 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11559 { "shdr_end", "res.hdr", PAT_MATCH_END },
11560 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11561 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11562 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011563
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011564 { "url", "url", PAT_MATCH_STR },
11565 { "url_beg", "url", PAT_MATCH_BEG },
11566 { "url_dir", "url", PAT_MATCH_DIR },
11567 { "url_dom", "url", PAT_MATCH_DOM },
11568 { "url_end", "url", PAT_MATCH_END },
11569 { "url_len", "url", PAT_MATCH_LEN },
11570 { "url_reg", "url", PAT_MATCH_REG },
11571 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011572
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011573 { "urlp", "urlp", PAT_MATCH_STR },
11574 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11575 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11576 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11577 { "urlp_end", "urlp", PAT_MATCH_END },
11578 { "urlp_len", "urlp", PAT_MATCH_LEN },
11579 { "urlp_reg", "urlp", PAT_MATCH_REG },
11580 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011581
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011582 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011583}};
11584
11585/************************************************************************/
11586/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011587/************************************************************************/
11588/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011589static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011590 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011591 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11592 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11593
William Lallemanda43ba4e2014-01-28 18:14:25 +010011594 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011595 { "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 +020011596
11597 /* retrieve these captures from the HTTP logs */
11598 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11599 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11600 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11601
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011602 { "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 +020011603 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011604
Willy Tarreau409bcde2013-01-08 00:31:00 +010011605 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11606 * are only here to match the ACL's name, are request-only and are used
11607 * for ACL compatibility only.
11608 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011609 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11610 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011611 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11612 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11613
11614 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11615 * only here to match the ACL's name, are request-only and are used for
11616 * ACL compatibility only.
11617 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011618 { "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 +010011619 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11620 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11621 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11622
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011623 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011624 { "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 +010011625 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011626 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011627 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011628
11629 /* HTTP protocol on the request path */
11630 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011631 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011632
11633 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011634 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11635 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011636
11637 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011638 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11639 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011640
Willy Tarreau18ed2562013-01-14 15:56:36 +010011641 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011642 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011643 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11644 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11645
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011646 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011647 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011648 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011649 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11650 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11651 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11652
11653 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011654 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011655 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11656 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11657
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011658 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011659 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011660 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011661 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11662 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11663 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11664
Willy Tarreau409bcde2013-01-08 00:31:00 +010011665 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011666 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011667 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11668 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011669 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011670
11671 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011672 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011673 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11674 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11675 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11676
11677 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011678 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011679 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11680 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011681 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11682 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011683 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11684 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011685 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11686 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011687}};
11688
Willy Tarreau8797c062007-05-07 00:55:35 +020011689
Willy Tarreau276fae92013-07-25 14:36:01 +020011690/* Note: must not be declared <const> as its list will be overwritten */
11691static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011692 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11693 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011694 { NULL, NULL, 0, 0, 0 },
11695}};
11696
Willy Tarreau8797c062007-05-07 00:55:35 +020011697__attribute__((constructor))
11698static void __http_protocol_init(void)
11699{
11700 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011701 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011702 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020011703}
11704
11705
Willy Tarreau58f10d72006-12-04 02:26:12 +010011706/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011707 * Local variables:
11708 * c-indent-level: 8
11709 * c-basic-offset: 8
11710 * End:
11711 */