blob: 17742c6ffe9f58416addf83bab842c2a2eb6c8a4 [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);
1405 if (!p || p == h)
1406 return 0;
1407
1408 chunk_initlen(&auth_method, h, 0, p-h);
1409 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1410
1411 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1412
1413 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001414 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001415
1416 if (len < 0)
1417 return 0;
1418
1419
1420 get_http_auth_buff[len] = '\0';
1421
1422 p = strchr(get_http_auth_buff, ':');
1423
1424 if (!p)
1425 return 0;
1426
1427 txn->auth.user = get_http_auth_buff;
1428 *p = '\0';
1429 txn->auth.pass = p+1;
1430
1431 txn->auth.method = HTTP_AUTH_BASIC;
1432 return 1;
1433 }
1434
1435 return 0;
1436}
1437
Willy Tarreau58f10d72006-12-04 02:26:12 +01001438
Willy Tarreau8973c702007-01-21 23:58:29 +01001439/*
1440 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001441 * depending on the initial msg->msg_state. The caller is responsible for
1442 * ensuring that the message does not wrap. The function can be preempted
1443 * everywhere when data are missing and recalled at the exact same location
1444 * with no information loss. The message may even be realigned between two
1445 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001446 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001447 * fields. Note that msg->sol will be initialized after completing the first
1448 * state, so that none of the msg pointers has to be initialized prior to the
1449 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001451void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452{
Willy Tarreau3770f232013-12-07 00:01:53 +01001453 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001455 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001456
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001457 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001458 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001459 ptr = buf->p + msg->next;
1460 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001461
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001462 if (unlikely(ptr >= end))
1463 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001464
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001466 /*
1467 * First, states that are specific to the response only.
1468 * We check them first so that request and headers are
1469 * closer to each other (accessed more often).
1470 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001471 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001472 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001473 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001474 /* we have a start of message, but we have to check
1475 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001476 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001477 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001478 if (unlikely(ptr != buf->p)) {
1479 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001480 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001481 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001482 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001483 }
Willy Tarreau26927362012-05-18 23:22:52 +02001484 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001485 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001486 hdr_idx_init(idx);
1487 state = HTTP_MSG_RPVER;
1488 goto http_msg_rpver;
1489 }
1490
1491 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1492 goto http_msg_invalid;
1493
1494 if (unlikely(*ptr == '\n'))
1495 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1496 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1497 /* stop here */
1498
Willy Tarreau8973c702007-01-21 23:58:29 +01001499 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001500 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001501 EXPECT_LF_HERE(ptr, http_msg_invalid);
1502 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1503 /* stop here */
1504
Willy Tarreau8973c702007-01-21 23:58:29 +01001505 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001506 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001507 case HTTP_MSG_RPVER_SP:
1508 case HTTP_MSG_RPCODE:
1509 case HTTP_MSG_RPCODE_SP:
1510 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001511 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001512 state, ptr, end,
1513 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001514 if (unlikely(!ptr))
1515 return;
1516
1517 /* we have a full response and we know that we have either a CR
1518 * or an LF at <ptr>.
1519 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001520 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1521
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001522 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001523 if (likely(*ptr == '\r'))
1524 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1525 goto http_msg_rpline_end;
1526
Willy Tarreau8973c702007-01-21 23:58:29 +01001527 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001528 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001529 /* msg->sol must point to the first of CR or LF. */
1530 EXPECT_LF_HERE(ptr, http_msg_invalid);
1531 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1532 /* stop here */
1533
1534 /*
1535 * Second, states that are specific to the request only
1536 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001538 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001540 /* we have a start of message, but we have to check
1541 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001542 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001543 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001544 if (likely(ptr != buf->p)) {
1545 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001546 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001547 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001548 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 }
Willy Tarreau26927362012-05-18 23:22:52 +02001550 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001551 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001552 state = HTTP_MSG_RQMETH;
1553 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001555
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1557 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001558
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 if (unlikely(*ptr == '\n'))
1560 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1561 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001562 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001563
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001565 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 EXPECT_LF_HERE(ptr, http_msg_invalid);
1567 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001568 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001571 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 case HTTP_MSG_RQMETH_SP:
1573 case HTTP_MSG_RQURI:
1574 case HTTP_MSG_RQURI_SP:
1575 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001576 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001577 state, ptr, end,
1578 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 if (unlikely(!ptr))
1580 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001581
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 /* we have a full request and we know that we have either a CR
1583 * or an LF at <ptr>.
1584 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001586
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001587 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 if (likely(*ptr == '\r'))
1589 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001591
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001593 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 /* check for HTTP/0.9 request : no version information available.
1595 * msg->sol must point to the first of CR or LF.
1596 */
1597 if (unlikely(msg->sl.rq.v_l == 0))
1598 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001599
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 EXPECT_LF_HERE(ptr, http_msg_invalid);
1601 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001602 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001603
Willy Tarreau8973c702007-01-21 23:58:29 +01001604 /*
1605 * Common states below
1606 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001608 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001609 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001610 if (likely(!HTTP_IS_CRLF(*ptr))) {
1611 goto http_msg_hdr_name;
1612 }
1613
1614 if (likely(*ptr == '\r'))
1615 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1616 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001617
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001619 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001620 /* assumes msg->sol points to the first char */
1621 if (likely(HTTP_IS_TOKEN(*ptr)))
1622 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001623
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001624 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001625 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001626
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001627 if (likely(msg->err_pos < -1) || *ptr == '\n')
1628 goto http_msg_invalid;
1629
1630 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001631 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001632
1633 /* and we still accept this non-token character */
1634 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001635
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001636 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001637 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001638 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001639 if (likely(HTTP_IS_SPHT(*ptr)))
1640 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001641
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001643 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001644
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 if (likely(!HTTP_IS_CRLF(*ptr))) {
1646 goto http_msg_hdr_val;
1647 }
1648
1649 if (likely(*ptr == '\r'))
1650 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1651 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001652
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001654 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001655 EXPECT_LF_HERE(ptr, http_msg_invalid);
1656 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001657
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001659 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001660 if (likely(HTTP_IS_SPHT(*ptr))) {
1661 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001662 for (; buf->p + msg->sov < ptr; msg->sov++)
1663 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001664 goto http_msg_hdr_l1_sp;
1665 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001666 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001667 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 goto http_msg_complete_header;
1669
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001670 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001671 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001672 /* assumes msg->sol points to the first char, and msg->sov
1673 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 */
1675 if (likely(!HTTP_IS_CRLF(*ptr)))
1676 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001677
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001678 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 /* Note: we could also copy eol into ->eoh so that we have the
1680 * real header end in case it ends with lots of LWS, but is this
1681 * really needed ?
1682 */
1683 if (likely(*ptr == '\r'))
1684 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1685 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001686
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001687 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001688 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001689 EXPECT_LF_HERE(ptr, http_msg_invalid);
1690 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001691
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001693 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1695 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001696 for (; buf->p + msg->eol < ptr; msg->eol++)
1697 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001698 goto http_msg_hdr_val;
1699 }
1700 http_msg_complete_header:
1701 /*
1702 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001703 * Assumes msg->sol points to the first char, msg->sov points
1704 * to the first character of the value and msg->eol to the
1705 * first CR or LF so we know how the line ends. We insert last
1706 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001707 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001708 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001709 idx, idx->tail) < 0))
1710 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001711
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001712 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001713 if (likely(!HTTP_IS_CRLF(*ptr))) {
1714 goto http_msg_hdr_name;
1715 }
1716
1717 if (likely(*ptr == '\r'))
1718 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1719 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001720
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001722 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001723 /* Assumes msg->sol points to the first of either CR or LF.
1724 * Sets ->sov and ->next to the total header length, ->eoh to
1725 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1726 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001727 EXPECT_LF_HERE(ptr, http_msg_invalid);
1728 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001729 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001730 msg->eoh = msg->sol;
1731 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001732 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001733 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001734 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001735
1736 case HTTP_MSG_ERROR:
1737 /* this may only happen if we call http_msg_analyser() twice with an error */
1738 break;
1739
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001741#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001742 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1743 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001744#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001745 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 }
1747 http_msg_ood:
1748 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001749 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001750 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001752
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 http_msg_invalid:
1754 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001755 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001756 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001757 return;
1758}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001759
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001760/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1761 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1762 * nothing is done and 1 is returned.
1763 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001764static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001765{
1766 int delta;
1767 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001768 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001769
1770 if (msg->sl.rq.v_l != 0)
1771 return 1;
1772
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001773 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1774 if (txn->meth != HTTP_METH_GET)
1775 return 0;
1776
Willy Tarreau9b28e032012-10-12 23:49:43 +02001777 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001778 delta = 0;
1779
1780 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001781 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1782 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001783 }
1784 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001785 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001786 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001787 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001788 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001789 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001790 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001791 NULL, NULL);
1792 if (unlikely(!cur_end))
1793 return 0;
1794
1795 /* we have a full HTTP/1.0 request now and we know that
1796 * we have either a CR or an LF at <ptr>.
1797 */
1798 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1799 return 1;
1800}
1801
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001802/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001803 * and "keep-alive" values. If we already know that some headers may safely
1804 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001805 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1806 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001807 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001808 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1809 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1810 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001811 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001812 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001813void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001814{
Willy Tarreau5b154472009-12-21 20:11:07 +01001815 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001816 const char *hdr_val = "Connection";
1817 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001818
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001819 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001820 return;
1821
Willy Tarreau88d349d2010-01-25 12:15:43 +01001822 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1823 hdr_val = "Proxy-Connection";
1824 hdr_len = 16;
1825 }
1826
Willy Tarreau5b154472009-12-21 20:11:07 +01001827 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001828 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001829 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001830 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1831 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001832 if (to_del & 2)
1833 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001834 else
1835 txn->flags |= TX_CON_KAL_SET;
1836 }
1837 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1838 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001839 if (to_del & 1)
1840 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001841 else
1842 txn->flags |= TX_CON_CLO_SET;
1843 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001844 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1845 txn->flags |= TX_HDR_CONN_UPG;
1846 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001847 }
1848
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001849 txn->flags |= TX_HDR_CONN_PRS;
1850 return;
1851}
Willy Tarreau5b154472009-12-21 20:11:07 +01001852
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001853/* Apply desired changes on the Connection: header. Values may be removed and/or
1854 * added depending on the <wanted> flags, which are exclusively composed of
1855 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1856 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1857 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001858void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001859{
1860 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001861 const char *hdr_val = "Connection";
1862 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001863
1864 ctx.idx = 0;
1865
Willy Tarreau88d349d2010-01-25 12:15:43 +01001866
1867 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1868 hdr_val = "Proxy-Connection";
1869 hdr_len = 16;
1870 }
1871
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001872 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001873 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001874 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1875 if (wanted & TX_CON_KAL_SET)
1876 txn->flags |= TX_CON_KAL_SET;
1877 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001878 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001879 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001880 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1881 if (wanted & TX_CON_CLO_SET)
1882 txn->flags |= TX_CON_CLO_SET;
1883 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001884 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001885 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001886 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001887
1888 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1889 return;
1890
1891 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1892 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001893 hdr_val = "Connection: close";
1894 hdr_len = 17;
1895 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1896 hdr_val = "Proxy-Connection: close";
1897 hdr_len = 23;
1898 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001899 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001900 }
1901
1902 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1903 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001904 hdr_val = "Connection: keep-alive";
1905 hdr_len = 22;
1906 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1907 hdr_val = "Proxy-Connection: keep-alive";
1908 hdr_len = 28;
1909 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001910 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001911 }
1912 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001913}
1914
Willy Tarreauc24715e2014-04-17 15:21:20 +02001915/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1916 * the first byte of data after the chunk size, so that we know we can forward
1917 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1918 * the chunk size. That way it is always possible to differentiate between the
1919 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001920 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001922 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001923static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001924{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001925 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001926 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001927 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001928 const char *end = buf->data + buf->size;
1929 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001930 unsigned int chunk = 0;
1931
1932 /* The chunk size is in the following form, though we are only
1933 * interested in the size and CRLF :
1934 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1935 */
1936 while (1) {
1937 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001938 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001939 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001941 if (c < 0) /* not a hex digit anymore */
1942 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001943 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001944 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001945 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001946 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001947 chunk = (chunk << 4) + c;
1948 }
1949
Willy Tarreaud98cf932009-12-27 22:54:55 +01001950 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001951 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001952 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953
1954 while (http_is_spht[(unsigned char)*ptr]) {
1955 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001956 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001957 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001958 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001959 }
1960
Willy Tarreaud98cf932009-12-27 22:54:55 +01001961 /* Up to there, we know that at least one byte is present at *ptr. Check
1962 * for the end of chunk size.
1963 */
1964 while (1) {
1965 if (likely(HTTP_IS_CRLF(*ptr))) {
1966 /* we now have a CR or an LF at ptr */
1967 if (likely(*ptr == '\r')) {
1968 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001969 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001970 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001971 return 0;
1972 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001973
Willy Tarreaud98cf932009-12-27 22:54:55 +01001974 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001975 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001976 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001977 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001978 /* done */
1979 break;
1980 }
1981 else if (*ptr == ';') {
1982 /* chunk extension, ends at next CRLF */
1983 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001984 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001985 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001986 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001987
1988 while (!HTTP_IS_CRLF(*ptr)) {
1989 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001990 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001991 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001992 return 0;
1993 }
1994 /* we have a CRLF now, loop above */
1995 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001996 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001997 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001998 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001999 }
2000
Willy Tarreaud98cf932009-12-27 22:54:55 +01002001 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002002 * which may or may not be present. We save that into ->next,
2003 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002004 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002005 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002006 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002007 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002008 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002009 msg->chunk_len = chunk;
2010 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002011 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002012 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002013 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002014 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002015 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002016}
2017
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002018/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002019 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002020 * the trailers is found, it is automatically scheduled to be forwarded,
2021 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2022 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002023 * except maybe msg->next if it could parse some lines, and returns zero.
2024 * If a parse error is encountered, the function returns < 0 and does not
2025 * change anything except maybe msg->next. Note that the message must
2026 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002027 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002028 * forwarding, and that msg->next exactly matches the length of trailers
2029 * already parsed and not forwarded. It is also important to note that this
2030 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002031 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002032static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002033{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002034 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002035
Willy Tarreaua458b672012-03-05 11:17:50 +01002036 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002037 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002038 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002039 const char *ptr = b_ptr(buf, msg->next);
2040 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002041 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002042
2043 /* scan current line and stop at LF or CRLF */
2044 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002045 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002046 return 0;
2047
2048 if (*ptr == '\n') {
2049 if (!p1)
2050 p1 = ptr;
2051 p2 = ptr;
2052 break;
2053 }
2054
2055 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002056 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002057 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002058 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002059 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002060 p1 = ptr;
2061 }
2062
2063 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002064 if (ptr >= buf->data + buf->size)
2065 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002066 }
2067
2068 /* after LF; point to beginning of next line */
2069 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002070 if (p2 >= buf->data + buf->size)
2071 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002072
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002073 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002074 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002075 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002076
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002077 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002078 /* LF/CRLF at beginning of line => end of trailers at p2.
2079 * Everything was scheduled for forwarding, there's nothing
2080 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002081 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002082 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002083 msg->msg_state = HTTP_MSG_DONE;
2084 return 1;
2085 }
2086 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002087 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002088 }
2089}
2090
Willy Tarreauc24715e2014-04-17 15:21:20 +02002091/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2092 * or a possible LF alone at the end of a chunk. It automatically adjusts
2093 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002094 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002095 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2096 * not enough data are available, the function does not change anything and
2097 * returns zero. If a parse error is encountered, the function returns < 0 and
2098 * does not change anything. Note: this function is designed to parse wrapped
2099 * CRLF at the end of the buffer.
2100 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002101static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002102{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002103 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002104 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002105 int bytes;
2106
2107 /* NB: we'll check data availabilty at the end. It's not a
2108 * problem because whatever we match first will be checked
2109 * against the correct length.
2110 */
2111 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002112 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002113 if (*ptr == '\r') {
2114 bytes++;
2115 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002116 if (ptr >= buf->data + buf->size)
2117 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002118 }
2119
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002120 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002121 return 0;
2122
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002123 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002124 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002125 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002126 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002127
2128 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002129 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002130 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002131 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002132 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002133 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2134 return 1;
2135}
Willy Tarreau5b154472009-12-21 20:11:07 +01002136
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002137/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2138 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2139 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2140 * Unparsable qvalues return 1000 as "q=1.000".
2141 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002142int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002143{
2144 int q = 1000;
2145
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002146 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002147 goto out;
2148 q = (*qvalue++ - '0') * 1000;
2149
2150 if (*qvalue++ != '.')
2151 goto out;
2152
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002153 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002154 goto out;
2155 q += (*qvalue++ - '0') * 100;
2156
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002157 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002158 goto out;
2159 q += (*qvalue++ - '0') * 10;
2160
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002161 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002162 goto out;
2163 q += (*qvalue++ - '0') * 1;
2164 out:
2165 if (q > 1000)
2166 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002167 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002168 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002169 return q;
2170}
William Lallemand82fe75c2012-10-23 10:25:10 +02002171
2172/*
2173 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002174 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002175int select_compression_request_header(struct session *s, struct buffer *req)
2176{
2177 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002178 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002179 struct hdr_ctx ctx;
2180 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002181 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002182
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002183 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2184 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002185 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2186 */
2187 ctx.idx = 0;
2188 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2189 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002190 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2191 (ctx.vlen < 31 ||
2192 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2193 ctx.line[ctx.val + 30] < '6' ||
2194 (ctx.line[ctx.val + 30] == '6' &&
2195 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2196 s->comp_algo = NULL;
2197 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002198 }
2199
William Lallemand82fe75c2012-10-23 10:25:10 +02002200 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002201 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002202 int best_q = 0;
2203
William Lallemand82fe75c2012-10-23 10:25:10 +02002204 ctx.idx = 0;
2205 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002206 const char *qval;
2207 int q;
2208 int toklen;
2209
2210 /* try to isolate the token from the optional q-value */
2211 toklen = 0;
2212 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2213 toklen++;
2214
2215 qval = ctx.line + ctx.val + toklen;
2216 while (1) {
2217 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2218 qval++;
2219
2220 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2221 qval = NULL;
2222 break;
2223 }
2224 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002225
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002226 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2227 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002228
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002229 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2230 qval = NULL;
2231 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002232 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002233 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2234 break;
2235
2236 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2237 qval++;
2238 }
2239
2240 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002241 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002242
2243 if (q <= best_q)
2244 continue;
2245
2246 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2247 if (*(ctx.line + ctx.val) == '*' ||
2248 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2249 s->comp_algo = comp_algo;
2250 best_q = q;
2251 break;
2252 }
2253 }
2254 }
2255 }
2256
2257 /* remove all occurrences of the header when "compression offload" is set */
2258 if (s->comp_algo) {
2259 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2260 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2261 ctx.idx = 0;
2262 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2263 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002264 }
2265 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002266 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002267 }
2268
2269 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002270 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2271 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002272 if (comp_algo->add_data == identity_add_data) {
2273 s->comp_algo = comp_algo;
2274 return 1;
2275 }
2276 }
2277 }
2278
2279 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002280 return 0;
2281}
2282
2283/*
2284 * Selects a comression algorithm depending of the server response.
2285 */
2286int select_compression_response_header(struct session *s, struct buffer *res)
2287{
2288 struct http_txn *txn = &s->txn;
2289 struct http_msg *msg = &txn->rsp;
2290 struct hdr_ctx ctx;
2291 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002292
2293 /* no common compression algorithm was found in request header */
2294 if (s->comp_algo == NULL)
2295 goto fail;
2296
2297 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002298 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002299 goto fail;
2300
William Lallemandd3002612012-11-26 14:34:47 +01002301 /* 200 only */
2302 if (txn->status != 200)
2303 goto fail;
2304
William Lallemand82fe75c2012-10-23 10:25:10 +02002305 /* Content-Length is null */
2306 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2307 goto fail;
2308
2309 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002310 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002311 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2312 goto fail;
2313
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002314 /* no compression when Cache-Control: no-transform is present in the message */
2315 ctx.idx = 0;
2316 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2317 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2318 goto fail;
2319 }
2320
William Lallemand82fe75c2012-10-23 10:25:10 +02002321 comp_type = NULL;
2322
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002323 /* we don't want to compress multipart content-types, nor content-types that are
2324 * not listed in the "compression type" directive if any. If no content-type was
2325 * found but configuration requires one, we don't compress either. Backend has
2326 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002327 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002328 ctx.idx = 0;
2329 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2330 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2331 goto fail;
2332
2333 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2334 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002335 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002336 if (ctx.vlen >= comp_type->name_len &&
2337 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002338 /* this Content-Type should be compressed */
2339 break;
2340 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002341 /* this Content-Type should not be compressed */
2342 if (comp_type == NULL)
2343 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002344 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002345 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002346 else { /* no content-type header */
2347 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2348 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002349 }
2350
William Lallemandd85f9172012-11-09 17:05:39 +01002351 /* limit compression rate */
2352 if (global.comp_rate_lim > 0)
2353 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2354 goto fail;
2355
William Lallemand072a2bf2012-11-20 17:01:01 +01002356 /* limit cpu usage */
2357 if (idle_pct < compress_min_idle)
2358 goto fail;
2359
William Lallemand4c49fae2012-11-07 15:00:23 +01002360 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002361 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002362 goto fail;
2363
William Lallemandec3e3892012-11-12 17:02:18 +01002364 s->flags |= SN_COMP_READY;
2365
William Lallemand82fe75c2012-10-23 10:25:10 +02002366 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002367 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002368 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2369 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2370
2371 /* add Transfer-Encoding header */
2372 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2373 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2374
2375 /*
2376 * Add Content-Encoding header when it's not identity encoding.
2377 * RFC 2616 : Identity encoding: This content-coding is used only in the
2378 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2379 * header.
2380 */
2381 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002382 trash.len = 18;
2383 memcpy(trash.str, "Content-Encoding: ", trash.len);
2384 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2385 trash.len += s->comp_algo->name_len;
2386 trash.str[trash.len] = '\0';
2387 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002388 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002389 return 1;
2390
2391fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002392 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002393 return 0;
2394}
2395
Willy Tarreau2e47a3a2014-09-30 18:44:22 +02002396void http_adjust_conn_mode(struct session *s, struct http_txn *txn, struct http_msg *msg)
2397{
2398 int tmp = TX_CON_WANT_KAL;
2399
2400 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2401 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
2402 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2403 tmp = TX_CON_WANT_TUN;
2404
2405 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2406 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2407 tmp = TX_CON_WANT_TUN;
2408 }
2409
2410 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
2411 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2412 /* option httpclose + server_close => forceclose */
2413 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2414 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2415 tmp = TX_CON_WANT_CLO;
2416 else
2417 tmp = TX_CON_WANT_SCL;
2418 }
2419
2420 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
2421 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2422 tmp = TX_CON_WANT_CLO;
2423
2424 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2425 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2426
2427 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2428 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2429 /* parse the Connection header and possibly clean it */
2430 int to_del = 0;
2431 if ((msg->flags & HTTP_MSGF_VER_11) ||
2432 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2433 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
2434 to_del |= 2; /* remove "keep-alive" */
2435 if (!(msg->flags & HTTP_MSGF_VER_11))
2436 to_del |= 1; /* remove "close" */
2437 http_parse_connection_header(txn, msg, to_del);
2438 }
2439
2440 /* check if client or config asks for explicit close in KAL/SCL */
2441 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2442 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2443 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2444 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2445 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
2446 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
2447 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2448}
William Lallemand82fe75c2012-10-23 10:25:10 +02002449
Willy Tarreaud787e662009-07-07 10:14:51 +02002450/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2451 * processing can continue on next analysers, or zero if it either needs more
2452 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2453 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2454 * when it has nothing left to do, and may remove any analyser when it wants to
2455 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002456 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002457int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002458{
Willy Tarreau59234e92008-11-30 23:51:27 +01002459 /*
2460 * We will parse the partial (or complete) lines.
2461 * We will check the request syntax, and also join multi-line
2462 * headers. An index of all the lines will be elaborated while
2463 * parsing.
2464 *
2465 * For the parsing, we use a 28 states FSM.
2466 *
2467 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002468 * req->buf->p = beginning of request
2469 * req->buf->p + msg->eoh = end of processed headers / start of current one
2470 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002471 * msg->eol = end of current header or line (LF or CRLF)
2472 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002473 *
2474 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002475 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002476 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2477 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002478 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002479
Willy Tarreau59234e92008-11-30 23:51:27 +01002480 int cur_idx;
2481 struct http_txn *txn = &s->txn;
2482 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002483 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002484
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002485 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002486 now_ms, __FUNCTION__,
2487 s,
2488 req,
2489 req->rex, req->wex,
2490 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002491 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002492 req->analysers);
2493
Willy Tarreau52a0c602009-08-16 22:45:38 +02002494 /* we're speaking HTTP here, so let's speak HTTP to the client */
2495 s->srv_error = http_return_srv_error;
2496
Willy Tarreau83e3af02009-12-28 17:39:57 +01002497 /* There's a protected area at the end of the buffer for rewriting
2498 * purposes. We don't want to start to parse the request if the
2499 * protected area is affected, because we may have to move processed
2500 * data later, which is much more complicated.
2501 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002502 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002503 if (txn->flags & TX_NOT_FIRST) {
2504 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002505 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002506 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002507 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002508 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002509 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002510 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002511 return 0;
2512 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002513 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2514 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2515 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002516 }
2517
Willy Tarreau065e8332010-01-08 00:30:20 +01002518 /* Note that we have the same problem with the response ; we
2519 * may want to send a redirect, error or anything which requires
2520 * some spare space. So we'll ensure that we have at least
2521 * maxrewrite bytes available in the response buffer before
2522 * processing that one. This will only affect pipelined
2523 * keep-alive requests.
2524 */
2525 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002526 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002527 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2528 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2529 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002530 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002531 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002532 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002533 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002534 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002535 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002536 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002537 return 0;
2538 }
2539 }
2540
Willy Tarreau9b28e032012-10-12 23:49:43 +02002541 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002542 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002543 }
2544
Willy Tarreau59234e92008-11-30 23:51:27 +01002545 /* 1: we might have to print this header in debug mode */
2546 if (unlikely((global.mode & MODE_DEBUG) &&
2547 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau8767b132014-10-21 19:36:09 +02002548 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002549 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002550
Willy Tarreau9b28e032012-10-12 23:49:43 +02002551 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002552 /* this is a bit complex : in case of error on the request line,
2553 * we know that rq.l is still zero, so we display only the part
2554 * up to the end of the line (truncated by debug_hdr).
2555 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002556 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002557 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002558
Willy Tarreau59234e92008-11-30 23:51:27 +01002559 sol += hdr_idx_first_pos(&txn->hdr_idx);
2560 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002561
Willy Tarreau59234e92008-11-30 23:51:27 +01002562 while (cur_idx) {
2563 eol = sol + txn->hdr_idx.v[cur_idx].len;
2564 debug_hdr("clihdr", s, sol, eol);
2565 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2566 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002567 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002568 }
2569
Willy Tarreau58f10d72006-12-04 02:26:12 +01002570
Willy Tarreau59234e92008-11-30 23:51:27 +01002571 /*
2572 * Now we quickly check if we have found a full valid request.
2573 * If not so, we check the FD and buffer states before leaving.
2574 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002575 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002576 * requests are checked first. When waiting for a second request
2577 * on a keep-alive session, if we encounter and error, close, t/o,
2578 * we note the error in the session flags but don't set any state.
2579 * Since the error will be noted there, it will not be counted by
2580 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002581 * Last, we may increase some tracked counters' http request errors on
2582 * the cases that are deliberately the client's fault. For instance,
2583 * a timeout or connection reset is not counted as an error. However
2584 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002585 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002586
Willy Tarreau655dce92009-11-08 13:10:58 +01002587 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002588 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002589 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002590 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002591 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002592 session_inc_http_req_ctr(s);
2593 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002594 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002595 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002596 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002597
Willy Tarreau59234e92008-11-30 23:51:27 +01002598 /* 1: Since we are in header mode, if there's no space
2599 * left for headers, we won't be able to free more
2600 * later, so the session will never terminate. We
2601 * must terminate it now.
2602 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002603 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 /* FIXME: check if URI is set and return Status
2605 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002606 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002607 session_inc_http_req_ctr(s);
2608 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002609 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002610 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002611 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002612 goto return_bad_req;
2613 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002614
Willy Tarreau59234e92008-11-30 23:51:27 +01002615 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002616 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002617 if (!(s->flags & SN_ERR_MASK))
2618 s->flags |= SN_ERR_CLICL;
2619
Willy Tarreaufcffa692010-01-10 14:21:19 +01002620 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002621 goto failed_keep_alive;
2622
Willy Tarreau1c3a6122015-05-01 15:37:53 +02002623 if (s->fe->options & PR_O_IGNORE_PRB)
2624 goto failed_keep_alive;
2625
Willy Tarreau59234e92008-11-30 23:51:27 +01002626 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002627 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002628 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002629 session_inc_http_err_ctr(s);
2630 }
2631
Willy Tarreaudc979f22012-12-04 10:39:01 +01002632 txn->status = 400;
2633 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002634 msg->msg_state = HTTP_MSG_ERROR;
2635 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002636
Willy Tarreauda7ff642010-06-23 11:44:09 +02002637 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002638 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002639 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002640 if (s->listener->counters)
2641 s->listener->counters->failed_req++;
2642
Willy Tarreau59234e92008-11-30 23:51:27 +01002643 if (!(s->flags & SN_FINST_MASK))
2644 s->flags |= SN_FINST_R;
2645 return 0;
2646 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002647
Willy Tarreau59234e92008-11-30 23:51:27 +01002648 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002649 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002650 if (!(s->flags & SN_ERR_MASK))
2651 s->flags |= SN_ERR_CLITO;
2652
Willy Tarreaufcffa692010-01-10 14:21:19 +01002653 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002654 goto failed_keep_alive;
2655
Willy Tarreau1c3a6122015-05-01 15:37:53 +02002656 if (s->fe->options & PR_O_IGNORE_PRB)
2657 goto failed_keep_alive;
2658
Willy Tarreau59234e92008-11-30 23:51:27 +01002659 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002660 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002661 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002662 session_inc_http_err_ctr(s);
2663 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002664 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002665 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002666 msg->msg_state = HTTP_MSG_ERROR;
2667 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002668
Willy Tarreauda7ff642010-06-23 11:44:09 +02002669 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002670 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002671 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002672 if (s->listener->counters)
2673 s->listener->counters->failed_req++;
2674
Willy Tarreau59234e92008-11-30 23:51:27 +01002675 if (!(s->flags & SN_FINST_MASK))
2676 s->flags |= SN_FINST_R;
2677 return 0;
2678 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002679
Willy Tarreau59234e92008-11-30 23:51:27 +01002680 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002681 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002682 if (!(s->flags & SN_ERR_MASK))
2683 s->flags |= SN_ERR_CLICL;
2684
Willy Tarreaufcffa692010-01-10 14:21:19 +01002685 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002686 goto failed_keep_alive;
2687
Willy Tarreau1c3a6122015-05-01 15:37:53 +02002688 if (s->fe->options & PR_O_IGNORE_PRB)
2689 goto failed_keep_alive;
2690
Willy Tarreau4076a152009-04-02 15:18:36 +02002691 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002692 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002693 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002694 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002695 msg->msg_state = HTTP_MSG_ERROR;
2696 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002697
Willy Tarreauda7ff642010-06-23 11:44:09 +02002698 session_inc_http_err_ctr(s);
2699 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002700 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002701 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002702 if (s->listener->counters)
2703 s->listener->counters->failed_req++;
2704
Willy Tarreau59234e92008-11-30 23:51:27 +01002705 if (!(s->flags & SN_FINST_MASK))
2706 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002707 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002708 }
2709
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002710 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002711 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2712 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002713#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002714 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 +01002715 /* We need more data, we have to re-enable quick-ack in case we
2716 * previously disabled it, otherwise we might cause the client
2717 * to delay next data.
2718 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002719 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002720 }
2721#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002722
Willy Tarreaufcffa692010-01-10 14:21:19 +01002723 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2724 /* If the client starts to talk, let's fall back to
2725 * request timeout processing.
2726 */
2727 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002728 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002729 }
2730
Willy Tarreau59234e92008-11-30 23:51:27 +01002731 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002732 if (!tick_isset(req->analyse_exp)) {
2733 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2734 (txn->flags & TX_WAIT_NEXT_RQ) &&
2735 tick_isset(s->be->timeout.httpka))
2736 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2737 else
2738 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2739 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002740
Willy Tarreau59234e92008-11-30 23:51:27 +01002741 /* we're not ready yet */
2742 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002743
2744 failed_keep_alive:
2745 /* Here we process low-level errors for keep-alive requests. In
2746 * short, if the request is not the first one and it experiences
2747 * a timeout, read error or shutdown, we just silently close so
2748 * that the client can try again.
2749 */
2750 txn->status = 0;
2751 msg->msg_state = HTTP_MSG_RQBEFORE;
2752 req->analysers = 0;
2753 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002754 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002755 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002756 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002757 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002758 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002759
Willy Tarreaud787e662009-07-07 10:14:51 +02002760 /* OK now we have a complete HTTP request with indexed headers. Let's
2761 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002762 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002763 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002764 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002765 * byte after the last LF. msg->sov points to the first byte of data.
2766 * msg->eol cannot be trusted because it may have been left uninitialized
2767 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002768 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002769
Willy Tarreauda7ff642010-06-23 11:44:09 +02002770 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002771 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2772
Willy Tarreaub16a5742010-01-10 14:46:16 +01002773 if (txn->flags & TX_WAIT_NEXT_RQ) {
2774 /* kill the pending keep-alive timeout */
2775 txn->flags &= ~TX_WAIT_NEXT_RQ;
2776 req->analyse_exp = TICK_ETERNITY;
2777 }
2778
2779
Willy Tarreaud787e662009-07-07 10:14:51 +02002780 /* Maybe we found in invalid header name while we were configured not
2781 * to block on that, so we have to capture it now.
2782 */
2783 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002784 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002785
Willy Tarreau59234e92008-11-30 23:51:27 +01002786 /*
2787 * 1: identify the method
2788 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002789 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002790
2791 /* we can make use of server redirect on GET and HEAD */
2792 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2793 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002794
Willy Tarreau59234e92008-11-30 23:51:27 +01002795 /*
2796 * 2: check if the URI matches the monitor_uri.
2797 * We have to do this for every request which gets in, because
2798 * the monitor-uri is defined by the frontend.
2799 */
2800 if (unlikely((s->fe->monitor_uri_len != 0) &&
2801 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002802 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002803 s->fe->monitor_uri,
2804 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002805 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002806 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002807 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002808 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002809
Willy Tarreau59234e92008-11-30 23:51:27 +01002810 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002811 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002812
Willy Tarreau59234e92008-11-30 23:51:27 +01002813 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002814 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002815 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002816
Willy Tarreau59234e92008-11-30 23:51:27 +01002817 ret = acl_pass(ret);
2818 if (cond->pol == ACL_COND_UNLESS)
2819 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002820
Willy Tarreau59234e92008-11-30 23:51:27 +01002821 if (ret) {
2822 /* we fail this request, let's return 503 service unavail */
2823 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002824 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002825 if (!(s->flags & SN_ERR_MASK))
2826 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002827 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002828 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002829 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002830
Willy Tarreau59234e92008-11-30 23:51:27 +01002831 /* nothing to fail, let's reply normaly */
2832 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002833 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002834 if (!(s->flags & SN_ERR_MASK))
2835 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002836 goto return_prx_cond;
2837 }
2838
2839 /*
2840 * 3: Maybe we have to copy the original REQURI for the logs ?
2841 * Note: we cannot log anymore if the request has been
2842 * classified as invalid.
2843 */
2844 if (unlikely(s->logs.logwait & LW_REQ)) {
2845 /* we have a complete HTTP request that we must log */
2846 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2847 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002848
Willy Tarreau59234e92008-11-30 23:51:27 +01002849 if (urilen >= REQURI_LEN)
2850 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002851 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002852 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002853
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002854 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002855 s->do_log(s);
2856 } else {
2857 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002858 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002859 }
Willy Tarreau06619262006-12-17 08:37:22 +01002860
Willy Tarreau59234e92008-11-30 23:51:27 +01002861 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002862 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002863 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002864
Willy Tarreau55645552015-05-01 13:26:00 +02002865 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2866 * exactly one digit "." one digit. This check may be disabled using
2867 * option accept-invalid-http-request.
2868 */
2869 if (!(s->fe->options2 & PR_O2_REQBUG_OK)) {
2870 if (msg->sl.rq.v_l != 8) {
2871 msg->err_pos = msg->sl.rq.v;
2872 goto return_bad_req;
2873 }
2874
2875 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2876 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2877 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2878 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2879 msg->err_pos = msg->sl.rq.v + 4;
2880 goto return_bad_req;
2881 }
2882 }
2883
Willy Tarreau5b154472009-12-21 20:11:07 +01002884 /* ... and check if the request is HTTP/1.1 or above */
2885 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002886 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2887 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2888 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002889 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002890
2891 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002892 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 +01002893
Willy Tarreau88d349d2010-01-25 12:15:43 +01002894 /* if the frontend has "option http-use-proxy-header", we'll check if
2895 * we have what looks like a proxied connection instead of a connection,
2896 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2897 * Note that this is *not* RFC-compliant, however browsers and proxies
2898 * happen to do that despite being non-standard :-(
2899 * We consider that a request not beginning with either '/' or '*' is
2900 * a proxied connection, which covers both "scheme://location" and
2901 * CONNECT ip:port.
2902 */
2903 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002904 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002905 txn->flags |= TX_USE_PX_CONN;
2906
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002907 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002908 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002909
Willy Tarreau59234e92008-11-30 23:51:27 +01002910 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002911 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002912 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002913 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002914
Willy Tarreau4db603d2015-05-01 10:05:17 +02002915 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2916 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002917 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002918 * The length of a message body is determined by one of the following
2919 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002920 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002921 * 1. Any response to a HEAD request and any response with a 1xx
2922 * (Informational), 204 (No Content), or 304 (Not Modified) status
2923 * code is always terminated by the first empty line after the
2924 * header fields, regardless of the header fields present in the
2925 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002926 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002927 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2928 * the connection will become a tunnel immediately after the empty
2929 * line that concludes the header fields. A client MUST ignore any
2930 * Content-Length or Transfer-Encoding header fields received in
2931 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002932 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002933 * 3. If a Transfer-Encoding header field is present and the chunked
2934 * transfer coding (Section 4.1) is the final encoding, the message
2935 * body length is determined by reading and decoding the chunked
2936 * data until the transfer coding indicates the data is complete.
2937 *
2938 * If a Transfer-Encoding header field is present in a response and
2939 * the chunked transfer coding is not the final encoding, the
2940 * message body length is determined by reading the connection until
2941 * it is closed by the server. If a Transfer-Encoding header field
2942 * is present in a request and the chunked transfer coding is not
2943 * the final encoding, the message body length cannot be determined
2944 * reliably; the server MUST respond with the 400 (Bad Request)
2945 * status code and then close the connection.
2946 *
2947 * If a message is received with both a Transfer-Encoding and a
2948 * Content-Length header field, the Transfer-Encoding overrides the
2949 * Content-Length. Such a message might indicate an attempt to
2950 * perform request smuggling (Section 9.5) or response splitting
2951 * (Section 9.4) and ought to be handled as an error. A sender MUST
2952 * remove the received Content-Length field prior to forwarding such
2953 * a message downstream.
2954 *
2955 * 4. If a message is received without Transfer-Encoding and with
2956 * either multiple Content-Length header fields having differing
2957 * field-values or a single Content-Length header field having an
2958 * invalid value, then the message framing is invalid and the
2959 * recipient MUST treat it as an unrecoverable error. If this is a
2960 * request message, the server MUST respond with a 400 (Bad Request)
2961 * status code and then close the connection. If this is a response
2962 * message received by a proxy, the proxy MUST close the connection
2963 * to the server, discard the received response, and send a 502 (Bad
2964 * Gateway) response to the client. If this is a response message
2965 * received by a user agent, the user agent MUST close the
2966 * connection to the server and discard the received response.
2967 *
2968 * 5. If a valid Content-Length header field is present without
2969 * Transfer-Encoding, its decimal value defines the expected message
2970 * body length in octets. If the sender closes the connection or
2971 * the recipient times out before the indicated number of octets are
2972 * received, the recipient MUST consider the message to be
2973 * incomplete and close the connection.
2974 *
2975 * 6. If this is a request message and none of the above are true, then
2976 * the message body length is zero (no message body is present).
2977 *
2978 * 7. Otherwise, this is a response message without a declared message
2979 * body length, so the message body length is determined by the
2980 * number of octets received prior to the server closing the
2981 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002982 */
2983
Willy Tarreau32b47f42009-10-18 20:55:02 +02002984 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002985 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaue77bc172015-05-01 10:06:30 +02002986 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002987 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002988 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2989 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau49efa262015-05-01 10:09:49 +02002990 /* chunked not last, return badreq */
2991 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002992 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002993 }
2994
Willy Tarreaudfa3d922015-04-30 10:57:51 +02002995 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002996 ctx.idx = 0;
Willy Tarreaudfa3d922015-04-30 10:57:51 +02002997 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2998 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
2999 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3000 }
Willy Tarreau49efa262015-05-01 10:09:49 +02003001 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003002 signed long long cl;
3003
Willy Tarreauad14f752011-09-02 20:33:27 +02003004 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003005 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003006 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003007 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003008
Willy Tarreauad14f752011-09-02 20:33:27 +02003009 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003010 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003011 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003012 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003013
Willy Tarreauad14f752011-09-02 20:33:27 +02003014 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003015 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003016 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003017 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003018
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003019 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003020 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003021 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003022 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003023
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003024 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003025 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003026 }
3027
Willy Tarreau49efa262015-05-01 10:09:49 +02003028 /* even bodyless requests have a known length */
3029 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003030
Willy Tarreau179085c2014-04-28 16:48:56 +02003031 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3032 * only change if both the request and the config reference something else.
3033 * Option httpclose by itself sets tunnel mode where headers are mangled.
3034 * However, if another mode is set, it will affect it (eg: server-close/
3035 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3036 * if FE and BE have the same settings (common). The method consists in
3037 * checking if options changed between the two calls (implying that either
3038 * one is non-null, or one of them is non-null and we are there for the first
3039 * time.
3040 */
3041 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau2e47a3a2014-09-30 18:44:22 +02003042 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
3043 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003044
Willy Tarreaud787e662009-07-07 10:14:51 +02003045 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003046 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003047 req->analyse_exp = TICK_ETERNITY;
3048 return 1;
3049
3050 return_bad_req:
3051 /* We centralize bad requests processing here */
3052 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3053 /* we detected a parsing error. We want to archive this request
3054 * in the dedicated proxy area for later troubleshooting.
3055 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003056 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003057 }
3058
3059 txn->req.msg_state = HTTP_MSG_ERROR;
3060 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003061 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003062
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003063 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003064 if (s->listener->counters)
3065 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003066
3067 return_prx_cond:
3068 if (!(s->flags & SN_ERR_MASK))
3069 s->flags |= SN_ERR_PRXCOND;
3070 if (!(s->flags & SN_FINST_MASK))
3071 s->flags |= SN_FINST_R;
3072
3073 req->analysers = 0;
3074 req->analyse_exp = TICK_ETERNITY;
3075 return 0;
3076}
3077
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003078
Willy Tarreau347a35d2013-11-22 17:51:09 +01003079/* This function prepares an applet to handle the stats. It can deal with the
3080 * "100-continue" expectation, check that admin rules are met for POST requests,
3081 * and program a response message if something was unexpected. It cannot fail
3082 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003083 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003084 * s->target which is supposed to already point to the stats applet. The caller
3085 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003086 */
3087int http_handle_stats(struct session *s, struct channel *req)
3088{
3089 struct stats_admin_rule *stats_admin_rule;
3090 struct stream_interface *si = s->rep->prod;
3091 struct http_txn *txn = &s->txn;
3092 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003093 struct uri_auth *uri_auth = s->be->uri_auth;
3094 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003095 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003096
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003097 appctx = si_appctx(si);
3098 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3099 appctx->st1 = appctx->st2 = 0;
3100 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3101 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003102 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3103 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003104
3105 uri = msg->chn->buf->p + msg->sl.rq.u;
3106 lookup = uri + uri_auth->uri_len;
3107
3108 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3109 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003110 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003111 break;
3112 }
3113 }
3114
3115 if (uri_auth->refresh) {
3116 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3117 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003118 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003119 break;
3120 }
3121 }
3122 }
3123
3124 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3125 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003126 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003127 break;
3128 }
3129 }
3130
3131 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3132 if (memcmp(h, ";st=", 4) == 0) {
3133 int i;
3134 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003135 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003136 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3137 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003138 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003139 break;
3140 }
3141 }
3142 break;
3143 }
3144 }
3145
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003146 appctx->ctx.stats.scope_str = 0;
3147 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003148 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3149 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3150 int itx = 0;
3151 const char *h2;
3152 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3153 const char *err;
3154
3155 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3156 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003157 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003158 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3159 itx++;
3160 h++;
3161 }
3162
3163 if (itx > STAT_SCOPE_TXT_MAXLEN)
3164 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003165 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003166
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003167 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003168 memcpy(scope_txt, h2, itx);
3169 scope_txt[itx] = '\0';
3170 err = invalid_char(scope_txt);
3171 if (err) {
3172 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003173 appctx->ctx.stats.scope_str = 0;
3174 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003175 }
3176 break;
3177 }
3178 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003179
3180 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003181 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003182 int ret = 1;
3183
3184 if (stats_admin_rule->cond) {
3185 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3186 ret = acl_pass(ret);
3187 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3188 ret = !ret;
3189 }
3190
3191 if (ret) {
3192 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003193 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003194 break;
3195 }
3196 }
3197
3198 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003199 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003200 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003201 /* we'll need the request body, possibly after sending 100-continue */
3202 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003203 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003204 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003205 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003206 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3207 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003208 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003209 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003210 else {
3211 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003212 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003213 }
3214
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003215 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003216 return 1;
3217}
3218
Lukas Tribus67db8df2013-06-23 17:37:13 +02003219/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3220 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3221 */
3222static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3223{
3224#ifdef IP_TOS
3225 if (from.ss_family == AF_INET)
3226 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3227#endif
3228#ifdef IPV6_TCLASS
3229 if (from.ss_family == AF_INET6) {
3230 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3231 /* v4-mapped addresses need IP_TOS */
3232 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3233 else
3234 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3235 }
3236#endif
3237}
3238
Sasha Pachev218f0642014-06-16 12:05:59 -06003239static int http_transform_header(struct session* s, struct http_msg *msg, const char* name, uint name_len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003240 char* buf, struct hdr_idx* idx, struct list *fmt, struct my_regex *re,
Sasha Pachev218f0642014-06-16 12:05:59 -06003241 struct hdr_ctx* ctx, int action)
3242{
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003243 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3244 struct hdr_idx *idx, struct hdr_ctx *ctx);
3245 struct chunk *replace = get_trash_chunk();
3246 struct chunk *output = get_trash_chunk();
3247
3248 replace->len = build_logline(s, replace->str, replace->size, fmt);
3249 if (replace->len >= replace->size - 1)
3250 return -1;
3251
Sasha Pachev218f0642014-06-16 12:05:59 -06003252 ctx->idx = 0;
3253
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003254 /* Choose the header browsing function. */
3255 switch (action) {
3256 case HTTP_REQ_ACT_REPLACE_VAL:
3257 case HTTP_RES_ACT_REPLACE_VAL:
3258 http_find_hdr_func = http_find_header2;
3259 break;
3260 case HTTP_REQ_ACT_REPLACE_HDR:
3261 case HTTP_RES_ACT_REPLACE_HDR:
3262 http_find_hdr_func = http_find_full_header2;
3263 break;
3264 default: /* impossible */
3265 return -1;
3266 }
3267
3268 while (http_find_hdr_func(name, name_len, buf, idx, ctx)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06003269 struct hdr_idx_elem *hdr = idx->v + ctx->idx;
3270 int delta;
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003271 char *val = ctx->line + ctx->val;
3272 char* val_end = val + ctx->vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003273
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003274 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch))
3275 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003276
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003277 output->len = exp_replace(output->str, output->size, val, replace->str, pmatch);
3278 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003279 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003280
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003281 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003282
3283 hdr->len += delta;
3284 http_msg_move_end(msg, delta);
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003285
3286 /* Adjust the length of the current value of the index. */
3287 ctx->vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003288 }
3289
3290 return 0;
3291}
3292
Willy Tarreau20b0de52012-12-24 15:45:22 +01003293/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003294 * transaction <txn>. Returns the verdict of the first rule that prevents
3295 * further processing of the request (auth, deny, ...), and defaults to
3296 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3297 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3298 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003299 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003300enum rule_result
Willy Tarreau96257ec2012-12-27 10:46:37 +01003301http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003302{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003303 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003304 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003305 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003306 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003307
Willy Tarreauff011f22011-01-06 17:51:27 +01003308 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003309 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003310 continue;
3311
Willy Tarreau96257ec2012-12-27 10:46:37 +01003312 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003313 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003314 int ret;
3315
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003316 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003317 ret = acl_pass(ret);
3318
Willy Tarreauff011f22011-01-06 17:51:27 +01003319 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003320 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003321
3322 if (!ret) /* condition not matched */
3323 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003324 }
3325
Willy Tarreau20b0de52012-12-24 15:45:22 +01003326
Willy Tarreau96257ec2012-12-27 10:46:37 +01003327 switch (rule->action) {
3328 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003329 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003330
3331 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003332 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003333
Willy Tarreauccbcc372012-12-27 12:37:57 +01003334 case HTTP_REQ_ACT_TARPIT:
3335 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003336 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003337
Willy Tarreau96257ec2012-12-27 10:46:37 +01003338 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003339 /* Auth might be performed on regular http-req rules as well as on stats */
3340 auth_realm = rule->arg.auth.realm;
3341 if (!auth_realm) {
3342 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3343 auth_realm = STATS_DEFAULT_REALM;
3344 else
3345 auth_realm = px->id;
3346 }
3347 /* send 401/407 depending on whether we use a proxy or not. We still
3348 * count one error, because normal browsing won't significantly
3349 * increase the counter but brute force attempts will.
3350 */
3351 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3352 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3353 stream_int_retnclose(&s->si[0], &trash);
3354 session_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003355 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003356
Willy Tarreau81499eb2012-12-27 12:19:02 +01003357 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003358 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3359 return HTTP_RULE_RES_BADREQ;
3360 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003361
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003362 case HTTP_REQ_ACT_SET_NICE:
3363 s->task->nice = rule->arg.nice;
3364 break;
3365
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003366 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003367 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003368 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003369 break;
3370
Willy Tarreau51347ed2013-06-11 19:34:13 +02003371 case HTTP_REQ_ACT_SET_MARK:
3372#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003373 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003374 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003375#endif
3376 break;
3377
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003378 case HTTP_REQ_ACT_SET_LOGL:
3379 s->logs.level = rule->arg.loglevel;
3380 break;
3381
Sasha Pachev218f0642014-06-16 12:05:59 -06003382 case HTTP_REQ_ACT_REPLACE_HDR:
3383 case HTTP_REQ_ACT_REPLACE_VAL:
3384 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3385 txn->req.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003386 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003387 return HTTP_RULE_RES_BADREQ;
3388 break;
3389
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003390 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003391 ctx.idx = 0;
3392 /* remove all occurrences of the header */
3393 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3394 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3395 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003396 }
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003397 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003398
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003399 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003400 case HTTP_REQ_ACT_ADD_HDR:
3401 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3402 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3403 trash.len = rule->arg.hdr_add.name_len;
3404 trash.str[trash.len++] = ':';
3405 trash.str[trash.len++] = ' ';
3406 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 +01003407
3408 if (rule->action == HTTP_REQ_ACT_SET_HDR) {
3409 /* remove all occurrences of the header */
3410 ctx.idx = 0;
3411 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3412 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3413 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3414 }
3415 }
3416
Willy Tarreau96257ec2012-12-27 10:46:37 +01003417 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3418 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003419
3420 case HTTP_REQ_ACT_DEL_ACL:
3421 case HTTP_REQ_ACT_DEL_MAP: {
3422 struct pat_ref *ref;
3423 char *key;
3424 int len;
3425
3426 /* collect reference */
3427 ref = pat_ref_lookup(rule->arg.map.ref);
3428 if (!ref)
3429 continue;
3430
3431 /* collect key */
3432 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3433 key = trash.str;
3434 key[len] = '\0';
3435
3436 /* perform update */
3437 /* returned code: 1=ok, 0=ko */
3438 pat_ref_delete(ref, key);
3439
3440 break;
3441 }
3442
3443 case HTTP_REQ_ACT_ADD_ACL: {
3444 struct pat_ref *ref;
3445 char *key;
3446 struct chunk *trash_key;
3447 int len;
3448
3449 trash_key = get_trash_chunk();
3450
3451 /* collect reference */
3452 ref = pat_ref_lookup(rule->arg.map.ref);
3453 if (!ref)
3454 continue;
3455
3456 /* collect key */
3457 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3458 key = trash_key->str;
3459 key[len] = '\0';
3460
3461 /* perform update */
3462 /* add entry only if it does not already exist */
3463 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003464 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003465
3466 break;
3467 }
3468
3469 case HTTP_REQ_ACT_SET_MAP: {
3470 struct pat_ref *ref;
3471 char *key, *value;
3472 struct chunk *trash_key, *trash_value;
3473 int len;
3474
3475 trash_key = get_trash_chunk();
3476 trash_value = get_trash_chunk();
3477
3478 /* collect reference */
3479 ref = pat_ref_lookup(rule->arg.map.ref);
3480 if (!ref)
3481 continue;
3482
3483 /* collect key */
3484 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3485 key = trash_key->str;
3486 key[len] = '\0';
3487
3488 /* collect value */
3489 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3490 value = trash_value->str;
3491 value[len] = '\0';
3492
3493 /* perform update */
3494 if (pat_ref_find_elt(ref, key) != NULL)
3495 /* update entry if it exists */
3496 pat_ref_set(ref, key, value, NULL);
3497 else
3498 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003499 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003500
3501 break;
3502 }
William Lallemand73025dd2014-04-24 14:38:37 +02003503
3504 case HTTP_REQ_ACT_CUSTOM_CONT:
3505 rule->action_ptr(rule, px, s, txn);
3506 break;
3507
3508 case HTTP_REQ_ACT_CUSTOM_STOP:
3509 rule->action_ptr(rule, px, s, txn);
Willy Tarreau0b748332014-04-29 00:13:29 +02003510 return HTTP_RULE_RES_DONE;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003511 }
3512 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003513
3514 /* we reached the end of the rules, nothing to report */
Willy Tarreau0b748332014-04-29 00:13:29 +02003515 return HTTP_RULE_RES_CONT;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003516}
3517
Willy Tarreau71241ab2012-12-27 11:30:54 +01003518
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003519/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3520 * transaction <txn>. Returns the first rule that prevents further processing
3521 * of the response (deny, ...) or NULL if it executed all rules or stopped
3522 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3523 * rule.
3524 */
3525static struct http_res_rule *
3526http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3527{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003528 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003529 struct http_res_rule *rule;
3530 struct hdr_ctx ctx;
3531
3532 list_for_each_entry(rule, rules, list) {
3533 if (rule->action >= HTTP_RES_ACT_MAX)
3534 continue;
3535
3536 /* check optional condition */
3537 if (rule->cond) {
3538 int ret;
3539
3540 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3541 ret = acl_pass(ret);
3542
3543 if (rule->cond->pol == ACL_COND_UNLESS)
3544 ret = !ret;
3545
3546 if (!ret) /* condition not matched */
3547 continue;
3548 }
3549
3550
3551 switch (rule->action) {
3552 case HTTP_RES_ACT_ALLOW:
3553 return NULL; /* "allow" rules are OK */
3554
3555 case HTTP_RES_ACT_DENY:
3556 txn->flags |= TX_SVDENY;
3557 return rule;
3558
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003559 case HTTP_RES_ACT_SET_NICE:
3560 s->task->nice = rule->arg.nice;
3561 break;
3562
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003563 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003564 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003565 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003566 break;
3567
Willy Tarreau51347ed2013-06-11 19:34:13 +02003568 case HTTP_RES_ACT_SET_MARK:
3569#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003570 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003571 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003572#endif
3573 break;
3574
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003575 case HTTP_RES_ACT_SET_LOGL:
3576 s->logs.level = rule->arg.loglevel;
3577 break;
3578
Sasha Pachev218f0642014-06-16 12:05:59 -06003579 case HTTP_RES_ACT_REPLACE_HDR:
3580 case HTTP_RES_ACT_REPLACE_VAL:
3581 if (http_transform_header(s, &txn->rsp, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3582 txn->rsp.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003583 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003584 return NULL; /* note: we should report an error here */
3585 break;
3586
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003587 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003588 ctx.idx = 0;
3589 /* remove all occurrences of the header */
3590 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3591 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3592 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3593 }
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003594 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003595
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003596 case HTTP_RES_ACT_SET_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003597 case HTTP_RES_ACT_ADD_HDR:
3598 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3599 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3600 trash.len = rule->arg.hdr_add.name_len;
3601 trash.str[trash.len++] = ':';
3602 trash.str[trash.len++] = ' ';
3603 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 +01003604
3605 if (rule->action == HTTP_RES_ACT_SET_HDR) {
3606 /* remove all occurrences of the header */
3607 ctx.idx = 0;
3608 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3609 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3610 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3611 }
3612 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003613 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3614 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003615
3616 case HTTP_RES_ACT_DEL_ACL:
3617 case HTTP_RES_ACT_DEL_MAP: {
3618 struct pat_ref *ref;
3619 char *key;
3620 int len;
3621
3622 /* collect reference */
3623 ref = pat_ref_lookup(rule->arg.map.ref);
3624 if (!ref)
3625 continue;
3626
3627 /* collect key */
3628 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3629 key = trash.str;
3630 key[len] = '\0';
3631
3632 /* perform update */
3633 /* returned code: 1=ok, 0=ko */
3634 pat_ref_delete(ref, key);
3635
3636 break;
3637 }
3638
3639 case HTTP_RES_ACT_ADD_ACL: {
3640 struct pat_ref *ref;
3641 char *key;
3642 struct chunk *trash_key;
3643 int len;
3644
3645 trash_key = get_trash_chunk();
3646
3647 /* collect reference */
3648 ref = pat_ref_lookup(rule->arg.map.ref);
3649 if (!ref)
3650 continue;
3651
3652 /* collect key */
3653 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3654 key = trash_key->str;
3655 key[len] = '\0';
3656
3657 /* perform update */
3658 /* check if the entry already exists */
3659 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003660 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003661
3662 break;
3663 }
3664
3665 case HTTP_RES_ACT_SET_MAP: {
3666 struct pat_ref *ref;
3667 char *key, *value;
3668 struct chunk *trash_key, *trash_value;
3669 int len;
3670
3671 trash_key = get_trash_chunk();
3672 trash_value = get_trash_chunk();
3673
3674 /* collect reference */
3675 ref = pat_ref_lookup(rule->arg.map.ref);
3676 if (!ref)
3677 continue;
3678
3679 /* collect key */
3680 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3681 key = trash_key->str;
3682 key[len] = '\0';
3683
3684 /* collect value */
3685 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3686 value = trash_value->str;
3687 value[len] = '\0';
3688
3689 /* perform update */
3690 if (pat_ref_find_elt(ref, key) != NULL)
3691 /* update entry if it exists */
3692 pat_ref_set(ref, key, value, NULL);
3693 else
3694 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003695 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003696
3697 break;
3698 }
William Lallemand73025dd2014-04-24 14:38:37 +02003699
3700 case HTTP_RES_ACT_CUSTOM_CONT:
3701 rule->action_ptr(rule, px, s, txn);
3702 break;
3703
3704 case HTTP_RES_ACT_CUSTOM_STOP:
3705 rule->action_ptr(rule, px, s, txn);
3706 return rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003707 }
3708 }
3709
3710 /* we reached the end of the rules, nothing to report */
3711 return NULL;
3712}
3713
3714
Willy Tarreau71241ab2012-12-27 11:30:54 +01003715/* Perform an HTTP redirect based on the information in <rule>. The function
3716 * returns non-zero on success, or zero in case of a, irrecoverable error such
3717 * as too large a request to build a valid response.
3718 */
3719static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3720{
3721 struct http_msg *msg = &txn->req;
3722 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003723 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003724
3725 /* build redirect message */
3726 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003727 case 308:
3728 msg_fmt = HTTP_308;
3729 break;
3730 case 307:
3731 msg_fmt = HTTP_307;
3732 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003733 case 303:
3734 msg_fmt = HTTP_303;
3735 break;
3736 case 301:
3737 msg_fmt = HTTP_301;
3738 break;
3739 case 302:
3740 default:
3741 msg_fmt = HTTP_302;
3742 break;
3743 }
3744
3745 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3746 return 0;
3747
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003748 location = trash.str + trash.len;
3749
Willy Tarreau71241ab2012-12-27 11:30:54 +01003750 switch(rule->type) {
3751 case REDIRECT_TYPE_SCHEME: {
3752 const char *path;
3753 const char *host;
3754 struct hdr_ctx ctx;
3755 int pathlen;
3756 int hostlen;
3757
3758 host = "";
3759 hostlen = 0;
3760 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003761 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003762 host = ctx.line + ctx.val;
3763 hostlen = ctx.vlen;
3764 }
3765
3766 path = http_get_path(txn);
3767 /* build message using path */
3768 if (path) {
3769 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3770 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3771 int qs = 0;
3772 while (qs < pathlen) {
3773 if (path[qs] == '?') {
3774 pathlen = qs;
3775 break;
3776 }
3777 qs++;
3778 }
3779 }
3780 } else {
3781 path = "/";
3782 pathlen = 1;
3783 }
3784
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003785 if (rule->rdr_str) { /* this is an old "redirect" rule */
3786 /* check if we can add scheme + "://" + host + path */
3787 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3788 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003789
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003790 /* add scheme */
3791 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3792 trash.len += rule->rdr_len;
3793 }
3794 else {
3795 /* add scheme with executing log format */
3796 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003797
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003798 /* check if we can add scheme + "://" + host + path */
3799 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3800 return 0;
3801 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003802 /* add "://" */
3803 memcpy(trash.str + trash.len, "://", 3);
3804 trash.len += 3;
3805
3806 /* add host */
3807 memcpy(trash.str + trash.len, host, hostlen);
3808 trash.len += hostlen;
3809
3810 /* add path */
3811 memcpy(trash.str + trash.len, path, pathlen);
3812 trash.len += pathlen;
3813
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003814 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003815 if (trash.len && trash.str[trash.len - 1] != '/' &&
3816 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3817 if (trash.len > trash.size - 5)
3818 return 0;
3819 trash.str[trash.len] = '/';
3820 trash.len++;
3821 }
3822
3823 break;
3824 }
3825 case REDIRECT_TYPE_PREFIX: {
3826 const char *path;
3827 int pathlen;
3828
3829 path = http_get_path(txn);
3830 /* build message using path */
3831 if (path) {
3832 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3833 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3834 int qs = 0;
3835 while (qs < pathlen) {
3836 if (path[qs] == '?') {
3837 pathlen = qs;
3838 break;
3839 }
3840 qs++;
3841 }
3842 }
3843 } else {
3844 path = "/";
3845 pathlen = 1;
3846 }
3847
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003848 if (rule->rdr_str) { /* this is an old "redirect" rule */
3849 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3850 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003851
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003852 /* add prefix. Note that if prefix == "/", we don't want to
3853 * add anything, otherwise it makes it hard for the user to
3854 * configure a self-redirection.
3855 */
3856 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3857 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3858 trash.len += rule->rdr_len;
3859 }
3860 }
3861 else {
3862 /* add prefix with executing log format */
3863 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3864
3865 /* Check length */
3866 if (trash.len + pathlen > trash.size - 4)
3867 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003868 }
3869
3870 /* add path */
3871 memcpy(trash.str + trash.len, path, pathlen);
3872 trash.len += pathlen;
3873
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003874 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003875 if (trash.len && trash.str[trash.len - 1] != '/' &&
3876 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3877 if (trash.len > trash.size - 5)
3878 return 0;
3879 trash.str[trash.len] = '/';
3880 trash.len++;
3881 }
3882
3883 break;
3884 }
3885 case REDIRECT_TYPE_LOCATION:
3886 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003887 if (rule->rdr_str) { /* this is an old "redirect" rule */
3888 if (trash.len + rule->rdr_len > trash.size - 4)
3889 return 0;
3890
3891 /* add location */
3892 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3893 trash.len += rule->rdr_len;
3894 }
3895 else {
3896 /* add location with executing log format */
3897 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003898
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003899 /* Check left length */
3900 if (trash.len > trash.size - 4)
3901 return 0;
3902 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003903 break;
3904 }
3905
3906 if (rule->cookie_len) {
3907 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3908 trash.len += 14;
3909 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3910 trash.len += rule->cookie_len;
3911 memcpy(trash.str + trash.len, "\r\n", 2);
3912 trash.len += 2;
3913 }
3914
3915 /* add end of headers and the keep-alive/close status.
3916 * We may choose to set keep-alive if the Location begins
3917 * with a slash, because the client will come back to the
3918 * same server.
3919 */
3920 txn->status = rule->code;
3921 /* let's log the request time */
3922 s->logs.tv_request = now;
3923
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003924 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003925 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3926 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3927 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3928 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3929 /* keep-alive possible */
3930 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3931 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3932 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3933 trash.len += 30;
3934 } else {
3935 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3936 trash.len += 24;
3937 }
3938 }
3939 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3940 trash.len += 4;
3941 bo_inject(txn->rsp.chn, trash.str, trash.len);
3942 /* "eat" the request */
3943 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02003944 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003945 msg->sov = 0;
3946 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3947 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3948 txn->req.msg_state = HTTP_MSG_CLOSED;
3949 txn->rsp.msg_state = HTTP_MSG_DONE;
3950 } else {
3951 /* keep-alive not possible */
3952 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3953 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3954 trash.len += 29;
3955 } else {
3956 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3957 trash.len += 23;
3958 }
3959 stream_int_retnclose(txn->req.chn->prod, &trash);
3960 txn->req.chn->analysers = 0;
3961 }
3962
3963 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003964 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003965 if (!(s->flags & SN_FINST_MASK))
3966 s->flags |= SN_FINST_R;
3967
3968 return 1;
3969}
3970
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003971/* This stream analyser runs all HTTP request processing which is common to
3972 * frontends and backends, which means blocking ACLs, filters, connection-close,
3973 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003974 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003975 * either needs more data or wants to immediately abort the request (eg: deny,
3976 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003977 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003978int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003979{
Willy Tarreaud787e662009-07-07 10:14:51 +02003980 struct http_txn *txn = &s->txn;
3981 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003982 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003983 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003984 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02003985
Willy Tarreau655dce92009-11-08 13:10:58 +01003986 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003987 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003988 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003989 return 0;
3990 }
3991
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003992 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 +02003993 now_ms, __FUNCTION__,
3994 s,
3995 req,
3996 req->rex, req->wex,
3997 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003998 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003999 req->analysers);
4000
Willy Tarreau65410832014-04-28 21:25:43 +02004001 /* just in case we have some per-backend tracking */
4002 session_inc_be_http_req_ctr(s);
4003
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004004 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004005 if (!LIST_ISEMPTY(&px->http_req_rules)) {
4006 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01004007
Willy Tarreau0b748332014-04-29 00:13:29 +02004008 switch (verdict) {
4009 case HTTP_RULE_RES_CONT:
4010 case HTTP_RULE_RES_STOP: /* nothing to do */
4011 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004012
Willy Tarreau0b748332014-04-29 00:13:29 +02004013 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4014 if (txn->flags & TX_CLTARPIT)
4015 goto tarpit;
4016 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004017
Willy Tarreau0b748332014-04-29 00:13:29 +02004018 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4019 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004020
Willy Tarreau0b748332014-04-29 00:13:29 +02004021 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004022 goto done;
4023
Willy Tarreau0b748332014-04-29 00:13:29 +02004024 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4025 goto return_bad_req;
4026 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004027 }
4028
Willy Tarreau52542592014-04-28 18:33:26 +02004029 /* OK at this stage, we know that the request was accepted according to
4030 * the http-request rules, we can check for the stats. Note that the
4031 * URI is detected *before* the req* rules in order not to be affected
4032 * by a possible reqrep, while they are processed *after* so that a
4033 * reqdeny can still block them. This clearly needs to change in 1.6!
4034 */
4035 if (stats_check_uri(s->rep->prod, txn, px)) {
4036 s->target = &http_stats_applet.obj_type;
4037 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
4038 txn->status = 500;
4039 s->logs.tv_request = now;
4040 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004041
Willy Tarreau52542592014-04-28 18:33:26 +02004042 if (!(s->flags & SN_ERR_MASK))
4043 s->flags |= SN_ERR_RESOURCE;
4044 goto return_prx_cond;
4045 }
4046
4047 /* parse the whole stats request and extract the relevant information */
4048 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004049 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4050 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004051
Willy Tarreau0b748332014-04-29 00:13:29 +02004052 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4053 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004054
Willy Tarreau0b748332014-04-29 00:13:29 +02004055 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4056 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004057 }
4058
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004059 /* evaluate the req* rules except reqadd */
4060 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004061 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004062 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004063
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004064 if (txn->flags & TX_CLDENY)
4065 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004066
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004067 if (txn->flags & TX_CLTARPIT)
4068 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004069 }
Willy Tarreau06619262006-12-17 08:37:22 +01004070
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004071 /* add request headers from the rule sets in the same order */
4072 list_for_each_entry(wl, &px->req_add, list) {
4073 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004074 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004075 ret = acl_pass(ret);
4076 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4077 ret = !ret;
4078 if (!ret)
4079 continue;
4080 }
4081
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004082 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004083 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004084 }
4085
Willy Tarreau52542592014-04-28 18:33:26 +02004086
4087 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004088 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004089 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004090 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
4091 s->fe->fe_counters.intercepted_req++;
4092
4093 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
4094 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
4095 if (!(s->flags & SN_FINST_MASK))
4096 s->flags |= SN_FINST_R;
4097
Willy Tarreau70730dd2014-04-24 18:06:27 +02004098 /* we may want to compress the stats page */
4099 if (s->fe->comp || s->be->comp)
4100 select_compression_request_header(s, req->buf);
4101
4102 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreaude8ca962014-11-20 22:23:10 +01004103 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004104 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004105 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004106
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004107 /* check whether we have some ACLs set to redirect this request */
4108 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004109 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004110 int ret;
4111
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004112 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004113 ret = acl_pass(ret);
4114 if (rule->cond->pol == ACL_COND_UNLESS)
4115 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004116 if (!ret)
4117 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004118 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004119 if (!http_apply_redirect_rule(rule, s, txn))
4120 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004121 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004122 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004123
Willy Tarreau2be39392010-01-03 17:24:51 +01004124 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4125 * If this happens, then the data will not come immediately, so we must
4126 * send all what we have without waiting. Note that due to the small gain
4127 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004128 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004129 * itself once used.
4130 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004131 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004132
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004133 done: /* done with this analyser, continue with next ones that the calling
4134 * points will have set, if any.
4135 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004136 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004137 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4138 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004139 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004140
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004141 tarpit:
4142 /* When a connection is tarpitted, we use the tarpit timeout,
4143 * which may be the same as the connect timeout if unspecified.
4144 * If unset, then set it to zero because we really want it to
4145 * eventually expire. We build the tarpit as an analyser.
4146 */
4147 channel_erase(s->req);
4148
4149 /* wipe the request out so that we can drop the connection early
4150 * if the client closes first.
4151 */
4152 channel_dont_connect(req);
4153 req->analysers = 0; /* remove switching rules etc... */
4154 req->analysers |= AN_REQ_HTTP_TARPIT;
4155 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4156 if (!req->analyse_exp)
4157 req->analyse_exp = tick_add(now_ms, 0);
4158 session_inc_http_err_ctr(s);
4159 s->fe->fe_counters.denied_req++;
4160 if (s->fe != s->be)
4161 s->be->be_counters.denied_req++;
4162 if (s->listener->counters)
4163 s->listener->counters->denied_req++;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004164 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004165
4166 deny: /* this request was blocked (denied) */
Willy Tarreau0b748332014-04-29 00:13:29 +02004167 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004168 txn->status = 403;
4169 s->logs.tv_request = now;
4170 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4171 session_inc_http_err_ctr(s);
4172 s->fe->fe_counters.denied_req++;
4173 if (s->fe != s->be)
4174 s->be->be_counters.denied_req++;
4175 if (s->listener->counters)
4176 s->listener->counters->denied_req++;
4177 goto return_prx_cond;
4178
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004179 return_bad_req:
4180 /* We centralize bad requests processing here */
4181 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4182 /* we detected a parsing error. We want to archive this request
4183 * in the dedicated proxy area for later troubleshooting.
4184 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004185 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004186 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004187
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004188 txn->req.msg_state = HTTP_MSG_ERROR;
4189 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004190 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004191
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004192 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004193 if (s->listener->counters)
4194 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004195
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004196 return_prx_cond:
4197 if (!(s->flags & SN_ERR_MASK))
4198 s->flags |= SN_ERR_PRXCOND;
4199 if (!(s->flags & SN_FINST_MASK))
4200 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004201
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004202 req->analysers = 0;
4203 req->analyse_exp = TICK_ETERNITY;
4204 return 0;
4205}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004206
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004207/* This function performs all the processing enabled for the current request.
4208 * It returns 1 if the processing can continue on next analysers, or zero if it
4209 * needs more data, encounters an error, or wants to immediately abort the
4210 * request. It relies on buffers flags, and updates s->req->analysers.
4211 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004212int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004213{
4214 struct http_txn *txn = &s->txn;
4215 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004216 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004217
Willy Tarreau655dce92009-11-08 13:10:58 +01004218 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004219 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004220 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004221 return 0;
4222 }
4223
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004224 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 +02004225 now_ms, __FUNCTION__,
4226 s,
4227 req,
4228 req->rex, req->wex,
4229 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004230 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004231 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004232
William Lallemand82fe75c2012-10-23 10:25:10 +02004233 if (s->fe->comp || s->be->comp)
4234 select_compression_request_header(s, req->buf);
4235
Willy Tarreau59234e92008-11-30 23:51:27 +01004236 /*
4237 * Right now, we know that we have processed the entire headers
4238 * and that unwanted requests have been filtered out. We can do
4239 * whatever we want with the remaining request. Also, now we
4240 * may have separate values for ->fe, ->be.
4241 */
Willy Tarreau06619262006-12-17 08:37:22 +01004242
Willy Tarreau59234e92008-11-30 23:51:27 +01004243 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004244 * If HTTP PROXY is set we simply get remote server address parsing
4245 * incoming request. Note that this requires that a connection is
4246 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004247 */
4248 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004249 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004250 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004251
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004252 /* Note that for now we don't reuse existing proxy connections */
4253 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004254 txn->req.msg_state = HTTP_MSG_ERROR;
4255 txn->status = 500;
4256 req->analysers = 0;
4257 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4258
4259 if (!(s->flags & SN_ERR_MASK))
4260 s->flags |= SN_ERR_RESOURCE;
4261 if (!(s->flags & SN_FINST_MASK))
4262 s->flags |= SN_FINST_R;
4263
4264 return 0;
4265 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004266
4267 path = http_get_path(txn);
4268 url2sa(req->buf->p + msg->sl.rq.u,
4269 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004270 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004271 /* if the path was found, we have to remove everything between
4272 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4273 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4274 * u_l characters by a single "/".
4275 */
4276 if (path) {
4277 char *cur_ptr = req->buf->p;
4278 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4279 int delta;
4280
4281 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4282 http_msg_move_end(&txn->req, delta);
4283 cur_end += delta;
4284 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4285 goto return_bad_req;
4286 }
4287 else {
4288 char *cur_ptr = req->buf->p;
4289 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4290 int delta;
4291
4292 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4293 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4294 http_msg_move_end(&txn->req, delta);
4295 cur_end += delta;
4296 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4297 goto return_bad_req;
4298 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004299 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004300
Willy Tarreau59234e92008-11-30 23:51:27 +01004301 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004302 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004303 * Note that doing so might move headers in the request, but
4304 * the fields will stay coherent and the URI will not move.
4305 * This should only be performed in the backend.
4306 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004307 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004308 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4309 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004310
Willy Tarreau59234e92008-11-30 23:51:27 +01004311 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004312 * 8: the appsession cookie was looked up very early in 1.2,
4313 * so let's do the same now.
4314 */
4315
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004316 /* It needs to look into the URI unless persistence must be ignored */
4317 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004318 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 +01004319 }
4320
William Lallemanda73203e2012-03-12 12:48:57 +01004321 /* add unique-id if "header-unique-id" is specified */
4322
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004323 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4324 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4325 goto return_bad_req;
4326 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004327 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004328 }
William Lallemanda73203e2012-03-12 12:48:57 +01004329
4330 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004331 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4332 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004333 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004334 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004335 goto return_bad_req;
4336 }
4337
Cyril Bontéb21570a2009-11-29 20:04:48 +01004338 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004339 * 9: add X-Forwarded-For if either the frontend or the backend
4340 * asks for it.
4341 */
4342 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004343 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004344 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004345 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4346 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004347 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004348 /* The header is set to be added only if none is present
4349 * and we found it, so don't do anything.
4350 */
4351 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004352 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004353 /* Add an X-Forwarded-For header unless the source IP is
4354 * in the 'except' network range.
4355 */
4356 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004357 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004358 != s->fe->except_net.s_addr) &&
4359 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004360 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004361 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004362 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004363 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004364 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004365
4366 /* Note: we rely on the backend to get the header name to be used for
4367 * x-forwarded-for, because the header is really meant for the backends.
4368 * However, if the backend did not specify any option, we have to rely
4369 * on the frontend's header name.
4370 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004371 if (s->be->fwdfor_hdr_len) {
4372 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004373 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004374 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004375 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004376 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004377 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004378 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 +01004379
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004380 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004381 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004382 }
4383 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004384 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004385 /* FIXME: for the sake of completeness, we should also support
4386 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004387 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004388 int len;
4389 char pn[INET6_ADDRSTRLEN];
4390 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004391 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004392 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004393
Willy Tarreau59234e92008-11-30 23:51:27 +01004394 /* Note: we rely on the backend to get the header name to be used for
4395 * x-forwarded-for, because the header is really meant for the backends.
4396 * However, if the backend did not specify any option, we have to rely
4397 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004398 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004399 if (s->be->fwdfor_hdr_len) {
4400 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004401 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004402 } else {
4403 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004404 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004405 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004406 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004407
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004408 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004409 goto return_bad_req;
4410 }
4411 }
4412
4413 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004414 * 10: add X-Original-To if either the frontend or the backend
4415 * asks for it.
4416 */
4417 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4418
4419 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004420 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004421 /* Add an X-Original-To header unless the destination IP is
4422 * in the 'except' network range.
4423 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004424 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004425
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004426 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004427 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004428 (((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 +02004429 != s->fe->except_to.s_addr) &&
4430 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004431 (((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 +02004432 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004433 int len;
4434 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004435 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004436
4437 /* Note: we rely on the backend to get the header name to be used for
4438 * x-original-to, because the header is really meant for the backends.
4439 * However, if the backend did not specify any option, we have to rely
4440 * on the frontend's header name.
4441 */
4442 if (s->be->orgto_hdr_len) {
4443 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004444 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004445 } else {
4446 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004447 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004448 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004449 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 +02004450
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004451 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004452 goto return_bad_req;
4453 }
4454 }
4455 }
4456
Willy Tarreau50fc7772012-11-11 22:19:57 +01004457 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4458 * If an "Upgrade" token is found, the header is left untouched in order not to have
4459 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4460 * "Upgrade" is present in the Connection header.
4461 */
4462 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4463 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004464 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4465 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004466 unsigned int want_flags = 0;
4467
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004468 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004469 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004470 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4471 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004472 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004473 want_flags |= TX_CON_CLO_SET;
4474 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004475 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004476 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4477 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004478 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004479 want_flags |= TX_CON_KAL_SET;
4480 }
4481
4482 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004483 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004484 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004485
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004486
Willy Tarreau522d6c02009-12-06 18:49:18 +01004487 /* If we have no server assigned yet and we're balancing on url_param
4488 * with a POST request, we may be interested in checking the body for
4489 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004490 */
4491 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4492 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004493 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004494 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004495 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004496 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004497
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004498 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004499 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004500#ifdef TCP_QUICKACK
4501 /* We expect some data from the client. Unless we know for sure
4502 * we already have a full request, we have to re-enable quick-ack
4503 * in case we previously disabled it, otherwise we might cause
4504 * the client to delay further data.
4505 */
4506 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004507 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004508 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004509 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004510 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004511#endif
4512 }
Willy Tarreau03945942009-12-22 16:50:27 +01004513
Willy Tarreau59234e92008-11-30 23:51:27 +01004514 /*************************************************************
4515 * OK, that's finished for the headers. We have done what we *
4516 * could. Let's switch to the DATA state. *
4517 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004518 req->analyse_exp = TICK_ETERNITY;
4519 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004520
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004521 /* if the server closes the connection, we want to immediately react
4522 * and close the socket to save packets and syscalls.
4523 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004524 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4525 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004526
Willy Tarreau59234e92008-11-30 23:51:27 +01004527 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004528 /* OK let's go on with the BODY now */
4529 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004530
Willy Tarreau59234e92008-11-30 23:51:27 +01004531 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004532 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004533 /* we detected a parsing error. We want to archive this request
4534 * in the dedicated proxy area for later troubleshooting.
4535 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004536 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004537 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004538
Willy Tarreau59234e92008-11-30 23:51:27 +01004539 txn->req.msg_state = HTTP_MSG_ERROR;
4540 txn->status = 400;
4541 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004542 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004543
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004544 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004545 if (s->listener->counters)
4546 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004547
Willy Tarreau59234e92008-11-30 23:51:27 +01004548 if (!(s->flags & SN_ERR_MASK))
4549 s->flags |= SN_ERR_PRXCOND;
4550 if (!(s->flags & SN_FINST_MASK))
4551 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004552 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004553}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004554
Willy Tarreau60b85b02008-11-30 23:28:40 +01004555/* This function is an analyser which processes the HTTP tarpit. It always
4556 * returns zero, at the beginning because it prevents any other processing
4557 * from occurring, and at the end because it terminates the request.
4558 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004559int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004560{
4561 struct http_txn *txn = &s->txn;
4562
4563 /* This connection is being tarpitted. The CLIENT side has
4564 * already set the connect expiration date to the right
4565 * timeout. We just have to check that the client is still
4566 * there and that the timeout has not expired.
4567 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004568 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004569 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004570 !tick_is_expired(req->analyse_exp, now_ms))
4571 return 0;
4572
4573 /* We will set the queue timer to the time spent, just for
4574 * logging purposes. We fake a 500 server error, so that the
4575 * attacker will not suspect his connection has been tarpitted.
4576 * It will not cause trouble to the logs because we can exclude
4577 * the tarpitted connections by filtering on the 'PT' status flags.
4578 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004579 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4580
4581 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004582 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004583 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004584
4585 req->analysers = 0;
4586 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004587
Willy Tarreau60b85b02008-11-30 23:28:40 +01004588 if (!(s->flags & SN_ERR_MASK))
4589 s->flags |= SN_ERR_PRXCOND;
4590 if (!(s->flags & SN_FINST_MASK))
4591 s->flags |= SN_FINST_T;
4592 return 0;
4593}
4594
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004595/* This function is an analyser which waits for the HTTP request body. It waits
4596 * for either the buffer to be full, or the full advertised contents to have
4597 * reached the buffer. It must only be called after the standard HTTP request
4598 * processing has occurred, because it expects the request to be parsed and will
4599 * look for the Expect header. It may send a 100-Continue interim response. It
4600 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4601 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4602 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004603 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004604int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004605{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004606 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004607 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004608
4609 /* We have to parse the HTTP request body to find any required data.
4610 * "balance url_param check_post" should have been the only way to get
4611 * into this. We were brought here after HTTP header analysis, so all
4612 * related structures are ready.
4613 */
4614
Willy Tarreau890988f2014-04-10 11:59:33 +02004615 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4616 /* This is the first call */
4617 if (msg->msg_state < HTTP_MSG_BODY)
4618 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004619
Willy Tarreau890988f2014-04-10 11:59:33 +02004620 if (msg->msg_state < HTTP_MSG_100_SENT) {
4621 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4622 * send an HTTP/1.1 100 Continue intermediate response.
4623 */
4624 if (msg->flags & HTTP_MSGF_VER_11) {
4625 struct hdr_ctx ctx;
4626 ctx.idx = 0;
4627 /* Expect is allowed in 1.1, look for it */
4628 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4629 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4630 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4631 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004632 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004633 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004634 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004635
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004636 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004637 * req->buf->p still points to the beginning of the message. We
4638 * must save the body in msg->next because it survives buffer
4639 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004640 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004641 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004642
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004643 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004644 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4645 else
4646 msg->msg_state = HTTP_MSG_DATA;
4647 }
4648
Willy Tarreau890988f2014-04-10 11:59:33 +02004649 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4650 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreau5b7f9242015-05-01 23:05:14 +02004651 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004652 goto missing_data;
4653
4654 /* OK we have everything we need now */
4655 goto http_end;
4656 }
4657
4658 /* OK here we're parsing a chunked-encoded message */
4659
Willy Tarreau522d6c02009-12-06 18:49:18 +01004660 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004661 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004662 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004663 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004664 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004665 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004666
Willy Tarreau115acb92009-12-26 13:56:06 +01004667 if (!ret)
4668 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004669 else if (ret < 0) {
4670 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004671 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004672 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004673 }
4674
Willy Tarreaud98cf932009-12-27 22:54:55 +01004675 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau5b7f9242015-05-01 23:05:14 +02004676 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4677 * for at least a whole chunk or the whole content length bytes after
4678 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004679 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004680 if (msg->msg_state == HTTP_MSG_TRAILERS)
4681 goto http_end;
4682
Willy Tarreau5b7f9242015-05-01 23:05:14 +02004683 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004684 goto http_end;
4685
4686 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004687 /* we get here if we need to wait for more data. If the buffer is full,
4688 * we have the maximum we can expect.
4689 */
4690 if (buffer_full(req->buf, global.tune.maxrewrite))
4691 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004692
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004693 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004694 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004695 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004696
4697 if (!(s->flags & SN_ERR_MASK))
4698 s->flags |= SN_ERR_CLITO;
4699 if (!(s->flags & SN_FINST_MASK))
4700 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004701 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004702 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004703
4704 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004705 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004706 /* Not enough data. We'll re-use the http-request
4707 * timeout here. Ideally, we should set the timeout
4708 * relative to the accept() date. We just set the
4709 * request timeout once at the beginning of the
4710 * request.
4711 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004712 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004713 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004714 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004715 return 0;
4716 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004717
4718 http_end:
4719 /* The situation will not evolve, so let's give up on the analysis. */
4720 s->logs.tv_request = now; /* update the request timer to reflect full request */
4721 req->analysers &= ~an_bit;
4722 req->analyse_exp = TICK_ETERNITY;
4723 return 1;
4724
4725 return_bad_req: /* let's centralize all bad requests */
4726 txn->req.msg_state = HTTP_MSG_ERROR;
4727 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004728 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004729
Willy Tarreau79ebac62010-06-07 13:47:49 +02004730 if (!(s->flags & SN_ERR_MASK))
4731 s->flags |= SN_ERR_PRXCOND;
4732 if (!(s->flags & SN_FINST_MASK))
4733 s->flags |= SN_FINST_R;
4734
Willy Tarreau522d6c02009-12-06 18:49:18 +01004735 return_err_msg:
4736 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004737 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004738 if (s->listener->counters)
4739 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004740 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004741}
4742
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004743/* send a server's name with an outgoing request over an established connection.
4744 * Note: this function is designed to be called once the request has been scheduled
4745 * for being forwarded. This is the reason why it rewinds the buffer before
4746 * proceeding.
4747 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004748int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004749
4750 struct hdr_ctx ctx;
4751
Mark Lamourinec2247f02012-01-04 13:02:01 -05004752 char *hdr_name = be->server_id_hdr_name;
4753 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004754 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004755 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004756 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004757
William Lallemandd9e90662012-01-30 17:27:17 +01004758 ctx.idx = 0;
4759
Willy Tarreau211cdec2014-04-17 20:18:08 +02004760 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004761 if (old_o) {
4762 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004763 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004764 txn->req.next += old_o;
4765 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004766 }
4767
Willy Tarreau9b28e032012-10-12 23:49:43 +02004768 old_i = chn->buf->i;
4769 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 -05004770 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004771 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004772 }
4773
4774 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004775 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004776 memcpy(hdr_val, hdr_name, hdr_name_len);
4777 hdr_val += hdr_name_len;
4778 *hdr_val++ = ':';
4779 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004780 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4781 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004782
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004783 if (old_o) {
4784 /* If this was a forwarded request, we must readjust the amount of
4785 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004786 * variations. Note that the current state is >= HTTP_MSG_BODY,
4787 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004788 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004789 old_o += chn->buf->i - old_i;
4790 b_adv(chn->buf, old_o);
4791 txn->req.next -= old_o;
4792 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004793 }
4794
Mark Lamourinec2247f02012-01-04 13:02:01 -05004795 return 0;
4796}
4797
Willy Tarreau610ecce2010-01-04 21:15:02 +01004798/* Terminate current transaction and prepare a new one. This is very tricky
4799 * right now but it works.
4800 */
4801void http_end_txn_clean_session(struct session *s)
4802{
Willy Tarreau068621e2013-12-23 15:11:25 +01004803 int prev_status = s->txn.status;
4804
Willy Tarreau610ecce2010-01-04 21:15:02 +01004805 /* FIXME: We need a more portable way of releasing a backend's and a
4806 * server's connections. We need a safer way to reinitialize buffer
4807 * flags. We also need a more accurate method for computing per-request
4808 * data.
4809 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004810
Willy Tarreau4213a112013-12-15 10:25:42 +01004811 /* unless we're doing keep-alive, we want to quickly close the connection
4812 * to the server.
4813 */
4814 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4815 !si_conn_ready(s->req->cons)) {
4816 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4817 si_shutr(s->req->cons);
4818 si_shutw(s->req->cons);
4819 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004820
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004821 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004822 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004823 if (unlikely(s->srv_conn))
4824 sess_change_server(s, NULL);
4825 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004826
4827 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4828 session_process_counters(s);
4829
4830 if (s->txn.status) {
4831 int n;
4832
4833 n = s->txn.status / 100;
4834 if (n < 1 || n > 5)
4835 n = 0;
4836
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004837 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004838 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004839 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004840 s->fe->fe_counters.p.http.comp_rsp++;
4841 }
Willy Tarreau24657792010-02-26 10:30:28 +01004842 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004843 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004844 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004845 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004846 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004847 s->be->be_counters.p.http.comp_rsp++;
4848 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004849 }
4850
4851 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004852 s->logs.bytes_in -= s->req->buf->i;
4853 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004854
4855 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004856 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004857 !(s->flags & SN_MONITOR) &&
4858 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4859 s->do_log(s);
4860 }
4861
Willy Tarreauc177ea72014-06-25 15:36:04 +02004862 /* stop tracking content-based counters */
4863 session_stop_content_counters(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004864 session_update_time_stats(s);
4865
Willy Tarreau610ecce2010-01-04 21:15:02 +01004866 s->logs.accept_date = date; /* user-visible date for logging */
4867 s->logs.tv_accept = now; /* corrected date for internal use */
4868 tv_zero(&s->logs.tv_request);
4869 s->logs.t_queue = -1;
4870 s->logs.t_connect = -1;
4871 s->logs.t_data = -1;
4872 s->logs.t_close = 0;
4873 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4874 s->logs.srv_queue_size = 0; /* we will get this number soon */
4875
Willy Tarreau9b28e032012-10-12 23:49:43 +02004876 s->logs.bytes_in = s->req->total = s->req->buf->i;
4877 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004878
4879 if (s->pend_pos)
4880 pendconn_free(s->pend_pos);
4881
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004882 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004883 if (s->flags & SN_CURR_SESS) {
4884 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004885 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004886 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004887 if (may_dequeue_tasks(objt_server(s->target), s->be))
4888 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004889 }
4890
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004891 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004892
Willy Tarreau4213a112013-12-15 10:25:42 +01004893 /* only release our endpoint if we don't intend to reuse the
4894 * connection.
4895 */
4896 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4897 !si_conn_ready(s->req->cons)) {
4898 si_release_endpoint(s->req->cons);
4899 }
4900
Willy Tarreau610ecce2010-01-04 21:15:02 +01004901 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004902 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004903 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004904 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004905 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreaub4d05092014-09-01 20:35:55 +02004906 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);
4907 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 +02004908 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 +01004909 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Cyril Bonté17f77072014-10-22 22:30:13 +02004910 s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004911
Willy Tarreau610ecce2010-01-04 21:15:02 +01004912 s->txn.meth = 0;
4913 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004914 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004915
4916 if (prev_status == 401 || prev_status == 407) {
4917 /* In HTTP keep-alive mode, if we receive a 401, we still have
4918 * a chance of being able to send the visitor again to the same
4919 * server over the same connection. This is required by some
4920 * broken protocols such as NTLM, and anyway whenever there is
4921 * an opportunity for sending the challenge to the proper place,
4922 * it's better to do it (at least it helps with debugging).
4923 */
4924 s->txn.flags |= TX_PREFER_LAST;
4925 }
4926
Willy Tarreauee55dc02010-06-01 10:56:34 +02004927 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004928 s->req->cons->flags |= SI_FL_INDEP_STR;
4929
Willy Tarreau96e31212011-05-30 18:10:30 +02004930 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004931 s->req->flags |= CF_NEVER_WAIT;
4932 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004933 }
4934
Willy Tarreau610ecce2010-01-04 21:15:02 +01004935 /* if the request buffer is not empty, it means we're
4936 * about to process another request, so send pending
4937 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004938 * Just don't do this if the buffer is close to be full,
4939 * because the request will wait for it to flush a little
4940 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004941 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004942 if (s->req->buf->i) {
4943 if (s->rep->buf->o &&
4944 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4945 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004946 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004947 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004948
4949 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004950 channel_auto_read(s->req);
4951 channel_auto_close(s->req);
4952 channel_auto_read(s->rep);
4953 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004954
Willy Tarreau27375622013-12-17 00:00:28 +01004955 /* we're in keep-alive with an idle connection, monitor it */
4956 si_idle_conn(s->req->cons);
4957
Willy Tarreau342b11c2010-11-24 16:22:09 +01004958 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004959 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004960}
4961
4962
4963/* This function updates the request state machine according to the response
4964 * state machine and buffer flags. It returns 1 if it changes anything (flag
4965 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4966 * it is only used to find when a request/response couple is complete. Both
4967 * this function and its equivalent should loop until both return zero. It
4968 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4969 */
4970int http_sync_req_state(struct session *s)
4971{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004972 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004973 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004974 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004975 unsigned int old_state = txn->req.msg_state;
4976
Willy Tarreau610ecce2010-01-04 21:15:02 +01004977 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4978 return 0;
4979
4980 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004981 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004982 * We can shut the read side unless we want to abort_on_close,
4983 * or we have a POST request. The issue with POST requests is
4984 * that some browsers still send a CRLF after the request, and
4985 * this CRLF must be read so that it does not remain in the kernel
4986 * buffers, otherwise a close could cause an RST on some systems
4987 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004988 * Note that if we're using keep-alive on the client side, we'd
4989 * rather poll now and keep the polling enabled for the whole
4990 * session's life than enabling/disabling it between each
4991 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004992 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004993 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4994 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4995 !(s->be->options & PR_O_ABRT_CLOSE) &&
4996 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004997 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004998
Willy Tarreau40f151a2012-12-20 12:10:09 +01004999 /* if the server closes the connection, we want to immediately react
5000 * and close the socket to save packets and syscalls.
5001 */
5002 chn->cons->flags |= SI_FL_NOHALF;
5003
Willy Tarreau610ecce2010-01-04 21:15:02 +01005004 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5005 goto wait_other_side;
5006
5007 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5008 /* The server has not finished to respond, so we
5009 * don't want to move in order not to upset it.
5010 */
5011 goto wait_other_side;
5012 }
5013
5014 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5015 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005016 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005017 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005018 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005019 goto wait_other_side;
5020 }
5021
5022 /* When we get here, it means that both the request and the
5023 * response have finished receiving. Depending on the connection
5024 * mode, we'll have to wait for the last bytes to leave in either
5025 * direction, and sometimes for a close to be effective.
5026 */
5027
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005028 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5029 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005030 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5031 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005032 }
5033 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5034 /* Option forceclose is set, or either side wants to close,
5035 * let's enforce it now that we're not expecting any new
5036 * data to come. The caller knows the session is complete
5037 * once both states are CLOSED.
5038 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005039 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5040 channel_shutr_now(chn);
5041 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005042 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005043 }
5044 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005045 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5046 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005047 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005048 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5049 channel_auto_read(chn);
5050 txn->req.msg_state = HTTP_MSG_TUNNEL;
5051 chn->flags |= CF_NEVER_WAIT;
5052 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005053 }
5054
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005055 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005056 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005057 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005058
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005059 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005060 txn->req.msg_state = HTTP_MSG_CLOSING;
5061 goto http_msg_closing;
5062 }
5063 else {
5064 txn->req.msg_state = HTTP_MSG_CLOSED;
5065 goto http_msg_closed;
5066 }
5067 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005068 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005069 }
5070
5071 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5072 http_msg_closing:
5073 /* nothing else to forward, just waiting for the output buffer
5074 * to be empty and for the shutw_now to take effect.
5075 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005076 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005077 txn->req.msg_state = HTTP_MSG_CLOSED;
5078 goto http_msg_closed;
5079 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005080 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005081 txn->req.msg_state = HTTP_MSG_ERROR;
5082 goto wait_other_side;
5083 }
5084 }
5085
5086 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5087 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005088 /* see above in MSG_DONE why we only do this in these states */
5089 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5090 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5091 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005092 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005093 goto wait_other_side;
5094 }
5095
5096 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005097 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005098}
5099
5100
5101/* This function updates the response state machine according to the request
5102 * state machine and buffer flags. It returns 1 if it changes anything (flag
5103 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5104 * it is only used to find when a request/response couple is complete. Both
5105 * this function and its equivalent should loop until both return zero. It
5106 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5107 */
5108int http_sync_res_state(struct session *s)
5109{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005110 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005111 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005112 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005113 unsigned int old_state = txn->rsp.msg_state;
5114
Willy Tarreau610ecce2010-01-04 21:15:02 +01005115 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5116 return 0;
5117
5118 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5119 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005120 * still monitor the server connection for a possible close
5121 * while the request is being uploaded, so we don't disable
5122 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005123 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005124 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005125
5126 if (txn->req.msg_state == HTTP_MSG_ERROR)
5127 goto wait_other_side;
5128
5129 if (txn->req.msg_state < HTTP_MSG_DONE) {
5130 /* The client seems to still be sending data, probably
5131 * because we got an error response during an upload.
5132 * We have the choice of either breaking the connection
5133 * or letting it pass through. Let's do the later.
5134 */
5135 goto wait_other_side;
5136 }
5137
5138 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5139 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005140 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005141 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005142 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005143 goto wait_other_side;
5144 }
5145
5146 /* When we get here, it means that both the request and the
5147 * response have finished receiving. Depending on the connection
5148 * mode, we'll have to wait for the last bytes to leave in either
5149 * direction, and sometimes for a close to be effective.
5150 */
5151
5152 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5153 /* Server-close mode : shut read and wait for the request
5154 * side to close its output buffer. The caller will detect
5155 * when we're in DONE and the other is in CLOSED and will
5156 * catch that for the final cleanup.
5157 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005158 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5159 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005160 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005161 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5162 /* Option forceclose is set, or either side wants to close,
5163 * let's enforce it now that we're not expecting any new
5164 * data to come. The caller knows the session is complete
5165 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005166 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005167 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5168 channel_shutr_now(chn);
5169 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005170 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005171 }
5172 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005173 /* The last possible modes are keep-alive and tunnel. Tunnel will
5174 * need to forward remaining data. Keep-alive will need to monitor
5175 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005176 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005177 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005178 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005179 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5180 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005181 }
5182
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005183 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005184 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005185 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005186 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5187 goto http_msg_closing;
5188 }
5189 else {
5190 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5191 goto http_msg_closed;
5192 }
5193 }
5194 goto wait_other_side;
5195 }
5196
5197 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5198 http_msg_closing:
5199 /* nothing else to forward, just waiting for the output buffer
5200 * to be empty and for the shutw_now to take effect.
5201 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005202 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005203 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5204 goto http_msg_closed;
5205 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005206 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005207 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005208 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005209 if (objt_server(s->target))
5210 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005211 goto wait_other_side;
5212 }
5213 }
5214
5215 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5216 http_msg_closed:
5217 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005218 bi_erase(chn);
5219 channel_auto_close(chn);
5220 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005221 goto wait_other_side;
5222 }
5223
5224 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005225 /* We force the response to leave immediately if we're waiting for the
5226 * other side, since there is no pending shutdown to push it out.
5227 */
5228 if (!channel_is_empty(chn))
5229 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005230 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005231}
5232
5233
5234/* Resync the request and response state machines. Return 1 if either state
5235 * changes.
5236 */
5237int http_resync_states(struct session *s)
5238{
5239 struct http_txn *txn = &s->txn;
5240 int old_req_state = txn->req.msg_state;
5241 int old_res_state = txn->rsp.msg_state;
5242
Willy Tarreau610ecce2010-01-04 21:15:02 +01005243 http_sync_req_state(s);
5244 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005245 if (!http_sync_res_state(s))
5246 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005247 if (!http_sync_req_state(s))
5248 break;
5249 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005250
Willy Tarreau610ecce2010-01-04 21:15:02 +01005251 /* OK, both state machines agree on a compatible state.
5252 * There are a few cases we're interested in :
5253 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5254 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5255 * directions, so let's simply disable both analysers.
5256 * - HTTP_MSG_CLOSED on the response only means we must abort the
5257 * request.
5258 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5259 * with server-close mode means we've completed one request and we
5260 * must re-initialize the server connection.
5261 */
5262
5263 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5264 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5265 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5266 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5267 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005268 channel_auto_close(s->req);
5269 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005270 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005271 channel_auto_close(s->rep);
5272 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005273 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005274 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5275 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005276 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005277 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +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 Tarreau90deb182010-01-07 00:20:41 +01005281 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005282 channel_abort(s->req);
5283 channel_auto_close(s->req);
5284 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005285 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005286 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005287 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5288 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005289 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005290 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5291 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5292 /* server-close/keep-alive: terminate this transaction,
5293 * possibly killing the server connection and reinitialize
5294 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005295 */
5296 http_end_txn_clean_session(s);
5297 }
5298
Willy Tarreau610ecce2010-01-04 21:15:02 +01005299 return txn->req.msg_state != old_req_state ||
5300 txn->rsp.msg_state != old_res_state;
5301}
5302
Willy Tarreaud98cf932009-12-27 22:54:55 +01005303/* This function is an analyser which forwards request body (including chunk
5304 * sizes if any). It is called as soon as we must forward, even if we forward
5305 * zero byte. The only situation where it must not be called is when we're in
5306 * tunnel mode and we want to forward till the close. It's used both to forward
5307 * remaining data and to resync after end of body. It expects the msg_state to
5308 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5309 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005310 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005311 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005312 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005313int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005314{
5315 struct http_txn *txn = &s->txn;
5316 struct http_msg *msg = &s->txn.req;
5317
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005318 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5319 return 0;
5320
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005321 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005322 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005323 /* Output closed while we were sending data. We must abort and
5324 * wake the other side up.
5325 */
5326 msg->msg_state = HTTP_MSG_ERROR;
5327 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005328 return 1;
5329 }
5330
Willy Tarreaud98cf932009-12-27 22:54:55 +01005331 /* Note that we don't have to send 100-continue back because we don't
5332 * need the data to complete our job, and it's up to the server to
5333 * decide whether to return 100, 417 or anything else in return of
5334 * an "Expect: 100-continue" header.
5335 */
5336
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005337 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005338 /* we have msg->sov which points to the first byte of message
5339 * body, and req->buf.p still points to the beginning of the
5340 * message. We forward the headers now, as we don't need them
5341 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005342 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005343 b_adv(req->buf, msg->sov);
5344 msg->next -= msg->sov;
5345 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005346
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005347 /* The previous analysers guarantee that the state is somewhere
5348 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5349 * msg->next are always correct.
5350 */
5351 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5352 if (msg->flags & HTTP_MSGF_TE_CHNK)
5353 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5354 else
5355 msg->msg_state = HTTP_MSG_DATA;
5356 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005357 }
5358
Willy Tarreau7ba23542014-04-17 21:50:00 +02005359 /* Some post-connect processing might want us to refrain from starting to
5360 * forward data. Currently, the only reason for this is "balance url_param"
5361 * whichs need to parse/process the request after we've enabled forwarding.
5362 */
5363 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5364 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5365 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005366 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005367 goto missing_data;
5368 }
5369 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5370 }
5371
Willy Tarreau80a92c02014-03-12 10:41:13 +01005372 /* in most states, we should abort in case of early close */
5373 channel_auto_close(req);
5374
Willy Tarreauefdf0942014-04-24 20:08:57 +02005375 if (req->to_forward) {
5376 /* We can't process the buffer's contents yet */
5377 req->flags |= CF_WAKE_WRITE;
5378 goto missing_data;
5379 }
5380
Willy Tarreaud98cf932009-12-27 22:54:55 +01005381 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005382 if (msg->msg_state == HTTP_MSG_DATA) {
5383 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005384 /* we may have some pending data starting at req->buf->p */
5385 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005386 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005387 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005388 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005389 msg->next += msg->chunk_len;
5390 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005391
5392 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005393 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005394 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005395 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005396 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005397 }
5398 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005399 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005400 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005401 * TRAILERS state.
5402 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005403 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005404
Willy Tarreau54d23df2012-10-25 19:04:45 +02005405 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005406 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005407 else if (ret < 0) {
5408 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005409 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005410 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005411 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005412 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005413 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005414 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005415 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005416 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005417 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005418
5419 if (ret == 0)
5420 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005421 else if (ret < 0) {
5422 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005423 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005424 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005425 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005426 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005427 /* we're in MSG_CHUNK_SIZE now */
5428 }
5429 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005430 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005431
5432 if (ret == 0)
5433 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005434 else if (ret < 0) {
5435 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005436 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005437 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005438 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005439 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005440 /* we're in HTTP_MSG_DONE now */
5441 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005442 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005443 int old_state = msg->msg_state;
5444
Willy Tarreau610ecce2010-01-04 21:15:02 +01005445 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005446
5447 /* we may have some pending data starting at req->buf->p
5448 * such as last chunk of data or trailers.
5449 */
5450 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005451 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005452 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005453 msg->next = 0;
5454
Willy Tarreau294e4672015-05-11 18:30:33 +02005455 /* we don't want to forward closes on DONE except in
5456 * tunnel mode.
5457 */
5458 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005459 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005460 if (http_resync_states(s)) {
5461 /* some state changes occurred, maybe the analyser
5462 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005463 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005464 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005465 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005466 /* request errors are most likely due to
5467 * the server aborting the transfer.
5468 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005469 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005470 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005471 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005472 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005473 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005474 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005475 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005476 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005477
5478 /* If "option abortonclose" is set on the backend, we
5479 * want to monitor the client's connection and forward
5480 * any shutdown notification to the server, which will
5481 * decide whether to close or to go on processing the
Willy Tarreau294e4672015-05-11 18:30:33 +02005482 * request. We only do that in tunnel mode, and not in
5483 * other modes since it can be abused to exhaust source
5484 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005485 */
5486 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005487 channel_auto_read(req);
Willy Tarreau294e4672015-05-11 18:30:33 +02005488 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5489 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5490 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005491 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005492 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005493 else if (s->txn.meth == HTTP_METH_POST) {
5494 /* POST requests may require to read extra CRLF
5495 * sent by broken browsers and which could cause
5496 * an RST to be sent upon close on some systems
5497 * (eg: Linux).
5498 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005499 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005500 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005501
Willy Tarreau610ecce2010-01-04 21:15:02 +01005502 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005503 }
5504 }
5505
Willy Tarreaud98cf932009-12-27 22:54:55 +01005506 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005507 /* we may have some pending data starting at req->buf->p */
5508 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005509 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005510 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5511
Willy Tarreaubed410e2014-04-22 08:19:34 +02005512 msg->next = 0;
5513 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5514
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005515 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005516 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005517 if (!(s->flags & SN_ERR_MASK))
5518 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005519 if (!(s->flags & SN_FINST_MASK)) {
5520 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5521 s->flags |= SN_FINST_H;
5522 else
5523 s->flags |= SN_FINST_D;
5524 }
5525
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005526 s->fe->fe_counters.cli_aborts++;
5527 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005528 if (objt_server(s->target))
5529 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005530
5531 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005532 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005533
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005534 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005535 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005536 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005537
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005538 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005539 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005540 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005541 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005542 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005543
Willy Tarreau5c620922011-05-11 19:56:11 +02005544 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005545 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005546 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005547 * modes are already handled by the stream sock layer. We must not do
5548 * this in content-length mode because it could present the MSG_MORE
5549 * flag with the last block of forwarded data, which would cause an
5550 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005551 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005552 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005553 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005554
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005555 return 0;
5556
Willy Tarreaud98cf932009-12-27 22:54:55 +01005557 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005558 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005559 if (s->listener->counters)
5560 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005561
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005562 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005563 /* we may have some pending data starting at req->buf->p */
5564 b_adv(req->buf, msg->next);
5565 msg->next = 0;
5566
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005567 txn->req.msg_state = HTTP_MSG_ERROR;
5568 if (txn->status) {
5569 /* Note: we don't send any error if some data were already sent */
5570 stream_int_retnclose(req->prod, NULL);
5571 } else {
5572 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005573 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005574 }
5575 req->analysers = 0;
5576 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005577
5578 if (!(s->flags & SN_ERR_MASK))
5579 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005580 if (!(s->flags & SN_FINST_MASK)) {
5581 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5582 s->flags |= SN_FINST_H;
5583 else
5584 s->flags |= SN_FINST_D;
5585 }
5586 return 0;
5587
5588 aborted_xfer:
5589 txn->req.msg_state = HTTP_MSG_ERROR;
5590 if (txn->status) {
5591 /* Note: we don't send any error if some data were already sent */
5592 stream_int_retnclose(req->prod, NULL);
5593 } else {
5594 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005595 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005596 }
5597 req->analysers = 0;
5598 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5599
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005600 s->fe->fe_counters.srv_aborts++;
5601 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005602 if (objt_server(s->target))
5603 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005604
5605 if (!(s->flags & SN_ERR_MASK))
5606 s->flags |= SN_ERR_SRVCL;
5607 if (!(s->flags & SN_FINST_MASK)) {
5608 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5609 s->flags |= SN_FINST_H;
5610 else
5611 s->flags |= SN_FINST_D;
5612 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005613 return 0;
5614}
5615
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005616/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5617 * processing can continue on next analysers, or zero if it either needs more
5618 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5619 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5620 * when it has nothing left to do, and may remove any analyser when it wants to
5621 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005622 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005623int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005624{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005625 struct http_txn *txn = &s->txn;
5626 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005627 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005628 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005629 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005630 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005631
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005632 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 +02005633 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005634 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005635 rep,
5636 rep->rex, rep->wex,
5637 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005638 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005639 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005640
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005641 /*
5642 * Now parse the partial (or complete) lines.
5643 * We will check the response syntax, and also join multi-line
5644 * headers. An index of all the lines will be elaborated while
5645 * parsing.
5646 *
5647 * For the parsing, we use a 28 states FSM.
5648 *
5649 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005650 * rep->buf->p = beginning of response
5651 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5652 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005653 * msg->eol = end of current header or line (LF or CRLF)
5654 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005655 */
5656
Willy Tarreau628c40c2014-04-24 19:11:26 +02005657 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005658 /* There's a protected area at the end of the buffer for rewriting
5659 * purposes. We don't want to start to parse the request if the
5660 * protected area is affected, because we may have to move processed
5661 * data later, which is much more complicated.
5662 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005663 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005664 if (unlikely(!channel_reserved(rep))) {
5665 /* some data has still not left the buffer, wake us once that's done */
5666 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5667 goto abort_response;
5668 channel_dont_close(rep);
5669 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005670 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005671 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005672 }
5673
Willy Tarreau379357a2013-06-08 12:55:46 +02005674 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5675 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5676 buffer_slow_realign(rep->buf);
5677
Willy Tarreau9b28e032012-10-12 23:49:43 +02005678 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005679 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005680 }
5681
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005682 /* 1: we might have to print this header in debug mode */
5683 if (unlikely((global.mode & MODE_DEBUG) &&
5684 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau8767b132014-10-21 19:36:09 +02005685 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005686 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005687
Willy Tarreau9b28e032012-10-12 23:49:43 +02005688 sol = rep->buf->p;
5689 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005690 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005691
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005692 sol += hdr_idx_first_pos(&txn->hdr_idx);
5693 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005694
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005695 while (cur_idx) {
5696 eol = sol + txn->hdr_idx.v[cur_idx].len;
5697 debug_hdr("srvhdr", s, sol, eol);
5698 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5699 cur_idx = txn->hdr_idx.v[cur_idx].next;
5700 }
5701 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005702
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005703 /*
5704 * Now we quickly check if we have found a full valid response.
5705 * If not so, we check the FD and buffer states before leaving.
5706 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005707 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005708 * responses are checked first.
5709 *
5710 * Depending on whether the client is still there or not, we
5711 * may send an error response back or not. Note that normally
5712 * we should only check for HTTP status there, and check I/O
5713 * errors somewhere else.
5714 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005715
Willy Tarreau655dce92009-11-08 13:10:58 +01005716 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005717 /* Invalid response */
5718 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5719 /* we detected a parsing error. We want to archive this response
5720 * in the dedicated proxy area for later troubleshooting.
5721 */
5722 hdr_response_bad:
5723 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005724 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005725
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005726 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005727 if (objt_server(s->target)) {
5728 objt_server(s->target)->counters.failed_resp++;
5729 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005730 }
Willy Tarreau64648412010-03-05 10:41:54 +01005731 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005732 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005733 rep->analysers = 0;
5734 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005735 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005736 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005737 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005738
5739 if (!(s->flags & SN_ERR_MASK))
5740 s->flags |= SN_ERR_PRXCOND;
5741 if (!(s->flags & SN_FINST_MASK))
5742 s->flags |= SN_FINST_H;
5743
5744 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005745 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005746
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005747 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005748 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005749 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005750 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005751 goto hdr_response_bad;
5752 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005753
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005754 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005755 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005756 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005757 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005758 else if (txn->flags & TX_NOT_FIRST)
5759 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005760
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005761 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005762 if (objt_server(s->target)) {
5763 objt_server(s->target)->counters.failed_resp++;
5764 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005765 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005766
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005767 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005768 rep->analysers = 0;
5769 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005770 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005771 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005772 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005773
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005774 if (!(s->flags & SN_ERR_MASK))
5775 s->flags |= SN_ERR_SRVCL;
5776 if (!(s->flags & SN_FINST_MASK))
5777 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005778 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005779 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005780
Willy Tarreau2a4f5112014-06-23 15:22:31 +02005781 /* read timeout : return a 504 to the client. */
5782 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005783 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005784 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005785 else if (txn->flags & TX_NOT_FIRST)
5786 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005787
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005788 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005789 if (objt_server(s->target)) {
5790 objt_server(s->target)->counters.failed_resp++;
5791 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005792 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005793
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005794 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005795 rep->analysers = 0;
5796 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005797 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005798 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005799 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005800
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005801 if (!(s->flags & SN_ERR_MASK))
5802 s->flags |= SN_ERR_SRVTO;
5803 if (!(s->flags & SN_FINST_MASK))
5804 s->flags |= SN_FINST_H;
5805 return 0;
5806 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005807
Willy Tarreauf003d372012-11-26 13:35:37 +01005808 /* client abort with an abortonclose */
5809 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5810 s->fe->fe_counters.cli_aborts++;
5811 s->be->be_counters.cli_aborts++;
5812 if (objt_server(s->target))
5813 objt_server(s->target)->counters.cli_aborts++;
5814
5815 rep->analysers = 0;
5816 channel_auto_close(rep);
5817
5818 txn->status = 400;
5819 bi_erase(rep);
5820 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5821
5822 if (!(s->flags & SN_ERR_MASK))
5823 s->flags |= SN_ERR_CLICL;
5824 if (!(s->flags & SN_FINST_MASK))
5825 s->flags |= SN_FINST_H;
5826
5827 /* process_session() will take care of the error */
5828 return 0;
5829 }
5830
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005831 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005832 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005833 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005834 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005835 else if (txn->flags & TX_NOT_FIRST)
5836 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005837
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005838 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005839 if (objt_server(s->target)) {
5840 objt_server(s->target)->counters.failed_resp++;
5841 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005842 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005843
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005844 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005845 rep->analysers = 0;
5846 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005847 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005848 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005849 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005850
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005851 if (!(s->flags & SN_ERR_MASK))
5852 s->flags |= SN_ERR_SRVCL;
5853 if (!(s->flags & SN_FINST_MASK))
5854 s->flags |= SN_FINST_H;
5855 return 0;
5856 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005857
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005858 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005859 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005860 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005861 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005862 else if (txn->flags & TX_NOT_FIRST)
5863 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005864
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005865 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005866 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005867 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005868
5869 if (!(s->flags & SN_ERR_MASK))
5870 s->flags |= SN_ERR_CLICL;
5871 if (!(s->flags & SN_FINST_MASK))
5872 s->flags |= SN_FINST_H;
5873
5874 /* process_session() will take care of the error */
5875 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005876 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005877
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005878 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005879 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005880 return 0;
5881 }
5882
5883 /* More interesting part now : we know that we have a complete
5884 * response which at least looks like HTTP. We have an indicator
5885 * of each header's length, so we can parse them quickly.
5886 */
5887
5888 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005889 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005890
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005891 /*
5892 * 1: get the status code
5893 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005894 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005895 if (n < 1 || n > 5)
5896 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005897 /* when the client triggers a 4xx from the server, it's most often due
5898 * to a missing object or permission. These events should be tracked
5899 * because if they happen often, it may indicate a brute force or a
5900 * vulnerability scan.
5901 */
5902 if (n == 4)
5903 session_inc_http_err_ctr(s);
5904
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005905 if (objt_server(s->target))
5906 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005907
Willy Tarreau55645552015-05-01 13:26:00 +02005908 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5909 * exactly one digit "." one digit. This check may be disabled using
5910 * option accept-invalid-http-response.
5911 */
5912 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5913 if (msg->sl.st.v_l != 8) {
5914 msg->err_pos = 0;
5915 goto hdr_response_bad;
5916 }
5917
5918 if (rep->buf->p[4] != '/' ||
5919 !isdigit((unsigned char)rep->buf->p[5]) ||
5920 rep->buf->p[6] != '.' ||
5921 !isdigit((unsigned char)rep->buf->p[7])) {
5922 msg->err_pos = 4;
5923 goto hdr_response_bad;
5924 }
5925 }
5926
Willy Tarreau5b154472009-12-21 20:11:07 +01005927 /* check if the response is HTTP/1.1 or above */
5928 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005929 ((rep->buf->p[5] > '1') ||
5930 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005931 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005932
5933 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005934 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 +01005935
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005936 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005937 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005938
Willy Tarreau9b28e032012-10-12 23:49:43 +02005939 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005940
Willy Tarreau39650402010-03-15 19:44:39 +01005941 /* Adjust server's health based on status code. Note: status codes 501
5942 * and 505 are triggered on demand by client request, so we must not
5943 * count them as server failures.
5944 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005945 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005946 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005947 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005948 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005949 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005950 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005951
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005952 /*
5953 * 2: check for cacheability.
5954 */
5955
5956 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005957 case 100:
5958 /*
5959 * We may be facing a 100-continue response, in which case this
5960 * is not the right response, and we're waiting for the next one.
5961 * Let's allow this response to go to the client and wait for the
5962 * next one.
5963 */
5964 hdr_idx_init(&txn->hdr_idx);
5965 msg->next -= channel_forward(rep, msg->next);
5966 msg->msg_state = HTTP_MSG_RPBEFORE;
5967 txn->status = 0;
5968 s->logs.t_data = -1; /* was not a response yet */
5969 goto next_one;
5970
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005971 case 200:
5972 case 203:
5973 case 206:
5974 case 300:
5975 case 301:
5976 case 410:
5977 /* RFC2616 @13.4:
5978 * "A response received with a status code of
5979 * 200, 203, 206, 300, 301 or 410 MAY be stored
5980 * by a cache (...) unless a cache-control
5981 * directive prohibits caching."
5982 *
5983 * RFC2616 @9.5: POST method :
5984 * "Responses to this method are not cacheable,
5985 * unless the response includes appropriate
5986 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005987 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005988 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005989 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005990 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5991 break;
5992 default:
5993 break;
5994 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005995
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005996 /*
5997 * 3: we may need to capture headers
5998 */
5999 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01006000 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006001 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006002 txn->rsp.cap, s->fe->rsp_cap);
6003
Willy Tarreau4db603d2015-05-01 10:05:17 +02006004 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6005 * by RFC7230#3.3.3 :
6006 *
6007 * The length of a message body is determined by one of the following
6008 * (in order of precedence):
6009 *
6010 * 1. Any response to a HEAD request and any response with a 1xx
6011 * (Informational), 204 (No Content), or 304 (Not Modified) status
6012 * code is always terminated by the first empty line after the
6013 * header fields, regardless of the header fields present in the
6014 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006015 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006016 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6017 * the connection will become a tunnel immediately after the empty
6018 * line that concludes the header fields. A client MUST ignore any
6019 * Content-Length or Transfer-Encoding header fields received in
6020 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006021 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006022 * 3. If a Transfer-Encoding header field is present and the chunked
6023 * transfer coding (Section 4.1) is the final encoding, the message
6024 * body length is determined by reading and decoding the chunked
6025 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006026 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006027 * If a Transfer-Encoding header field is present in a response and
6028 * the chunked transfer coding is not the final encoding, the
6029 * message body length is determined by reading the connection until
6030 * it is closed by the server. If a Transfer-Encoding header field
6031 * is present in a request and the chunked transfer coding is not
6032 * the final encoding, the message body length cannot be determined
6033 * reliably; the server MUST respond with the 400 (Bad Request)
6034 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006035 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006036 * If a message is received with both a Transfer-Encoding and a
6037 * Content-Length header field, the Transfer-Encoding overrides the
6038 * Content-Length. Such a message might indicate an attempt to
6039 * perform request smuggling (Section 9.5) or response splitting
6040 * (Section 9.4) and ought to be handled as an error. A sender MUST
6041 * remove the received Content-Length field prior to forwarding such
6042 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006043 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006044 * 4. If a message is received without Transfer-Encoding and with
6045 * either multiple Content-Length header fields having differing
6046 * field-values or a single Content-Length header field having an
6047 * invalid value, then the message framing is invalid and the
6048 * recipient MUST treat it as an unrecoverable error. If this is a
6049 * request message, the server MUST respond with a 400 (Bad Request)
6050 * status code and then close the connection. If this is a response
6051 * message received by a proxy, the proxy MUST close the connection
6052 * to the server, discard the received response, and send a 502 (Bad
6053 * Gateway) response to the client. If this is a response message
6054 * received by a user agent, the user agent MUST close the
6055 * connection to the server and discard the received response.
6056 *
6057 * 5. If a valid Content-Length header field is present without
6058 * Transfer-Encoding, its decimal value defines the expected message
6059 * body length in octets. If the sender closes the connection or
6060 * the recipient times out before the indicated number of octets are
6061 * received, the recipient MUST consider the message to be
6062 * incomplete and close the connection.
6063 *
6064 * 6. If this is a request message and none of the above are true, then
6065 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006066 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006067 * 7. Otherwise, this is a response message without a declared message
6068 * body length, so the message body length is determined by the
6069 * number of octets received prior to the server closing the
6070 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006071 */
6072
6073 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006074 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006075 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006076 * FIXME: should we parse anyway and return an error on chunked encoding ?
6077 */
6078 if (txn->meth == HTTP_METH_HEAD ||
6079 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006080 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006081 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006082 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006083 goto skip_content_length;
6084 }
6085
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006086 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006087 ctx.idx = 0;
Willy Tarreaue77bc172015-05-01 10:06:30 +02006088 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006089 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006090 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6091 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006092 /* bad transfer-encoding (chunked followed by something else) */
6093 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006094 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006095 break;
6096 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006097 }
6098
Willy Tarreaudfa3d922015-04-30 10:57:51 +02006099 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006100 ctx.idx = 0;
Willy Tarreau660418d2015-05-01 10:25:45 +02006101 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreaudfa3d922015-04-30 10:57:51 +02006102 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6103 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6104 }
Willy Tarreau660418d2015-05-01 10:25:45 +02006105 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006106 signed long long cl;
6107
Willy Tarreauad14f752011-09-02 20:33:27 +02006108 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006109 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006110 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006111 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006112
Willy Tarreauad14f752011-09-02 20:33:27 +02006113 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006114 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006115 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006116 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006117
Willy Tarreauad14f752011-09-02 20:33:27 +02006118 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006119 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006120 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006121 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006122
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006123 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006124 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006125 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006126 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006127
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006128 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006129 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006130 }
6131
William Lallemand82fe75c2012-10-23 10:25:10 +02006132 if (s->fe->comp || s->be->comp)
6133 select_compression_response_header(s, rep->buf);
6134
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006135skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006136 /* Now we have to check if we need to modify the Connection header.
6137 * This is more difficult on the response than it is on the request,
6138 * because we can have two different HTTP versions and we don't know
6139 * how the client will interprete a response. For instance, let's say
6140 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6141 * HTTP/1.1 response without any header. Maybe it will bound itself to
6142 * HTTP/1.0 because it only knows about it, and will consider the lack
6143 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6144 * the lack of header as a keep-alive. Thus we will use two flags
6145 * indicating how a request MAY be understood by the client. In case
6146 * of multiple possibilities, we'll fix the header to be explicit. If
6147 * ambiguous cases such as both close and keepalive are seen, then we
6148 * will fall back to explicit close. Note that we won't take risks with
6149 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006150 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006151 */
6152
Willy Tarreaudc008c52010-02-01 16:20:08 +01006153 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6154 txn->status == 101)) {
6155 /* Either we've established an explicit tunnel, or we're
6156 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006157 * to understand the next protocols. We have to switch to tunnel
6158 * mode, so that we transfer the request and responses then let
6159 * this protocol pass unmodified. When we later implement specific
6160 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006161 * header which contains information about that protocol for
6162 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006163 */
6164 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6165 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006166 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6167 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006168 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6169 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006170 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006171
Willy Tarreau70dffda2014-01-30 03:07:23 +01006172 /* this situation happens when combining pretend-keepalive with httpclose. */
6173 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
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 Tarreau70dffda2014-01-30 03:07:23 +01006176 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6177
Willy Tarreau60466522010-01-18 19:08:45 +01006178 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006179 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006180 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6181 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006182
Willy Tarreau60466522010-01-18 19:08:45 +01006183 /* now adjust header transformations depending on current state */
6184 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6185 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6186 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006187 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006188 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006189 }
Willy Tarreau60466522010-01-18 19:08:45 +01006190 else { /* SCL / KAL */
6191 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006192 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006193 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006194 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006195
Willy Tarreau60466522010-01-18 19:08:45 +01006196 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006197 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006198
Willy Tarreau60466522010-01-18 19:08:45 +01006199 /* Some keep-alive responses are converted to Server-close if
6200 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006201 */
Willy Tarreau60466522010-01-18 19:08:45 +01006202 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6203 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006204 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006205 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006206 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006207 }
6208
Willy Tarreau7959a552013-09-23 16:44:27 +02006209 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006210 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006211
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006212 /* end of job, return OK */
6213 rep->analysers &= ~an_bit;
6214 rep->analyse_exp = TICK_ETERNITY;
6215 channel_auto_close(rep);
6216 return 1;
6217
6218 abort_keep_alive:
6219 /* A keep-alive request to the server failed on a network error.
6220 * The client is required to retry. We need to close without returning
6221 * any other information so that the client retries.
6222 */
6223 txn->status = 0;
6224 rep->analysers = 0;
6225 s->req->analysers = 0;
6226 channel_auto_close(rep);
6227 s->logs.logwait = 0;
6228 s->logs.level = 0;
6229 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
6230 bi_erase(rep);
6231 stream_int_retnclose(rep->cons, NULL);
6232 return 0;
6233}
6234
6235/* This function performs all the processing enabled for the current response.
6236 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6237 * and updates s->rep->analysers. It might make sense to explode it into several
6238 * other functions. It works like process_request (see indications above).
6239 */
6240int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6241{
6242 struct http_txn *txn = &s->txn;
6243 struct http_msg *msg = &txn->rsp;
6244 struct proxy *cur_proxy;
6245 struct cond_wordlist *wl;
6246 struct http_res_rule *http_res_last_rule = NULL;
6247
6248 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6249 now_ms, __FUNCTION__,
6250 s,
6251 rep,
6252 rep->rex, rep->wex,
6253 rep->flags,
6254 rep->buf->i,
6255 rep->analysers);
6256
6257 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6258 return 0;
6259
6260 rep->analysers &= ~an_bit;
6261 rep->analyse_exp = TICK_ETERNITY;
6262
Willy Tarreau70730dd2014-04-24 18:06:27 +02006263 /* The stats applet needs to adjust the Connection header but we don't
6264 * apply any filter there.
6265 */
6266 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6267 goto skip_filters;
6268
Willy Tarreau58975672014-04-24 21:13:57 +02006269 /*
6270 * We will have to evaluate the filters.
6271 * As opposed to version 1.2, now they will be evaluated in the
6272 * filters order and not in the header order. This means that
6273 * each filter has to be validated among all headers.
6274 *
6275 * Filters are tried with ->be first, then with ->fe if it is
6276 * different from ->be.
6277 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006278
Willy Tarreau58975672014-04-24 21:13:57 +02006279 cur_proxy = s->be;
6280 while (1) {
6281 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006282
Willy Tarreau58975672014-04-24 21:13:57 +02006283 /* evaluate http-response rules */
6284 if (!http_res_last_rule)
6285 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 +02006286
Willy Tarreau58975672014-04-24 21:13:57 +02006287 /* try headers filters */
6288 if (rule_set->rsp_exp != NULL) {
6289 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6290 return_bad_resp:
6291 if (objt_server(s->target)) {
6292 objt_server(s->target)->counters.failed_resp++;
6293 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006294 }
Willy Tarreau58975672014-04-24 21:13:57 +02006295 s->be->be_counters.failed_resp++;
6296 return_srv_prx_502:
6297 rep->analysers = 0;
6298 txn->status = 502;
6299 s->logs.t_data = -1; /* was not a valid response */
6300 rep->prod->flags |= SI_FL_NOLINGER;
6301 bi_erase(rep);
6302 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6303 if (!(s->flags & SN_ERR_MASK))
6304 s->flags |= SN_ERR_PRXCOND;
6305 if (!(s->flags & SN_FINST_MASK))
6306 s->flags |= SN_FINST_H;
6307 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006308 }
Willy Tarreau58975672014-04-24 21:13:57 +02006309 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006310
Willy Tarreau58975672014-04-24 21:13:57 +02006311 /* has the response been denied ? */
6312 if (txn->flags & TX_SVDENY) {
6313 if (objt_server(s->target))
6314 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006315
Willy Tarreau58975672014-04-24 21:13:57 +02006316 s->be->be_counters.denied_resp++;
6317 s->fe->fe_counters.denied_resp++;
6318 if (s->listener->counters)
6319 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006320
Willy Tarreau58975672014-04-24 21:13:57 +02006321 goto return_srv_prx_502;
6322 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006323
Willy Tarreau58975672014-04-24 21:13:57 +02006324 /* add response headers from the rule sets in the same order */
6325 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006326 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006327 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006328 if (wl->cond) {
6329 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6330 ret = acl_pass(ret);
6331 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6332 ret = !ret;
6333 if (!ret)
6334 continue;
6335 }
6336 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6337 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006338 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006339
Willy Tarreau58975672014-04-24 21:13:57 +02006340 /* check whether we're already working on the frontend */
6341 if (cur_proxy == s->fe)
6342 break;
6343 cur_proxy = s->fe;
6344 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006345
Willy Tarreau58975672014-04-24 21:13:57 +02006346 /* OK that's all we can do for 1xx responses */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006347 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006348 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006349
Willy Tarreau58975672014-04-24 21:13:57 +02006350 /*
6351 * Now check for a server cookie.
6352 */
6353 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6354 (s->be->options & PR_O_CHK_CACHE))
6355 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006356
Willy Tarreau58975672014-04-24 21:13:57 +02006357 /*
6358 * Check for cache-control or pragma headers if required.
6359 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006360 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 +02006361 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006362
Willy Tarreau58975672014-04-24 21:13:57 +02006363 /*
6364 * Add server cookie in the response if needed
6365 */
6366 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6367 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6368 (!(s->flags & SN_DIRECT) ||
6369 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6370 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6371 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6372 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6373 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6374 !(s->flags & SN_IGNORE_PRST)) {
6375 /* the server is known, it's not the one the client requested, or the
6376 * cookie's last seen date needs to be refreshed. We have to
6377 * insert a set-cookie here, except if we want to insert only on POST
6378 * requests and this one isn't. Note that servers which don't have cookies
6379 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006380 */
Willy Tarreau58975672014-04-24 21:13:57 +02006381 if (!objt_server(s->target)->cookie) {
6382 chunk_printf(&trash,
6383 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6384 s->be->cookie_name);
6385 }
6386 else {
6387 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006388
Willy Tarreau58975672014-04-24 21:13:57 +02006389 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6390 /* emit last_date, which is mandatory */
6391 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6392 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6393 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006394
Willy Tarreau58975672014-04-24 21:13:57 +02006395 if (s->be->cookie_maxlife) {
6396 /* emit first_date, which is either the original one or
6397 * the current date.
6398 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006399 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006400 s30tob64(txn->cookie_first_date ?
6401 txn->cookie_first_date >> 2 :
6402 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006403 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006404 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006405 }
Willy Tarreau58975672014-04-24 21:13:57 +02006406 chunk_appendf(&trash, "; path=/");
6407 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006408
Willy Tarreau58975672014-04-24 21:13:57 +02006409 if (s->be->cookie_domain)
6410 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006411
Willy Tarreau58975672014-04-24 21:13:57 +02006412 if (s->be->ck_opts & PR_CK_HTTPONLY)
6413 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006414
Willy Tarreau58975672014-04-24 21:13:57 +02006415 if (s->be->ck_opts & PR_CK_SECURE)
6416 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006417
Willy Tarreau58975672014-04-24 21:13:57 +02006418 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6419 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006420
Willy Tarreau58975672014-04-24 21:13:57 +02006421 txn->flags &= ~TX_SCK_MASK;
6422 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6423 /* the server did not change, only the date was updated */
6424 txn->flags |= TX_SCK_UPDATED;
6425 else
6426 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006427
Willy Tarreau58975672014-04-24 21:13:57 +02006428 /* Here, we will tell an eventual cache on the client side that we don't
6429 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6430 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6431 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006432 */
Willy Tarreau58975672014-04-24 21:13:57 +02006433 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006434
Willy Tarreau58975672014-04-24 21:13:57 +02006435 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006436
Willy Tarreau58975672014-04-24 21:13:57 +02006437 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6438 "Cache-control: private", 22) < 0))
6439 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006440 }
Willy Tarreau58975672014-04-24 21:13:57 +02006441 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006442
Willy Tarreau58975672014-04-24 21:13:57 +02006443 /*
6444 * Check if result will be cacheable with a cookie.
6445 * We'll block the response if security checks have caught
6446 * nasty things such as a cacheable cookie.
6447 */
6448 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6449 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6450 (s->be->options & PR_O_CHK_CACHE)) {
6451 /* we're in presence of a cacheable response containing
6452 * a set-cookie header. We'll block it as requested by
6453 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006454 */
Willy Tarreau58975672014-04-24 21:13:57 +02006455 if (objt_server(s->target))
6456 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006457
Willy Tarreau58975672014-04-24 21:13:57 +02006458 s->be->be_counters.denied_resp++;
6459 s->fe->fe_counters.denied_resp++;
6460 if (s->listener->counters)
6461 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006462
Willy Tarreau58975672014-04-24 21:13:57 +02006463 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6464 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6465 send_log(s->be, LOG_ALERT,
6466 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6467 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6468 goto return_srv_prx_502;
6469 }
Willy Tarreau03945942009-12-22 16:50:27 +01006470
Willy Tarreau70730dd2014-04-24 18:06:27 +02006471 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006472 /*
6473 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6474 * If an "Upgrade" token is found, the header is left untouched in order
6475 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006476 * if anything but "Upgrade" is present in the Connection header. We don't
6477 * want to touch any 101 response either since it's switching to another
6478 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006479 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006480 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006481 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6482 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6483 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6484 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006485
Willy Tarreau58975672014-04-24 21:13:57 +02006486 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6487 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6488 /* we want a keep-alive response here. Keep-alive header
6489 * required if either side is not 1.1.
6490 */
6491 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6492 want_flags |= TX_CON_KAL_SET;
6493 }
6494 else {
6495 /* we want a close response here. Close header required if
6496 * the server is 1.1, regardless of the client.
6497 */
6498 if (msg->flags & HTTP_MSGF_VER_11)
6499 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006500 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006501
Willy Tarreau58975672014-04-24 21:13:57 +02006502 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6503 http_change_connection_header(txn, msg, want_flags);
6504 }
6505
6506 skip_header_mangling:
6507 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6508 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6509 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006510
Willy Tarreau58975672014-04-24 21:13:57 +02006511 /* if the user wants to log as soon as possible, without counting
6512 * bytes from the server, then this is the right moment. We have
6513 * to temporarily assign bytes_out to log what we currently have.
6514 */
6515 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6516 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6517 s->logs.bytes_out = txn->rsp.eoh;
6518 s->do_log(s);
6519 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006520 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006521 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006522}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006523
Willy Tarreaud98cf932009-12-27 22:54:55 +01006524/* This function is an analyser which forwards response body (including chunk
6525 * sizes if any). It is called as soon as we must forward, even if we forward
6526 * zero byte. The only situation where it must not be called is when we're in
6527 * tunnel mode and we want to forward till the close. It's used both to forward
6528 * remaining data and to resync after end of body. It expects the msg_state to
6529 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6530 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006531 *
6532 * It is capable of compressing response data both in content-length mode and
6533 * in chunked mode. The state machines follows different flows depending on
6534 * whether content-length and chunked modes are used, since there are no
6535 * trailers in content-length :
6536 *
6537 * chk-mode cl-mode
6538 * ,----- BODY -----.
6539 * / \
6540 * V size > 0 V chk-mode
6541 * .--> SIZE -------------> DATA -------------> CRLF
6542 * | | size == 0 | last byte |
6543 * | v final crlf v inspected |
6544 * | TRAILERS -----------> DONE |
6545 * | |
6546 * `----------------------------------------------'
6547 *
6548 * Compression only happens in the DATA state, and must be flushed in final
6549 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6550 * is performed at once on final states for all bytes parsed, or when leaving
6551 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006552 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006553int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006554{
6555 struct http_txn *txn = &s->txn;
6556 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006557 static struct buffer *tmpbuf = NULL;
6558 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006559 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006560
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006561 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6562 return 0;
6563
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006564 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006565 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006566 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006567 /* Output closed while we were sending data. We must abort and
6568 * wake the other side up.
6569 */
6570 msg->msg_state = HTTP_MSG_ERROR;
6571 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006572 return 1;
6573 }
6574
Willy Tarreau4fe41902010-06-07 22:27:41 +02006575 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006576 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006577
Willy Tarreau5bebcd02014-07-10 19:06:10 +02006578 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006579 /* we have msg->sov which points to the first byte of message
6580 * body, and res->buf.p still points to the beginning of the
6581 * message. We forward the headers now, as we don't need them
6582 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006583 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006584 b_adv(res->buf, msg->sov);
6585 msg->next -= msg->sov;
6586 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006587
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006588 /* The previous analysers guarantee that the state is somewhere
6589 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6590 * msg->next are always correct.
6591 */
6592 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6593 if (msg->flags & HTTP_MSGF_TE_CHNK)
6594 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6595 else
6596 msg->msg_state = HTTP_MSG_DATA;
6597 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006598 }
6599
Willy Tarreauefdf0942014-04-24 20:08:57 +02006600 if (res->to_forward) {
6601 /* We can't process the buffer's contents yet */
6602 res->flags |= CF_WAKE_WRITE;
6603 goto missing_data;
6604 }
6605
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006606 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6607 /* We need a compression buffer in the DATA state to put the
6608 * output of compressed data, and in CRLF state to let the
6609 * TRAILERS state finish the job of removing the trailing CRLF.
6610 */
6611 if (unlikely(tmpbuf == NULL)) {
6612 /* this is the first time we need the compression buffer */
6613 tmpbuf = pool_alloc2(pool2_buffer);
6614 if (tmpbuf == NULL)
6615 goto aborted_xfer; /* no memory */
6616 }
6617
6618 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006619 if (ret < 0) {
6620 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006621 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006622 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006623 compressing = 1;
6624 }
6625
Willy Tarreaud98cf932009-12-27 22:54:55 +01006626 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006627 switch (msg->msg_state - HTTP_MSG_DATA) {
6628 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006629 /* we may have some pending data starting at res->buf->p */
6630 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006631 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006632 if (ret < 0)
6633 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006634
Willy Tarreaud5a67832014-04-21 10:54:27 +02006635 if (msg->chunk_len) {
6636 /* input empty or output full */
6637 if (res->buf->i > msg->next)
6638 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006639 goto missing_data;
6640 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006641 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006642 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006643 if (msg->chunk_len > res->buf->i - msg->next) {
6644 /* output full */
6645 res->flags |= CF_WAKE_WRITE;
6646 goto missing_data;
6647 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006648 msg->next += msg->chunk_len;
6649 msg->chunk_len = 0;
6650 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006651
6652 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006653 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006654 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006655 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006656 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006657 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006658 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006659 /* fall through for HTTP_MSG_CHUNK_CRLF */
6660
6661 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6662 /* we want the CRLF after the data */
6663
6664 ret = http_skip_chunk_crlf(msg);
6665 if (ret == 0)
6666 goto missing_data;
6667 else if (ret < 0) {
6668 if (msg->err_pos >= 0)
6669 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6670 goto return_bad_res;
6671 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006672 /* we're in MSG_CHUNK_SIZE now, fall through */
6673
6674 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006675 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006676 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006677 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006678 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006679
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006680 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006681 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006682 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006683 else if (ret < 0) {
6684 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006685 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006686 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006687 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006688 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006689 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006690
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006691 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006692 if (unlikely(compressing)) {
6693 /* we need to flush output contents before syncing FSMs */
6694 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6695 compressing = 0;
6696 }
6697
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006698 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006699 if (ret == 0)
6700 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006701 else if (ret < 0) {
6702 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006703 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006704 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006705 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006706 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006707
6708 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006709 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006710 if (unlikely(compressing)) {
6711 /* we need to flush output contents before syncing FSMs */
6712 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6713 compressing = 0;
6714 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006715
Willy Tarreauc623c172014-04-18 09:53:50 +02006716 /* we may have some pending data starting at res->buf->p
6717 * such as a last chunk of data or trailers.
6718 */
6719 b_adv(res->buf, msg->next);
6720 msg->next = 0;
6721
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006722 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006723 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006724 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6725 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006726 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006727
Willy Tarreau610ecce2010-01-04 21:15:02 +01006728 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006729 /* some state changes occurred, maybe the analyser
6730 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006731 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006732 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006733 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006734 /* response errors are most likely due to
6735 * the client aborting the transfer.
6736 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006737 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006738 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006739 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006740 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006741 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006742 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006743 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006744 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006745 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006746 }
6747 }
6748
Willy Tarreaud98cf932009-12-27 22:54:55 +01006749 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006750 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006751 if (unlikely(compressing)) {
6752 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006753 compressing = 0;
6754 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006755
Willy Tarreauc623c172014-04-18 09:53:50 +02006756 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6757 b_adv(res->buf, msg->next);
6758 msg->next = 0;
6759 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6760 }
6761
Willy Tarreauf003d372012-11-26 13:35:37 +01006762 if (res->flags & CF_SHUTW)
6763 goto aborted_xfer;
6764
6765 /* stop waiting for data if the input is closed before the end. If the
6766 * client side was already closed, it means that the client has aborted,
6767 * so we don't want to count this as a server abort. Otherwise it's a
6768 * server abort.
6769 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006770 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006771 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006772 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006773 if (!(s->flags & SN_ERR_MASK))
6774 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006775 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006776 if (objt_server(s->target))
6777 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006778 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006779 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006780
Willy Tarreau40dba092010-03-04 18:14:51 +01006781 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006782 if (!s->req->analysers)
6783 goto return_bad_res;
6784
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006785 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006786 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006787 * Similarly, with keep-alive on the client side, we don't want to forward a
6788 * close.
6789 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006790 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006791 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6792 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006793 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006794
Willy Tarreau5c620922011-05-11 19:56:11 +02006795 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006796 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006797 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006798 * modes are already handled by the stream sock layer. We must not do
6799 * this in content-length mode because it could present the MSG_MORE
6800 * flag with the last block of forwarded data, which would cause an
6801 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006802 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006803 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006804 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006805
Willy Tarreaud98cf932009-12-27 22:54:55 +01006806 /* the session handler will take care of timeouts and errors */
6807 return 0;
6808
Willy Tarreau40dba092010-03-04 18:14:51 +01006809 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006810 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006811 if (objt_server(s->target))
6812 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006813
6814 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006815 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006816 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006817 compressing = 0;
6818 }
6819
Willy Tarreauc623c172014-04-18 09:53:50 +02006820 /* we may have some pending data starting at res->buf->p */
6821 if (s->comp_algo == NULL) {
6822 b_adv(res->buf, msg->next);
6823 msg->next = 0;
6824 }
6825
Willy Tarreaud98cf932009-12-27 22:54:55 +01006826 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006827 /* don't send any error message as we're in the body */
6828 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006829 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006830 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006831 if (objt_server(s->target))
6832 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006833
6834 if (!(s->flags & SN_ERR_MASK))
6835 s->flags |= SN_ERR_PRXCOND;
6836 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006837 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006838 return 0;
6839
6840 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006841 if (unlikely(compressing)) {
6842 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6843 compressing = 0;
6844 }
6845
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006846 txn->rsp.msg_state = HTTP_MSG_ERROR;
6847 /* don't send any error message as we're in the body */
6848 stream_int_retnclose(res->cons, NULL);
6849 res->analysers = 0;
6850 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6851
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006852 s->fe->fe_counters.cli_aborts++;
6853 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006854 if (objt_server(s->target))
6855 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006856
6857 if (!(s->flags & SN_ERR_MASK))
6858 s->flags |= SN_ERR_CLICL;
6859 if (!(s->flags & SN_FINST_MASK))
6860 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006861 return 0;
6862}
6863
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006864/* Iterate the same filter through all request headers.
6865 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006866 * Since it can manage the switch to another backend, it updates the per-proxy
6867 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006868 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006869int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006870{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006871 char *cur_ptr, *cur_end, *cur_next;
6872 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006873 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006874 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006875 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006876
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006877 last_hdr = 0;
6878
Willy Tarreau9b28e032012-10-12 23:49:43 +02006879 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006880 old_idx = 0;
6881
6882 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006883 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006884 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006885 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006886 (exp->action == ACT_ALLOW ||
6887 exp->action == ACT_DENY ||
6888 exp->action == ACT_TARPIT))
6889 return 0;
6890
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006891 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006892 if (!cur_idx)
6893 break;
6894
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006895 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006896 cur_ptr = cur_next;
6897 cur_end = cur_ptr + cur_hdr->len;
6898 cur_next = cur_end + cur_hdr->cr + 1;
6899
6900 /* Now we have one header between cur_ptr and cur_end,
6901 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006902 */
6903
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02006904 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006905 switch (exp->action) {
6906 case ACT_SETBE:
6907 /* It is not possible to jump a second time.
6908 * FIXME: should we return an HTTP/500 here so that
6909 * the admin knows there's a problem ?
6910 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006911 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006912 break;
6913
6914 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006915 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006916 last_hdr = 1;
6917 break;
6918
6919 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006920 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006921 last_hdr = 1;
6922 break;
6923
6924 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006925 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006926 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006927 break;
6928
6929 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006930 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006931 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006932 break;
6933
6934 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006935 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6936 if (trash.len < 0)
6937 return -1;
6938
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006939 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006940 /* FIXME: if the user adds a newline in the replacement, the
6941 * index will not be recalculated for now, and the new line
6942 * will not be counted as a new header.
6943 */
6944
6945 cur_end += delta;
6946 cur_next += delta;
6947 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006948 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006949 break;
6950
6951 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006952 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006953 cur_next += delta;
6954
Willy Tarreaufa355d42009-11-29 18:12:29 +01006955 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006956 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6957 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006958 cur_hdr->len = 0;
6959 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006960 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006961 break;
6962
6963 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006964 }
6965
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006966 /* keep the link from this header to next one in case of later
6967 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006968 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006969 old_idx = cur_idx;
6970 }
6971 return 0;
6972}
6973
6974
6975/* Apply the filter to the request line.
6976 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6977 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006978 * Since it can manage the switch to another backend, it updates the per-proxy
6979 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006980 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006981int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006982{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006983 char *cur_ptr, *cur_end;
6984 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006985 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006986 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006987
Willy Tarreau3d300592007-03-18 18:34:41 +01006988 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006989 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006990 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006991 (exp->action == ACT_ALLOW ||
6992 exp->action == ACT_DENY ||
6993 exp->action == ACT_TARPIT))
6994 return 0;
6995 else if (exp->action == ACT_REMOVE)
6996 return 0;
6997
6998 done = 0;
6999
Willy Tarreau9b28e032012-10-12 23:49:43 +02007000 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007001 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007002
7003 /* Now we have the request line between cur_ptr and cur_end */
7004
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007005 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007006 switch (exp->action) {
7007 case ACT_SETBE:
7008 /* It is not possible to jump a second time.
7009 * FIXME: should we return an HTTP/500 here so that
7010 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007011 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007012 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007013 break;
7014
7015 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007016 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007017 done = 1;
7018 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007019
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007020 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007021 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007022 done = 1;
7023 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007024
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007025 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007026 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007027 done = 1;
7028 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007029
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007030 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007031 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007032 done = 1;
7033 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007034
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007035 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007036 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7037 if (trash.len < 0)
7038 return -1;
7039
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007040 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007041 /* FIXME: if the user adds a newline in the replacement, the
7042 * index will not be recalculated for now, and the new line
7043 * will not be counted as a new header.
7044 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007045
Willy Tarreaufa355d42009-11-29 18:12:29 +01007046 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007047 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007048 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007049 HTTP_MSG_RQMETH,
7050 cur_ptr, cur_end + 1,
7051 NULL, NULL);
7052 if (unlikely(!cur_end))
7053 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007054
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007055 /* we have a full request and we know that we have either a CR
7056 * or an LF at <ptr>.
7057 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007058 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7059 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007060 /* there is no point trying this regex on headers */
7061 return 1;
7062 }
7063 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007064 return done;
7065}
Willy Tarreau97de6242006-12-27 17:18:38 +01007066
Willy Tarreau58f10d72006-12-04 02:26:12 +01007067
Willy Tarreau58f10d72006-12-04 02:26:12 +01007068
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007069/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01007070 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007071 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007072 * unparsable request. Since it can manage the switch to another backend, it
7073 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007074 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007075int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007076{
Willy Tarreau6c123b12010-01-28 20:22:06 +01007077 struct http_txn *txn = &s->txn;
7078 struct hdr_exp *exp;
7079
7080 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007081 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007083 /*
7084 * The interleaving of transformations and verdicts
7085 * makes it difficult to decide to continue or stop
7086 * the evaluation.
7087 */
7088
Willy Tarreau6c123b12010-01-28 20:22:06 +01007089 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7090 break;
7091
Willy Tarreau3d300592007-03-18 18:34:41 +01007092 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007093 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007094 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007095 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007096
7097 /* if this filter had a condition, evaluate it now and skip to
7098 * next filter if the condition does not match.
7099 */
7100 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007101 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007102 ret = acl_pass(ret);
7103 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7104 ret = !ret;
7105
7106 if (!ret)
7107 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007108 }
7109
7110 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007111 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007112 if (unlikely(ret < 0))
7113 return -1;
7114
7115 if (likely(ret == 0)) {
7116 /* The filter did not match the request, it can be
7117 * iterated through all headers.
7118 */
Willy Tarreaua256fae2015-01-30 20:58:58 +01007119 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7120 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007121 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007122 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007123 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007124}
7125
7126
Willy Tarreaua15645d2007-03-18 16:22:39 +01007127
Willy Tarreau58f10d72006-12-04 02:26:12 +01007128/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007129 * Try to retrieve the server associated to the appsession.
7130 * If the server is found, it's assigned to the session.
7131 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007132void manage_client_side_appsession(struct session *s, const char *buf, int len) {
7133 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007134 appsess *asession = NULL;
7135 char *sessid_temp = NULL;
7136
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007137 if (len > s->be->appsession_len) {
7138 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007139 }
7140
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007141 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007142 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007143 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007144 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007145 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007146 }
7147
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007148 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007149 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007150 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007151 return;
7152 }
7153
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007154 memcpy(txn->sessid, buf, len);
7155 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007156 }
7157
7158 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7159 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007160 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007161 return;
7162 }
7163
Cyril Bontéb21570a2009-11-29 20:04:48 +01007164 memcpy(sessid_temp, buf, len);
7165 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007166
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007167 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007168 /* free previously allocated memory */
7169 pool_free2(apools.sessid, sessid_temp);
7170
7171 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007172 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7173 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007174 asession->request_count++;
7175
7176 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007177 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007178
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007179 while (srv) {
7180 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007181 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007182 (s->be->options & PR_O_PERSIST) ||
7183 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007184 /* we found the server and it's usable */
7185 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007186 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007187 s->flags |= SN_DIRECT | SN_ASSIGNED;
7188 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007189
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007190 break;
7191 } else {
7192 txn->flags &= ~TX_CK_MASK;
7193 txn->flags |= TX_CK_DOWN;
7194 }
7195 }
7196 srv = srv->next;
7197 }
7198 }
7199 }
7200}
7201
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007202/* Find the end of a cookie value contained between <s> and <e>. It works the
7203 * same way as with headers above except that the semi-colon also ends a token.
7204 * See RFC2965 for more information. Note that it requires a valid header to
7205 * return a valid result.
7206 */
7207char *find_cookie_value_end(char *s, const char *e)
7208{
7209 int quoted, qdpair;
7210
7211 quoted = qdpair = 0;
7212 for (; s < e; s++) {
7213 if (qdpair) qdpair = 0;
7214 else if (quoted) {
7215 if (*s == '\\') qdpair = 1;
7216 else if (*s == '"') quoted = 0;
7217 }
7218 else if (*s == '"') quoted = 1;
7219 else if (*s == ',' || *s == ';') return s;
7220 }
7221 return s;
7222}
7223
7224/* Delete a value in a header between delimiters <from> and <next> in buffer
7225 * <buf>. The number of characters displaced is returned, and the pointer to
7226 * the first delimiter is updated if required. The function tries as much as
7227 * possible to respect the following principles :
7228 * - replace <from> delimiter by the <next> one unless <from> points to a
7229 * colon, in which case <next> is simply removed
7230 * - set exactly one space character after the new first delimiter, unless
7231 * there are not enough characters in the block being moved to do so.
7232 * - remove unneeded spaces before the previous delimiter and after the new
7233 * one.
7234 *
7235 * It is the caller's responsibility to ensure that :
7236 * - <from> points to a valid delimiter or the colon ;
7237 * - <next> points to a valid delimiter or the final CR/LF ;
7238 * - there are non-space chars before <from> ;
7239 * - there is a CR/LF at or after <next>.
7240 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007241int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007242{
7243 char *prev = *from;
7244
7245 if (*prev == ':') {
7246 /* We're removing the first value, preserve the colon and add a
7247 * space if possible.
7248 */
7249 if (!http_is_crlf[(unsigned char)*next])
7250 next++;
7251 prev++;
7252 if (prev < next)
7253 *prev++ = ' ';
7254
7255 while (http_is_spht[(unsigned char)*next])
7256 next++;
7257 } else {
7258 /* Remove useless spaces before the old delimiter. */
7259 while (http_is_spht[(unsigned char)*(prev-1)])
7260 prev--;
7261 *from = prev;
7262
7263 /* copy the delimiter and if possible a space if we're
7264 * not at the end of the line.
7265 */
7266 if (!http_is_crlf[(unsigned char)*next]) {
7267 *prev++ = *next++;
7268 if (prev + 1 < next)
7269 *prev++ = ' ';
7270 while (http_is_spht[(unsigned char)*next])
7271 next++;
7272 }
7273 }
7274 return buffer_replace2(buf, prev, next, NULL, 0);
7275}
7276
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007277/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007278 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007279 * desirable to call it only when needed. This code is quite complex because
7280 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7281 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007282 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007283void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007284{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007285 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007286 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007287 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007288 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7289 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007290
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007291 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007292 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007293 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007294
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007295 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007296 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007297 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007298
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007299 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007300 hdr_beg = hdr_next;
7301 hdr_end = hdr_beg + cur_hdr->len;
7302 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007303
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007304 /* We have one full header between hdr_beg and hdr_end, and the
7305 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007306 * "Cookie:" headers.
7307 */
7308
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007309 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007310 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007311 old_idx = cur_idx;
7312 continue;
7313 }
7314
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007315 del_from = NULL; /* nothing to be deleted */
7316 preserve_hdr = 0; /* assume we may kill the whole header */
7317
Willy Tarreau58f10d72006-12-04 02:26:12 +01007318 /* Now look for cookies. Conforming to RFC2109, we have to support
7319 * attributes whose name begin with a '$', and associate them with
7320 * the right cookie, if we want to delete this cookie.
7321 * So there are 3 cases for each cookie read :
7322 * 1) it's a special attribute, beginning with a '$' : ignore it.
7323 * 2) it's a server id cookie that we *MAY* want to delete : save
7324 * some pointers on it (last semi-colon, beginning of cookie...)
7325 * 3) it's an application cookie : we *MAY* have to delete a previous
7326 * "special" cookie.
7327 * At the end of loop, if a "special" cookie remains, we may have to
7328 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007329 * *MUST* delete it.
7330 *
7331 * Note: RFC2965 is unclear about the processing of spaces around
7332 * the equal sign in the ATTR=VALUE form. A careful inspection of
7333 * the RFC explicitly allows spaces before it, and not within the
7334 * tokens (attrs or values). An inspection of RFC2109 allows that
7335 * too but section 10.1.3 lets one think that spaces may be allowed
7336 * after the equal sign too, resulting in some (rare) buggy
7337 * implementations trying to do that. So let's do what servers do.
7338 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7339 * allowed quoted strings in values, with any possible character
7340 * after a backslash, including control chars and delimitors, which
7341 * causes parsing to become ambiguous. Browsers also allow spaces
7342 * within values even without quotes.
7343 *
7344 * We have to keep multiple pointers in order to support cookie
7345 * removal at the beginning, middle or end of header without
7346 * corrupting the header. All of these headers are valid :
7347 *
7348 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7349 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7350 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7351 * | | | | | | | | |
7352 * | | | | | | | | hdr_end <--+
7353 * | | | | | | | +--> next
7354 * | | | | | | +----> val_end
7355 * | | | | | +-----------> val_beg
7356 * | | | | +--------------> equal
7357 * | | | +----------------> att_end
7358 * | | +---------------------> att_beg
7359 * | +--------------------------> prev
7360 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007361 */
7362
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007363 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7364 /* Iterate through all cookies on this line */
7365
7366 /* find att_beg */
7367 att_beg = prev + 1;
7368 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7369 att_beg++;
7370
7371 /* find att_end : this is the first character after the last non
7372 * space before the equal. It may be equal to hdr_end.
7373 */
7374 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007375
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007376 while (equal < hdr_end) {
7377 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007378 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007379 if (http_is_spht[(unsigned char)*equal++])
7380 continue;
7381 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007382 }
7383
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007384 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7385 * is between <att_beg> and <equal>, both may be identical.
7386 */
7387
7388 /* look for end of cookie if there is an equal sign */
7389 if (equal < hdr_end && *equal == '=') {
7390 /* look for the beginning of the value */
7391 val_beg = equal + 1;
7392 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7393 val_beg++;
7394
7395 /* find the end of the value, respecting quotes */
7396 next = find_cookie_value_end(val_beg, hdr_end);
7397
7398 /* make val_end point to the first white space or delimitor after the value */
7399 val_end = next;
7400 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7401 val_end--;
7402 } else {
7403 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007404 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007405
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007406 /* We have nothing to do with attributes beginning with '$'. However,
7407 * they will automatically be removed if a header before them is removed,
7408 * since they're supposed to be linked together.
7409 */
7410 if (*att_beg == '$')
7411 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007412
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007413 /* Ignore cookies with no equal sign */
7414 if (equal == next) {
7415 /* This is not our cookie, so we must preserve it. But if we already
7416 * scheduled another cookie for removal, we cannot remove the
7417 * complete header, but we can remove the previous block itself.
7418 */
7419 preserve_hdr = 1;
7420 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007421 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007422 val_end += delta;
7423 next += delta;
7424 hdr_end += delta;
7425 hdr_next += delta;
7426 cur_hdr->len += delta;
7427 http_msg_move_end(&txn->req, delta);
7428 prev = del_from;
7429 del_from = NULL;
7430 }
7431 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007432 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007433
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007434 /* if there are spaces around the equal sign, we need to
7435 * strip them otherwise we'll get trouble for cookie captures,
7436 * or even for rewrites. Since this happens extremely rarely,
7437 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007438 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007439 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7440 int stripped_before = 0;
7441 int stripped_after = 0;
7442
7443 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007444 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007445 equal += stripped_before;
7446 val_beg += stripped_before;
7447 }
7448
7449 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007450 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007451 val_beg += stripped_after;
7452 stripped_before += stripped_after;
7453 }
7454
7455 val_end += stripped_before;
7456 next += stripped_before;
7457 hdr_end += stripped_before;
7458 hdr_next += stripped_before;
7459 cur_hdr->len += stripped_before;
7460 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007461 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007462 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007463
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007464 /* First, let's see if we want to capture this cookie. We check
7465 * that we don't already have a client side cookie, because we
7466 * can only capture one. Also as an optimisation, we ignore
7467 * cookies shorter than the declared name.
7468 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007469 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7470 (val_end - att_beg >= s->fe->capture_namelen) &&
7471 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007472 int log_len = val_end - att_beg;
7473
7474 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7475 Alert("HTTP logging : out of memory.\n");
7476 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007477 if (log_len > s->fe->capture_len)
7478 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007479 memcpy(txn->cli_cookie, att_beg, log_len);
7480 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007481 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007482 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007483
Willy Tarreaubca99692010-10-06 19:25:55 +02007484 /* Persistence cookies in passive, rewrite or insert mode have the
7485 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007486 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007487 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007488 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007489 * For cookies in prefix mode, the form is :
7490 *
7491 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007492 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007493 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7494 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7495 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007496 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007497
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007498 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7499 * have the server ID between val_beg and delim, and the original cookie between
7500 * delim+1 and val_end. Otherwise, delim==val_end :
7501 *
7502 * Cookie: NAME=SRV; # in all but prefix modes
7503 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7504 * | || || | |+-> next
7505 * | || || | +--> val_end
7506 * | || || +---------> delim
7507 * | || |+------------> val_beg
7508 * | || +-------------> att_end = equal
7509 * | |+-----------------> att_beg
7510 * | +------------------> prev
7511 * +-------------------------> hdr_beg
7512 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007513
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007514 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007515 for (delim = val_beg; delim < val_end; delim++)
7516 if (*delim == COOKIE_DELIM)
7517 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007518 } else {
7519 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007520 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007521 /* Now check if the cookie contains a date field, which would
7522 * appear after a vertical bar ('|') just after the server name
7523 * and before the delimiter.
7524 */
7525 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7526 if (vbar1) {
7527 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007528 * right is the last seen date. It is a base64 encoded
7529 * 30-bit value representing the UNIX date since the
7530 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007531 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007532 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007533 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007534 if (val_end - vbar1 >= 5) {
7535 val = b64tos30(vbar1);
7536 if (val > 0)
7537 txn->cookie_last_date = val << 2;
7538 }
7539 /* look for a second vertical bar */
7540 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7541 if (vbar1 && (val_end - vbar1 > 5)) {
7542 val = b64tos30(vbar1 + 1);
7543 if (val > 0)
7544 txn->cookie_first_date = val << 2;
7545 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007546 }
7547 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007548
Willy Tarreauf64d1412010-10-07 20:06:11 +02007549 /* if the cookie has an expiration date and the proxy wants to check
7550 * it, then we do that now. We first check if the cookie is too old,
7551 * then only if it has expired. We detect strict overflow because the
7552 * time resolution here is not great (4 seconds). Cookies with dates
7553 * in the future are ignored if their offset is beyond one day. This
7554 * allows an admin to fix timezone issues without expiring everyone
7555 * and at the same time avoids keeping unwanted side effects for too
7556 * long.
7557 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007558 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7559 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007560 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007561 txn->flags &= ~TX_CK_MASK;
7562 txn->flags |= TX_CK_OLD;
7563 delim = val_beg; // let's pretend we have not found the cookie
7564 txn->cookie_first_date = 0;
7565 txn->cookie_last_date = 0;
7566 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007567 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7568 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007569 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007570 txn->flags &= ~TX_CK_MASK;
7571 txn->flags |= TX_CK_EXPIRED;
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 }
7576
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007577 /* Here, we'll look for the first running server which supports the cookie.
7578 * This allows to share a same cookie between several servers, for example
7579 * to dedicate backup servers to specific servers only.
7580 * However, to prevent clients from sticking to cookie-less backup server
7581 * when they have incidentely learned an empty cookie, we simply ignore
7582 * empty cookies and mark them as invalid.
7583 * The same behaviour is applied when persistence must be ignored.
7584 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007585 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007586 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007587
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007588 while (srv) {
7589 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7590 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007591 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007592 (s->be->options & PR_O_PERSIST) ||
7593 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007594 /* we found the server and we can use it */
7595 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007596 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007597 s->flags |= SN_DIRECT | SN_ASSIGNED;
7598 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007599 break;
7600 } else {
7601 /* we found a server, but it's down,
7602 * mark it as such and go on in case
7603 * another one is available.
7604 */
7605 txn->flags &= ~TX_CK_MASK;
7606 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007607 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007608 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007609 srv = srv->next;
7610 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007611
Willy Tarreauf64d1412010-10-07 20:06:11 +02007612 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007613 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007614 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007615 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007616 txn->flags |= TX_CK_UNUSED;
7617 else
7618 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007619 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007620
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007621 /* depending on the cookie mode, we may have to either :
7622 * - delete the complete cookie if we're in insert+indirect mode, so that
7623 * the server never sees it ;
7624 * - remove the server id from the cookie value, and tag the cookie as an
7625 * application cookie so that it does not get accidentely removed later,
7626 * if we're in cookie prefix mode
7627 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007628 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007629 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007630
Willy Tarreau9b28e032012-10-12 23:49:43 +02007631 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007632 val_end += delta;
7633 next += delta;
7634 hdr_end += delta;
7635 hdr_next += delta;
7636 cur_hdr->len += delta;
7637 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007638
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007639 del_from = NULL;
7640 preserve_hdr = 1; /* we want to keep this cookie */
7641 }
7642 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007643 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007644 del_from = prev;
7645 }
7646 } else {
7647 /* This is not our cookie, so we must preserve it. But if we already
7648 * scheduled another cookie for removal, we cannot remove the
7649 * complete header, but we can remove the previous block itself.
7650 */
7651 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007652
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007653 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007654 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007655 if (att_beg >= del_from)
7656 att_beg += delta;
7657 if (att_end >= del_from)
7658 att_end += delta;
7659 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007660 val_end += delta;
7661 next += delta;
7662 hdr_end += delta;
7663 hdr_next += delta;
7664 cur_hdr->len += delta;
7665 http_msg_move_end(&txn->req, delta);
7666 prev = del_from;
7667 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007668 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007669 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007670
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007671 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007672 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007673 int cmp_len, value_len;
7674 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007675
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007676 if (s->be->options2 & PR_O2_AS_PFX) {
7677 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7678 value_begin = att_beg + s->be->appsession_name_len;
7679 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007680 } else {
7681 cmp_len = att_end - att_beg;
7682 value_begin = val_beg;
7683 value_len = val_end - val_beg;
7684 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007685
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007686 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007687 if (cmp_len == s->be->appsession_name_len &&
7688 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7689 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007690 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007691 }
7692
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007693 /* continue with next cookie on this header line */
7694 att_beg = next;
7695 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007696
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007697 /* There are no more cookies on this line.
7698 * We may still have one (or several) marked for deletion at the
7699 * end of the line. We must do this now in two ways :
7700 * - if some cookies must be preserved, we only delete from the
7701 * mark to the end of line ;
7702 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007703 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007704 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007705 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007706 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007707 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007708 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007709 cur_hdr->len += delta;
7710 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007711 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007712
7713 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007714 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7715 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007716 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007717 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007718 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007719 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007720 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007721 }
7722
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007723 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007724 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007725 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007726}
7727
7728
Willy Tarreaua15645d2007-03-18 16:22:39 +01007729/* Iterate the same filter through all response headers contained in <rtr>.
7730 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7731 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007732int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007733{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007734 char *cur_ptr, *cur_end, *cur_next;
7735 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007736 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007737 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007738 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007739
7740 last_hdr = 0;
7741
Willy Tarreau9b28e032012-10-12 23:49:43 +02007742 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007743 old_idx = 0;
7744
7745 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007746 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007747 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007748 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007749 (exp->action == ACT_ALLOW ||
7750 exp->action == ACT_DENY))
7751 return 0;
7752
7753 cur_idx = txn->hdr_idx.v[old_idx].next;
7754 if (!cur_idx)
7755 break;
7756
7757 cur_hdr = &txn->hdr_idx.v[cur_idx];
7758 cur_ptr = cur_next;
7759 cur_end = cur_ptr + cur_hdr->len;
7760 cur_next = cur_end + cur_hdr->cr + 1;
7761
7762 /* Now we have one header between cur_ptr and cur_end,
7763 * and the next header starts at cur_next.
7764 */
7765
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007766 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007767 switch (exp->action) {
7768 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007769 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007770 last_hdr = 1;
7771 break;
7772
7773 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007774 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007775 last_hdr = 1;
7776 break;
7777
7778 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007779 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7780 if (trash.len < 0)
7781 return -1;
7782
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007783 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007784 /* FIXME: if the user adds a newline in the replacement, the
7785 * index will not be recalculated for now, and the new line
7786 * will not be counted as a new header.
7787 */
7788
7789 cur_end += delta;
7790 cur_next += delta;
7791 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007792 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007793 break;
7794
7795 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007796 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007797 cur_next += delta;
7798
Willy Tarreaufa355d42009-11-29 18:12:29 +01007799 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007800 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7801 txn->hdr_idx.used--;
7802 cur_hdr->len = 0;
7803 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007804 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007805 break;
7806
7807 }
7808 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007809
7810 /* keep the link from this header to next one in case of later
7811 * removal of next header.
7812 */
7813 old_idx = cur_idx;
7814 }
7815 return 0;
7816}
7817
7818
7819/* Apply the filter to the status line in the response buffer <rtr>.
7820 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7821 * or -1 if a replacement resulted in an invalid status line.
7822 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007823int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007824{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007825 char *cur_ptr, *cur_end;
7826 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007827 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007828 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007829
7830
Willy Tarreau3d300592007-03-18 18:34:41 +01007831 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007832 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007833 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007834 (exp->action == ACT_ALLOW ||
7835 exp->action == ACT_DENY))
7836 return 0;
7837 else if (exp->action == ACT_REMOVE)
7838 return 0;
7839
7840 done = 0;
7841
Willy Tarreau9b28e032012-10-12 23:49:43 +02007842 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007843 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007844
7845 /* Now we have the status line between cur_ptr and cur_end */
7846
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007847 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007848 switch (exp->action) {
7849 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007850 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007851 done = 1;
7852 break;
7853
7854 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007855 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007856 done = 1;
7857 break;
7858
7859 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007860 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7861 if (trash.len < 0)
7862 return -1;
7863
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007864 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007865 /* FIXME: if the user adds a newline in the replacement, the
7866 * index will not be recalculated for now, and the new line
7867 * will not be counted as a new header.
7868 */
7869
Willy Tarreaufa355d42009-11-29 18:12:29 +01007870 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007871 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007872 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007873 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007874 cur_ptr, cur_end + 1,
7875 NULL, NULL);
7876 if (unlikely(!cur_end))
7877 return -1;
7878
7879 /* we have a full respnse and we know that we have either a CR
7880 * or an LF at <ptr>.
7881 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007882 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007883 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007884 /* there is no point trying this regex on headers */
7885 return 1;
7886 }
7887 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007888 return done;
7889}
7890
7891
7892
7893/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007894 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007895 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7896 * unparsable response.
7897 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007898int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007899{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007900 struct http_txn *txn = &s->txn;
7901 struct hdr_exp *exp;
7902
7903 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007904 int ret;
7905
7906 /*
7907 * The interleaving of transformations and verdicts
7908 * makes it difficult to decide to continue or stop
7909 * the evaluation.
7910 */
7911
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007912 if (txn->flags & TX_SVDENY)
7913 break;
7914
Willy Tarreau3d300592007-03-18 18:34:41 +01007915 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007916 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7917 exp->action == ACT_PASS)) {
7918 exp = exp->next;
7919 continue;
7920 }
7921
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007922 /* if this filter had a condition, evaluate it now and skip to
7923 * next filter if the condition does not match.
7924 */
7925 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007926 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007927 ret = acl_pass(ret);
7928 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7929 ret = !ret;
7930 if (!ret)
7931 continue;
7932 }
7933
Willy Tarreaua15645d2007-03-18 16:22:39 +01007934 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007935 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007936 if (unlikely(ret < 0))
7937 return -1;
7938
7939 if (likely(ret == 0)) {
7940 /* The filter did not match the response, it can be
7941 * iterated through all headers.
7942 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007943 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7944 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007945 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007946 }
7947 return 0;
7948}
7949
7950
Willy Tarreaua15645d2007-03-18 16:22:39 +01007951/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007952 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007953 * desirable to call it only when needed. This function is also used when we
7954 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007955 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007956void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007957{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007958 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007959 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007960 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007961 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007962 char *hdr_beg, *hdr_end, *hdr_next;
7963 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007964
Willy Tarreaua15645d2007-03-18 16:22:39 +01007965 /* Iterate through the headers.
7966 * we start with the start line.
7967 */
7968 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007969 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007970
7971 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7972 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007973 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007974
7975 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007976 hdr_beg = hdr_next;
7977 hdr_end = hdr_beg + cur_hdr->len;
7978 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007979
Willy Tarreau24581ba2010-08-31 22:39:35 +02007980 /* We have one full header between hdr_beg and hdr_end, and the
7981 * next header starts at hdr_next. We're only interested in
7982 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007983 */
7984
Willy Tarreau24581ba2010-08-31 22:39:35 +02007985 is_cookie2 = 0;
7986 prev = hdr_beg + 10;
7987 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007988 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007989 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7990 if (!val) {
7991 old_idx = cur_idx;
7992 continue;
7993 }
7994 is_cookie2 = 1;
7995 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007996 }
7997
Willy Tarreau24581ba2010-08-31 22:39:35 +02007998 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7999 * <prev> points to the colon.
8000 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008001 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008002
Willy Tarreau24581ba2010-08-31 22:39:35 +02008003 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8004 * check-cache is enabled) and we are not interested in checking
8005 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008006 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008007 if (s->be->cookie_name == NULL &&
8008 s->be->appsession_name == NULL &&
8009 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008010 return;
8011
Willy Tarreau24581ba2010-08-31 22:39:35 +02008012 /* OK so now we know we have to process this response cookie.
8013 * The format of the Set-Cookie header is slightly different
8014 * from the format of the Cookie header in that it does not
8015 * support the comma as a cookie delimiter (thus the header
8016 * cannot be folded) because the Expires attribute described in
8017 * the original Netscape's spec may contain an unquoted date
8018 * with a comma inside. We have to live with this because
8019 * many browsers don't support Max-Age and some browsers don't
8020 * support quoted strings. However the Set-Cookie2 header is
8021 * clean.
8022 *
8023 * We have to keep multiple pointers in order to support cookie
8024 * removal at the beginning, middle or end of header without
8025 * corrupting the header (in case of set-cookie2). A special
8026 * pointer, <scav> points to the beginning of the set-cookie-av
8027 * fields after the first semi-colon. The <next> pointer points
8028 * either to the end of line (set-cookie) or next unquoted comma
8029 * (set-cookie2). All of these headers are valid :
8030 *
8031 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8032 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8033 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8034 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8035 * | | | | | | | | | |
8036 * | | | | | | | | +-> next hdr_end <--+
8037 * | | | | | | | +------------> scav
8038 * | | | | | | +--------------> val_end
8039 * | | | | | +--------------------> val_beg
8040 * | | | | +----------------------> equal
8041 * | | | +------------------------> att_end
8042 * | | +----------------------------> att_beg
8043 * | +------------------------------> prev
8044 * +-----------------------------------------> hdr_beg
8045 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008046
Willy Tarreau24581ba2010-08-31 22:39:35 +02008047 for (; prev < hdr_end; prev = next) {
8048 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008049
Willy Tarreau24581ba2010-08-31 22:39:35 +02008050 /* find att_beg */
8051 att_beg = prev + 1;
8052 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8053 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008054
Willy Tarreau24581ba2010-08-31 22:39:35 +02008055 /* find att_end : this is the first character after the last non
8056 * space before the equal. It may be equal to hdr_end.
8057 */
8058 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008059
Willy Tarreau24581ba2010-08-31 22:39:35 +02008060 while (equal < hdr_end) {
8061 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8062 break;
8063 if (http_is_spht[(unsigned char)*equal++])
8064 continue;
8065 att_end = equal;
8066 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008067
Willy Tarreau24581ba2010-08-31 22:39:35 +02008068 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8069 * is between <att_beg> and <equal>, both may be identical.
8070 */
8071
8072 /* look for end of cookie if there is an equal sign */
8073 if (equal < hdr_end && *equal == '=') {
8074 /* look for the beginning of the value */
8075 val_beg = equal + 1;
8076 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8077 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008078
Willy Tarreau24581ba2010-08-31 22:39:35 +02008079 /* find the end of the value, respecting quotes */
8080 next = find_cookie_value_end(val_beg, hdr_end);
8081
8082 /* make val_end point to the first white space or delimitor after the value */
8083 val_end = next;
8084 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8085 val_end--;
8086 } else {
8087 /* <equal> points to next comma, semi-colon or EOL */
8088 val_beg = val_end = next = equal;
8089 }
8090
8091 if (next < hdr_end) {
8092 /* Set-Cookie2 supports multiple cookies, and <next> points to
8093 * a colon or semi-colon before the end. So skip all attr-value
8094 * pairs and look for the next comma. For Set-Cookie, since
8095 * commas are permitted in values, skip to the end.
8096 */
8097 if (is_cookie2)
8098 next = find_hdr_value_end(next, hdr_end);
8099 else
8100 next = hdr_end;
8101 }
8102
8103 /* Now everything is as on the diagram above */
8104
8105 /* Ignore cookies with no equal sign */
8106 if (equal == val_end)
8107 continue;
8108
8109 /* If there are spaces around the equal sign, we need to
8110 * strip them otherwise we'll get trouble for cookie captures,
8111 * or even for rewrites. Since this happens extremely rarely,
8112 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008113 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008114 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8115 int stripped_before = 0;
8116 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008117
Willy Tarreau24581ba2010-08-31 22:39:35 +02008118 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008119 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008120 equal += stripped_before;
8121 val_beg += stripped_before;
8122 }
8123
8124 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008125 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008126 val_beg += stripped_after;
8127 stripped_before += stripped_after;
8128 }
8129
8130 val_end += stripped_before;
8131 next += stripped_before;
8132 hdr_end += stripped_before;
8133 hdr_next += stripped_before;
8134 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008135 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008136 }
8137
8138 /* First, let's see if we want to capture this cookie. We check
8139 * that we don't already have a server side cookie, because we
8140 * can only capture one. Also as an optimisation, we ignore
8141 * cookies shorter than the declared name.
8142 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008143 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008144 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008145 (val_end - att_beg >= s->fe->capture_namelen) &&
8146 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008147 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008148 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008149 Alert("HTTP logging : out of memory.\n");
8150 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008151 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008152 if (log_len > s->fe->capture_len)
8153 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008154 memcpy(txn->srv_cookie, att_beg, log_len);
8155 txn->srv_cookie[log_len] = 0;
8156 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008157 }
8158
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008159 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008160 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008161 if (!(s->flags & SN_IGNORE_PRST) &&
8162 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8163 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008164 /* assume passive cookie by default */
8165 txn->flags &= ~TX_SCK_MASK;
8166 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008167
8168 /* If the cookie is in insert mode on a known server, we'll delete
8169 * this occurrence because we'll insert another one later.
8170 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008171 * a direct access.
8172 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008173 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008174 /* The "preserve" flag was set, we don't want to touch the
8175 * server's cookie.
8176 */
8177 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008178 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8179 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008180 /* this cookie must be deleted */
8181 if (*prev == ':' && next == hdr_end) {
8182 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008183 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008184 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8185 txn->hdr_idx.used--;
8186 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008187 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008188 hdr_next += delta;
8189 http_msg_move_end(&txn->rsp, delta);
8190 /* note: while both invalid now, <next> and <hdr_end>
8191 * are still equal, so the for() will stop as expected.
8192 */
8193 } else {
8194 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008195 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008196 next = prev;
8197 hdr_end += delta;
8198 hdr_next += delta;
8199 cur_hdr->len += delta;
8200 http_msg_move_end(&txn->rsp, delta);
8201 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008202 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008203 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008204 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008205 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008206 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008207 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008208 * with this server since we know it.
8209 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008210 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008211 next += delta;
8212 hdr_end += delta;
8213 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008214 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008215 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008216
Willy Tarreauf1348312010-10-07 15:54:11 +02008217 txn->flags &= ~TX_SCK_MASK;
8218 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008219 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008220 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008221 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008222 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008223 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008224 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008225 next += delta;
8226 hdr_end += delta;
8227 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008228 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008229 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008230
Willy Tarreau827aee92011-03-10 16:55:02 +01008231 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008232 txn->flags &= ~TX_SCK_MASK;
8233 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008234 }
8235 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008236 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008237 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008238 int cmp_len, value_len;
8239 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008240
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008241 if (s->be->options2 & PR_O2_AS_PFX) {
8242 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8243 value_begin = att_beg + s->be->appsession_name_len;
8244 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008245 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008246 cmp_len = att_end - att_beg;
8247 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008248 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008249 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008250
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008251 if ((cmp_len == s->be->appsession_name_len) &&
8252 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008253 /* free a possibly previously allocated memory */
8254 pool_free2(apools.sessid, txn->sessid);
8255
Cyril Bontéb21570a2009-11-29 20:04:48 +01008256 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008257 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008258 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008259 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008260 return;
8261 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008262 memcpy(txn->sessid, value_begin, value_len);
8263 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008264 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008265 }
8266 /* that's done for this cookie, check the next one on the same
8267 * line when next != hdr_end (only if is_cookie2).
8268 */
8269 }
8270 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008271 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008272 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008273
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008274 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008275 appsess *asession = NULL;
8276 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008277 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008278 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008279 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008280 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8281 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008282 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008283 return;
8284 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008285 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8286
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008287 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8288 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008289 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8290 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008291 return;
8292 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008293 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8294 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008295
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008296 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008297 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008298 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008299 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8300 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008301 return;
8302 }
8303 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008304 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008305
8306 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008307 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008308 }
8309
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008310 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008311 asession->request_count++;
8312 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008313}
8314
8315
Willy Tarreaua15645d2007-03-18 16:22:39 +01008316/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008317 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008318 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008319void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008320{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008321 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008322 char *p1, *p2;
8323
8324 char *cur_ptr, *cur_end, *cur_next;
8325 int cur_idx;
8326
Willy Tarreau5df51872007-11-25 16:20:08 +01008327 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008328 return;
8329
8330 /* Iterate through the headers.
8331 * we start with the start line.
8332 */
8333 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008334 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008335
8336 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8337 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008338 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008339
8340 cur_hdr = &txn->hdr_idx.v[cur_idx];
8341 cur_ptr = cur_next;
8342 cur_end = cur_ptr + cur_hdr->len;
8343 cur_next = cur_end + cur_hdr->cr + 1;
8344
8345 /* We have one full header between cur_ptr and cur_end, and the
8346 * next header starts at cur_next. We're only interested in
8347 * "Cookie:" headers.
8348 */
8349
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008350 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8351 if (val) {
8352 if ((cur_end - (cur_ptr + val) >= 8) &&
8353 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8354 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8355 return;
8356 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008357 }
8358
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008359 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8360 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008361 continue;
8362
8363 /* OK, right now we know we have a cache-control header at cur_ptr */
8364
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008365 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008366
8367 if (p1 >= cur_end) /* no more info */
8368 continue;
8369
8370 /* p1 is at the beginning of the value */
8371 p2 = p1;
8372
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008373 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008374 p2++;
8375
8376 /* we have a complete value between p1 and p2 */
8377 if (p2 < cur_end && *p2 == '=') {
8378 /* we have something of the form no-cache="set-cookie" */
8379 if ((cur_end - p1 >= 21) &&
8380 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8381 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008382 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008383 continue;
8384 }
8385
8386 /* OK, so we know that either p2 points to the end of string or to a comma */
8387 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008388 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008389 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8390 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8391 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008392 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008393 return;
8394 }
8395
8396 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008397 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008398 continue;
8399 }
8400 }
8401}
8402
8403
Willy Tarreau58f10d72006-12-04 02:26:12 +01008404/*
8405 * Try to retrieve a known appsession in the URI, then the associated server.
8406 * If the server is found, it's assigned to the session.
8407 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008408void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008409{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008410 char *end_params, *first_param, *cur_param, *next_param;
8411 char separator;
8412 int value_len;
8413
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008414 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008415
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008416 if (s->be->appsession_name == NULL ||
8417 (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 +01008418 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008419 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008420
Cyril Bontéb21570a2009-11-29 20:04:48 +01008421 first_param = NULL;
8422 switch (mode) {
8423 case PR_O2_AS_M_PP:
8424 first_param = memchr(begin, ';', len);
8425 break;
8426 case PR_O2_AS_M_QS:
8427 first_param = memchr(begin, '?', len);
8428 break;
8429 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008430
Cyril Bontéb21570a2009-11-29 20:04:48 +01008431 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008432 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008433 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008434
Cyril Bontéb21570a2009-11-29 20:04:48 +01008435 switch (mode) {
8436 case PR_O2_AS_M_PP:
8437 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8438 end_params = (char *) begin + len;
8439 }
8440 separator = ';';
8441 break;
8442 case PR_O2_AS_M_QS:
8443 end_params = (char *) begin + len;
8444 separator = '&';
8445 break;
8446 default:
8447 /* unknown mode, shouldn't happen */
8448 return;
8449 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008450
Cyril Bontéb21570a2009-11-29 20:04:48 +01008451 cur_param = next_param = end_params;
8452 while (cur_param > first_param) {
8453 cur_param--;
8454 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8455 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008456 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8457 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8458 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008459 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008460 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8461 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008462 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008463 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008464 }
8465 break;
8466 }
8467 next_param = cur_param;
8468 }
8469 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008470#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008471 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008472 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008473#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008474}
8475
Willy Tarreaub2513902006-12-17 14:52:38 +01008476/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008477 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008478 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008479 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008480 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008481 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008482 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008483 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008484 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008485int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008486{
8487 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008488 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008489 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008490
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008491 if (!uri_auth)
8492 return 0;
8493
Cyril Bonté70be45d2010-10-12 00:14:35 +02008494 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008495 return 0;
8496
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008497 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008498 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008499 return 0;
8500
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008501 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008502 return 0;
8503
Willy Tarreaub2513902006-12-17 14:52:38 +01008504 return 1;
8505}
8506
Willy Tarreau4076a152009-04-02 15:18:36 +02008507/*
8508 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008509 * By default it tries to report the error position as msg->err_pos. However if
8510 * this one is not set, it will then report msg->next, which is the last known
8511 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008512 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008513 */
8514void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008515 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008516 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008517{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008518 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008519 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008520
Willy Tarreau9b28e032012-10-12 23:49:43 +02008521 es->len = MIN(chn->buf->i, sizeof(es->buf));
8522 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008523 len1 = MIN(len1, es->len);
8524 len2 = es->len - len1; /* remaining data if buffer wraps */
8525
Willy Tarreau9b28e032012-10-12 23:49:43 +02008526 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008527 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008528 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008529
Willy Tarreau4076a152009-04-02 15:18:36 +02008530 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008531 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008532 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008533 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008534
Willy Tarreau4076a152009-04-02 15:18:36 +02008535 es->when = date; // user-visible date
8536 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008537 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008538 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008539 if (objt_conn(s->req->prod->end))
8540 es->src = __objt_conn(s->req->prod->end)->addr.from;
8541 else
8542 memset(&es->src, 0, sizeof(es->src));
8543
Willy Tarreau078272e2010-12-12 12:46:33 +01008544 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008545 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008546 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008547 es->s_flags = s->flags;
8548 es->t_flags = s->txn.flags;
8549 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008550 es->b_out = chn->buf->o;
8551 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008552 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008553 es->m_clen = msg->chunk_len;
8554 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008555}
Willy Tarreaub2513902006-12-17 14:52:38 +01008556
Willy Tarreau294c4732011-12-16 21:35:50 +01008557/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8558 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8559 * performed over the whole headers. Otherwise it must contain a valid header
8560 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8561 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8562 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8563 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008564 * -1. The value fetch stops at commas, so this function is suited for use with
8565 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008566 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008567 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008568unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008569 struct hdr_idx *idx, int occ,
8570 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008571{
Willy Tarreau294c4732011-12-16 21:35:50 +01008572 struct hdr_ctx local_ctx;
8573 char *ptr_hist[MAX_HDR_HISTORY];
8574 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008575 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008576 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008577
Willy Tarreau294c4732011-12-16 21:35:50 +01008578 if (!ctx) {
8579 local_ctx.idx = 0;
8580 ctx = &local_ctx;
8581 }
8582
Willy Tarreaubce70882009-09-07 11:51:47 +02008583 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008584 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008585 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008586 occ--;
8587 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008588 *vptr = ctx->line + ctx->val;
8589 *vlen = ctx->vlen;
8590 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008591 }
8592 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008593 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008594 }
8595
8596 /* negative occurrence, we scan all the list then walk back */
8597 if (-occ > MAX_HDR_HISTORY)
8598 return 0;
8599
Willy Tarreau294c4732011-12-16 21:35:50 +01008600 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008601 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008602 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8603 len_hist[hist_ptr] = ctx->vlen;
8604 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008605 hist_ptr = 0;
8606 found++;
8607 }
8608 if (-occ > found)
8609 return 0;
8610 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008611 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8612 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8613 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008614 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008615 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008616 if (hist_ptr >= MAX_HDR_HISTORY)
8617 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008618 *vptr = ptr_hist[hist_ptr];
8619 *vlen = len_hist[hist_ptr];
8620 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008621}
8622
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008623/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8624 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8625 * performed over the whole headers. Otherwise it must contain a valid header
8626 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8627 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8628 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8629 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8630 * -1. This function differs from http_get_hdr() in that it only returns full
8631 * line header values and does not stop at commas.
8632 * The return value is 0 if nothing was found, or non-zero otherwise.
8633 */
8634unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8635 struct hdr_idx *idx, int occ,
8636 struct hdr_ctx *ctx, char **vptr, int *vlen)
8637{
8638 struct hdr_ctx local_ctx;
8639 char *ptr_hist[MAX_HDR_HISTORY];
8640 int len_hist[MAX_HDR_HISTORY];
8641 unsigned int hist_ptr;
8642 int found;
8643
8644 if (!ctx) {
8645 local_ctx.idx = 0;
8646 ctx = &local_ctx;
8647 }
8648
8649 if (occ >= 0) {
8650 /* search from the beginning */
8651 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8652 occ--;
8653 if (occ <= 0) {
8654 *vptr = ctx->line + ctx->val;
8655 *vlen = ctx->vlen;
8656 return 1;
8657 }
8658 }
8659 return 0;
8660 }
8661
8662 /* negative occurrence, we scan all the list then walk back */
8663 if (-occ > MAX_HDR_HISTORY)
8664 return 0;
8665
8666 found = hist_ptr = 0;
8667 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8668 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8669 len_hist[hist_ptr] = ctx->vlen;
8670 if (++hist_ptr >= MAX_HDR_HISTORY)
8671 hist_ptr = 0;
8672 found++;
8673 }
8674 if (-occ > found)
8675 return 0;
8676 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8677 * find occurrence -occ, so we have to check [hist_ptr+occ].
8678 */
8679 hist_ptr += occ;
8680 if (hist_ptr >= MAX_HDR_HISTORY)
8681 hist_ptr -= MAX_HDR_HISTORY;
8682 *vptr = ptr_hist[hist_ptr];
8683 *vlen = len_hist[hist_ptr];
8684 return 1;
8685}
8686
Willy Tarreaubaaee002006-06-26 02:48:02 +02008687/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008688 * Print a debug line with a header. Always stop at the first CR or LF char,
8689 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8690 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008691 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008692void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008693{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008694 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008695 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008696 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008697 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8698 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 +02008699
8700 for (max = 0; start + max < end; max++)
8701 if (start[max] == '\r' || start[max] == '\n')
8702 break;
8703
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008704 UBOUND(max, trash.size - trash.len - 3);
8705 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8706 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008707 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008708}
8709
Willy Tarreau0937bc42009-12-22 15:03:09 +01008710/*
8711 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8712 * the required fields are properly allocated and that we only need to (re)init
8713 * them. This should be used before processing any new request.
8714 */
8715void http_init_txn(struct session *s)
8716{
8717 struct http_txn *txn = &s->txn;
8718 struct proxy *fe = s->fe;
8719
8720 txn->flags = 0;
8721 txn->status = -1;
8722
Willy Tarreauf64d1412010-10-07 20:06:11 +02008723 txn->cookie_first_date = 0;
8724 txn->cookie_last_date = 0;
8725
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008726 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008727 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008728 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008729 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008730 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008731 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008732 txn->req.chunk_len = 0LL;
8733 txn->req.body_len = 0LL;
8734 txn->rsp.chunk_len = 0LL;
8735 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008736 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8737 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008738 txn->req.chn = s->req;
8739 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008740
8741 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008742
8743 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8744 if (fe->options2 & PR_O2_REQBUG_OK)
8745 txn->req.err_pos = -1; /* let buggy requests pass */
8746
Willy Tarreau46023632010-01-07 22:51:47 +01008747 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008748 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8749
Willy Tarreau46023632010-01-07 22:51:47 +01008750 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008751 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8752
8753 if (txn->hdr_idx.v)
8754 hdr_idx_init(&txn->hdr_idx);
8755}
8756
8757/* to be used at the end of a transaction */
8758void http_end_txn(struct session *s)
8759{
8760 struct http_txn *txn = &s->txn;
8761
Willy Tarreau75195602014-03-11 15:48:55 +01008762 /* release any possible compression context */
8763 if (s->flags & SN_COMP_READY)
8764 s->comp_algo->end(&s->comp_ctx);
8765 s->comp_algo = NULL;
8766 s->flags &= ~SN_COMP_READY;
8767
Willy Tarreau0937bc42009-12-22 15:03:09 +01008768 /* these ones will have been dynamically allocated */
8769 pool_free2(pool2_requri, txn->uri);
8770 pool_free2(pool2_capture, txn->cli_cookie);
8771 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008772 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008773 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008774
William Lallemanda73203e2012-03-12 12:48:57 +01008775 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008776 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008777 txn->uri = NULL;
8778 txn->srv_cookie = NULL;
8779 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008780
8781 if (txn->req.cap) {
8782 struct cap_hdr *h;
8783 for (h = s->fe->req_cap; h; h = h->next)
8784 pool_free2(h->pool, txn->req.cap[h->index]);
8785 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8786 }
8787
8788 if (txn->rsp.cap) {
8789 struct cap_hdr *h;
8790 for (h = s->fe->rsp_cap; h; h = h->next)
8791 pool_free2(h->pool, txn->rsp.cap[h->index]);
8792 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8793 }
8794
Willy Tarreau0937bc42009-12-22 15:03:09 +01008795}
8796
8797/* to be used at the end of a transaction to prepare a new one */
8798void http_reset_txn(struct session *s)
8799{
8800 http_end_txn(s);
8801 http_init_txn(s);
8802
8803 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008804 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008805 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008806 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008807 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008808 /* re-init store persistence */
8809 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008810 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008811
Willy Tarreau0937bc42009-12-22 15:03:09 +01008812 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008813
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008814 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008815
Willy Tarreau739cfba2010-01-25 23:11:14 +01008816 /* We must trim any excess data from the response buffer, because we
8817 * may have blocked an invalid response from a server that we don't
8818 * want to accidentely forward once we disable the analysers, nor do
8819 * we want those data to come along with next response. A typical
8820 * example of such data would be from a buggy server responding to
8821 * a HEAD with some data, or sending more than the advertised
8822 * content-length.
8823 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008824 if (unlikely(s->rep->buf->i))
8825 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008826
Willy Tarreau0937bc42009-12-22 15:03:09 +01008827 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008828 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008829
Willy Tarreaud04e8582010-05-31 12:31:35 +02008830 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008831 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008832
8833 s->req->rex = TICK_ETERNITY;
8834 s->req->wex = TICK_ETERNITY;
8835 s->req->analyse_exp = TICK_ETERNITY;
8836 s->rep->rex = TICK_ETERNITY;
8837 s->rep->wex = TICK_ETERNITY;
8838 s->rep->analyse_exp = TICK_ETERNITY;
8839}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008840
Sasha Pachev218f0642014-06-16 12:05:59 -06008841void free_http_res_rules(struct list *r)
8842{
8843 struct http_res_rule *tr, *pr;
8844
8845 list_for_each_entry_safe(pr, tr, r, list) {
8846 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008847 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008848 free(pr);
8849 }
8850}
8851
8852void free_http_req_rules(struct list *r)
8853{
Willy Tarreauff011f22011-01-06 17:51:27 +01008854 struct http_req_rule *tr, *pr;
8855
8856 list_for_each_entry_safe(pr, tr, r, list) {
8857 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008858 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008859 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008860
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008861 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008862 free(pr);
8863 }
8864}
8865
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008866/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008867struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8868{
8869 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008870 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008871 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008872 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008873
8874 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8875 if (!rule) {
8876 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008877 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008878 }
8879
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008880 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008881 rule->action = HTTP_REQ_ACT_ALLOW;
8882 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008883 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008884 rule->action = HTTP_REQ_ACT_DENY;
8885 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008886 } else if (!strcmp(args[0], "tarpit")) {
8887 rule->action = HTTP_REQ_ACT_TARPIT;
8888 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008889 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008890 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008891 cur_arg = 1;
8892
8893 while(*args[cur_arg]) {
8894 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008895 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008896 cur_arg+=2;
8897 continue;
8898 } else
8899 break;
8900 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008901 } else if (!strcmp(args[0], "set-nice")) {
8902 rule->action = HTTP_REQ_ACT_SET_NICE;
8903 cur_arg = 1;
8904
8905 if (!*args[cur_arg] ||
8906 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8907 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8908 file, linenum, args[0]);
8909 goto out_err;
8910 }
8911 rule->arg.nice = atoi(args[cur_arg]);
8912 if (rule->arg.nice < -1024)
8913 rule->arg.nice = -1024;
8914 else if (rule->arg.nice > 1024)
8915 rule->arg.nice = 1024;
8916 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008917 } else if (!strcmp(args[0], "set-tos")) {
8918#ifdef IP_TOS
8919 char *err;
8920 rule->action = HTTP_REQ_ACT_SET_TOS;
8921 cur_arg = 1;
8922
8923 if (!*args[cur_arg] ||
8924 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8925 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8926 file, linenum, args[0]);
8927 goto out_err;
8928 }
8929
8930 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8931 if (err && *err != '\0') {
8932 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8933 file, linenum, err, args[0]);
8934 goto out_err;
8935 }
8936 cur_arg++;
8937#else
8938 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8939 goto out_err;
8940#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008941 } else if (!strcmp(args[0], "set-mark")) {
8942#ifdef SO_MARK
8943 char *err;
8944 rule->action = HTTP_REQ_ACT_SET_MARK;
8945 cur_arg = 1;
8946
8947 if (!*args[cur_arg] ||
8948 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8949 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8950 file, linenum, args[0]);
8951 goto out_err;
8952 }
8953
8954 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8955 if (err && *err != '\0') {
8956 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8957 file, linenum, err, args[0]);
8958 goto out_err;
8959 }
8960 cur_arg++;
8961 global.last_checks |= LSTCHK_NETADM;
8962#else
8963 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8964 goto out_err;
8965#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008966 } else if (!strcmp(args[0], "set-log-level")) {
8967 rule->action = HTTP_REQ_ACT_SET_LOGL;
8968 cur_arg = 1;
8969
8970 if (!*args[cur_arg] ||
8971 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8972 bad_log_level:
8973 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8974 file, linenum, args[0]);
8975 goto out_err;
8976 }
8977 if (strcmp(args[cur_arg], "silent") == 0)
8978 rule->arg.loglevel = -1;
8979 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8980 goto bad_log_level;
8981 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008982 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8983 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8984 cur_arg = 1;
8985
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008986 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8987 (*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 +01008988 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8989 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008990 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008991 }
8992
8993 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8994 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8995 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008996
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008997 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008998 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008999 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9000 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009001 free(proxy->conf.lfs_file);
9002 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9003 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009004 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009005 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9006 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009007 cur_arg = 1;
9008
9009 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann8f249f42014-06-24 11:10:00 +02009010 (*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 -06009011 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9012 file, linenum, args[0]);
9013 goto out_err;
9014 }
9015
9016 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9017 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9018 LIST_INIT(&rule->arg.hdr_add.fmt);
9019
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009020 error = NULL;
9021 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9022 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9023 args[cur_arg + 1], error);
9024 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009025 goto out_err;
9026 }
9027
9028 proxy->conf.args.ctx = ARGC_HRQ;
9029 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9030 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9031 file, linenum);
9032
9033 free(proxy->conf.lfs_file);
9034 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9035 proxy->conf.lfs_line = proxy->conf.args.line;
9036 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009037 } else if (strcmp(args[0], "del-header") == 0) {
9038 rule->action = HTTP_REQ_ACT_DEL_HDR;
9039 cur_arg = 1;
9040
9041 if (!*args[cur_arg] ||
9042 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9043 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9044 file, linenum, args[0]);
9045 goto out_err;
9046 }
9047
9048 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9049 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9050
9051 proxy->conf.args.ctx = ARGC_HRQ;
9052 free(proxy->conf.lfs_file);
9053 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9054 proxy->conf.lfs_line = proxy->conf.args.line;
9055 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009056 } else if (strcmp(args[0], "redirect") == 0) {
9057 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009058 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009059
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009060 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009061 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9062 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9063 goto out_err;
9064 }
9065
9066 /* this redirect rule might already contain a parsed condition which
9067 * we'll pass to the http-request rule.
9068 */
9069 rule->action = HTTP_REQ_ACT_REDIR;
9070 rule->arg.redir = redir;
9071 rule->cond = redir->cond;
9072 redir->cond = NULL;
9073 cur_arg = 2;
9074 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009075 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9076 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9077 rule->action = HTTP_REQ_ACT_ADD_ACL;
9078 /*
9079 * '+ 8' for 'add-acl('
9080 * '- 9' for 'add-acl(' + trailing ')'
9081 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009082 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009083
9084 cur_arg = 1;
9085
9086 if (!*args[cur_arg] ||
9087 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9088 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9089 file, linenum, args[0]);
9090 goto out_err;
9091 }
9092
9093 LIST_INIT(&rule->arg.map.key);
9094 proxy->conf.args.ctx = ARGC_HRQ;
9095 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9096 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9097 file, linenum);
9098 free(proxy->conf.lfs_file);
9099 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9100 proxy->conf.lfs_line = proxy->conf.args.line;
9101 cur_arg += 1;
9102 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9103 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9104 rule->action = HTTP_REQ_ACT_DEL_ACL;
9105 /*
9106 * '+ 8' for 'del-acl('
9107 * '- 9' for 'del-acl(' + trailing ')'
9108 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009109 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009110
9111 cur_arg = 1;
9112
9113 if (!*args[cur_arg] ||
9114 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9115 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9116 file, linenum, args[0]);
9117 goto out_err;
9118 }
9119
9120 LIST_INIT(&rule->arg.map.key);
9121 proxy->conf.args.ctx = ARGC_HRQ;
9122 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9123 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9124 file, linenum);
9125 free(proxy->conf.lfs_file);
9126 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9127 proxy->conf.lfs_line = proxy->conf.args.line;
9128 cur_arg += 1;
9129 } else if (strncmp(args[0], "del-map", 7) == 0) {
9130 /* http-request del-map(<reference (map name)>) <key pattern> */
9131 rule->action = HTTP_REQ_ACT_DEL_MAP;
9132 /*
9133 * '+ 8' for 'del-map('
9134 * '- 9' for 'del-map(' + trailing ')'
9135 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009136 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009137
9138 cur_arg = 1;
9139
9140 if (!*args[cur_arg] ||
9141 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9142 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9143 file, linenum, args[0]);
9144 goto out_err;
9145 }
9146
9147 LIST_INIT(&rule->arg.map.key);
9148 proxy->conf.args.ctx = ARGC_HRQ;
9149 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9150 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9151 file, linenum);
9152 free(proxy->conf.lfs_file);
9153 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9154 proxy->conf.lfs_line = proxy->conf.args.line;
9155 cur_arg += 1;
9156 } else if (strncmp(args[0], "set-map", 7) == 0) {
9157 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9158 rule->action = HTTP_REQ_ACT_SET_MAP;
9159 /*
9160 * '+ 8' for 'set-map('
9161 * '- 9' for 'set-map(' + trailing ')'
9162 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009163 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009164
9165 cur_arg = 1;
9166
9167 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9168 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9169 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9170 file, linenum, args[0]);
9171 goto out_err;
9172 }
9173
9174 LIST_INIT(&rule->arg.map.key);
9175 LIST_INIT(&rule->arg.map.value);
9176 proxy->conf.args.ctx = ARGC_HRQ;
9177
9178 /* key pattern */
9179 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9180 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9181 file, linenum);
9182
9183 /* value pattern */
9184 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9185 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9186 file, linenum);
9187 free(proxy->conf.lfs_file);
9188 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9189 proxy->conf.lfs_line = proxy->conf.args.line;
9190
9191 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009192 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9193 char *errmsg = NULL;
9194 cur_arg = 1;
9195 /* try in the module list */
9196 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9197 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9198 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9199 free(errmsg);
9200 goto out_err;
9201 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009202 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009203 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 +01009204 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009205 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009206 }
9207
9208 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9209 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009210 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009211
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009212 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9213 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9214 file, linenum, args[0], errmsg);
9215 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009216 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009217 }
9218 rule->cond = cond;
9219 }
9220 else if (*args[cur_arg]) {
9221 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9222 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9223 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009224 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009225 }
9226
9227 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009228 out_err:
9229 free(rule);
9230 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009231}
9232
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009233/* parse an "http-respose" rule */
9234struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9235{
9236 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009237 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009238 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009239 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009240
9241 rule = calloc(1, sizeof(*rule));
9242 if (!rule) {
9243 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9244 goto out_err;
9245 }
9246
9247 if (!strcmp(args[0], "allow")) {
9248 rule->action = HTTP_RES_ACT_ALLOW;
9249 cur_arg = 1;
9250 } else if (!strcmp(args[0], "deny")) {
9251 rule->action = HTTP_RES_ACT_DENY;
9252 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009253 } else if (!strcmp(args[0], "set-nice")) {
9254 rule->action = HTTP_RES_ACT_SET_NICE;
9255 cur_arg = 1;
9256
9257 if (!*args[cur_arg] ||
9258 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9259 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9260 file, linenum, args[0]);
9261 goto out_err;
9262 }
9263 rule->arg.nice = atoi(args[cur_arg]);
9264 if (rule->arg.nice < -1024)
9265 rule->arg.nice = -1024;
9266 else if (rule->arg.nice > 1024)
9267 rule->arg.nice = 1024;
9268 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009269 } else if (!strcmp(args[0], "set-tos")) {
9270#ifdef IP_TOS
9271 char *err;
9272 rule->action = HTTP_RES_ACT_SET_TOS;
9273 cur_arg = 1;
9274
9275 if (!*args[cur_arg] ||
9276 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9277 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9278 file, linenum, args[0]);
9279 goto out_err;
9280 }
9281
9282 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9283 if (err && *err != '\0') {
9284 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9285 file, linenum, err, args[0]);
9286 goto out_err;
9287 }
9288 cur_arg++;
9289#else
9290 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9291 goto out_err;
9292#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009293 } else if (!strcmp(args[0], "set-mark")) {
9294#ifdef SO_MARK
9295 char *err;
9296 rule->action = HTTP_RES_ACT_SET_MARK;
9297 cur_arg = 1;
9298
9299 if (!*args[cur_arg] ||
9300 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9301 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9302 file, linenum, args[0]);
9303 goto out_err;
9304 }
9305
9306 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9307 if (err && *err != '\0') {
9308 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9309 file, linenum, err, args[0]);
9310 goto out_err;
9311 }
9312 cur_arg++;
9313 global.last_checks |= LSTCHK_NETADM;
9314#else
9315 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9316 goto out_err;
9317#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009318 } else if (!strcmp(args[0], "set-log-level")) {
9319 rule->action = HTTP_RES_ACT_SET_LOGL;
9320 cur_arg = 1;
9321
9322 if (!*args[cur_arg] ||
9323 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9324 bad_log_level:
9325 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9326 file, linenum, args[0]);
9327 goto out_err;
9328 }
9329 if (strcmp(args[cur_arg], "silent") == 0)
9330 rule->arg.loglevel = -1;
9331 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9332 goto bad_log_level;
9333 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009334 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9335 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9336 cur_arg = 1;
9337
9338 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9339 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9340 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9341 file, linenum, args[0]);
9342 goto out_err;
9343 }
9344
9345 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9346 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9347 LIST_INIT(&rule->arg.hdr_add.fmt);
9348
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009349 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009350 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009351 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9352 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009353 free(proxy->conf.lfs_file);
9354 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9355 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009356 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009357 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009358 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009359 cur_arg = 1;
9360
9361 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmanna772b942014-08-08 17:29:06 +02009362 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9363 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009364 file, linenum, args[0]);
9365 goto out_err;
9366 }
9367
9368 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9369 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9370 LIST_INIT(&rule->arg.hdr_add.fmt);
9371
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009372 error = NULL;
9373 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9374 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9375 args[cur_arg + 1], error);
9376 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009377 goto out_err;
9378 }
9379
9380 proxy->conf.args.ctx = ARGC_HRQ;
9381 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9382 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9383 file, linenum);
9384
9385 free(proxy->conf.lfs_file);
9386 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9387 proxy->conf.lfs_line = proxy->conf.args.line;
9388 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009389 } else if (strcmp(args[0], "del-header") == 0) {
9390 rule->action = HTTP_RES_ACT_DEL_HDR;
9391 cur_arg = 1;
9392
9393 if (!*args[cur_arg] ||
9394 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9395 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9396 file, linenum, args[0]);
9397 goto out_err;
9398 }
9399
9400 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9401 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9402
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009403 proxy->conf.args.ctx = ARGC_HRS;
9404 free(proxy->conf.lfs_file);
9405 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9406 proxy->conf.lfs_line = proxy->conf.args.line;
9407 cur_arg += 1;
9408 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9409 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9410 rule->action = HTTP_RES_ACT_ADD_ACL;
9411 /*
9412 * '+ 8' for 'add-acl('
9413 * '- 9' for 'add-acl(' + trailing ')'
9414 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009415 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009416
9417 cur_arg = 1;
9418
9419 if (!*args[cur_arg] ||
9420 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9421 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9422 file, linenum, args[0]);
9423 goto out_err;
9424 }
9425
9426 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009427 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009428 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9429 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9430 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009431 free(proxy->conf.lfs_file);
9432 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9433 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009434
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009435 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009436 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9437 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9438 rule->action = HTTP_RES_ACT_DEL_ACL;
9439 /*
9440 * '+ 8' for 'del-acl('
9441 * '- 9' for 'del-acl(' + trailing ')'
9442 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009443 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009444
9445 cur_arg = 1;
9446
9447 if (!*args[cur_arg] ||
9448 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9449 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9450 file, linenum, args[0]);
9451 goto out_err;
9452 }
9453
9454 LIST_INIT(&rule->arg.map.key);
9455 proxy->conf.args.ctx = ARGC_HRS;
9456 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9457 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9458 file, linenum);
9459 free(proxy->conf.lfs_file);
9460 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9461 proxy->conf.lfs_line = proxy->conf.args.line;
9462 cur_arg += 1;
9463 } else if (strncmp(args[0], "del-map", 7) == 0) {
9464 /* http-response del-map(<reference (map name)>) <key pattern> */
9465 rule->action = HTTP_RES_ACT_DEL_MAP;
9466 /*
9467 * '+ 8' for 'del-map('
9468 * '- 9' for 'del-map(' + trailing ')'
9469 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009470 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009471
9472 cur_arg = 1;
9473
9474 if (!*args[cur_arg] ||
9475 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9476 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9477 file, linenum, args[0]);
9478 goto out_err;
9479 }
9480
9481 LIST_INIT(&rule->arg.map.key);
9482 proxy->conf.args.ctx = ARGC_HRS;
9483 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9484 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9485 file, linenum);
9486 free(proxy->conf.lfs_file);
9487 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9488 proxy->conf.lfs_line = proxy->conf.args.line;
9489 cur_arg += 1;
9490 } else if (strncmp(args[0], "set-map", 7) == 0) {
9491 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9492 rule->action = HTTP_RES_ACT_SET_MAP;
9493 /*
9494 * '+ 8' for 'set-map('
9495 * '- 9' for 'set-map(' + trailing ')'
9496 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009497 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009498
9499 cur_arg = 1;
9500
9501 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9502 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9503 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9504 file, linenum, args[0]);
9505 goto out_err;
9506 }
9507
9508 LIST_INIT(&rule->arg.map.key);
9509 LIST_INIT(&rule->arg.map.value);
9510
9511 proxy->conf.args.ctx = ARGC_HRS;
9512
9513 /* key pattern */
9514 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9515 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9516 file, linenum);
9517
9518 /* value pattern */
9519 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9520 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9521 file, linenum);
9522
9523 free(proxy->conf.lfs_file);
9524 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9525 proxy->conf.lfs_line = proxy->conf.args.line;
9526
9527 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009528 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9529 char *errmsg = NULL;
9530 cur_arg = 1;
9531 /* try in the module list */
9532 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9533 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9534 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9535 free(errmsg);
9536 goto out_err;
9537 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009538 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009539 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 +02009540 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9541 goto out_err;
9542 }
9543
9544 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9545 struct acl_cond *cond;
9546 char *errmsg = NULL;
9547
9548 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9549 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9550 file, linenum, args[0], errmsg);
9551 free(errmsg);
9552 goto out_err;
9553 }
9554 rule->cond = cond;
9555 }
9556 else if (*args[cur_arg]) {
9557 Alert("parsing [%s:%d]: 'http-response %s' expects"
9558 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9559 file, linenum, args[0], args[cur_arg]);
9560 goto out_err;
9561 }
9562
9563 return rule;
9564 out_err:
9565 free(rule);
9566 return NULL;
9567}
9568
Willy Tarreau4baae242012-12-27 12:00:31 +01009569/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009570 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9571 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009572 */
9573struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009574 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009575{
9576 struct redirect_rule *rule;
9577 int cur_arg;
9578 int type = REDIRECT_TYPE_NONE;
9579 int code = 302;
9580 const char *destination = NULL;
9581 const char *cookie = NULL;
9582 int cookie_set = 0;
9583 unsigned int flags = REDIRECT_FLAG_NONE;
9584 struct acl_cond *cond = NULL;
9585
9586 cur_arg = 0;
9587 while (*(args[cur_arg])) {
9588 if (strcmp(args[cur_arg], "location") == 0) {
9589 if (!*args[cur_arg + 1])
9590 goto missing_arg;
9591
9592 type = REDIRECT_TYPE_LOCATION;
9593 cur_arg++;
9594 destination = args[cur_arg];
9595 }
9596 else if (strcmp(args[cur_arg], "prefix") == 0) {
9597 if (!*args[cur_arg + 1])
9598 goto missing_arg;
9599
9600 type = REDIRECT_TYPE_PREFIX;
9601 cur_arg++;
9602 destination = args[cur_arg];
9603 }
9604 else if (strcmp(args[cur_arg], "scheme") == 0) {
9605 if (!*args[cur_arg + 1])
9606 goto missing_arg;
9607
9608 type = REDIRECT_TYPE_SCHEME;
9609 cur_arg++;
9610 destination = args[cur_arg];
9611 }
9612 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9613 if (!*args[cur_arg + 1])
9614 goto missing_arg;
9615
9616 cur_arg++;
9617 cookie = args[cur_arg];
9618 cookie_set = 1;
9619 }
9620 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9621 if (!*args[cur_arg + 1])
9622 goto missing_arg;
9623
9624 cur_arg++;
9625 cookie = args[cur_arg];
9626 cookie_set = 0;
9627 }
9628 else if (strcmp(args[cur_arg], "code") == 0) {
9629 if (!*args[cur_arg + 1])
9630 goto missing_arg;
9631
9632 cur_arg++;
9633 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009634 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009635 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009636 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009637 args[cur_arg - 1], args[cur_arg]);
9638 return NULL;
9639 }
9640 }
9641 else if (!strcmp(args[cur_arg],"drop-query")) {
9642 flags |= REDIRECT_FLAG_DROP_QS;
9643 }
9644 else if (!strcmp(args[cur_arg],"append-slash")) {
9645 flags |= REDIRECT_FLAG_APPEND_SLASH;
9646 }
9647 else if (strcmp(args[cur_arg], "if") == 0 ||
9648 strcmp(args[cur_arg], "unless") == 0) {
9649 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9650 if (!cond) {
9651 memprintf(errmsg, "error in condition: %s", *errmsg);
9652 return NULL;
9653 }
9654 break;
9655 }
9656 else {
9657 memprintf(errmsg,
9658 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9659 args[cur_arg]);
9660 return NULL;
9661 }
9662 cur_arg++;
9663 }
9664
9665 if (type == REDIRECT_TYPE_NONE) {
9666 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9667 return NULL;
9668 }
9669
9670 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9671 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009672 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009673
9674 if (!use_fmt) {
9675 /* old-style static redirect rule */
9676 rule->rdr_str = strdup(destination);
9677 rule->rdr_len = strlen(destination);
9678 }
9679 else {
9680 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009681
9682 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9683 * if prefix == "/", we don't want to add anything, otherwise it
9684 * makes it hard for the user to configure a self-redirection.
9685 */
Godbach543b9782014-12-18 15:44:58 +08009686 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009687 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009688 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009689 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9690 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009691 free(curproxy->conf.lfs_file);
9692 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9693 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009694 }
9695 }
9696
Willy Tarreau4baae242012-12-27 12:00:31 +01009697 if (cookie) {
9698 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9699 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9700 */
9701 rule->cookie_len = strlen(cookie);
9702 if (cookie_set) {
9703 rule->cookie_str = malloc(rule->cookie_len + 10);
9704 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9705 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9706 rule->cookie_len += 9;
9707 } else {
9708 rule->cookie_str = malloc(rule->cookie_len + 21);
9709 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9710 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9711 rule->cookie_len += 20;
9712 }
9713 }
9714 rule->type = type;
9715 rule->code = code;
9716 rule->flags = flags;
9717 LIST_INIT(&rule->list);
9718 return rule;
9719
9720 missing_arg:
9721 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9722 return NULL;
9723}
9724
Willy Tarreau8797c062007-05-07 00:55:35 +02009725/************************************************************************/
9726/* The code below is dedicated to ACL parsing and matching */
9727/************************************************************************/
9728
9729
Willy Tarreau14174bc2012-04-16 14:34:04 +02009730/* This function ensures that the prerequisites for an L7 fetch are ready,
9731 * which means that a request or response is ready. If some data is missing,
9732 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009733 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9734 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009735 *
9736 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009737 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9738 * decide whether or not an HTTP message is present ;
9739 * 0 if the requested data cannot be fetched or if it is certain that
9740 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009741 * 1 if an HTTP message is ready
9742 */
9743static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009744smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009745 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009746{
9747 struct http_txn *txn = l7;
9748 struct http_msg *msg = &txn->req;
9749
9750 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9751 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9752 */
9753
9754 if (unlikely(!s || !txn))
9755 return 0;
9756
9757 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009758 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009759
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009760 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009761 if (unlikely(!s->req))
9762 return 0;
9763
Willy Tarreauaae75e32013-03-29 12:31:49 +01009764 /* If the buffer does not leave enough free space at the end,
9765 * we must first realign it.
9766 */
9767 if (s->req->buf->p > s->req->buf->data &&
9768 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9769 buffer_slow_realign(s->req->buf);
9770
Willy Tarreau14174bc2012-04-16 14:34:04 +02009771 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009772 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009773 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009774
9775 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009776 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009777 http_msg_analyzer(msg, &txn->hdr_idx);
9778
9779 /* Still no valid request ? */
9780 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009781 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009782 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009783 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009784 }
9785 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009786 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009787 return 0;
9788 }
9789
9790 /* OK we just got a valid HTTP request. We have some minor
9791 * preparation to perform so that further checks can rely
9792 * on HTTP tests.
9793 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009794
9795 /* If the request was parsed but was too large, we must absolutely
9796 * return an error so that it is not processed. At the moment this
9797 * cannot happen, but if the parsers are to change in the future,
9798 * we want this check to be maintained.
9799 */
9800 if (unlikely(s->req->buf->i + s->req->buf->p >
9801 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9802 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009803 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009804 return 1;
9805 }
9806
Willy Tarreau9b28e032012-10-12 23:49:43 +02009807 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009808 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9809 s->flags |= SN_REDIRECTABLE;
9810
Willy Tarreau506d0502013-07-06 13:29:24 +02009811 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9812 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009813 }
9814
Willy Tarreau506d0502013-07-06 13:29:24 +02009815 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009816 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009817 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009818
9819 /* otherwise everything's ready for the request */
9820 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009821 else {
9822 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009823 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9824 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009825 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009826 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009827 }
9828
9829 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009830 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009831 return 1;
9832}
Willy Tarreau8797c062007-05-07 00:55:35 +02009833
Willy Tarreaua4ba9db2014-06-25 16:56:41 +02009834/* Note: these functinos *do* modify the sample. Even in case of success, at
9835 * least the type and uint value are modified.
9836 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009837#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009838 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 +02009839
Willy Tarreau24e32d82012-04-23 23:55:44 +02009840#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009841 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 +02009842
Willy Tarreau8797c062007-05-07 00:55:35 +02009843
9844/* 1. Check on METHOD
9845 * We use the pre-parsed method if it is known, and store its number as an
9846 * integer. If it is unknown, we use the pointer and the length.
9847 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009848static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009849{
9850 int len, meth;
9851
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009852 len = strlen(text);
9853 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009854
9855 pattern->val.i = meth;
9856 if (meth == HTTP_METH_OTHER) {
Willy Tarreau71196f32014-08-29 15:15:50 +02009857 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009858 pattern->len = len;
9859 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009860 else {
9861 pattern->ptr.str = NULL;
9862 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009863 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009864 return 1;
9865}
9866
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009867/* This function fetches the method of current HTTP request and stores
9868 * it in the global pattern struct as a chunk. There are two possibilities :
9869 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9870 * in <len> and <ptr> is NULL ;
9871 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9872 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009873 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009874 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009875static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009876smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009877 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009878{
9879 int meth;
9880 struct http_txn *txn = l7;
9881
Willy Tarreau24e32d82012-04-23 23:55:44 +02009882 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009883
Willy Tarreau8797c062007-05-07 00:55:35 +02009884 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009885 smp->type = SMP_T_METH;
9886 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009887 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009888 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9889 /* ensure the indexes are not affected */
9890 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009891 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009892 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9893 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009894 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009895 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009896 return 1;
9897}
9898
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009899/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009900static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009901{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009902 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009903 struct pattern_list *lst;
9904 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009905
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009906 list_for_each_entry(lst, &expr->patterns, list) {
9907 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009908
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009909 /* well-known method */
9910 if (pattern->val.i != HTTP_METH_OTHER) {
9911 if (smp->data.meth.meth == pattern->val.i)
9912 return pattern;
9913 else
9914 continue;
9915 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009916
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009917 /* Other method, we must compare the strings */
9918 if (pattern->len != smp->data.meth.str.len)
9919 continue;
9920
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009921 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreauc62a0c62014-08-28 20:42:57 +02009922 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
9923 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009924 return pattern;
9925 }
9926 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009927}
9928
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009929static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009930smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009931 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009932{
9933 struct http_txn *txn = l7;
9934 char *ptr;
9935 int len;
9936
Willy Tarreauc0239e02012-04-16 14:42:55 +02009937 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009938
Willy Tarreau8797c062007-05-07 00:55:35 +02009939 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009940 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009941
9942 while ((len-- > 0) && (*ptr++ != '/'));
9943 if (len <= 0)
9944 return 0;
9945
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009946 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009947 smp->data.str.str = ptr;
9948 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009949
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009950 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009951 return 1;
9952}
9953
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009954static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009955smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009956 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009957{
9958 struct http_txn *txn = l7;
9959 char *ptr;
9960 int len;
9961
Willy Tarreauc0239e02012-04-16 14:42:55 +02009962 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009963
Willy Tarreauf26b2522012-12-14 08:33:14 +01009964 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9965 return 0;
9966
Willy Tarreau8797c062007-05-07 00:55:35 +02009967 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009968 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009969
9970 while ((len-- > 0) && (*ptr++ != '/'));
9971 if (len <= 0)
9972 return 0;
9973
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009974 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009975 smp->data.str.str = ptr;
9976 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009977
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009978 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009979 return 1;
9980}
9981
9982/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009983static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009984smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009985 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009986{
9987 struct http_txn *txn = l7;
9988 char *ptr;
9989 int len;
9990
Willy Tarreauc0239e02012-04-16 14:42:55 +02009991 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009992
Willy Tarreauf26b2522012-12-14 08:33:14 +01009993 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9994 return 0;
9995
Willy Tarreau8797c062007-05-07 00:55:35 +02009996 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009997 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009998
Willy Tarreauf853c462012-04-23 18:53:56 +02009999 smp->type = SMP_T_UINT;
10000 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010001 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010002 return 1;
10003}
10004
10005/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010006static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010007smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010008 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +020010009{
10010 struct http_txn *txn = l7;
10011
Willy Tarreauc0239e02012-04-16 14:42:55 +020010012 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010013
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010014 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010015 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010016 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010017 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010018 return 1;
10019}
10020
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010021static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010022smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010023 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010024{
10025 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010026 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010027
Willy Tarreauc0239e02012-04-16 14:42:55 +020010028 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010029
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010030 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 +020010031 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010032 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010033
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010034 smp->type = SMP_T_IPV4;
10035 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010036 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010037 return 1;
10038}
10039
10040static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010041smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010042 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010043{
10044 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010045 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010046
Willy Tarreauc0239e02012-04-16 14:42:55 +020010047 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010048
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010049 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 +020010050 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10051 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010052
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010053 smp->type = SMP_T_UINT;
10054 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010055 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010056 return 1;
10057}
10058
Willy Tarreau185b5c42012-04-26 15:11:51 +020010059/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10060 * Accepts an optional argument of type string containing the header field name,
10061 * and an optional argument of type signed or unsigned integer to request an
10062 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010063 * headers are considered from the first one. It does not stop on commas and
10064 * returns full lines instead (useful for User-Agent or Date for example).
10065 */
10066static int
10067smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010068 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010069{
10070 struct http_txn *txn = l7;
10071 struct hdr_idx *idx = &txn->hdr_idx;
10072 struct hdr_ctx *ctx = smp->ctx.a[0];
10073 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10074 int occ = 0;
10075 const char *name_str = NULL;
10076 int name_len = 0;
10077
10078 if (!ctx) {
10079 /* first call */
10080 ctx = &static_hdr_ctx;
10081 ctx->idx = 0;
10082 smp->ctx.a[0] = ctx;
10083 }
10084
10085 if (args) {
10086 if (args[0].type != ARGT_STR)
10087 return 0;
10088 name_str = args[0].data.str.str;
10089 name_len = args[0].data.str.len;
10090
10091 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10092 occ = args[1].data.uint;
10093 }
10094
10095 CHECK_HTTP_MESSAGE_FIRST();
10096
10097 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10098 /* search for header from the beginning */
10099 ctx->idx = 0;
10100
10101 if (!occ && !(opt & SMP_OPT_ITERATE))
10102 /* no explicit occurrence and single fetch => last header by default */
10103 occ = -1;
10104
10105 if (!occ)
10106 /* prepare to report multiple occurrences for ACL fetches */
10107 smp->flags |= SMP_F_NOT_LAST;
10108
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010109 smp->type = SMP_T_STR;
10110 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010111 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10112 return 1;
10113
10114 smp->flags &= ~SMP_F_NOT_LAST;
10115 return 0;
10116}
10117
10118/* 6. Check on HTTP header count. The number of occurrences is returned.
10119 * Accepts exactly 1 argument of type string. It does not stop on commas and
10120 * returns full lines instead (useful for User-Agent or Date for example).
10121 */
10122static int
10123smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010124 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010125{
10126 struct http_txn *txn = l7;
10127 struct hdr_idx *idx = &txn->hdr_idx;
10128 struct hdr_ctx ctx;
10129 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10130 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010131 const char *name = NULL;
10132 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010133
Willy Tarreau29437342015-04-01 19:16:09 +020010134 if (args && args->type == ARGT_STR) {
10135 name = args->data.str.str;
10136 len = args->data.str.len;
10137 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010138
10139 CHECK_HTTP_MESSAGE_FIRST();
10140
10141 ctx.idx = 0;
10142 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010143 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010144 cnt++;
10145
10146 smp->type = SMP_T_UINT;
10147 smp->data.uint = cnt;
10148 smp->flags = SMP_F_VOL_HDR;
10149 return 1;
10150}
10151
10152/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10153 * Accepts an optional argument of type string containing the header field name,
10154 * and an optional argument of type signed or unsigned integer to request an
10155 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010156 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010157 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010158static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010159smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010160 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010161{
10162 struct http_txn *txn = l7;
10163 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010164 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010165 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010166 int occ = 0;
10167 const char *name_str = NULL;
10168 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010169
Willy Tarreaua890d072013-04-02 12:01:06 +020010170 if (!ctx) {
10171 /* first call */
10172 ctx = &static_hdr_ctx;
10173 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010174 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010175 }
10176
Willy Tarreau185b5c42012-04-26 15:11:51 +020010177 if (args) {
10178 if (args[0].type != ARGT_STR)
10179 return 0;
10180 name_str = args[0].data.str.str;
10181 name_len = args[0].data.str.len;
10182
10183 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10184 occ = args[1].data.uint;
10185 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010186
Willy Tarreaue333ec92012-04-16 16:26:40 +020010187 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010188
Willy Tarreau185b5c42012-04-26 15:11:51 +020010189 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010190 /* search for header from the beginning */
10191 ctx->idx = 0;
10192
Willy Tarreau185b5c42012-04-26 15:11:51 +020010193 if (!occ && !(opt & SMP_OPT_ITERATE))
10194 /* no explicit occurrence and single fetch => last header by default */
10195 occ = -1;
10196
10197 if (!occ)
10198 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010199 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010200
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010201 smp->type = SMP_T_STR;
10202 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010203 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 +020010204 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010205
Willy Tarreau37406352012-04-23 16:16:37 +020010206 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010207 return 0;
10208}
10209
Willy Tarreauc11416f2007-06-17 16:58:38 +020010210/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010211 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010212 */
10213static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010214smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010215 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010216{
10217 struct http_txn *txn = l7;
10218 struct hdr_idx *idx = &txn->hdr_idx;
10219 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010220 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010221 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010222 const char *name = NULL;
10223 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010224
Willy Tarreau29437342015-04-01 19:16:09 +020010225 if (args && args->type == ARGT_STR) {
10226 name = args->data.str.str;
10227 len = args->data.str.len;
10228 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010229
Willy Tarreaue333ec92012-04-16 16:26:40 +020010230 CHECK_HTTP_MESSAGE_FIRST();
10231
Willy Tarreau33a7e692007-06-10 19:45:56 +020010232 ctx.idx = 0;
10233 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010234 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010235 cnt++;
10236
Willy Tarreauf853c462012-04-23 18:53:56 +020010237 smp->type = SMP_T_UINT;
10238 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010239 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010240 return 1;
10241}
10242
Willy Tarreau185b5c42012-04-26 15:11:51 +020010243/* Fetch an HTTP header's integer value. The integer value is returned. It
10244 * takes a mandatory argument of type string and an optional one of type int
10245 * to designate a specific occurrence. It returns an unsigned integer, which
10246 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010247 */
10248static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010249smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010250 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010251{
Willy Tarreauef38c392013-07-22 16:29:32 +020010252 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010253
Willy Tarreauf853c462012-04-23 18:53:56 +020010254 if (ret > 0) {
10255 smp->type = SMP_T_UINT;
10256 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10257 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010258
Willy Tarreaud53e2422012-04-16 17:21:11 +020010259 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010260}
10261
Cyril Bonté69fa9922012-10-25 00:01:06 +020010262/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10263 * and an optional one of type int to designate a specific occurrence.
10264 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010265 */
10266static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010267smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010268 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +020010269{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010270 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010271
Willy Tarreauef38c392013-07-22 16:29:32 +020010272 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010273 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10274 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010275 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010276 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010277 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010278 if (smp->data.str.len < temp->size - 1) {
10279 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10280 temp->str[smp->data.str.len] = '\0';
10281 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10282 smp->type = SMP_T_IPV6;
10283 break;
10284 }
10285 }
10286 }
10287
Willy Tarreaud53e2422012-04-16 17:21:11 +020010288 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010289 if (!(smp->flags & SMP_F_NOT_LAST))
10290 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010291 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010292 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010293}
10294
Willy Tarreau737b0c12007-06-10 21:28:46 +020010295/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10296 * the first '/' after the possible hostname, and ends before the possible '?'.
10297 */
10298static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010299smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010300 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010301{
10302 struct http_txn *txn = l7;
10303 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010304
Willy Tarreauc0239e02012-04-16 14:42:55 +020010305 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010306
Willy Tarreau9b28e032012-10-12 23:49:43 +020010307 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010308 ptr = http_get_path(txn);
10309 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010310 return 0;
10311
10312 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010313 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010314 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010315
10316 while (ptr < end && *ptr != '?')
10317 ptr++;
10318
Willy Tarreauf853c462012-04-23 18:53:56 +020010319 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010320 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010321 return 1;
10322}
10323
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010324/* This produces a concatenation of the first occurrence of the Host header
10325 * followed by the path component if it begins with a slash ('/'). This means
10326 * that '*' will not be added, resulting in exactly the first Host entry.
10327 * If no Host header is found, then the path is returned as-is. The returned
10328 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010329 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010330 */
10331static int
10332smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010333 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010334{
10335 struct http_txn *txn = l7;
10336 char *ptr, *end, *beg;
10337 struct hdr_ctx ctx;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010338 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010339
10340 CHECK_HTTP_MESSAGE_FIRST();
10341
10342 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010343 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +020010344 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010345
10346 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010347 temp = get_trash_chunk();
10348 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010349 smp->type = SMP_T_STR;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010350 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010351 smp->data.str.len = ctx.vlen;
10352
10353 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010354 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010355 beg = http_get_path(txn);
10356 if (!beg)
10357 beg = end;
10358
10359 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10360
10361 if (beg < ptr && *beg == '/') {
10362 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10363 smp->data.str.len += ptr - beg;
10364 }
10365
10366 smp->flags = SMP_F_VOL_1ST;
10367 return 1;
10368}
10369
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010370/* This produces a 32-bit hash of the concatenation of the first occurrence of
10371 * the Host header followed by the path component if it begins with a slash ('/').
10372 * This means that '*' will not be added, resulting in exactly the first Host
10373 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010374 * is hashed using the path hash followed by a full avalanche hash and provides a
10375 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010376 * high-traffic sites without having to store whole paths.
10377 */
10378static int
10379smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010380 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010381{
10382 struct http_txn *txn = l7;
10383 struct hdr_ctx ctx;
10384 unsigned int hash = 0;
10385 char *ptr, *beg, *end;
10386 int len;
10387
10388 CHECK_HTTP_MESSAGE_FIRST();
10389
10390 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010391 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010392 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10393 ptr = ctx.line + ctx.val;
10394 len = ctx.vlen;
10395 while (len--)
10396 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10397 }
10398
10399 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010400 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010401 beg = http_get_path(txn);
10402 if (!beg)
10403 beg = end;
10404
10405 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10406
10407 if (beg < ptr && *beg == '/') {
10408 while (beg < ptr)
10409 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10410 }
10411 hash = full_hash(hash);
10412
10413 smp->type = SMP_T_UINT;
10414 smp->data.uint = hash;
10415 smp->flags = SMP_F_VOL_1ST;
10416 return 1;
10417}
10418
Willy Tarreau4a550602012-12-09 14:53:32 +010010419/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010420 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10421 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10422 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010423 * that in environments where IPv6 is insignificant, truncating the output to
10424 * 8 bytes would still work.
10425 */
10426static int
10427smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010428 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +010010429{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010430 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010431 struct connection *cli_conn = objt_conn(l4->si[0].end);
10432
10433 if (!cli_conn)
10434 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010435
Willy Tarreauef38c392013-07-22 16:29:32 +020010436 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +010010437 return 0;
10438
Willy Tarreau47ca5452012-12-23 20:22:19 +010010439 temp = get_trash_chunk();
Willy Tarreau0dff81c2014-07-15 21:34:06 +020010440 *(unsigned int *)temp->str = htonl(smp->data.uint);
10441 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010442
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010443 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010444 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010445 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010446 temp->len += 4;
10447 break;
10448 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010449 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010450 temp->len += 16;
10451 break;
10452 default:
10453 return 0;
10454 }
10455
10456 smp->data.str = *temp;
10457 smp->type = SMP_T_BIN;
10458 return 1;
10459}
10460
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010461static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010462smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010463 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010464{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010465 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10466 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10467 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010468
Willy Tarreau24e32d82012-04-23 23:55:44 +020010469 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010470
Willy Tarreauf853c462012-04-23 18:53:56 +020010471 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010472 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010473 return 1;
10474}
10475
Willy Tarreau7f18e522010-10-22 20:04:13 +020010476/* return a valid test if the current request is the first one on the connection */
10477static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010478smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010479 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010480{
10481 if (!s)
10482 return 0;
10483
Willy Tarreauf853c462012-04-23 18:53:56 +020010484 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010485 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010486 return 1;
10487}
10488
Willy Tarreau34db1082012-04-19 17:16:54 +020010489/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010490static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010491smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010492 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010493{
10494
Willy Tarreau24e32d82012-04-23 23:55:44 +020010495 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010496 return 0;
10497
Willy Tarreauc0239e02012-04-16 14:42:55 +020010498 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010499
Willy Tarreauc0239e02012-04-16 14:42:55 +020010500 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010501 return 0;
10502
Willy Tarreauf853c462012-04-23 18:53:56 +020010503 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010504 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010505 return 1;
10506}
Willy Tarreau8797c062007-05-07 00:55:35 +020010507
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010508/* Accepts exactly 1 argument of type userlist */
10509static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010510smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010511 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010512{
10513
10514 if (!args || args->type != ARGT_USR)
10515 return 0;
10516
10517 CHECK_HTTP_MESSAGE_FIRST();
10518
10519 if (!get_http_auth(l4))
10520 return 0;
10521
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010522 /* if the user does not belong to the userlist or has a wrong password,
10523 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010524 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010525 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010526 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10527 return 0;
10528
10529 /* pat_match_auth() will need the user list */
10530 smp->ctx.a[0] = args->data.usr;
10531
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010532 smp->type = SMP_T_STR;
10533 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010534 smp->data.str.str = l4->txn.auth.user;
10535 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010536
10537 return 1;
10538}
10539
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010540/* Try to find the next occurrence of a cookie name in a cookie header value.
10541 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10542 * the cookie value is returned into *value and *value_l, and the function
10543 * returns a pointer to the next pointer to search from if the value was found.
10544 * Otherwise if the cookie was not found, NULL is returned and neither value
10545 * nor value_l are touched. The input <hdr> string should first point to the
10546 * header's value, and the <hdr_end> pointer must point to the first character
10547 * not part of the value. <list> must be non-zero if value may represent a list
10548 * of values (cookie headers). This makes it faster to abort parsing when no
10549 * list is expected.
10550 */
10551static char *
10552extract_cookie_value(char *hdr, const char *hdr_end,
10553 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010554 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010555{
10556 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10557 char *next;
10558
10559 /* we search at least a cookie name followed by an equal, and more
10560 * generally something like this :
10561 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10562 */
10563 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10564 /* Iterate through all cookies on this line */
10565
10566 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10567 att_beg++;
10568
10569 /* find att_end : this is the first character after the last non
10570 * space before the equal. It may be equal to hdr_end.
10571 */
10572 equal = att_end = att_beg;
10573
10574 while (equal < hdr_end) {
10575 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10576 break;
10577 if (http_is_spht[(unsigned char)*equal++])
10578 continue;
10579 att_end = equal;
10580 }
10581
10582 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10583 * is between <att_beg> and <equal>, both may be identical.
10584 */
10585
10586 /* look for end of cookie if there is an equal sign */
10587 if (equal < hdr_end && *equal == '=') {
10588 /* look for the beginning of the value */
10589 val_beg = equal + 1;
10590 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10591 val_beg++;
10592
10593 /* find the end of the value, respecting quotes */
10594 next = find_cookie_value_end(val_beg, hdr_end);
10595
10596 /* make val_end point to the first white space or delimitor after the value */
10597 val_end = next;
10598 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10599 val_end--;
10600 } else {
10601 val_beg = val_end = next = equal;
10602 }
10603
10604 /* We have nothing to do with attributes beginning with '$'. However,
10605 * they will automatically be removed if a header before them is removed,
10606 * since they're supposed to be linked together.
10607 */
10608 if (*att_beg == '$')
10609 continue;
10610
10611 /* Ignore cookies with no equal sign */
10612 if (equal == next)
10613 continue;
10614
10615 /* Now we have the cookie name between att_beg and att_end, and
10616 * its value between val_beg and val_end.
10617 */
10618
10619 if (att_end - att_beg == cookie_name_l &&
10620 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10621 /* let's return this value and indicate where to go on from */
10622 *value = val_beg;
10623 *value_l = val_end - val_beg;
10624 return next + 1;
10625 }
10626
10627 /* Set-Cookie headers only have the name in the first attr=value part */
10628 if (!list)
10629 break;
10630 }
10631
10632 return NULL;
10633}
10634
William Lallemanda43ba4e2014-01-28 18:14:25 +010010635/* Fetch a captured HTTP request header. The index is the position of
10636 * the "capture" option in the configuration file
10637 */
10638static int
10639smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10640 const struct arg *args, struct sample *smp, const char *kw)
10641{
10642 struct proxy *fe = l4->fe;
10643 struct http_txn *txn = l7;
10644 int idx;
10645
10646 if (!args || args->type != ARGT_UINT)
10647 return 0;
10648
10649 idx = args->data.uint;
10650
10651 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10652 return 0;
10653
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010654 smp->type = SMP_T_STR;
10655 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010656 smp->data.str.str = txn->req.cap[idx];
10657 smp->data.str.len = strlen(txn->req.cap[idx]);
10658
10659 return 1;
10660}
10661
10662/* Fetch a captured HTTP response header. The index is the position of
10663 * the "capture" option in the configuration file
10664 */
10665static int
10666smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10667 const struct arg *args, struct sample *smp, const char *kw)
10668{
10669 struct proxy *fe = l4->fe;
10670 struct http_txn *txn = l7;
10671 int idx;
10672
10673 if (!args || args->type != ARGT_UINT)
10674 return 0;
10675
10676 idx = args->data.uint;
10677
10678 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10679 return 0;
10680
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010681 smp->type = SMP_T_STR;
10682 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010683 smp->data.str.str = txn->rsp.cap[idx];
10684 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10685
10686 return 1;
10687}
10688
William Lallemand65ad6e12014-01-31 15:08:02 +010010689/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10690static int
10691smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10692 const struct arg *args, struct sample *smp, const char *kw)
10693{
10694 struct chunk *temp;
10695 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010696 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010697
10698 if (!txn->uri)
10699 return 0;
10700
William Lallemand96a77852014-02-05 00:30:02 +010010701 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010702
William Lallemand96a77852014-02-05 00:30:02 +010010703 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10704 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010705
William Lallemand96a77852014-02-05 00:30:02 +010010706 temp = get_trash_chunk();
10707 temp->str = txn->uri;
10708 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010709 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010710 smp->type = SMP_T_STR;
10711 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010712
10713 return 1;
10714
10715}
10716
10717/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10718static int
10719smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10720 const struct arg *args, struct sample *smp, const char *kw)
10721{
10722 struct chunk *temp;
10723 struct http_txn *txn = l7;
10724 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010725
10726 if (!txn->uri)
10727 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010728
William Lallemand65ad6e12014-01-31 15:08:02 +010010729 ptr = txn->uri;
10730
10731 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10732 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010733
William Lallemand65ad6e12014-01-31 15:08:02 +010010734 if (!*ptr)
10735 return 0;
10736
10737 ptr++; /* skip the space */
10738
10739 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010740 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010741 if (!ptr)
10742 return 0;
10743 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10744 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010745
10746 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010747 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010748 smp->type = SMP_T_STR;
10749 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010750
10751 return 1;
10752}
10753
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010754/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10755 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10756 */
10757static int
10758smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10759 const struct arg *args, struct sample *smp, const char *kw)
10760{
10761 struct http_txn *txn = l7;
10762
10763 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10764 return 0;
10765
10766 if (txn->req.flags & HTTP_MSGF_VER_11)
10767 smp->data.str.str = "HTTP/1.1";
10768 else
10769 smp->data.str.str = "HTTP/1.0";
10770
10771 smp->data.str.len = 8;
10772 smp->type = SMP_T_STR;
10773 smp->flags = SMP_F_CONST;
10774 return 1;
10775
10776}
10777
10778/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10779 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10780 */
10781static int
10782smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10783 const struct arg *args, struct sample *smp, const char *kw)
10784{
10785 struct http_txn *txn = l7;
10786
10787 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10788 return 0;
10789
10790 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10791 smp->data.str.str = "HTTP/1.1";
10792 else
10793 smp->data.str.str = "HTTP/1.0";
10794
10795 smp->data.str.len = 8;
10796 smp->type = SMP_T_STR;
10797 smp->flags = SMP_F_CONST;
10798 return 1;
10799
10800}
10801
William Lallemand65ad6e12014-01-31 15:08:02 +010010802
Willy Tarreaue333ec92012-04-16 16:26:40 +020010803/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010804 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010805 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010806 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010807 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010808 * Accepts exactly 1 argument of type string. If the input options indicate
10809 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010810 * The returned sample is of type CSTR. Can be used to parse cookies in other
10811 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010812 */
William Lallemand07c8b242014-05-02 17:11:07 +020010813int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010814 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010815{
10816 struct http_txn *txn = l7;
10817 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010818 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010819 const struct http_msg *msg;
10820 const char *hdr_name;
10821 int hdr_name_len;
10822 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010823 int occ = 0;
10824 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010825
Willy Tarreau24e32d82012-04-23 23:55:44 +020010826 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010827 return 0;
10828
Willy Tarreaua890d072013-04-02 12:01:06 +020010829 if (!ctx) {
10830 /* first call */
10831 ctx = &static_hdr_ctx;
10832 ctx->idx = 0;
10833 smp->ctx.a[2] = ctx;
10834 }
10835
Willy Tarreaue333ec92012-04-16 16:26:40 +020010836 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010837
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010838 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010839 msg = &txn->req;
10840 hdr_name = "Cookie";
10841 hdr_name_len = 6;
10842 } else {
10843 msg = &txn->rsp;
10844 hdr_name = "Set-Cookie";
10845 hdr_name_len = 10;
10846 }
10847
Willy Tarreau28376d62012-04-26 21:26:10 +020010848 if (!occ && !(opt & SMP_OPT_ITERATE))
10849 /* no explicit occurrence and single fetch => last cookie by default */
10850 occ = -1;
10851
10852 /* OK so basically here, either we want only one value and it's the
10853 * last one, or we want to iterate over all of them and we fetch the
10854 * next one.
10855 */
10856
Willy Tarreau9b28e032012-10-12 23:49:43 +020010857 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010858 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010859 /* search for the header from the beginning, we must first initialize
10860 * the search parameters.
10861 */
Willy Tarreau37406352012-04-23 16:16:37 +020010862 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010863 ctx->idx = 0;
10864 }
10865
Willy Tarreau28376d62012-04-26 21:26:10 +020010866 smp->flags |= SMP_F_VOL_HDR;
10867
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010868 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010869 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10870 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010871 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10872 goto out;
10873
Willy Tarreau24e32d82012-04-23 23:55:44 +020010874 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010875 continue;
10876
Willy Tarreau37406352012-04-23 16:16:37 +020010877 smp->ctx.a[0] = ctx->line + ctx->val;
10878 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010879 }
10880
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010881 smp->type = SMP_T_STR;
10882 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010883 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010884 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010885 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010886 &smp->data.str.str,
10887 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010888 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010889 found = 1;
10890 if (occ >= 0) {
10891 /* one value was returned into smp->data.str.{str,len} */
10892 smp->flags |= SMP_F_NOT_LAST;
10893 return 1;
10894 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010895 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010896 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010897 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010898 /* all cookie headers and values were scanned. If we're looking for the
10899 * last occurrence, we may return it now.
10900 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010901 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010902 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010903 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010904}
10905
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010906/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010907 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010908 * multiple cookies may be parsed on the same line. The returned sample is of
10909 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010910 */
10911static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010912smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010913 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010914{
10915 struct http_txn *txn = l7;
10916 struct hdr_idx *idx = &txn->hdr_idx;
10917 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010918 const struct http_msg *msg;
10919 const char *hdr_name;
10920 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010921 int cnt;
10922 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010923 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010924
Willy Tarreau24e32d82012-04-23 23:55:44 +020010925 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010926 return 0;
10927
Willy Tarreaue333ec92012-04-16 16:26:40 +020010928 CHECK_HTTP_MESSAGE_FIRST();
10929
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010930 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010931 msg = &txn->req;
10932 hdr_name = "Cookie";
10933 hdr_name_len = 6;
10934 } else {
10935 msg = &txn->rsp;
10936 hdr_name = "Set-Cookie";
10937 hdr_name_len = 10;
10938 }
10939
Willy Tarreau9b28e032012-10-12 23:49:43 +020010940 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010941 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010942 ctx.idx = 0;
10943 cnt = 0;
10944
10945 while (1) {
10946 /* Note: val_beg == NULL every time we need to fetch a new header */
10947 if (!val_beg) {
10948 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10949 break;
10950
Willy Tarreau24e32d82012-04-23 23:55:44 +020010951 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010952 continue;
10953
10954 val_beg = ctx.line + ctx.val;
10955 val_end = val_beg + ctx.vlen;
10956 }
10957
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010958 smp->type = SMP_T_STR;
10959 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010960 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010961 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010962 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010963 &smp->data.str.str,
10964 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010965 cnt++;
10966 }
10967 }
10968
Willy Tarreaub169eba2013-12-16 15:14:43 +010010969 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010970 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010971 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010972 return 1;
10973}
10974
Willy Tarreau51539362012-05-08 12:46:28 +020010975/* Fetch an cookie's integer value. The integer value is returned. It
10976 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10977 */
10978static int
10979smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010980 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010981{
Willy Tarreauef38c392013-07-22 16:29:32 +020010982 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010983
10984 if (ret > 0) {
10985 smp->type = SMP_T_UINT;
10986 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10987 }
10988
10989 return ret;
10990}
10991
Willy Tarreau8797c062007-05-07 00:55:35 +020010992/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010993/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010994/************************************************************************/
10995
David Cournapeau16023ee2010-12-23 20:55:41 +090010996/*
10997 * Given a path string and its length, find the position of beginning of the
10998 * query string. Returns NULL if no query string is found in the path.
10999 *
11000 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11001 *
11002 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11003 */
bedis4c75cca2012-10-05 08:38:24 +020011004static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011005{
11006 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011007
bedis4c75cca2012-10-05 08:38:24 +020011008 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011009 return p ? p + 1 : NULL;
11010}
11011
bedis4c75cca2012-10-05 08:38:24 +020011012static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011013{
bedis4c75cca2012-10-05 08:38:24 +020011014 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011015}
11016
11017/*
11018 * Given a url parameter, find the starting position of the first occurence,
11019 * or NULL if the parameter is not found.
11020 *
11021 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11022 * the function will return query_string+8.
11023 */
11024static char*
11025find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011026 char* url_param_name, size_t url_param_name_l,
11027 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011028{
11029 char *pos, *last;
11030
11031 pos = query_string;
11032 last = query_string + query_string_l - url_param_name_l - 1;
11033
11034 while (pos <= last) {
11035 if (pos[url_param_name_l] == '=') {
11036 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11037 return pos;
11038 pos += url_param_name_l + 1;
11039 }
bedis4c75cca2012-10-05 08:38:24 +020011040 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011041 pos++;
11042 pos++;
11043 }
11044 return NULL;
11045}
11046
11047/*
11048 * Given a url parameter name, returns its value and size into *value and
11049 * *value_l respectively, and returns non-zero. If the parameter is not found,
11050 * zero is returned and value/value_l are not touched.
11051 */
11052static int
11053find_url_param_value(char* path, size_t path_l,
11054 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011055 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011056{
11057 char *query_string, *qs_end;
11058 char *arg_start;
11059 char *value_start, *value_end;
11060
bedis4c75cca2012-10-05 08:38:24 +020011061 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011062 if (!query_string)
11063 return 0;
11064
11065 qs_end = path + path_l;
11066 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011067 url_param_name, url_param_name_l,
11068 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011069 if (!arg_start)
11070 return 0;
11071
11072 value_start = arg_start + url_param_name_l + 1;
11073 value_end = value_start;
11074
bedis4c75cca2012-10-05 08:38:24 +020011075 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011076 value_end++;
11077
11078 *value = value_start;
11079 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010011080 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090011081}
11082
11083static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011084smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011085 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090011086{
bedis4c75cca2012-10-05 08:38:24 +020011087 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090011088 struct http_txn *txn = l7;
11089 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011090
bedis4c75cca2012-10-05 08:38:24 +020011091 if (!args || args[0].type != ARGT_STR ||
11092 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011093 return 0;
11094
11095 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011096
bedis4c75cca2012-10-05 08:38:24 +020011097 if (args[1].type)
11098 delim = *args[1].data.str.str;
11099
Willy Tarreau9b28e032012-10-12 23:49:43 +020011100 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011101 args->data.str.str, args->data.str.len,
11102 &smp->data.str.str, &smp->data.str.len,
11103 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011104 return 0;
11105
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011106 smp->type = SMP_T_STR;
11107 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011108 return 1;
11109}
11110
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011111/* Return the signed integer value for the specified url parameter (see url_param
11112 * above).
11113 */
11114static int
11115smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011116 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011117{
Willy Tarreauef38c392013-07-22 16:29:32 +020011118 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011119
11120 if (ret > 0) {
11121 smp->type = SMP_T_UINT;
11122 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11123 }
11124
11125 return ret;
11126}
11127
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011128/* This produces a 32-bit hash of the concatenation of the first occurrence of
11129 * the Host header followed by the path component if it begins with a slash ('/').
11130 * This means that '*' will not be added, resulting in exactly the first Host
11131 * entry. If no Host header is found, then the path is used. The resulting value
11132 * is hashed using the url hash followed by a full avalanche hash and provides a
11133 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11134 * high-traffic sites without having to store whole paths.
11135 * this differs from the base32 functions in that it includes the url parameters
11136 * as well as the path
11137 */
11138static int
11139smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011140 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011141{
11142 struct http_txn *txn = l7;
11143 struct hdr_ctx ctx;
11144 unsigned int hash = 0;
11145 char *ptr, *beg, *end;
11146 int len;
11147
11148 CHECK_HTTP_MESSAGE_FIRST();
11149
11150 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011151 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011152 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11153 ptr = ctx.line + ctx.val;
11154 len = ctx.vlen;
11155 while (len--)
11156 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11157 }
11158
11159 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011160 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011161 beg = http_get_path(txn);
11162 if (!beg)
11163 beg = end;
11164
11165 for (ptr = beg; ptr < end ; ptr++);
11166
11167 if (beg < ptr && *beg == '/') {
11168 while (beg < ptr)
11169 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11170 }
11171 hash = full_hash(hash);
11172
11173 smp->type = SMP_T_UINT;
11174 smp->data.uint = hash;
11175 smp->flags = SMP_F_VOL_1ST;
11176 return 1;
11177}
11178
11179/* This concatenates the source address with the 32-bit hash of the Host and
11180 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11181 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11182 * on the source address length. The URL hash is stored before the address so
11183 * that in environments where IPv6 is insignificant, truncating the output to
11184 * 8 bytes would still work.
11185 */
11186static int
11187smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011188 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011189{
11190 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011191 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011192
Willy Tarreaue155ec22013-11-18 18:33:22 +010011193 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011194 return 0;
11195
11196 temp = get_trash_chunk();
11197 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
11198 temp->len += sizeof(smp->data.uint);
11199
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011200 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011201 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011202 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011203 temp->len += 4;
11204 break;
11205 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011206 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011207 temp->len += 16;
11208 break;
11209 default:
11210 return 0;
11211 }
11212
11213 smp->data.str = *temp;
11214 smp->type = SMP_T_BIN;
11215 return 1;
11216}
11217
Willy Tarreau185b5c42012-04-26 15:11:51 +020011218/* This function is used to validate the arguments passed to any "hdr" fetch
11219 * keyword. These keywords support an optional positive or negative occurrence
11220 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11221 * is assumed that the types are already the correct ones. Returns 0 on error,
11222 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11223 * error message in case of error, that the caller is responsible for freeing.
11224 * The initial location must either be freeable or NULL.
11225 */
11226static int val_hdr(struct arg *arg, char **err_msg)
11227{
11228 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011229 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011230 return 0;
11231 }
11232 return 1;
11233}
11234
Willy Tarreau276fae92013-07-25 14:36:01 +020011235/* takes an UINT value on input supposed to represent the time since EPOCH,
11236 * adds an optional offset found in args[0] and emits a string representing
11237 * the date in RFC-1123/5322 format.
11238 */
11239static int sample_conv_http_date(const struct arg *args, struct sample *smp)
11240{
11241 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
11242 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11243 struct chunk *temp;
11244 struct tm *tm;
11245 time_t curr_date = smp->data.uint;
11246
11247 /* add offset */
11248 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11249 curr_date += args[0].data.sint;
11250
11251 tm = gmtime(&curr_date);
Thierry FOURNIER95558722015-07-08 00:15:20 +020011252 if (!tm)
11253 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011254
11255 temp = get_trash_chunk();
11256 temp->len = snprintf(temp->str, temp->size - temp->len,
11257 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11258 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11259 tm->tm_hour, tm->tm_min, tm->tm_sec);
11260
11261 smp->data.str = *temp;
11262 smp->type = SMP_T_STR;
11263 return 1;
11264}
11265
Thierry FOURNIERad903512014-04-11 17:51:01 +020011266/* Match language range with language tag. RFC2616 14.4:
11267 *
11268 * A language-range matches a language-tag if it exactly equals
11269 * the tag, or if it exactly equals a prefix of the tag such
11270 * that the first tag character following the prefix is "-".
11271 *
11272 * Return 1 if the strings match, else return 0.
11273 */
11274static inline int language_range_match(const char *range, int range_len,
11275 const char *tag, int tag_len)
11276{
11277 const char *end = range + range_len;
11278 const char *tend = tag + tag_len;
11279 while (range < end) {
11280 if (*range == '-' && tag == tend)
11281 return 1;
11282 if (*range != *tag || tag == tend)
11283 return 0;
11284 range++;
11285 tag++;
11286 }
11287 /* Return true only if the last char of the tag is matched. */
11288 return tag == tend;
11289}
11290
11291/* Arguments: The list of expected value, the number of parts returned and the separator */
11292static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
11293{
11294 const char *al = smp->data.str.str;
11295 const char *end = al + smp->data.str.len;
11296 const char *token;
11297 int toklen;
11298 int qvalue;
11299 const char *str;
11300 const char *w;
11301 int best_q = 0;
11302
11303 /* Set the constant to the sample, because the output of the
11304 * function will be peek in the constant configuration string.
11305 */
11306 smp->flags |= SMP_F_CONST;
11307 smp->data.str.size = 0;
11308 smp->data.str.str = "";
11309 smp->data.str.len = 0;
11310
11311 /* Parse the accept language */
11312 while (1) {
11313
11314 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011315 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011316 al++;
11317 if (al >= end)
11318 break;
11319
11320 /* Start of the fisrt word. */
11321 token = al;
11322
11323 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011324 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011325 al++;
11326 if (al == token)
11327 goto expect_comma;
11328
11329 /* Length of the token. */
11330 toklen = al - token;
11331 qvalue = 1000;
11332
11333 /* Check if the token exists in the list. If the token not exists,
11334 * jump to the next token.
11335 */
11336 str = args[0].data.str.str;
11337 w = str;
11338 while (1) {
11339 if (*str == ';' || *str == '\0') {
11340 if (language_range_match(token, toklen, w, str-w))
11341 goto look_for_q;
11342 if (*str == '\0')
11343 goto expect_comma;
11344 w = str + 1;
11345 }
11346 str++;
11347 }
11348 goto expect_comma;
11349
11350look_for_q:
11351
11352 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011353 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011354 al++;
11355 if (al >= end)
11356 goto process_value;
11357
11358 /* If ',' is found, process the result */
11359 if (*al == ',')
11360 goto process_value;
11361
11362 /* If the character is different from ';', look
11363 * for the end of the header part in best effort.
11364 */
11365 if (*al != ';')
11366 goto expect_comma;
11367
11368 /* Assumes that the char is ';', now expect "q=". */
11369 al++;
11370
11371 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011372 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011373 al++;
11374 if (al >= end)
11375 goto process_value;
11376
11377 /* Expect 'q'. If no 'q', continue in best effort */
11378 if (*al != 'q')
11379 goto process_value;
11380 al++;
11381
11382 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011383 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011384 al++;
11385 if (al >= end)
11386 goto process_value;
11387
11388 /* Expect '='. If no '=', continue in best effort */
11389 if (*al != '=')
11390 goto process_value;
11391 al++;
11392
11393 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011394 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011395 al++;
11396 if (al >= end)
11397 goto process_value;
11398
11399 /* Parse the q value. */
11400 qvalue = parse_qvalue(al, &al);
11401
11402process_value:
11403
11404 /* If the new q value is the best q value, then store the associated
11405 * language in the response. If qvalue is the biggest value (1000),
11406 * break the process.
11407 */
11408 if (qvalue > best_q) {
11409 smp->data.str.str = (char *)w;
11410 smp->data.str.len = str - w;
11411 if (qvalue >= 1000)
11412 break;
11413 best_q = qvalue;
11414 }
11415
11416expect_comma:
11417
11418 /* Expect comma or end. If the end is detected, quit the loop. */
11419 while (al < end && *al != ',')
11420 al++;
11421 if (al >= end)
11422 break;
11423
11424 /* Comma is found, jump it and restart the analyzer. */
11425 al++;
11426 }
11427
11428 /* Set default value if required. */
11429 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11430 smp->data.str.str = args[1].data.str.str;
11431 smp->data.str.len = args[1].data.str.len;
11432 }
11433
11434 /* Return true only if a matching language was found. */
11435 return smp->data.str.len != 0;
11436}
11437
William Lallemand73025dd2014-04-24 14:38:37 +020011438/*
11439 * Return the struct http_req_action_kw associated to a keyword.
11440 */
11441struct http_req_action_kw *action_http_req_custom(const char *kw)
11442{
11443 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11444 struct http_req_action_kw_list *kw_list;
11445 int i;
11446
11447 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11448 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11449 if (!strcmp(kw, kw_list->kw[i].kw))
11450 return &kw_list->kw[i];
11451 }
11452 }
11453 }
11454 return NULL;
11455}
11456
11457/*
11458 * Return the struct http_res_action_kw associated to a keyword.
11459 */
11460struct http_res_action_kw *action_http_res_custom(const char *kw)
11461{
11462 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11463 struct http_res_action_kw_list *kw_list;
11464 int i;
11465
11466 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11467 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11468 if (!strcmp(kw, kw_list->kw[i].kw))
11469 return &kw_list->kw[i];
11470 }
11471 }
11472 }
11473 return NULL;
11474}
11475
Willy Tarreau4a568972010-05-12 08:08:50 +020011476/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011477/* All supported ACL keywords must be declared here. */
11478/************************************************************************/
11479
11480/* Note: must not be declared <const> as its list will be overwritten.
11481 * Please take care of keeping this list alphabetically sorted.
11482 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011483static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011484 { "base", "base", PAT_MATCH_STR },
11485 { "base_beg", "base", PAT_MATCH_BEG },
11486 { "base_dir", "base", PAT_MATCH_DIR },
11487 { "base_dom", "base", PAT_MATCH_DOM },
11488 { "base_end", "base", PAT_MATCH_END },
11489 { "base_len", "base", PAT_MATCH_LEN },
11490 { "base_reg", "base", PAT_MATCH_REG },
11491 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011492
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011493 { "cook", "req.cook", PAT_MATCH_STR },
11494 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11495 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11496 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11497 { "cook_end", "req.cook", PAT_MATCH_END },
11498 { "cook_len", "req.cook", PAT_MATCH_LEN },
11499 { "cook_reg", "req.cook", PAT_MATCH_REG },
11500 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011501
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011502 { "hdr", "req.hdr", PAT_MATCH_STR },
11503 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11504 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11505 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11506 { "hdr_end", "req.hdr", PAT_MATCH_END },
11507 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11508 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11509 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011510
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011511 /* these two declarations uses strings with list storage (in place
11512 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11513 * and delete functions are relative to the list management. The parse
11514 * and match method are related to the corresponding fetch methods. This
11515 * is very particular ACL declaration mode.
11516 */
11517 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11518 { "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 +020011519
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011520 { "path", "path", PAT_MATCH_STR },
11521 { "path_beg", "path", PAT_MATCH_BEG },
11522 { "path_dir", "path", PAT_MATCH_DIR },
11523 { "path_dom", "path", PAT_MATCH_DOM },
11524 { "path_end", "path", PAT_MATCH_END },
11525 { "path_len", "path", PAT_MATCH_LEN },
11526 { "path_reg", "path", PAT_MATCH_REG },
11527 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011528
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011529 { "req_ver", "req.ver", PAT_MATCH_STR },
11530 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011531
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011532 { "scook", "res.cook", PAT_MATCH_STR },
11533 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11534 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11535 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11536 { "scook_end", "res.cook", PAT_MATCH_END },
11537 { "scook_len", "res.cook", PAT_MATCH_LEN },
11538 { "scook_reg", "res.cook", PAT_MATCH_REG },
11539 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011540
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011541 { "shdr", "res.hdr", PAT_MATCH_STR },
11542 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11543 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11544 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11545 { "shdr_end", "res.hdr", PAT_MATCH_END },
11546 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11547 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11548 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011549
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011550 { "url", "url", PAT_MATCH_STR },
11551 { "url_beg", "url", PAT_MATCH_BEG },
11552 { "url_dir", "url", PAT_MATCH_DIR },
11553 { "url_dom", "url", PAT_MATCH_DOM },
11554 { "url_end", "url", PAT_MATCH_END },
11555 { "url_len", "url", PAT_MATCH_LEN },
11556 { "url_reg", "url", PAT_MATCH_REG },
11557 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011558
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011559 { "urlp", "urlp", PAT_MATCH_STR },
11560 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11561 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11562 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11563 { "urlp_end", "urlp", PAT_MATCH_END },
11564 { "urlp_len", "urlp", PAT_MATCH_LEN },
11565 { "urlp_reg", "urlp", PAT_MATCH_REG },
11566 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011567
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011568 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011569}};
11570
11571/************************************************************************/
11572/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011573/************************************************************************/
11574/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011575static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011576 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011577 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11578 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11579
William Lallemanda43ba4e2014-01-28 18:14:25 +010011580 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011581 { "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 +020011582
11583 /* retrieve these captures from the HTTP logs */
11584 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11585 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11586 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11587
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011588 { "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 +020011589 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011590
Willy Tarreau409bcde2013-01-08 00:31:00 +010011591 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11592 * are only here to match the ACL's name, are request-only and are used
11593 * for ACL compatibility only.
11594 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011595 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11596 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011597 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11598 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11599
11600 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11601 * only here to match the ACL's name, are request-only and are used for
11602 * ACL compatibility only.
11603 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011604 { "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 +010011605 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11606 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11607 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11608
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011609 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011610 { "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 +010011611 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011612 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011613 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011614
11615 /* HTTP protocol on the request path */
11616 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011617 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011618
11619 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011620 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11621 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011622
11623 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011624 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11625 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011626
Willy Tarreau18ed2562013-01-14 15:56:36 +010011627 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011628 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011629 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11630 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11631
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011632 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011633 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011634 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011635 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11636 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11637 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11638
11639 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011640 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011641 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11642 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11643
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011644 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011645 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011646 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011647 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11648 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11649 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11650
Willy Tarreau409bcde2013-01-08 00:31:00 +010011651 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011652 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011653 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11654 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011655 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011656
11657 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011658 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011659 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11660 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11661 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11662
11663 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011664 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011665 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11666 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011667 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11668 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011669 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11670 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011671 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11672 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011673}};
11674
Willy Tarreau8797c062007-05-07 00:55:35 +020011675
Willy Tarreau276fae92013-07-25 14:36:01 +020011676/* Note: must not be declared <const> as its list will be overwritten */
11677static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011678 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11679 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011680 { NULL, NULL, 0, 0, 0 },
11681}};
11682
Willy Tarreau8797c062007-05-07 00:55:35 +020011683__attribute__((constructor))
11684static void __http_protocol_init(void)
11685{
11686 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011687 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011688 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020011689}
11690
11691
Willy Tarreau58f10d72006-12-04 02:26:12 +010011692/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011693 * Local variables:
11694 * c-indent-level: 8
11695 * c-basic-offset: 8
11696 * End:
11697 */