blob: 35c8e9906bd23cc9532d1fb1f6a2464f6be2bd4e [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Thierry FOURNIERed66c292013-11-28 11:05:19 +010057#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020058#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010060#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020062#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010063#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020064#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010065#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020066#include <proto/task.h>
Baptiste Assmannfabcbe02014-04-24 22:16:59 +020067#include <proto/pattern.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020068
Willy Tarreau522d6c02009-12-06 18:49:18 +010069const char HTTP_100[] =
70 "HTTP/1.1 100 Continue\r\n\r\n";
71
72const struct chunk http_100_chunk = {
73 .str = (char *)&HTTP_100,
74 .len = sizeof(HTTP_100)-1
75};
76
Willy Tarreaua9679ac2010-01-03 17:32:57 +010077/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010080 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020081 "Location: "; /* not terminated since it will be concatenated with the URL */
82
Willy Tarreau0f772532006-12-23 20:51:41 +010083const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010086 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010087 "Location: "; /* not terminated since it will be concatenated with the URL */
88
89/* same as 302 except that the browser MUST retry with the GET method */
90const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010093 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010094 "Location: "; /* not terminated since it will be concatenated with the URL */
95
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040096
97/* same as 302 except that the browser MUST retry with the same method */
98const char *HTTP_307 =
99 "HTTP/1.1 307 Temporary Redirect\r\n"
100 "Cache-Control: no-cache\r\n"
101 "Content-length: 0\r\n"
102 "Location: "; /* not terminated since it will be concatenated with the URL */
103
104/* same as 301 except that the browser MUST retry with the same method */
105const char *HTTP_308 =
106 "HTTP/1.1 308 Permanent Redirect\r\n"
107 "Content-length: 0\r\n"
108 "Location: "; /* not terminated since it will be concatenated with the URL */
109
Willy Tarreaubaaee002006-06-26 02:48:02 +0200110/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
111const char *HTTP_401_fmt =
112 "HTTP/1.0 401 Unauthorized\r\n"
113 "Cache-Control: no-cache\r\n"
114 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200115 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200116 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
117 "\r\n"
118 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
119
Willy Tarreau844a7e72010-01-31 21:46:18 +0100120const char *HTTP_407_fmt =
121 "HTTP/1.0 407 Unauthorized\r\n"
122 "Cache-Control: no-cache\r\n"
123 "Connection: close\r\n"
124 "Content-Type: text/html\r\n"
125 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
126 "\r\n"
Godbachb3016542014-12-17 16:32:05 +0800127 "<html><body><h1>407 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
Willy Tarreau844a7e72010-01-31 21:46:18 +0100128
Willy Tarreau0f772532006-12-23 20:51:41 +0100129
130const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200131 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100132 [HTTP_ERR_400] = 400,
133 [HTTP_ERR_403] = 403,
134 [HTTP_ERR_408] = 408,
135 [HTTP_ERR_500] = 500,
136 [HTTP_ERR_502] = 502,
137 [HTTP_ERR_503] = 503,
138 [HTTP_ERR_504] = 504,
139};
140
Willy Tarreau80587432006-12-24 17:47:20 +0100141static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200142 [HTTP_ERR_200] =
143 "HTTP/1.0 200 OK\r\n"
144 "Cache-Control: no-cache\r\n"
145 "Connection: close\r\n"
146 "Content-Type: text/html\r\n"
147 "\r\n"
148 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
149
Willy Tarreau0f772532006-12-23 20:51:41 +0100150 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100151 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100152 "Cache-Control: no-cache\r\n"
153 "Connection: close\r\n"
154 "Content-Type: text/html\r\n"
155 "\r\n"
156 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
157
158 [HTTP_ERR_403] =
159 "HTTP/1.0 403 Forbidden\r\n"
160 "Cache-Control: no-cache\r\n"
161 "Connection: close\r\n"
162 "Content-Type: text/html\r\n"
163 "\r\n"
164 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
165
166 [HTTP_ERR_408] =
167 "HTTP/1.0 408 Request Time-out\r\n"
168 "Cache-Control: no-cache\r\n"
169 "Connection: close\r\n"
170 "Content-Type: text/html\r\n"
171 "\r\n"
172 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
173
174 [HTTP_ERR_500] =
175 "HTTP/1.0 500 Server Error\r\n"
176 "Cache-Control: no-cache\r\n"
177 "Connection: close\r\n"
178 "Content-Type: text/html\r\n"
179 "\r\n"
180 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
181
182 [HTTP_ERR_502] =
183 "HTTP/1.0 502 Bad Gateway\r\n"
184 "Cache-Control: no-cache\r\n"
185 "Connection: close\r\n"
186 "Content-Type: text/html\r\n"
187 "\r\n"
188 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
189
190 [HTTP_ERR_503] =
191 "HTTP/1.0 503 Service Unavailable\r\n"
192 "Cache-Control: no-cache\r\n"
193 "Connection: close\r\n"
194 "Content-Type: text/html\r\n"
195 "\r\n"
196 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
197
198 [HTTP_ERR_504] =
199 "HTTP/1.0 504 Gateway Time-out\r\n"
200 "Cache-Control: no-cache\r\n"
201 "Connection: close\r\n"
202 "Content-Type: text/html\r\n"
203 "\r\n"
204 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
205
206};
207
Cyril Bonté19979e12012-04-04 12:57:21 +0200208/* status codes available for the stats admin page (strictly 4 chars length) */
209const char *stat_status_codes[STAT_STATUS_SIZE] = {
210 [STAT_STATUS_DENY] = "DENY",
211 [STAT_STATUS_DONE] = "DONE",
212 [STAT_STATUS_ERRP] = "ERRP",
213 [STAT_STATUS_EXCD] = "EXCD",
214 [STAT_STATUS_NONE] = "NONE",
215 [STAT_STATUS_PART] = "PART",
216 [STAT_STATUS_UNKN] = "UNKN",
217};
218
219
William Lallemand73025dd2014-04-24 14:38:37 +0200220/* List head of all known action keywords for "http-request" */
221struct http_req_action_kw_list http_req_keywords = {
222 .list = LIST_HEAD_INIT(http_req_keywords.list)
223};
224
225/* List head of all known action keywords for "http-response" */
226struct http_res_action_kw_list http_res_keywords = {
227 .list = LIST_HEAD_INIT(http_res_keywords.list)
228};
229
Willy Tarreau80587432006-12-24 17:47:20 +0100230/* We must put the messages here since GCC cannot initialize consts depending
231 * on strlen().
232 */
233struct chunk http_err_chunks[HTTP_ERR_SIZE];
234
Willy Tarreaua890d072013-04-02 12:01:06 +0200235/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
236static struct hdr_ctx static_hdr_ctx;
237
Willy Tarreau42250582007-04-01 01:30:43 +0200238#define FD_SETS_ARE_BITFIELDS
239#ifdef FD_SETS_ARE_BITFIELDS
240/*
241 * This map is used with all the FD_* macros to check whether a particular bit
242 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
243 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
244 * byte should be encoded. Be careful to always pass bytes from 0 to 255
245 * exclusively to the macros.
246 */
247fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
248fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100249fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200250
251#else
252#error "Check if your OS uses bitfields for fd_sets"
253#endif
254
Willy Tarreau0b748332014-04-29 00:13:29 +0200255static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn);
256
Willy Tarreau80587432006-12-24 17:47:20 +0100257void init_proto_http()
258{
Willy Tarreau42250582007-04-01 01:30:43 +0200259 int i;
260 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100261 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200262
Willy Tarreau80587432006-12-24 17:47:20 +0100263 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
264 if (!http_err_msgs[msg]) {
265 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
266 abort();
267 }
268
269 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
270 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
271 }
Willy Tarreau42250582007-04-01 01:30:43 +0200272
273 /* initialize the log header encoding map : '{|}"#' should be encoded with
274 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
275 * URL encoding only requires '"', '#' to be encoded as well as non-
276 * printable characters above.
277 */
278 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
279 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100280 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200281 for (i = 0; i < 32; i++) {
282 FD_SET(i, hdr_encode_map);
283 FD_SET(i, url_encode_map);
284 }
285 for (i = 127; i < 256; i++) {
286 FD_SET(i, hdr_encode_map);
287 FD_SET(i, url_encode_map);
288 }
289
290 tmp = "\"#{|}";
291 while (*tmp) {
292 FD_SET(*tmp, hdr_encode_map);
293 tmp++;
294 }
295
296 tmp = "\"#";
297 while (*tmp) {
298 FD_SET(*tmp, url_encode_map);
299 tmp++;
300 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200301
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100302 /* initialize the http header encoding map. The draft httpbis define the
303 * header content as:
304 *
305 * HTTP-message = start-line
306 * *( header-field CRLF )
307 * CRLF
308 * [ message-body ]
309 * header-field = field-name ":" OWS field-value OWS
310 * field-value = *( field-content / obs-fold )
311 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
312 * obs-fold = CRLF 1*( SP / HTAB )
313 * field-vchar = VCHAR / obs-text
314 * VCHAR = %x21-7E
315 * obs-text = %x80-FF
316 *
317 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
318 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
319 * "obs-fold" is volontary forgotten because haproxy remove this.
320 */
321 memset(http_encode_map, 0, sizeof(http_encode_map));
322 for (i = 0x00; i <= 0x08; i++)
323 FD_SET(i, http_encode_map);
324 for (i = 0x0a; i <= 0x1f; i++)
325 FD_SET(i, http_encode_map);
326 FD_SET(0x7f, http_encode_map);
327
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200328 /* memory allocations */
329 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100330 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100331}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332
Willy Tarreau53b6c742006-12-17 13:37:46 +0100333/*
334 * We have 26 list of methods (1 per first letter), each of which can have
335 * up to 3 entries (2 valid, 1 null).
336 */
337struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100338 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100339 int len;
340 const char text[8];
341};
342
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100343const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100344 ['C' - 'A'] = {
345 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
346 },
347 ['D' - 'A'] = {
348 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
349 },
350 ['G' - 'A'] = {
351 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
352 },
353 ['H' - 'A'] = {
354 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
355 },
Christopher Fauleta591ef22015-07-31 14:26:57 +0200356 ['O' - 'A'] = {
357 [0] = { .meth = HTTP_METH_OPTIONS , .len=7, .text="OPTIONS" },
358 },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100359 ['P' - 'A'] = {
360 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
361 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
362 },
363 ['T' - 'A'] = {
364 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
365 },
366 /* rest is empty like this :
Willy Tarreaubcd03362015-09-03 17:15:21 +0200367 * [0] = { .meth = HTTP_METH_OTHER , .len=0, .text="" },
Willy Tarreau53b6c742006-12-17 13:37:46 +0100368 */
369};
370
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100371const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100372 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
373 [HTTP_METH_GET] = { "GET", 3 },
374 [HTTP_METH_HEAD] = { "HEAD", 4 },
375 [HTTP_METH_POST] = { "POST", 4 },
376 [HTTP_METH_PUT] = { "PUT", 3 },
377 [HTTP_METH_DELETE] = { "DELETE", 6 },
378 [HTTP_METH_TRACE] = { "TRACE", 5 },
379 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
380};
381
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100382/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200383 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100384 * RFC2616 for those chars.
385 */
386
387const char http_is_spht[256] = {
388 [' '] = 1, ['\t'] = 1,
389};
390
391const char http_is_crlf[256] = {
392 ['\r'] = 1, ['\n'] = 1,
393};
394
395const char http_is_lws[256] = {
396 [' '] = 1, ['\t'] = 1,
397 ['\r'] = 1, ['\n'] = 1,
398};
399
400const char http_is_sep[256] = {
401 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
402 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
403 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
404 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
405 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
406};
407
408const char http_is_ctl[256] = {
409 [0 ... 31] = 1,
410 [127] = 1,
411};
412
413/*
414 * A token is any ASCII char that is neither a separator nor a CTL char.
415 * Do not overwrite values in assignment since gcc-2.95 will not handle
416 * them correctly. Instead, define every non-CTL char's status.
417 */
418const char http_is_token[256] = {
419 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
420 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
421 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
422 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
423 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
424 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
425 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
426 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
427 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
428 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
429 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
430 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
431 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
432 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
433 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
434 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
435 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
436 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
437 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
438 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
439 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
440 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
441 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
442 ['|'] = 1, ['}'] = 0, ['~'] = 1,
443};
444
445
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100446/*
447 * An http ver_token is any ASCII which can be found in an HTTP version,
448 * which includes 'H', 'T', 'P', '/', '.' and any digit.
449 */
450const char http_is_ver_token[256] = {
451 ['.'] = 1, ['/'] = 1,
452 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
453 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
454 ['H'] = 1, ['P'] = 1, ['T'] = 1,
455};
456
457
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100458/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100459 * Adds a header and its CRLF at the tail of the message's buffer, just before
460 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100461 * The header is also automatically added to the index <hdr_idx>, and the end
462 * of headers is automatically adjusted. The number of bytes added is returned
463 * on success, otherwise <0 is returned indicating an error.
464 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100465int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100466{
467 int bytes, len;
468
469 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200470 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100471 if (!bytes)
472 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100473 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100474 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
475}
476
477/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100478 * Adds a header and its CRLF at the tail of the message's buffer, just before
479 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100480 * the buffer is only opened and the space reserved, but nothing is copied.
481 * The header is also automatically added to the index <hdr_idx>, and the end
482 * of headers is automatically adjusted. The number of bytes added is returned
483 * on success, otherwise <0 is returned indicating an error.
484 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100485int http_header_add_tail2(struct http_msg *msg,
486 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100487{
488 int bytes;
489
Willy Tarreau9b28e032012-10-12 23:49:43 +0200490 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100491 if (!bytes)
492 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100493 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100494 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
495}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200496
497/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100498 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
499 * If so, returns the position of the first non-space character relative to
500 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
501 * to return a pointer to the place after the first space. Returns 0 if the
502 * header name does not match. Checks are case-insensitive.
503 */
504int http_header_match2(const char *hdr, const char *end,
505 const char *name, int len)
506{
507 const char *val;
508
509 if (hdr + len >= end)
510 return 0;
511 if (hdr[len] != ':')
512 return 0;
513 if (strncasecmp(hdr, name, len) != 0)
514 return 0;
515 val = hdr + len + 1;
516 while (val < end && HTTP_IS_SPHT(*val))
517 val++;
518 if ((val >= end) && (len + 2 <= end - hdr))
519 return len + 2; /* we may replace starting from second space */
520 return val - hdr;
521}
522
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200523/* Find the first or next occurrence of header <name> in message buffer <sol>
524 * using headers index <idx>, and return it in the <ctx> structure. This
525 * structure holds everything necessary to use the header and find next
526 * occurrence. If its <idx> member is 0, the header is searched from the
527 * beginning. Otherwise, the next occurrence is returned. The function returns
528 * 1 when it finds a value, and 0 when there is no more. It is very similar to
529 * http_find_header2() except that it is designed to work with full-line headers
530 * whose comma is not a delimiter but is part of the syntax. As a special case,
531 * if ctx->val is NULL when searching for a new values of a header, the current
532 * header is rescanned. This allows rescanning after a header deletion.
533 */
534int http_find_full_header2(const char *name, int len,
535 char *sol, struct hdr_idx *idx,
536 struct hdr_ctx *ctx)
537{
538 char *eol, *sov;
539 int cur_idx, old_idx;
540
541 cur_idx = ctx->idx;
542 if (cur_idx) {
543 /* We have previously returned a header, let's search another one */
544 sol = ctx->line;
545 eol = sol + idx->v[cur_idx].len;
546 goto next_hdr;
547 }
548
549 /* first request for this header */
550 sol += hdr_idx_first_pos(idx);
551 old_idx = 0;
552 cur_idx = hdr_idx_first_idx(idx);
553 while (cur_idx) {
554 eol = sol + idx->v[cur_idx].len;
555
556 if (len == 0) {
557 /* No argument was passed, we want any header.
558 * To achieve this, we simply build a fake request. */
559 while (sol + len < eol && sol[len] != ':')
560 len++;
561 name = sol;
562 }
563
564 if ((len < eol - sol) &&
565 (sol[len] == ':') &&
566 (strncasecmp(sol, name, len) == 0)) {
567 ctx->del = len;
568 sov = sol + len + 1;
569 while (sov < eol && http_is_lws[(unsigned char)*sov])
570 sov++;
571
572 ctx->line = sol;
573 ctx->prev = old_idx;
574 ctx->idx = cur_idx;
575 ctx->val = sov - sol;
576 ctx->tws = 0;
577 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
578 eol--;
579 ctx->tws++;
580 }
581 ctx->vlen = eol - sov;
582 return 1;
583 }
584 next_hdr:
585 sol = eol + idx->v[cur_idx].cr + 1;
586 old_idx = cur_idx;
587 cur_idx = idx->v[cur_idx].next;
588 }
589 return 0;
590}
591
Willy Tarreau68085d82010-01-18 14:54:04 +0100592/* Find the end of the header value contained between <s> and <e>. See RFC2616,
593 * par 2.2 for more information. Note that it requires a valid header to return
594 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200595 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100596char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200597{
598 int quoted, qdpair;
599
600 quoted = qdpair = 0;
601 for (; s < e; s++) {
602 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200603 else if (quoted) {
604 if (*s == '\\') qdpair = 1;
605 else if (*s == '"') quoted = 0;
606 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200607 else if (*s == '"') quoted = 1;
608 else if (*s == ',') return s;
609 }
610 return s;
611}
612
613/* Find the first or next occurrence of header <name> in message buffer <sol>
614 * using headers index <idx>, and return it in the <ctx> structure. This
615 * structure holds everything necessary to use the header and find next
616 * occurrence. If its <idx> member is 0, the header is searched from the
617 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100618 * 1 when it finds a value, and 0 when there is no more. It is designed to work
619 * with headers defined as comma-separated lists. As a special case, if ctx->val
620 * is NULL when searching for a new values of a header, the current header is
621 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200622 */
623int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100624 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200625 struct hdr_ctx *ctx)
626{
Willy Tarreau68085d82010-01-18 14:54:04 +0100627 char *eol, *sov;
628 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200629
Willy Tarreau68085d82010-01-18 14:54:04 +0100630 cur_idx = ctx->idx;
631 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200632 /* We have previously returned a value, let's search
633 * another one on the same line.
634 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200635 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200636 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100637 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200638 eol = sol + idx->v[cur_idx].len;
639
640 if (sov >= eol)
641 /* no more values in this header */
642 goto next_hdr;
643
Willy Tarreau68085d82010-01-18 14:54:04 +0100644 /* values remaining for this header, skip the comma but save it
645 * for later use (eg: for header deletion).
646 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200647 sov++;
648 while (sov < eol && http_is_lws[(unsigned char)*sov])
649 sov++;
650
651 goto return_hdr;
652 }
653
654 /* first request for this header */
655 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100656 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200657 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200658 while (cur_idx) {
659 eol = sol + idx->v[cur_idx].len;
660
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200661 if (len == 0) {
662 /* No argument was passed, we want any header.
663 * To achieve this, we simply build a fake request. */
664 while (sol + len < eol && sol[len] != ':')
665 len++;
666 name = sol;
667 }
668
Willy Tarreau33a7e692007-06-10 19:45:56 +0200669 if ((len < eol - sol) &&
670 (sol[len] == ':') &&
671 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100672 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200673 sov = sol + len + 1;
674 while (sov < eol && http_is_lws[(unsigned char)*sov])
675 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100676
Willy Tarreau33a7e692007-06-10 19:45:56 +0200677 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100678 ctx->prev = old_idx;
679 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200680 ctx->idx = cur_idx;
681 ctx->val = sov - sol;
682
683 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200684 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200685 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200686 eol--;
687 ctx->tws++;
688 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200689 ctx->vlen = eol - sov;
690 return 1;
691 }
692 next_hdr:
693 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100694 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200695 cur_idx = idx->v[cur_idx].next;
696 }
697 return 0;
698}
699
700int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100701 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200702 struct hdr_ctx *ctx)
703{
704 return http_find_header2(name, strlen(name), sol, idx, ctx);
705}
706
Willy Tarreau68085d82010-01-18 14:54:04 +0100707/* Remove one value of a header. This only works on a <ctx> returned by one of
708 * the http_find_header functions. The value is removed, as well as surrounding
709 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100710 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100711 * message <msg>. The new index is returned. If it is zero, it means there is
712 * no more header, so any processing may stop. The ctx is always left in a form
713 * that can be handled by http_find_header2() to find next occurrence.
714 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100715int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100716{
717 int cur_idx = ctx->idx;
718 char *sol = ctx->line;
719 struct hdr_idx_elem *hdr;
720 int delta, skip_comma;
721
722 if (!cur_idx)
723 return 0;
724
725 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200726 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100727 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200728 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100729 http_msg_move_end(msg, delta);
730 idx->used--;
731 hdr->len = 0; /* unused entry */
732 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100733 if (idx->tail == ctx->idx)
734 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100735 ctx->idx = ctx->prev; /* walk back to the end of previous header */
Willy Tarreau14acc652015-01-07 17:23:50 +0100736 ctx->line -= idx->v[ctx->idx].len + idx->v[ctx->idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100737 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200738 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100739 return ctx->idx;
740 }
741
742 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200743 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
744 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100745 */
746
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200747 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200748 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200749 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100750 NULL, 0);
751 hdr->len += delta;
752 http_msg_move_end(msg, delta);
753 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200754 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100755 return ctx->idx;
756}
757
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100758/* This function handles a server error at the stream interface level. The
759 * stream interface is assumed to be already in a closed state. An optional
760 * message is copied into the input buffer, and an HTTP status code stored.
761 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100762 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200763 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200764static void http_server_error(struct session *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100765 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200766{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200767 channel_auto_read(si->ob);
768 channel_abort(si->ob);
769 channel_auto_close(si->ob);
770 channel_erase(si->ob);
771 channel_auto_close(si->ib);
772 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100773 if (status > 0 && msg) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200774 s->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200775 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200776 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200777 if (!(s->flags & SN_ERR_MASK))
778 s->flags |= err;
779 if (!(s->flags & SN_FINST_MASK))
780 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200781}
782
Willy Tarreau80587432006-12-24 17:47:20 +0100783/* This function returns the appropriate error location for the given session
784 * and message.
785 */
786
Willy Tarreau783f2582012-09-04 12:19:04 +0200787struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100788{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200789 if (s->be->errmsg[msgnum].str)
790 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100791 else if (s->fe->errmsg[msgnum].str)
792 return &s->fe->errmsg[msgnum];
793 else
794 return &http_err_chunks[msgnum];
795}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200796
Willy Tarreau53b6c742006-12-17 13:37:46 +0100797/*
Willy Tarreaubcd03362015-09-03 17:15:21 +0200798 * returns a known method among HTTP_METH_* or HTTP_METH_OTHER for all unknown
799 * ones.
Willy Tarreau53b6c742006-12-17 13:37:46 +0100800 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100801enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100802{
803 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100804 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100805
806 m = ((unsigned)*str - 'A');
807
808 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100809 for (h = http_methods[m]; h->len > 0; h++) {
810 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100811 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100812 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100813 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100814 };
Willy Tarreau53b6c742006-12-17 13:37:46 +0100815 }
Willy Tarreaubcd03362015-09-03 17:15:21 +0200816 return HTTP_METH_OTHER;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100817}
818
Willy Tarreau21d2af32008-02-14 20:25:24 +0100819/* Parse the URI from the given transaction (which is assumed to be in request
820 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
821 * It is returned otherwise.
822 */
823static char *
824http_get_path(struct http_txn *txn)
825{
826 char *ptr, *end;
827
Willy Tarreau9b28e032012-10-12 23:49:43 +0200828 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100829 end = ptr + txn->req.sl.rq.u_l;
830
831 if (ptr >= end)
832 return NULL;
833
834 /* RFC2616, par. 5.1.2 :
835 * Request-URI = "*" | absuri | abspath | authority
836 */
837
838 if (*ptr == '*')
839 return NULL;
840
841 if (isalpha((unsigned char)*ptr)) {
842 /* this is a scheme as described by RFC3986, par. 3.1 */
843 ptr++;
844 while (ptr < end &&
845 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
846 ptr++;
847 /* skip '://' */
848 if (ptr == end || *ptr++ != ':')
849 return NULL;
850 if (ptr == end || *ptr++ != '/')
851 return NULL;
852 if (ptr == end || *ptr++ != '/')
853 return NULL;
854 }
855 /* skip [user[:passwd]@]host[:[port]] */
856
857 while (ptr < end && *ptr != '/')
858 ptr++;
859
860 if (ptr == end)
861 return NULL;
862
863 /* OK, we got the '/' ! */
864 return ptr;
865}
866
William Lallemand65ad6e12014-01-31 15:08:02 +0100867/* Parse the URI from the given string and look for the "/" beginning the PATH.
868 * If not found, return NULL. It is returned otherwise.
869 */
870static char *
871http_get_path_from_string(char *str)
872{
873 char *ptr = str;
874
875 /* RFC2616, par. 5.1.2 :
876 * Request-URI = "*" | absuri | abspath | authority
877 */
878
879 if (*ptr == '*')
880 return NULL;
881
882 if (isalpha((unsigned char)*ptr)) {
883 /* this is a scheme as described by RFC3986, par. 3.1 */
884 ptr++;
885 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
886 ptr++;
887 /* skip '://' */
888 if (*ptr == '\0' || *ptr++ != ':')
889 return NULL;
890 if (*ptr == '\0' || *ptr++ != '/')
891 return NULL;
892 if (*ptr == '\0' || *ptr++ != '/')
893 return NULL;
894 }
895 /* skip [user[:passwd]@]host[:[port]] */
896
897 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
898 ptr++;
899
900 if (*ptr == '\0' || *ptr == ' ')
901 return NULL;
902
903 /* OK, we got the '/' ! */
904 return ptr;
905}
906
Willy Tarreau71241ab2012-12-27 11:30:54 +0100907/* Returns a 302 for a redirectable request that reaches a server working in
908 * in redirect mode. This may only be called just after the stream interface
909 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
910 * follow normal proxy processing. NOTE: this function is designed to support
911 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100912 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100913void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100914{
915 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100916 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100917 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200918 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100919
920 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100921 trash.len = strlen(HTTP_302);
922 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100923
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100924 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100925
Willy Tarreauefb453c2008-10-26 20:49:47 +0100926 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100927 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100928 return;
929
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100930 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100931 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100932 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
933 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100934 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100935
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200936 /* 3: add the request URI. Since it was already forwarded, we need
937 * to temporarily rewind the buffer.
938 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100939 txn = &s->txn;
Willy Tarreau211cdec2014-04-17 20:18:08 +0200940 b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200941
Willy Tarreauefb453c2008-10-26 20:49:47 +0100942 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200943 len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200944
Willy Tarreau9b28e032012-10-12 23:49:43 +0200945 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200946
Willy Tarreauefb453c2008-10-26 20:49:47 +0100947 if (!path)
948 return;
949
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100950 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100951 return;
952
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100953 memcpy(trash.str + trash.len, path, len);
954 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100955
956 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100957 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
958 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100959 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100960 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
961 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100962 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100963
964 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200965 si_shutr(si);
966 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100967 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100968 si->state = SI_ST_CLO;
969
970 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200971 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100972
973 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100974 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500975 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100976}
977
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100978/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100979 * that the server side is closed. Note that err_type is actually a
980 * bitmask, where almost only aborts may be cumulated with other
981 * values. We consider that aborted operations are more important
982 * than timeouts or errors due to the fact that nobody else in the
983 * logs might explain incomplete retries. All others should avoid
984 * being cumulated. It should normally not be possible to have multiple
985 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100986 * Note that connection errors appearing on the second request of a keep-alive
987 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100988 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100989void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100991 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100992
993 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100994 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200995 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100996 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100997 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100998 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
999 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001000 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001001 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001002 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001003 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001004 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001005 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001006 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001007 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001008 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1009 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001010 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001011 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001012 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001013 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001014 else if (err_type & SI_ET_CONN_RES)
1015 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001016 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1017 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001018 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001019 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001020 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001021}
1022
Willy Tarreau42250582007-04-01 01:30:43 +02001023extern const char sess_term_cond[8];
1024extern const char sess_fin_state[8];
1025extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001026struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001027struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001028struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001029
Willy Tarreau117f59e2007-03-04 18:17:17 +01001030/*
1031 * Capture headers from message starting at <som> according to header list
Willy Tarreau54da8db2014-06-13 16:11:48 +02001032 * <cap_hdr>, and fill the <cap> pointers appropriately.
Willy Tarreau117f59e2007-03-04 18:17:17 +01001033 */
1034void capture_headers(char *som, struct hdr_idx *idx,
1035 char **cap, struct cap_hdr *cap_hdr)
1036{
1037 char *eol, *sol, *col, *sov;
1038 int cur_idx;
1039 struct cap_hdr *h;
1040 int len;
1041
1042 sol = som + hdr_idx_first_pos(idx);
1043 cur_idx = hdr_idx_first_idx(idx);
1044
1045 while (cur_idx) {
1046 eol = sol + idx->v[cur_idx].len;
1047
1048 col = sol;
1049 while (col < eol && *col != ':')
1050 col++;
1051
1052 sov = col + 1;
1053 while (sov < eol && http_is_lws[(unsigned char)*sov])
1054 sov++;
1055
1056 for (h = cap_hdr; h; h = h->next) {
Willy Tarreau54da8db2014-06-13 16:11:48 +02001057 if (h->namelen && (h->namelen == col - sol) &&
Willy Tarreau117f59e2007-03-04 18:17:17 +01001058 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1059 if (cap[h->index] == NULL)
1060 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001061 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001062
1063 if (cap[h->index] == NULL) {
1064 Alert("HTTP capture : out of memory.\n");
1065 continue;
1066 }
1067
1068 len = eol - sov;
1069 if (len > h->len)
1070 len = h->len;
1071
1072 memcpy(cap[h->index], sov, len);
1073 cap[h->index][len]=0;
1074 }
1075 }
1076 sol = eol + idx->v[cur_idx].cr + 1;
1077 cur_idx = idx->v[cur_idx].next;
1078 }
1079}
1080
1081
Willy Tarreau42250582007-04-01 01:30:43 +02001082/* either we find an LF at <ptr> or we jump to <bad>.
1083 */
1084#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1085
1086/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1087 * otherwise to <http_msg_ood> with <state> set to <st>.
1088 */
1089#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1090 ptr++; \
1091 if (likely(ptr < end)) \
1092 goto good; \
1093 else { \
1094 state = (st); \
1095 goto http_msg_ood; \
1096 } \
1097 } while (0)
1098
1099
Willy Tarreaubaaee002006-06-26 02:48:02 +02001100/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001101 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001102 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1103 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1104 * will give undefined results.
1105 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1106 * and that msg->sol points to the beginning of the response.
1107 * If a complete line is found (which implies that at least one CR or LF is
1108 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1109 * returned indicating an incomplete line (which does not mean that parts have
1110 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1111 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1112 * upon next call.
1113 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001114 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001115 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1116 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001117 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001118 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001119const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001120 enum ht_state state, const char *ptr, const char *end,
1121 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001122{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001123 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001124
Willy Tarreau8973c702007-01-21 23:58:29 +01001125 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001126 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001127 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001128 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001129 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1130
1131 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001132 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001133 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1134 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001135 state = HTTP_MSG_ERROR;
1136 break;
1137
Willy Tarreau8973c702007-01-21 23:58:29 +01001138 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001139 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001140 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001141 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001142 goto http_msg_rpcode;
1143 }
1144 if (likely(HTTP_IS_SPHT(*ptr)))
1145 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1146 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001147 state = HTTP_MSG_ERROR;
1148 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001149
Willy Tarreau8973c702007-01-21 23:58:29 +01001150 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001151 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001152 if (likely(!HTTP_IS_LWS(*ptr)))
1153 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1154
1155 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001156 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001157 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1158 }
1159
1160 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001161 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001162 http_msg_rsp_reason:
1163 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001164 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001165 msg->sl.st.r_l = 0;
1166 goto http_msg_rpline_eol;
1167
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001169 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001170 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001171 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001172 goto http_msg_rpreason;
1173 }
1174 if (likely(HTTP_IS_SPHT(*ptr)))
1175 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1176 /* so it's a CR/LF, so there is no reason phrase */
1177 goto http_msg_rsp_reason;
1178
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001180 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001181 if (likely(!HTTP_IS_CRLF(*ptr)))
1182 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001183 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001184 http_msg_rpline_eol:
1185 /* We have seen the end of line. Note that we do not
1186 * necessarily have the \n yet, but at least we know that we
1187 * have EITHER \r OR \n, otherwise the response would not be
1188 * complete. We can then record the response length and return
1189 * to the caller which will be able to register it.
1190 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001191 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001192 return ptr;
1193
Willy Tarreau8973c702007-01-21 23:58:29 +01001194 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001195#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001196 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1197 exit(1);
1198#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001199 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001200 }
1201
1202 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001203 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001204 if (ret_state)
1205 *ret_state = state;
1206 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001207 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001208 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001209}
1210
Willy Tarreau8973c702007-01-21 23:58:29 +01001211/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001212 * This function parses a request line between <ptr> and <end>, starting with
1213 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1214 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1215 * will give undefined results.
1216 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1217 * and that msg->sol points to the beginning of the request.
1218 * If a complete line is found (which implies that at least one CR or LF is
1219 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1220 * returned indicating an incomplete line (which does not mean that parts have
1221 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1222 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1223 * upon next call.
1224 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001225 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1227 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001228 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001229 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001230const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001231 enum ht_state state, const char *ptr, const char *end,
1232 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001233{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001234 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001235
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001236 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001238 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239 if (likely(HTTP_IS_TOKEN(*ptr)))
1240 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001241
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001242 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001243 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001244 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1245 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001246
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001247 if (likely(HTTP_IS_CRLF(*ptr))) {
1248 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001249 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001250 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001251 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001252 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001253 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001254 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001255 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001256 msg->sl.rq.v_l = 0;
1257 goto http_msg_rqline_eol;
1258 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001259 state = HTTP_MSG_ERROR;
1260 break;
1261
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001262 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001263 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001265 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001266 goto http_msg_rquri;
1267 }
1268 if (likely(HTTP_IS_SPHT(*ptr)))
1269 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1270 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1271 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001272
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001274 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001275 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001277
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001279 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001280 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1281 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001282
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001283 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001284 /* non-ASCII chars are forbidden unless option
1285 * accept-invalid-http-request is enabled in the frontend.
1286 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001287 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001288 if (msg->err_pos < -1)
1289 goto invalid_char;
1290 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001291 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001292 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1293 }
1294
1295 if (likely(HTTP_IS_CRLF(*ptr))) {
1296 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1297 goto http_msg_req09_uri_e;
1298 }
1299
1300 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001301 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001302 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001303 state = HTTP_MSG_ERROR;
1304 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001305
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001307 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001309 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 goto http_msg_rqver;
1311 }
1312 if (likely(HTTP_IS_SPHT(*ptr)))
1313 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1314 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1315 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001316
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001317 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001318 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001319 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001320 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001321
1322 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001323 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001324 http_msg_rqline_eol:
1325 /* We have seen the end of line. Note that we do not
1326 * necessarily have the \n yet, but at least we know that we
1327 * have EITHER \r OR \n, otherwise the request would not be
1328 * complete. We can then record the request length and return
1329 * to the caller which will be able to register it.
1330 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001331 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001332 return ptr;
1333 }
1334
1335 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001336 state = HTTP_MSG_ERROR;
1337 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001338
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001339 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001340#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001341 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1342 exit(1);
1343#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001344 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001345 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001346
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001348 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001349 if (ret_state)
1350 *ret_state = state;
1351 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001352 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001353 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001354}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001355
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001356/*
1357 * Returns the data from Authorization header. Function may be called more
1358 * than once so data is stored in txn->auth_data. When no header is found
1359 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001360 * searching again for something we are unable to find anyway. However, if
1361 * the result if valid, the cache is not reused because we would risk to
1362 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001363 */
1364
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001365/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1366 * set according to global.tune.bufsize.
1367 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001368char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001369
1370int
1371get_http_auth(struct session *s)
1372{
1373
1374 struct http_txn *txn = &s->txn;
1375 struct chunk auth_method;
1376 struct hdr_ctx ctx;
1377 char *h, *p;
1378 int len;
1379
1380#ifdef DEBUG_AUTH
1381 printf("Auth for session %p: %d\n", s, txn->auth.method);
1382#endif
1383
1384 if (txn->auth.method == HTTP_AUTH_WRONG)
1385 return 0;
1386
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001387 txn->auth.method = HTTP_AUTH_WRONG;
1388
1389 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001390
1391 if (txn->flags & TX_USE_PX_CONN) {
1392 h = "Proxy-Authorization";
1393 len = strlen(h);
1394 } else {
1395 h = "Authorization";
1396 len = strlen(h);
1397 }
1398
Willy Tarreau9b28e032012-10-12 23:49:43 +02001399 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001400 return 0;
1401
1402 h = ctx.line + ctx.val;
1403
1404 p = memchr(h, ' ', ctx.vlen);
Willy Tarreau2b9b25b2016-03-13 08:17:02 +01001405 len = p - h;
1406 if (!p || len <= 0)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001407 return 0;
1408
Willy Tarreau2b9b25b2016-03-13 08:17:02 +01001409 chunk_initlen(&auth_method, h, 0, len);
1410 chunk_initlen(&txn->auth.method_data, p + 1, 0, ctx.vlen - len - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001411
1412 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1413
1414 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001415 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001416
1417 if (len < 0)
1418 return 0;
1419
1420
1421 get_http_auth_buff[len] = '\0';
1422
1423 p = strchr(get_http_auth_buff, ':');
1424
1425 if (!p)
1426 return 0;
1427
1428 txn->auth.user = get_http_auth_buff;
1429 *p = '\0';
1430 txn->auth.pass = p+1;
1431
1432 txn->auth.method = HTTP_AUTH_BASIC;
1433 return 1;
1434 }
1435
1436 return 0;
1437}
1438
Willy Tarreau58f10d72006-12-04 02:26:12 +01001439
Willy Tarreau8973c702007-01-21 23:58:29 +01001440/*
1441 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001442 * depending on the initial msg->msg_state. The caller is responsible for
1443 * ensuring that the message does not wrap. The function can be preempted
1444 * everywhere when data are missing and recalled at the exact same location
1445 * with no information loss. The message may even be realigned between two
1446 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001447 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001448 * fields. Note that msg->sol will be initialized after completing the first
1449 * state, so that none of the msg pointers has to be initialized prior to the
1450 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001451 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001452void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453{
Willy Tarreau3770f232013-12-07 00:01:53 +01001454 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001456 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001457
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001458 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001459 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001460 ptr = buf->p + msg->next;
1461 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001462
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 if (unlikely(ptr >= end))
1464 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001465
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001467 /*
1468 * First, states that are specific to the response only.
1469 * We check them first so that request and headers are
1470 * closer to each other (accessed more often).
1471 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001472 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001473 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001474 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001475 /* we have a start of message, but we have to check
1476 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001477 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001478 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001479 if (unlikely(ptr != buf->p)) {
1480 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001481 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001482 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001483 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001484 }
Willy Tarreau26927362012-05-18 23:22:52 +02001485 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001486 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001487 hdr_idx_init(idx);
1488 state = HTTP_MSG_RPVER;
1489 goto http_msg_rpver;
1490 }
1491
1492 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1493 goto http_msg_invalid;
1494
1495 if (unlikely(*ptr == '\n'))
1496 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1497 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1498 /* stop here */
1499
Willy Tarreau8973c702007-01-21 23:58:29 +01001500 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001501 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001502 EXPECT_LF_HERE(ptr, http_msg_invalid);
1503 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1504 /* stop here */
1505
Willy Tarreau8973c702007-01-21 23:58:29 +01001506 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001507 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001508 case HTTP_MSG_RPVER_SP:
1509 case HTTP_MSG_RPCODE:
1510 case HTTP_MSG_RPCODE_SP:
1511 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001512 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001513 state, ptr, end,
1514 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001515 if (unlikely(!ptr))
1516 return;
1517
1518 /* we have a full response and we know that we have either a CR
1519 * or an LF at <ptr>.
1520 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001521 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1522
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001523 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001524 if (likely(*ptr == '\r'))
1525 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1526 goto http_msg_rpline_end;
1527
Willy Tarreau8973c702007-01-21 23:58:29 +01001528 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001529 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001530 /* msg->sol must point to the first of CR or LF. */
1531 EXPECT_LF_HERE(ptr, http_msg_invalid);
1532 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1533 /* stop here */
1534
1535 /*
1536 * Second, states that are specific to the request only
1537 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001539 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001541 /* we have a start of message, but we have to check
1542 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001543 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001544 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001545 if (likely(ptr != buf->p)) {
1546 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001547 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001548 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001549 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001550 }
Willy Tarreau26927362012-05-18 23:22:52 +02001551 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001552 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001553 state = HTTP_MSG_RQMETH;
1554 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001555 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001556
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1558 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 if (unlikely(*ptr == '\n'))
1561 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1562 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001563 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001564
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001565 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001566 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 EXPECT_LF_HERE(ptr, http_msg_invalid);
1568 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001569 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001572 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001573 case HTTP_MSG_RQMETH_SP:
1574 case HTTP_MSG_RQURI:
1575 case HTTP_MSG_RQURI_SP:
1576 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001577 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001578 state, ptr, end,
1579 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 if (unlikely(!ptr))
1581 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001582
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 /* we have a full request and we know that we have either a CR
1584 * or an LF at <ptr>.
1585 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001587
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001588 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 if (likely(*ptr == '\r'))
1590 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001591 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001592
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001593 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001594 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001595 /* check for HTTP/0.9 request : no version information available.
1596 * msg->sol must point to the first of CR or LF.
1597 */
1598 if (unlikely(msg->sl.rq.v_l == 0))
1599 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001600
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001601 EXPECT_LF_HERE(ptr, http_msg_invalid);
1602 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001603 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001604
Willy Tarreau8973c702007-01-21 23:58:29 +01001605 /*
1606 * Common states below
1607 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001608 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001609 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001610 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001611 if (likely(!HTTP_IS_CRLF(*ptr))) {
1612 goto http_msg_hdr_name;
1613 }
1614
1615 if (likely(*ptr == '\r'))
1616 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1617 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001618
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001619 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001620 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001621 /* assumes msg->sol points to the first char */
1622 if (likely(HTTP_IS_TOKEN(*ptr)))
1623 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001624
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001625 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001627
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001628 if (likely(msg->err_pos < -1) || *ptr == '\n')
1629 goto http_msg_invalid;
1630
1631 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001632 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001633
1634 /* and we still accept this non-token character */
1635 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001636
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001638 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001639 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 if (likely(HTTP_IS_SPHT(*ptr)))
1641 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001642
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001644 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001645
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001646 if (likely(!HTTP_IS_CRLF(*ptr))) {
1647 goto http_msg_hdr_val;
1648 }
1649
1650 if (likely(*ptr == '\r'))
1651 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1652 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001653
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001654 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001655 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 EXPECT_LF_HERE(ptr, http_msg_invalid);
1657 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001658
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001659 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001660 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001661 if (likely(HTTP_IS_SPHT(*ptr))) {
1662 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001663 for (; buf->p + msg->sov < ptr; msg->sov++)
1664 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001665 goto http_msg_hdr_l1_sp;
1666 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001667 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001668 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001669 goto http_msg_complete_header;
1670
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001671 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001672 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001673 /* assumes msg->sol points to the first char, and msg->sov
1674 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001675 */
1676 if (likely(!HTTP_IS_CRLF(*ptr)))
1677 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001678
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001679 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001680 /* Note: we could also copy eol into ->eoh so that we have the
1681 * real header end in case it ends with lots of LWS, but is this
1682 * really needed ?
1683 */
1684 if (likely(*ptr == '\r'))
1685 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1686 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001687
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001688 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001689 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 EXPECT_LF_HERE(ptr, http_msg_invalid);
1691 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001692
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001693 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001694 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001695 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1696 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001697 for (; buf->p + msg->eol < ptr; msg->eol++)
1698 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001699 goto http_msg_hdr_val;
1700 }
1701 http_msg_complete_header:
1702 /*
1703 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001704 * Assumes msg->sol points to the first char, msg->sov points
1705 * to the first character of the value and msg->eol to the
1706 * first CR or LF so we know how the line ends. We insert last
1707 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001709 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001710 idx, idx->tail) < 0))
1711 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001712
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001713 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001714 if (likely(!HTTP_IS_CRLF(*ptr))) {
1715 goto http_msg_hdr_name;
1716 }
1717
1718 if (likely(*ptr == '\r'))
1719 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1720 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001721
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001722 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001723 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001724 /* Assumes msg->sol points to the first of either CR or LF.
1725 * Sets ->sov and ->next to the total header length, ->eoh to
1726 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1727 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001728 EXPECT_LF_HERE(ptr, http_msg_invalid);
1729 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001730 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001731 msg->eoh = msg->sol;
1732 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001733 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001734 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001735 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001736
1737 case HTTP_MSG_ERROR:
1738 /* this may only happen if we call http_msg_analyser() twice with an error */
1739 break;
1740
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001741 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001742#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001743 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1744 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001745#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001746 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001747 }
1748 http_msg_ood:
1749 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001750 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001751 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001752 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001753
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001754 http_msg_invalid:
1755 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001756 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001757 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001758 return;
1759}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001760
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001761/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1762 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1763 * nothing is done and 1 is returned.
1764 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001765static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001766{
1767 int delta;
1768 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001769 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001770
1771 if (msg->sl.rq.v_l != 0)
1772 return 1;
1773
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001774 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1775 if (txn->meth != HTTP_METH_GET)
1776 return 0;
1777
Willy Tarreau9b28e032012-10-12 23:49:43 +02001778 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001779 delta = 0;
1780
1781 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001782 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1783 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001784 }
1785 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001786 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001787 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001788 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001789 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001790 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001791 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001792 NULL, NULL);
1793 if (unlikely(!cur_end))
1794 return 0;
1795
1796 /* we have a full HTTP/1.0 request now and we know that
1797 * we have either a CR or an LF at <ptr>.
1798 */
1799 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1800 return 1;
1801}
1802
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001803/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001804 * and "keep-alive" values. If we already know that some headers may safely
1805 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001806 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1807 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001808 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001809 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1810 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1811 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001812 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001813 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001814void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001815{
Willy Tarreau5b154472009-12-21 20:11:07 +01001816 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001817 const char *hdr_val = "Connection";
1818 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001819
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001820 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001821 return;
1822
Willy Tarreau88d349d2010-01-25 12:15:43 +01001823 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1824 hdr_val = "Proxy-Connection";
1825 hdr_len = 16;
1826 }
1827
Willy Tarreau5b154472009-12-21 20:11:07 +01001828 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001829 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001830 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001831 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1832 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001833 if (to_del & 2)
1834 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001835 else
1836 txn->flags |= TX_CON_KAL_SET;
1837 }
1838 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1839 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001840 if (to_del & 1)
1841 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001842 else
1843 txn->flags |= TX_CON_CLO_SET;
1844 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001845 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1846 txn->flags |= TX_HDR_CONN_UPG;
1847 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001848 }
1849
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001850 txn->flags |= TX_HDR_CONN_PRS;
1851 return;
1852}
Willy Tarreau5b154472009-12-21 20:11:07 +01001853
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001854/* Apply desired changes on the Connection: header. Values may be removed and/or
1855 * added depending on the <wanted> flags, which are exclusively composed of
1856 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1857 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1858 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001859void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001860{
1861 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001862 const char *hdr_val = "Connection";
1863 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001864
1865 ctx.idx = 0;
1866
Willy Tarreau88d349d2010-01-25 12:15:43 +01001867
1868 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1869 hdr_val = "Proxy-Connection";
1870 hdr_len = 16;
1871 }
1872
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001873 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001874 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001875 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1876 if (wanted & TX_CON_KAL_SET)
1877 txn->flags |= TX_CON_KAL_SET;
1878 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001879 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001880 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001881 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1882 if (wanted & TX_CON_CLO_SET)
1883 txn->flags |= TX_CON_CLO_SET;
1884 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001885 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001886 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001887 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001888
1889 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1890 return;
1891
1892 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1893 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001894 hdr_val = "Connection: close";
1895 hdr_len = 17;
1896 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1897 hdr_val = "Proxy-Connection: close";
1898 hdr_len = 23;
1899 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001900 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001901 }
1902
1903 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1904 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001905 hdr_val = "Connection: keep-alive";
1906 hdr_len = 22;
1907 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1908 hdr_val = "Proxy-Connection: keep-alive";
1909 hdr_len = 28;
1910 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001911 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001912 }
1913 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001914}
1915
Willy Tarreauc24715e2014-04-17 15:21:20 +02001916/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1917 * the first byte of data after the chunk size, so that we know we can forward
1918 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1919 * the chunk size. That way it is always possible to differentiate between the
1920 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001921 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001922 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001923 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001924static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001925{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001926 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001927 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001928 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001929 const char *end = buf->data + buf->size;
1930 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001931 unsigned int chunk = 0;
1932
1933 /* The chunk size is in the following form, though we are only
1934 * interested in the size and CRLF :
1935 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1936 */
1937 while (1) {
1938 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001939 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001940 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001941 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001942 if (c < 0) /* not a hex digit anymore */
1943 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001944 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001945 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001946 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001947 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001948 chunk = (chunk << 4) + c;
1949 }
1950
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001952 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001953 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001954
1955 while (http_is_spht[(unsigned char)*ptr]) {
1956 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001957 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001958 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001959 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001960 }
1961
Willy Tarreaud98cf932009-12-27 22:54:55 +01001962 /* Up to there, we know that at least one byte is present at *ptr. Check
1963 * for the end of chunk size.
1964 */
1965 while (1) {
1966 if (likely(HTTP_IS_CRLF(*ptr))) {
1967 /* we now have a CR or an LF at ptr */
1968 if (likely(*ptr == '\r')) {
1969 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001970 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001971 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001972 return 0;
1973 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001974
Willy Tarreaud98cf932009-12-27 22:54:55 +01001975 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001976 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001977 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001978 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001979 /* done */
1980 break;
1981 }
1982 else if (*ptr == ';') {
1983 /* chunk extension, ends at next CRLF */
1984 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001985 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001986 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001987 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001988
1989 while (!HTTP_IS_CRLF(*ptr)) {
1990 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001991 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001992 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001993 return 0;
1994 }
1995 /* we have a CRLF now, loop above */
1996 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001997 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001998 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001999 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002000 }
2001
Willy Tarreaud98cf932009-12-27 22:54:55 +01002002 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002003 * which may or may not be present. We save that into ->next,
2004 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002005 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002006 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002007 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002008 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002009 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002010 msg->chunk_len = chunk;
2011 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002012 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002013 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002014 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002015 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002016 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002017}
2018
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002019/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002020 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002021 * the trailers is found, it is automatically scheduled to be forwarded,
2022 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2023 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002024 * except maybe msg->next if it could parse some lines, and returns zero.
2025 * If a parse error is encountered, the function returns < 0 and does not
2026 * change anything except maybe msg->next. Note that the message must
2027 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002028 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002029 * forwarding, and that msg->next exactly matches the length of trailers
2030 * already parsed and not forwarded. It is also important to note that this
2031 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002032 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002033static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002034{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002035 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002036
Willy Tarreaua458b672012-03-05 11:17:50 +01002037 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002038 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002039 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002040 const char *ptr = b_ptr(buf, msg->next);
2041 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002042 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002043
2044 /* scan current line and stop at LF or CRLF */
2045 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002046 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002047 return 0;
2048
2049 if (*ptr == '\n') {
2050 if (!p1)
2051 p1 = ptr;
2052 p2 = ptr;
2053 break;
2054 }
2055
2056 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002057 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002058 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002059 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002060 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002061 p1 = ptr;
2062 }
2063
2064 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002065 if (ptr >= buf->data + buf->size)
2066 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002067 }
2068
2069 /* after LF; point to beginning of next line */
2070 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002071 if (p2 >= buf->data + buf->size)
2072 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002073
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002074 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002075 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002076 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002077
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002078 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002079 /* LF/CRLF at beginning of line => end of trailers at p2.
2080 * Everything was scheduled for forwarding, there's nothing
2081 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002082 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002083 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002084 msg->msg_state = HTTP_MSG_DONE;
2085 return 1;
2086 }
2087 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002088 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002089 }
2090}
2091
Willy Tarreauc24715e2014-04-17 15:21:20 +02002092/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2093 * or a possible LF alone at the end of a chunk. It automatically adjusts
2094 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002095 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002096 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2097 * not enough data are available, the function does not change anything and
2098 * returns zero. If a parse error is encountered, the function returns < 0 and
2099 * does not change anything. Note: this function is designed to parse wrapped
2100 * CRLF at the end of the buffer.
2101 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002102static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002103{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002104 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002105 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002106 int bytes;
2107
2108 /* NB: we'll check data availabilty at the end. It's not a
2109 * problem because whatever we match first will be checked
2110 * against the correct length.
2111 */
2112 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002113 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002114 if (*ptr == '\r') {
2115 bytes++;
2116 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002117 if (ptr >= buf->data + buf->size)
2118 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002119 }
2120
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002121 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002122 return 0;
2123
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002124 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002125 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002126 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002127 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002128
2129 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002130 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002131 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002132 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002133 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002134 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2135 return 1;
2136}
Willy Tarreau5b154472009-12-21 20:11:07 +01002137
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002138/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2139 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2140 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2141 * Unparsable qvalues return 1000 as "q=1.000".
2142 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002143int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002144{
2145 int q = 1000;
2146
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002147 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002148 goto out;
2149 q = (*qvalue++ - '0') * 1000;
2150
2151 if (*qvalue++ != '.')
2152 goto out;
2153
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002154 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002155 goto out;
2156 q += (*qvalue++ - '0') * 100;
2157
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002158 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002159 goto out;
2160 q += (*qvalue++ - '0') * 10;
2161
Willy Tarreau463ae6f2014-07-08 00:59:48 +02002162 if (!isdigit((unsigned char)*qvalue))
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002163 goto out;
2164 q += (*qvalue++ - '0') * 1;
2165 out:
2166 if (q > 1000)
2167 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002168 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002169 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002170 return q;
2171}
William Lallemand82fe75c2012-10-23 10:25:10 +02002172
2173/*
2174 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002175 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002176int select_compression_request_header(struct session *s, struct buffer *req)
2177{
2178 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002179 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002180 struct hdr_ctx ctx;
2181 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002182 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002183
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002184 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2185 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002186 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2187 */
2188 ctx.idx = 0;
2189 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2190 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002191 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2192 (ctx.vlen < 31 ||
2193 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2194 ctx.line[ctx.val + 30] < '6' ||
2195 (ctx.line[ctx.val + 30] == '6' &&
2196 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2197 s->comp_algo = NULL;
2198 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002199 }
2200
William Lallemand82fe75c2012-10-23 10:25:10 +02002201 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002202 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002203 int best_q = 0;
2204
William Lallemand82fe75c2012-10-23 10:25:10 +02002205 ctx.idx = 0;
2206 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002207 const char *qval;
2208 int q;
2209 int toklen;
2210
2211 /* try to isolate the token from the optional q-value */
2212 toklen = 0;
2213 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2214 toklen++;
2215
2216 qval = ctx.line + ctx.val + toklen;
2217 while (1) {
2218 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2219 qval++;
2220
2221 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2222 qval = NULL;
2223 break;
2224 }
2225 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002226
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002227 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2228 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002229
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002230 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2231 qval = NULL;
2232 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002233 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002234 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2235 break;
2236
2237 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2238 qval++;
2239 }
2240
2241 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002242 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002243
2244 if (q <= best_q)
2245 continue;
2246
2247 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2248 if (*(ctx.line + ctx.val) == '*' ||
2249 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2250 s->comp_algo = comp_algo;
2251 best_q = q;
2252 break;
2253 }
2254 }
2255 }
2256 }
2257
2258 /* remove all occurrences of the header when "compression offload" is set */
2259 if (s->comp_algo) {
2260 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2261 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2262 ctx.idx = 0;
2263 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2264 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002265 }
2266 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002267 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002268 }
2269
2270 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002271 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2272 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002273 if (comp_algo->add_data == identity_add_data) {
2274 s->comp_algo = comp_algo;
2275 return 1;
2276 }
2277 }
2278 }
2279
2280 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002281 return 0;
2282}
2283
2284/*
2285 * Selects a comression algorithm depending of the server response.
2286 */
2287int select_compression_response_header(struct session *s, struct buffer *res)
2288{
2289 struct http_txn *txn = &s->txn;
2290 struct http_msg *msg = &txn->rsp;
2291 struct hdr_ctx ctx;
2292 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002293
2294 /* no common compression algorithm was found in request header */
2295 if (s->comp_algo == NULL)
2296 goto fail;
2297
2298 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002299 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002300 goto fail;
2301
William Lallemandd3002612012-11-26 14:34:47 +01002302 /* 200 only */
2303 if (txn->status != 200)
2304 goto fail;
2305
William Lallemand82fe75c2012-10-23 10:25:10 +02002306 /* Content-Length is null */
2307 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2308 goto fail;
2309
2310 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002311 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002312 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2313 goto fail;
2314
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002315 /* no compression when Cache-Control: no-transform is present in the message */
2316 ctx.idx = 0;
2317 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2318 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2319 goto fail;
2320 }
2321
William Lallemand82fe75c2012-10-23 10:25:10 +02002322 comp_type = NULL;
2323
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002324 /* we don't want to compress multipart content-types, nor content-types that are
2325 * not listed in the "compression type" directive if any. If no content-type was
2326 * found but configuration requires one, we don't compress either. Backend has
2327 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002328 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002329 ctx.idx = 0;
2330 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2331 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2332 goto fail;
2333
2334 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2335 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002336 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002337 if (ctx.vlen >= comp_type->name_len &&
2338 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002339 /* this Content-Type should be compressed */
2340 break;
2341 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002342 /* this Content-Type should not be compressed */
2343 if (comp_type == NULL)
2344 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002345 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002346 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002347 else { /* no content-type header */
2348 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2349 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002350 }
2351
William Lallemandd85f9172012-11-09 17:05:39 +01002352 /* limit compression rate */
2353 if (global.comp_rate_lim > 0)
2354 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2355 goto fail;
2356
William Lallemand072a2bf2012-11-20 17:01:01 +01002357 /* limit cpu usage */
2358 if (idle_pct < compress_min_idle)
2359 goto fail;
2360
William Lallemand4c49fae2012-11-07 15:00:23 +01002361 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002362 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002363 goto fail;
2364
William Lallemandec3e3892012-11-12 17:02:18 +01002365 s->flags |= SN_COMP_READY;
2366
William Lallemand82fe75c2012-10-23 10:25:10 +02002367 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002368 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002369 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2370 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2371
2372 /* add Transfer-Encoding header */
2373 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2374 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2375
2376 /*
2377 * Add Content-Encoding header when it's not identity encoding.
2378 * RFC 2616 : Identity encoding: This content-coding is used only in the
2379 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2380 * header.
2381 */
2382 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002383 trash.len = 18;
2384 memcpy(trash.str, "Content-Encoding: ", trash.len);
2385 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2386 trash.len += s->comp_algo->name_len;
2387 trash.str[trash.len] = '\0';
2388 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002389 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002390 return 1;
2391
2392fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002393 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002394 return 0;
2395}
2396
Willy Tarreau2e47a3a2014-09-30 18:44:22 +02002397void http_adjust_conn_mode(struct session *s, struct http_txn *txn, struct http_msg *msg)
2398{
2399 int tmp = TX_CON_WANT_KAL;
2400
2401 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
2402 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
2403 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
2404 tmp = TX_CON_WANT_TUN;
2405
2406 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2407 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2408 tmp = TX_CON_WANT_TUN;
2409 }
2410
2411 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
2412 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
2413 /* option httpclose + server_close => forceclose */
2414 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
2415 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
2416 tmp = TX_CON_WANT_CLO;
2417 else
2418 tmp = TX_CON_WANT_SCL;
2419 }
2420
2421 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
2422 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
2423 tmp = TX_CON_WANT_CLO;
2424
2425 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2426 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
2427
2428 if (!(txn->flags & TX_HDR_CONN_PRS) &&
2429 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
2430 /* parse the Connection header and possibly clean it */
2431 int to_del = 0;
2432 if ((msg->flags & HTTP_MSGF_VER_11) ||
2433 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2434 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
2435 to_del |= 2; /* remove "keep-alive" */
2436 if (!(msg->flags & HTTP_MSGF_VER_11))
2437 to_del |= 1; /* remove "close" */
2438 http_parse_connection_header(txn, msg, to_del);
2439 }
2440
2441 /* check if client or config asks for explicit close in KAL/SCL */
2442 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2443 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2444 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2445 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
2446 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
2447 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
2448 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2449}
William Lallemand82fe75c2012-10-23 10:25:10 +02002450
Willy Tarreaud787e662009-07-07 10:14:51 +02002451/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2452 * processing can continue on next analysers, or zero if it either needs more
2453 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2454 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2455 * when it has nothing left to do, and may remove any analyser when it wants to
2456 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002457 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002458int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002459{
Willy Tarreau59234e92008-11-30 23:51:27 +01002460 /*
2461 * We will parse the partial (or complete) lines.
2462 * We will check the request syntax, and also join multi-line
2463 * headers. An index of all the lines will be elaborated while
2464 * parsing.
2465 *
2466 * For the parsing, we use a 28 states FSM.
2467 *
2468 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002469 * req->buf->p = beginning of request
2470 * req->buf->p + msg->eoh = end of processed headers / start of current one
2471 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002472 * msg->eol = end of current header or line (LF or CRLF)
2473 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002474 *
2475 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002476 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002477 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2478 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002479 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002480
Willy Tarreau59234e92008-11-30 23:51:27 +01002481 int cur_idx;
2482 struct http_txn *txn = &s->txn;
2483 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002484 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002485
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002486 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002487 now_ms, __FUNCTION__,
2488 s,
2489 req,
2490 req->rex, req->wex,
2491 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002492 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002493 req->analysers);
2494
Willy Tarreau52a0c602009-08-16 22:45:38 +02002495 /* we're speaking HTTP here, so let's speak HTTP to the client */
2496 s->srv_error = http_return_srv_error;
2497
Willy Tarreau83e3af02009-12-28 17:39:57 +01002498 /* There's a protected area at the end of the buffer for rewriting
2499 * purposes. We don't want to start to parse the request if the
2500 * protected area is affected, because we may have to move processed
2501 * data later, which is much more complicated.
2502 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002503 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002504 if (txn->flags & TX_NOT_FIRST) {
2505 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002506 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002507 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002508 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002509 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002510 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002511 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002512 return 0;
2513 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002514 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2515 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2516 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002517 }
2518
Willy Tarreau065e8332010-01-08 00:30:20 +01002519 /* Note that we have the same problem with the response ; we
2520 * may want to send a redirect, error or anything which requires
2521 * some spare space. So we'll ensure that we have at least
2522 * maxrewrite bytes available in the response buffer before
2523 * processing that one. This will only affect pipelined
2524 * keep-alive requests.
2525 */
2526 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002527 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002528 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2529 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2530 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002531 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002532 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002533 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002534 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002535 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002536 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002537 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002538 return 0;
2539 }
2540 }
2541
Willy Tarreau9b28e032012-10-12 23:49:43 +02002542 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002543 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002544 }
2545
Willy Tarreau59234e92008-11-30 23:51:27 +01002546 /* 1: we might have to print this header in debug mode */
2547 if (unlikely((global.mode & MODE_DEBUG) &&
2548 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau8767b132014-10-21 19:36:09 +02002549 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002551
Willy Tarreau9b28e032012-10-12 23:49:43 +02002552 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002553 /* this is a bit complex : in case of error on the request line,
2554 * we know that rq.l is still zero, so we display only the part
2555 * up to the end of the line (truncated by debug_hdr).
2556 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002557 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002558 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002559
Willy Tarreau59234e92008-11-30 23:51:27 +01002560 sol += hdr_idx_first_pos(&txn->hdr_idx);
2561 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002562
Willy Tarreau59234e92008-11-30 23:51:27 +01002563 while (cur_idx) {
2564 eol = sol + txn->hdr_idx.v[cur_idx].len;
2565 debug_hdr("clihdr", s, sol, eol);
2566 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2567 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002568 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002569 }
2570
Willy Tarreau58f10d72006-12-04 02:26:12 +01002571
Willy Tarreau59234e92008-11-30 23:51:27 +01002572 /*
2573 * Now we quickly check if we have found a full valid request.
2574 * If not so, we check the FD and buffer states before leaving.
2575 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002576 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002577 * requests are checked first. When waiting for a second request
2578 * on a keep-alive session, if we encounter and error, close, t/o,
2579 * we note the error in the session flags but don't set any state.
2580 * Since the error will be noted there, it will not be counted by
2581 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002582 * Last, we may increase some tracked counters' http request errors on
2583 * the cases that are deliberately the client's fault. For instance,
2584 * a timeout or connection reset is not counted as an error. However
2585 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002587
Willy Tarreau655dce92009-11-08 13:10:58 +01002588 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002589 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002590 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002591 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002592 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002593 session_inc_http_req_ctr(s);
2594 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002595 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002596 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002597 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002598
Willy Tarreau59234e92008-11-30 23:51:27 +01002599 /* 1: Since we are in header mode, if there's no space
2600 * left for headers, we won't be able to free more
2601 * later, so the session will never terminate. We
2602 * must terminate it now.
2603 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002604 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002605 /* FIXME: check if URI is set and return Status
2606 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002607 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002608 session_inc_http_req_ctr(s);
2609 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002610 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002611 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002612 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002613 goto return_bad_req;
2614 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002615
Willy Tarreau59234e92008-11-30 23:51:27 +01002616 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002617 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002618 if (!(s->flags & SN_ERR_MASK))
2619 s->flags |= SN_ERR_CLICL;
2620
Willy Tarreaufcffa692010-01-10 14:21:19 +01002621 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002622 goto failed_keep_alive;
2623
Willy Tarreau1c3a6122015-05-01 15:37:53 +02002624 if (s->fe->options & PR_O_IGNORE_PRB)
2625 goto failed_keep_alive;
2626
Willy Tarreau59234e92008-11-30 23:51:27 +01002627 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002628 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002629 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002630 session_inc_http_err_ctr(s);
2631 }
2632
Willy Tarreaudc979f22012-12-04 10:39:01 +01002633 txn->status = 400;
2634 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002635 msg->msg_state = HTTP_MSG_ERROR;
2636 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002637
Willy Tarreauda7ff642010-06-23 11:44:09 +02002638 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002639 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002640 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002641 if (s->listener->counters)
2642 s->listener->counters->failed_req++;
2643
Willy Tarreau59234e92008-11-30 23:51:27 +01002644 if (!(s->flags & SN_FINST_MASK))
2645 s->flags |= SN_FINST_R;
2646 return 0;
2647 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002648
Willy Tarreau59234e92008-11-30 23:51:27 +01002649 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002650 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002651 if (!(s->flags & SN_ERR_MASK))
2652 s->flags |= SN_ERR_CLITO;
2653
Willy Tarreaufcffa692010-01-10 14:21:19 +01002654 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002655 goto failed_keep_alive;
2656
Willy Tarreau1c3a6122015-05-01 15:37:53 +02002657 if (s->fe->options & PR_O_IGNORE_PRB)
2658 goto failed_keep_alive;
2659
Willy Tarreau59234e92008-11-30 23:51:27 +01002660 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002661 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002662 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002663 session_inc_http_err_ctr(s);
2664 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002665 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002666 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002667 msg->msg_state = HTTP_MSG_ERROR;
2668 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002669
Willy Tarreauda7ff642010-06-23 11:44:09 +02002670 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002671 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002672 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002673 if (s->listener->counters)
2674 s->listener->counters->failed_req++;
2675
Willy Tarreau59234e92008-11-30 23:51:27 +01002676 if (!(s->flags & SN_FINST_MASK))
2677 s->flags |= SN_FINST_R;
2678 return 0;
2679 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002680
Willy Tarreau59234e92008-11-30 23:51:27 +01002681 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002682 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002683 if (!(s->flags & SN_ERR_MASK))
2684 s->flags |= SN_ERR_CLICL;
2685
Willy Tarreaufcffa692010-01-10 14:21:19 +01002686 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002687 goto failed_keep_alive;
2688
Willy Tarreau1c3a6122015-05-01 15:37:53 +02002689 if (s->fe->options & PR_O_IGNORE_PRB)
2690 goto failed_keep_alive;
2691
Willy Tarreau4076a152009-04-02 15:18:36 +02002692 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002693 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002694 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002695 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002696 msg->msg_state = HTTP_MSG_ERROR;
2697 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002698
Willy Tarreauda7ff642010-06-23 11:44:09 +02002699 session_inc_http_err_ctr(s);
2700 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002701 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002702 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002703 if (s->listener->counters)
2704 s->listener->counters->failed_req++;
2705
Willy Tarreau59234e92008-11-30 23:51:27 +01002706 if (!(s->flags & SN_FINST_MASK))
2707 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002708 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002709 }
2710
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002711 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002712 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2713 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002714#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002715 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i && objt_conn(s->req->prod->end) && conn_ctrl_ready(__objt_conn(s->req->prod->end))) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002716 /* We need more data, we have to re-enable quick-ack in case we
2717 * previously disabled it, otherwise we might cause the client
2718 * to delay next data.
2719 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002720 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002721 }
2722#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002723
Willy Tarreaufcffa692010-01-10 14:21:19 +01002724 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2725 /* If the client starts to talk, let's fall back to
2726 * request timeout processing.
2727 */
2728 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002729 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002730 }
2731
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002733 if (!tick_isset(req->analyse_exp)) {
2734 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2735 (txn->flags & TX_WAIT_NEXT_RQ) &&
2736 tick_isset(s->be->timeout.httpka))
2737 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2738 else
2739 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2740 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002741
Willy Tarreau59234e92008-11-30 23:51:27 +01002742 /* we're not ready yet */
2743 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002744
2745 failed_keep_alive:
2746 /* Here we process low-level errors for keep-alive requests. In
2747 * short, if the request is not the first one and it experiences
2748 * a timeout, read error or shutdown, we just silently close so
2749 * that the client can try again.
2750 */
2751 txn->status = 0;
2752 msg->msg_state = HTTP_MSG_RQBEFORE;
2753 req->analysers = 0;
2754 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002755 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002756 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002757 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002758 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002759 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002760
Willy Tarreaud787e662009-07-07 10:14:51 +02002761 /* OK now we have a complete HTTP request with indexed headers. Let's
2762 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002763 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002764 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002765 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002766 * byte after the last LF. msg->sov points to the first byte of data.
2767 * msg->eol cannot be trusted because it may have been left uninitialized
2768 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002769 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002770
Willy Tarreauda7ff642010-06-23 11:44:09 +02002771 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002772 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2773
Willy Tarreaub16a5742010-01-10 14:46:16 +01002774 if (txn->flags & TX_WAIT_NEXT_RQ) {
2775 /* kill the pending keep-alive timeout */
2776 txn->flags &= ~TX_WAIT_NEXT_RQ;
2777 req->analyse_exp = TICK_ETERNITY;
2778 }
2779
2780
Willy Tarreaud787e662009-07-07 10:14:51 +02002781 /* Maybe we found in invalid header name while we were configured not
2782 * to block on that, so we have to capture it now.
2783 */
2784 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002785 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002786
Willy Tarreau59234e92008-11-30 23:51:27 +01002787 /*
2788 * 1: identify the method
2789 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002790 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002791
2792 /* we can make use of server redirect on GET and HEAD */
2793 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2794 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002795
Willy Tarreau59234e92008-11-30 23:51:27 +01002796 /*
2797 * 2: check if the URI matches the monitor_uri.
2798 * We have to do this for every request which gets in, because
2799 * the monitor-uri is defined by the frontend.
2800 */
2801 if (unlikely((s->fe->monitor_uri_len != 0) &&
2802 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002803 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002804 s->fe->monitor_uri,
2805 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002806 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002807 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002808 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002809 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002810
Willy Tarreau59234e92008-11-30 23:51:27 +01002811 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002812 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002813
Willy Tarreau59234e92008-11-30 23:51:27 +01002814 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002815 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002816 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002817
Willy Tarreau59234e92008-11-30 23:51:27 +01002818 ret = acl_pass(ret);
2819 if (cond->pol == ACL_COND_UNLESS)
2820 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002821
Willy Tarreau59234e92008-11-30 23:51:27 +01002822 if (ret) {
2823 /* we fail this request, let's return 503 service unavail */
2824 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002825 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002826 if (!(s->flags & SN_ERR_MASK))
2827 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002828 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002829 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002830 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002831
Willy Tarreau59234e92008-11-30 23:51:27 +01002832 /* nothing to fail, let's reply normaly */
2833 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002834 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002835 if (!(s->flags & SN_ERR_MASK))
2836 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002837 goto return_prx_cond;
2838 }
2839
2840 /*
2841 * 3: Maybe we have to copy the original REQURI for the logs ?
2842 * Note: we cannot log anymore if the request has been
2843 * classified as invalid.
2844 */
2845 if (unlikely(s->logs.logwait & LW_REQ)) {
2846 /* we have a complete HTTP request that we must log */
2847 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2848 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002849
Willy Tarreau59234e92008-11-30 23:51:27 +01002850 if (urilen >= REQURI_LEN)
2851 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002852 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002853 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002854
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002855 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002856 s->do_log(s);
2857 } else {
2858 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002859 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002860 }
Willy Tarreau06619262006-12-17 08:37:22 +01002861
Willy Tarreau59234e92008-11-30 23:51:27 +01002862 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002863 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002864 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002865
Willy Tarreau55645552015-05-01 13:26:00 +02002866 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
2867 * exactly one digit "." one digit. This check may be disabled using
2868 * option accept-invalid-http-request.
2869 */
2870 if (!(s->fe->options2 & PR_O2_REQBUG_OK)) {
2871 if (msg->sl.rq.v_l != 8) {
2872 msg->err_pos = msg->sl.rq.v;
2873 goto return_bad_req;
2874 }
2875
2876 if (req->buf->p[msg->sl.rq.v + 4] != '/' ||
2877 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 5]) ||
2878 req->buf->p[msg->sl.rq.v + 6] != '.' ||
2879 !isdigit((unsigned char)req->buf->p[msg->sl.rq.v + 7])) {
2880 msg->err_pos = msg->sl.rq.v + 4;
2881 goto return_bad_req;
2882 }
2883 }
2884
Willy Tarreau5b154472009-12-21 20:11:07 +01002885 /* ... and check if the request is HTTP/1.1 or above */
2886 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002887 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2888 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2889 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002890 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002891
2892 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002893 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002894
Willy Tarreau88d349d2010-01-25 12:15:43 +01002895 /* if the frontend has "option http-use-proxy-header", we'll check if
2896 * we have what looks like a proxied connection instead of a connection,
2897 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2898 * Note that this is *not* RFC-compliant, however browsers and proxies
2899 * happen to do that despite being non-standard :-(
2900 * We consider that a request not beginning with either '/' or '*' is
2901 * a proxied connection, which covers both "scheme://location" and
2902 * CONNECT ip:port.
2903 */
2904 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002905 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002906 txn->flags |= TX_USE_PX_CONN;
2907
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002908 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002909 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002910
Willy Tarreau59234e92008-11-30 23:51:27 +01002911 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002912 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002913 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002914 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002915
Willy Tarreau4db603d2015-05-01 10:05:17 +02002916 /* 6: determine the transfer-length according to RFC2616 #4.4, updated
2917 * by RFC7230#3.3.3 :
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002918 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002919 * The length of a message body is determined by one of the following
2920 * (in order of precedence):
Willy Tarreau32b47f42009-10-18 20:55:02 +02002921 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002922 * 1. Any response to a HEAD request and any response with a 1xx
2923 * (Informational), 204 (No Content), or 304 (Not Modified) status
2924 * code is always terminated by the first empty line after the
2925 * header fields, regardless of the header fields present in the
2926 * message, and thus cannot contain a message body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002927 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002928 * 2. Any 2xx (Successful) response to a CONNECT request implies that
2929 * the connection will become a tunnel immediately after the empty
2930 * line that concludes the header fields. A client MUST ignore any
2931 * Content-Length or Transfer-Encoding header fields received in
2932 * such a message.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002933 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02002934 * 3. If a Transfer-Encoding header field is present and the chunked
2935 * transfer coding (Section 4.1) is the final encoding, the message
2936 * body length is determined by reading and decoding the chunked
2937 * data until the transfer coding indicates the data is complete.
2938 *
2939 * If a Transfer-Encoding header field is present in a response and
2940 * the chunked transfer coding is not the final encoding, the
2941 * message body length is determined by reading the connection until
2942 * it is closed by the server. If a Transfer-Encoding header field
2943 * is present in a request and the chunked transfer coding is not
2944 * the final encoding, the message body length cannot be determined
2945 * reliably; the server MUST respond with the 400 (Bad Request)
2946 * status code and then close the connection.
2947 *
2948 * If a message is received with both a Transfer-Encoding and a
2949 * Content-Length header field, the Transfer-Encoding overrides the
2950 * Content-Length. Such a message might indicate an attempt to
2951 * perform request smuggling (Section 9.5) or response splitting
2952 * (Section 9.4) and ought to be handled as an error. A sender MUST
2953 * remove the received Content-Length field prior to forwarding such
2954 * a message downstream.
2955 *
2956 * 4. If a message is received without Transfer-Encoding and with
2957 * either multiple Content-Length header fields having differing
2958 * field-values or a single Content-Length header field having an
2959 * invalid value, then the message framing is invalid and the
2960 * recipient MUST treat it as an unrecoverable error. If this is a
2961 * request message, the server MUST respond with a 400 (Bad Request)
2962 * status code and then close the connection. If this is a response
2963 * message received by a proxy, the proxy MUST close the connection
2964 * to the server, discard the received response, and send a 502 (Bad
2965 * Gateway) response to the client. If this is a response message
2966 * received by a user agent, the user agent MUST close the
2967 * connection to the server and discard the received response.
2968 *
2969 * 5. If a valid Content-Length header field is present without
2970 * Transfer-Encoding, its decimal value defines the expected message
2971 * body length in octets. If the sender closes the connection or
2972 * the recipient times out before the indicated number of octets are
2973 * received, the recipient MUST consider the message to be
2974 * incomplete and close the connection.
2975 *
2976 * 6. If this is a request message and none of the above are true, then
2977 * the message body length is zero (no message body is present).
2978 *
2979 * 7. Otherwise, this is a response message without a declared message
2980 * body length, so the message body length is determined by the
2981 * number of octets received prior to the server closing the
2982 * connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002983 */
2984
Willy Tarreau32b47f42009-10-18 20:55:02 +02002985 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002986 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaue77bc172015-05-01 10:06:30 +02002987 while (http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002988 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002989 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2990 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau49efa262015-05-01 10:09:49 +02002991 /* chunked not last, return badreq */
2992 goto return_bad_req;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002993 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002994 }
2995
Willy Tarreaudfa3d922015-04-30 10:57:51 +02002996 /* Chunked requests must have their content-length removed */
Willy Tarreau32b47f42009-10-18 20:55:02 +02002997 ctx.idx = 0;
Willy Tarreaudfa3d922015-04-30 10:57:51 +02002998 if (msg->flags & HTTP_MSGF_TE_CHNK) {
2999 while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx))
3000 http_remove_header2(msg, &txn->hdr_idx, &ctx);
3001 }
Willy Tarreau49efa262015-05-01 10:09:49 +02003002 else while (http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02003003 signed long long cl;
3004
Willy Tarreauad14f752011-09-02 20:33:27 +02003005 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003006 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003007 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003008 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003009
Willy Tarreauad14f752011-09-02 20:33:27 +02003010 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003011 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003012 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02003013 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003014
Willy Tarreauad14f752011-09-02 20:33:27 +02003015 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003016 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003017 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02003018 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003019
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003020 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003021 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003022 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02003023 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02003024
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003025 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01003026 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02003027 }
3028
Willy Tarreau49efa262015-05-01 10:09:49 +02003029 /* even bodyless requests have a known length */
3030 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01003031
Willy Tarreau179085c2014-04-28 16:48:56 +02003032 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
3033 * only change if both the request and the config reference something else.
3034 * Option httpclose by itself sets tunnel mode where headers are mangled.
3035 * However, if another mode is set, it will affect it (eg: server-close/
3036 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3037 * if FE and BE have the same settings (common). The method consists in
3038 * checking if options changed between the two calls (implying that either
3039 * one is non-null, or one of them is non-null and we are there for the first
3040 * time.
3041 */
3042 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau2e47a3a2014-09-30 18:44:22 +02003043 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE)))
3044 http_adjust_conn_mode(s, txn, msg);
Willy Tarreau179085c2014-04-28 16:48:56 +02003045
Willy Tarreaud787e662009-07-07 10:14:51 +02003046 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02003047 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003048 req->analyse_exp = TICK_ETERNITY;
3049 return 1;
3050
3051 return_bad_req:
3052 /* We centralize bad requests processing here */
3053 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3054 /* we detected a parsing error. We want to archive this request
3055 * in the dedicated proxy area for later troubleshooting.
3056 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003057 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02003058 }
3059
3060 txn->req.msg_state = HTTP_MSG_ERROR;
3061 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003062 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003063
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003064 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003065 if (s->listener->counters)
3066 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02003067
3068 return_prx_cond:
3069 if (!(s->flags & SN_ERR_MASK))
3070 s->flags |= SN_ERR_PRXCOND;
3071 if (!(s->flags & SN_FINST_MASK))
3072 s->flags |= SN_FINST_R;
3073
3074 req->analysers = 0;
3075 req->analyse_exp = TICK_ETERNITY;
3076 return 0;
3077}
3078
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02003079
Willy Tarreau347a35d2013-11-22 17:51:09 +01003080/* This function prepares an applet to handle the stats. It can deal with the
3081 * "100-continue" expectation, check that admin rules are met for POST requests,
3082 * and program a response message if something was unexpected. It cannot fail
3083 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003084 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003085 * s->target which is supposed to already point to the stats applet. The caller
3086 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003087 */
3088int http_handle_stats(struct session *s, struct channel *req)
3089{
3090 struct stats_admin_rule *stats_admin_rule;
3091 struct stream_interface *si = s->rep->prod;
3092 struct http_txn *txn = &s->txn;
3093 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003094 struct uri_auth *uri_auth = s->be->uri_auth;
3095 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003096 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003097
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003098 appctx = si_appctx(si);
3099 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
3100 appctx->st1 = appctx->st2 = 0;
3101 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
3102 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02003103 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
3104 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003105
3106 uri = msg->chn->buf->p + msg->sl.rq.u;
3107 lookup = uri + uri_auth->uri_len;
3108
3109 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
3110 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003111 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003112 break;
3113 }
3114 }
3115
3116 if (uri_auth->refresh) {
3117 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
3118 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003119 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003120 break;
3121 }
3122 }
3123 }
3124
3125 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
3126 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003127 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003128 break;
3129 }
3130 }
3131
3132 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3133 if (memcmp(h, ";st=", 4) == 0) {
3134 int i;
3135 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003136 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003137 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3138 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003139 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003140 break;
3141 }
3142 }
3143 break;
3144 }
3145 }
3146
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003147 appctx->ctx.stats.scope_str = 0;
3148 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003149 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3150 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3151 int itx = 0;
3152 const char *h2;
3153 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3154 const char *err;
3155
3156 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3157 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003158 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003159 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3160 itx++;
3161 h++;
3162 }
3163
3164 if (itx > STAT_SCOPE_TXT_MAXLEN)
3165 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003166 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003167
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003168 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003169 memcpy(scope_txt, h2, itx);
3170 scope_txt[itx] = '\0';
3171 err = invalid_char(scope_txt);
3172 if (err) {
3173 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003174 appctx->ctx.stats.scope_str = 0;
3175 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003176 }
3177 break;
3178 }
3179 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003180
3181 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003182 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003183 int ret = 1;
3184
3185 if (stats_admin_rule->cond) {
3186 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3187 ret = acl_pass(ret);
3188 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3189 ret = !ret;
3190 }
3191
3192 if (ret) {
3193 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003194 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003195 break;
3196 }
3197 }
3198
3199 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003200 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003201 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003202 /* we'll need the request body, possibly after sending 100-continue */
3203 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003204 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003205 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003206 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003207 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3208 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003209 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003210 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003211 else {
3212 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003213 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003214 }
3215
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003216 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003217 return 1;
3218}
3219
Lukas Tribus67db8df2013-06-23 17:37:13 +02003220/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3221 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3222 */
Vincent Bernata72d3902016-05-19 11:15:51 +02003223static inline void inet_set_tos(int fd, struct sockaddr_storage *from, int tos)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003224{
3225#ifdef IP_TOS
Vincent Bernata72d3902016-05-19 11:15:51 +02003226 if (from->ss_family == AF_INET)
Lukas Tribus67db8df2013-06-23 17:37:13 +02003227 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3228#endif
3229#ifdef IPV6_TCLASS
Vincent Bernata72d3902016-05-19 11:15:51 +02003230 if (from->ss_family == AF_INET6) {
3231 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)from)->sin6_addr))
Lukas Tribus67db8df2013-06-23 17:37:13 +02003232 /* v4-mapped addresses need IP_TOS */
3233 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3234 else
3235 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3236 }
3237#endif
3238}
3239
Sasha Pachev218f0642014-06-16 12:05:59 -06003240static int http_transform_header(struct session* s, struct http_msg *msg, const char* name, uint name_len,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003241 char* buf, struct hdr_idx* idx, struct list *fmt, struct my_regex *re,
Sasha Pachev218f0642014-06-16 12:05:59 -06003242 struct hdr_ctx* ctx, int action)
3243{
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003244 int (*http_find_hdr_func)(const char *name, int len, char *sol,
3245 struct hdr_idx *idx, struct hdr_ctx *ctx);
3246 struct chunk *replace = get_trash_chunk();
3247 struct chunk *output = get_trash_chunk();
3248
3249 replace->len = build_logline(s, replace->str, replace->size, fmt);
3250 if (replace->len >= replace->size - 1)
3251 return -1;
3252
Sasha Pachev218f0642014-06-16 12:05:59 -06003253 ctx->idx = 0;
3254
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003255 /* Choose the header browsing function. */
3256 switch (action) {
3257 case HTTP_REQ_ACT_REPLACE_VAL:
3258 case HTTP_RES_ACT_REPLACE_VAL:
3259 http_find_hdr_func = http_find_header2;
3260 break;
3261 case HTTP_REQ_ACT_REPLACE_HDR:
3262 case HTTP_RES_ACT_REPLACE_HDR:
3263 http_find_hdr_func = http_find_full_header2;
3264 break;
3265 default: /* impossible */
3266 return -1;
3267 }
3268
3269 while (http_find_hdr_func(name, name_len, buf, idx, ctx)) {
Sasha Pachev218f0642014-06-16 12:05:59 -06003270 struct hdr_idx_elem *hdr = idx->v + ctx->idx;
3271 int delta;
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003272 char *val = ctx->line + ctx->val;
3273 char* val_end = val + ctx->vlen;
Sasha Pachev218f0642014-06-16 12:05:59 -06003274
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003275 if (!regex_exec_match2(re, val, val_end-val, MAX_MATCH, pmatch))
3276 continue;
Sasha Pachev218f0642014-06-16 12:05:59 -06003277
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003278 output->len = exp_replace(output->str, output->size, val, replace->str, pmatch);
3279 if (output->len == -1)
Sasha Pachev218f0642014-06-16 12:05:59 -06003280 return -1;
Sasha Pachev218f0642014-06-16 12:05:59 -06003281
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003282 delta = buffer_replace2(msg->chn->buf, val, val_end, output->str, output->len);
Sasha Pachev218f0642014-06-16 12:05:59 -06003283
3284 hdr->len += delta;
3285 http_msg_move_end(msg, delta);
Thierry FOURNIER06170c52015-03-16 23:23:53 +01003286
3287 /* Adjust the length of the current value of the index. */
3288 ctx->vlen += delta;
Sasha Pachev218f0642014-06-16 12:05:59 -06003289 }
3290
3291 return 0;
3292}
3293
Willy Tarreau20b0de52012-12-24 15:45:22 +01003294/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau0b748332014-04-29 00:13:29 +02003295 * transaction <txn>. Returns the verdict of the first rule that prevents
3296 * further processing of the request (auth, deny, ...), and defaults to
3297 * HTTP_RULE_RES_STOP if it executed all rules or stopped on an allow, or
3298 * HTTP_RULE_RES_CONT if the last rule was reached. It may set the TX_CLTARPIT
3299 * on txn->flags if it encounters a tarpit rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003300 */
Willy Tarreau0b748332014-04-29 00:13:29 +02003301enum rule_result
Willy Tarreau96257ec2012-12-27 10:46:37 +01003302http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003303{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003304 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003305 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003306 struct hdr_ctx ctx;
Willy Tarreauae3c0102014-04-28 23:22:08 +02003307 const char *auth_realm;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003308
Willy Tarreauff011f22011-01-06 17:51:27 +01003309 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003310 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003311 continue;
3312
Willy Tarreau96257ec2012-12-27 10:46:37 +01003313 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003314 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003315 int ret;
3316
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003317 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003318 ret = acl_pass(ret);
3319
Willy Tarreauff011f22011-01-06 17:51:27 +01003320 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003321 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003322
3323 if (!ret) /* condition not matched */
3324 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003325 }
3326
Willy Tarreau20b0de52012-12-24 15:45:22 +01003327
Willy Tarreau96257ec2012-12-27 10:46:37 +01003328 switch (rule->action) {
3329 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau0b748332014-04-29 00:13:29 +02003330 return HTTP_RULE_RES_STOP;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003331
3332 case HTTP_REQ_ACT_DENY:
Willy Tarreau0b748332014-04-29 00:13:29 +02003333 return HTTP_RULE_RES_DENY;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003334
Willy Tarreauccbcc372012-12-27 12:37:57 +01003335 case HTTP_REQ_ACT_TARPIT:
3336 txn->flags |= TX_CLTARPIT;
Willy Tarreau0b748332014-04-29 00:13:29 +02003337 return HTTP_RULE_RES_DENY;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003338
Willy Tarreau96257ec2012-12-27 10:46:37 +01003339 case HTTP_REQ_ACT_AUTH:
Willy Tarreauae3c0102014-04-28 23:22:08 +02003340 /* Auth might be performed on regular http-req rules as well as on stats */
3341 auth_realm = rule->arg.auth.realm;
3342 if (!auth_realm) {
3343 if (px->uri_auth && rules == &px->uri_auth->http_req_rules)
3344 auth_realm = STATS_DEFAULT_REALM;
3345 else
3346 auth_realm = px->id;
3347 }
3348 /* send 401/407 depending on whether we use a proxy or not. We still
3349 * count one error, because normal browsing won't significantly
3350 * increase the counter but brute force attempts will.
3351 */
3352 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, auth_realm);
3353 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
3354 stream_int_retnclose(&s->si[0], &trash);
3355 session_inc_http_err_ctr(s);
Willy Tarreau0b748332014-04-29 00:13:29 +02003356 return HTTP_RULE_RES_ABRT;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003357
Willy Tarreau81499eb2012-12-27 12:19:02 +01003358 case HTTP_REQ_ACT_REDIR:
Willy Tarreau0b748332014-04-29 00:13:29 +02003359 if (!http_apply_redirect_rule(rule->arg.redir, s, txn))
3360 return HTTP_RULE_RES_BADREQ;
3361 return HTTP_RULE_RES_DONE;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003362
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003363 case HTTP_REQ_ACT_SET_NICE:
3364 s->task->nice = rule->arg.nice;
3365 break;
3366
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003367 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003368 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Vincent Bernata72d3902016-05-19 11:15:51 +02003369 inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003370 break;
3371
Willy Tarreau51347ed2013-06-11 19:34:13 +02003372 case HTTP_REQ_ACT_SET_MARK:
3373#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003374 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003375 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003376#endif
3377 break;
3378
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003379 case HTTP_REQ_ACT_SET_LOGL:
3380 s->logs.level = rule->arg.loglevel;
3381 break;
3382
Sasha Pachev218f0642014-06-16 12:05:59 -06003383 case HTTP_REQ_ACT_REPLACE_HDR:
3384 case HTTP_REQ_ACT_REPLACE_VAL:
3385 if (http_transform_header(s, &txn->req, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3386 txn->req.chn->buf->p, &txn->hdr_idx, &rule->arg.hdr_add.fmt,
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02003387 &rule->arg.hdr_add.re, &ctx, rule->action))
Sasha Pachev218f0642014-06-16 12:05:59 -06003388 return HTTP_RULE_RES_BADREQ;
3389 break;
3390
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003391 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003392 ctx.idx = 0;
3393 /* remove all occurrences of the header */
3394 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3395 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3396 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003397 }
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003398 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003399
Willy Tarreaufcf75d22015-01-21 20:39:27 +01003400 case HTTP_REQ_ACT_SET_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003401 case HTTP_REQ_ACT_ADD_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003402 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))
Vincent Bernata72d3902016-05-19 11:15:51 +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;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003911 }
3912
3913 /* add end of headers and the keep-alive/close status.
3914 * We may choose to set keep-alive if the Location begins
3915 * with a slash, because the client will come back to the
3916 * same server.
3917 */
3918 txn->status = rule->code;
3919 /* let's log the request time */
3920 s->logs.tv_request = now;
3921
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003922 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003923 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3924 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3925 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3926 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3927 /* keep-alive possible */
3928 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3929 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3930 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3931 trash.len += 30;
3932 } else {
3933 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3934 trash.len += 24;
3935 }
3936 }
3937 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3938 trash.len += 4;
3939 bo_inject(txn->rsp.chn, trash.str, trash.len);
3940 /* "eat" the request */
3941 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02003942 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003943 msg->sov = 0;
3944 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3945 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3946 txn->req.msg_state = HTTP_MSG_CLOSED;
3947 txn->rsp.msg_state = HTTP_MSG_DONE;
3948 } else {
3949 /* keep-alive not possible */
3950 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3951 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3952 trash.len += 29;
3953 } else {
3954 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3955 trash.len += 23;
3956 }
3957 stream_int_retnclose(txn->req.chn->prod, &trash);
3958 txn->req.chn->analysers = 0;
3959 }
3960
3961 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003962 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003963 if (!(s->flags & SN_FINST_MASK))
3964 s->flags |= SN_FINST_R;
3965
3966 return 1;
3967}
3968
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003969/* This stream analyser runs all HTTP request processing which is common to
3970 * frontends and backends, which means blocking ACLs, filters, connection-close,
3971 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003972 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003973 * either needs more data or wants to immediately abort the request (eg: deny,
3974 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003975 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003976int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003977{
Willy Tarreaud787e662009-07-07 10:14:51 +02003978 struct http_txn *txn = &s->txn;
3979 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003980 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003981 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003982 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02003983
Willy Tarreau655dce92009-11-08 13:10:58 +01003984 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003985 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003986 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003987 return 0;
3988 }
3989
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003990 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 +02003991 now_ms, __FUNCTION__,
3992 s,
3993 req,
3994 req->rex, req->wex,
3995 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003996 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003997 req->analysers);
3998
Willy Tarreau65410832014-04-28 21:25:43 +02003999 /* just in case we have some per-backend tracking */
4000 session_inc_be_http_req_ctr(s);
4001
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004002 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004003 if (!LIST_ISEMPTY(&px->http_req_rules)) {
4004 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01004005
Willy Tarreau0b748332014-04-29 00:13:29 +02004006 switch (verdict) {
4007 case HTTP_RULE_RES_CONT:
4008 case HTTP_RULE_RES_STOP: /* nothing to do */
4009 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004010
Willy Tarreau0b748332014-04-29 00:13:29 +02004011 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4012 if (txn->flags & TX_CLTARPIT)
4013 goto tarpit;
4014 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004015
Willy Tarreau0b748332014-04-29 00:13:29 +02004016 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4017 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004018
Willy Tarreau0b748332014-04-29 00:13:29 +02004019 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004020 goto done;
4021
Willy Tarreau0b748332014-04-29 00:13:29 +02004022 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4023 goto return_bad_req;
4024 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004025 }
4026
Willy Tarreau52542592014-04-28 18:33:26 +02004027 /* OK at this stage, we know that the request was accepted according to
4028 * the http-request rules, we can check for the stats. Note that the
4029 * URI is detected *before* the req* rules in order not to be affected
4030 * by a possible reqrep, while they are processed *after* so that a
4031 * reqdeny can still block them. This clearly needs to change in 1.6!
4032 */
4033 if (stats_check_uri(s->rep->prod, txn, px)) {
4034 s->target = &http_stats_applet.obj_type;
4035 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
4036 txn->status = 500;
4037 s->logs.tv_request = now;
4038 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004039
Willy Tarreau52542592014-04-28 18:33:26 +02004040 if (!(s->flags & SN_ERR_MASK))
4041 s->flags |= SN_ERR_RESOURCE;
4042 goto return_prx_cond;
4043 }
4044
4045 /* parse the whole stats request and extract the relevant information */
4046 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004047 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4048 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004049
Willy Tarreau0b748332014-04-29 00:13:29 +02004050 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4051 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004052
Willy Tarreau0b748332014-04-29 00:13:29 +02004053 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4054 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004055 }
4056
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004057 /* evaluate the req* rules except reqadd */
4058 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004059 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004060 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004061
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004062 if (txn->flags & TX_CLDENY)
4063 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004064
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004065 if (txn->flags & TX_CLTARPIT)
4066 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004067 }
Willy Tarreau06619262006-12-17 08:37:22 +01004068
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004069 /* add request headers from the rule sets in the same order */
4070 list_for_each_entry(wl, &px->req_add, list) {
4071 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004072 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004073 ret = acl_pass(ret);
4074 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4075 ret = !ret;
4076 if (!ret)
4077 continue;
4078 }
4079
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004080 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004081 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004082 }
4083
Willy Tarreau52542592014-04-28 18:33:26 +02004084
4085 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004086 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004087 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004088 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
4089 s->fe->fe_counters.intercepted_req++;
4090
4091 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
4092 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
4093 if (!(s->flags & SN_FINST_MASK))
4094 s->flags |= SN_FINST_R;
4095
Willy Tarreau70730dd2014-04-24 18:06:27 +02004096 /* we may want to compress the stats page */
4097 if (s->fe->comp || s->be->comp)
4098 select_compression_request_header(s, req->buf);
4099
4100 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreaude8ca962014-11-20 22:23:10 +01004101 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004102 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004103 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004104
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004105 /* check whether we have some ACLs set to redirect this request */
4106 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004107 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004108 int ret;
4109
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004110 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004111 ret = acl_pass(ret);
4112 if (rule->cond->pol == ACL_COND_UNLESS)
4113 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004114 if (!ret)
4115 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004116 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004117 if (!http_apply_redirect_rule(rule, s, txn))
4118 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004119 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004120 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004121
Willy Tarreau2be39392010-01-03 17:24:51 +01004122 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4123 * If this happens, then the data will not come immediately, so we must
4124 * send all what we have without waiting. Note that due to the small gain
4125 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004126 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004127 * itself once used.
4128 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004129 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004130
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004131 done: /* done with this analyser, continue with next ones that the calling
4132 * points will have set, if any.
4133 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004134 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004135 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4136 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004137 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004138
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004139 tarpit:
4140 /* When a connection is tarpitted, we use the tarpit timeout,
4141 * which may be the same as the connect timeout if unspecified.
4142 * If unset, then set it to zero because we really want it to
4143 * eventually expire. We build the tarpit as an analyser.
4144 */
4145 channel_erase(s->req);
4146
4147 /* wipe the request out so that we can drop the connection early
4148 * if the client closes first.
4149 */
4150 channel_dont_connect(req);
4151 req->analysers = 0; /* remove switching rules etc... */
Bertrand Paquetc89b09b2016-04-06 11:58:31 +02004152
4153 /* Allow cookie logging
4154 */
4155 if (s->be->cookie_name || s->fe->capture_name)
4156 manage_client_side_cookies(s, req);
4157
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004158 req->analysers |= AN_REQ_HTTP_TARPIT;
4159 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4160 if (!req->analyse_exp)
4161 req->analyse_exp = tick_add(now_ms, 0);
4162 session_inc_http_err_ctr(s);
4163 s->fe->fe_counters.denied_req++;
4164 if (s->fe != s->be)
4165 s->be->be_counters.denied_req++;
4166 if (s->listener->counters)
4167 s->listener->counters->denied_req++;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004168 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004169
4170 deny: /* this request was blocked (denied) */
Bertrand Paquetc89b09b2016-04-06 11:58:31 +02004171
4172 /* Allow cookie logging
4173 */
4174 if (s->be->cookie_name || s->fe->capture_name)
4175 manage_client_side_cookies(s, req);
4176
Willy Tarreau0b748332014-04-29 00:13:29 +02004177 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004178 txn->status = 403;
4179 s->logs.tv_request = now;
4180 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4181 session_inc_http_err_ctr(s);
4182 s->fe->fe_counters.denied_req++;
4183 if (s->fe != s->be)
4184 s->be->be_counters.denied_req++;
4185 if (s->listener->counters)
4186 s->listener->counters->denied_req++;
4187 goto return_prx_cond;
4188
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004189 return_bad_req:
4190 /* We centralize bad requests processing here */
4191 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4192 /* we detected a parsing error. We want to archive this request
4193 * in the dedicated proxy area for later troubleshooting.
4194 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004195 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004196 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004197
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004198 txn->req.msg_state = HTTP_MSG_ERROR;
4199 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004200 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004201
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004202 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004203 if (s->listener->counters)
4204 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004205
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004206 return_prx_cond:
4207 if (!(s->flags & SN_ERR_MASK))
4208 s->flags |= SN_ERR_PRXCOND;
4209 if (!(s->flags & SN_FINST_MASK))
4210 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004211
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004212 req->analysers = 0;
4213 req->analyse_exp = TICK_ETERNITY;
4214 return 0;
4215}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004216
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004217/* This function performs all the processing enabled for the current request.
4218 * It returns 1 if the processing can continue on next analysers, or zero if it
4219 * needs more data, encounters an error, or wants to immediately abort the
4220 * request. It relies on buffers flags, and updates s->req->analysers.
4221 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004222int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004223{
4224 struct http_txn *txn = &s->txn;
4225 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004226 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004227
Willy Tarreau655dce92009-11-08 13:10:58 +01004228 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004229 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004230 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004231 return 0;
4232 }
4233
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004234 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 +02004235 now_ms, __FUNCTION__,
4236 s,
4237 req,
4238 req->rex, req->wex,
4239 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004240 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004241 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004242
William Lallemand82fe75c2012-10-23 10:25:10 +02004243 if (s->fe->comp || s->be->comp)
4244 select_compression_request_header(s, req->buf);
4245
Willy Tarreau59234e92008-11-30 23:51:27 +01004246 /*
4247 * Right now, we know that we have processed the entire headers
4248 * and that unwanted requests have been filtered out. We can do
4249 * whatever we want with the remaining request. Also, now we
4250 * may have separate values for ->fe, ->be.
4251 */
Willy Tarreau06619262006-12-17 08:37:22 +01004252
Willy Tarreau59234e92008-11-30 23:51:27 +01004253 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004254 * If HTTP PROXY is set we simply get remote server address parsing
4255 * incoming request. Note that this requires that a connection is
4256 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004257 */
4258 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004259 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004260 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004261
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004262 /* Note that for now we don't reuse existing proxy connections */
4263 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004264 txn->req.msg_state = HTTP_MSG_ERROR;
4265 txn->status = 500;
4266 req->analysers = 0;
4267 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4268
4269 if (!(s->flags & SN_ERR_MASK))
4270 s->flags |= SN_ERR_RESOURCE;
4271 if (!(s->flags & SN_FINST_MASK))
4272 s->flags |= SN_FINST_R;
4273
4274 return 0;
4275 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004276
4277 path = http_get_path(txn);
4278 url2sa(req->buf->p + msg->sl.rq.u,
4279 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004280 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004281 /* if the path was found, we have to remove everything between
4282 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4283 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4284 * u_l characters by a single "/".
4285 */
4286 if (path) {
4287 char *cur_ptr = req->buf->p;
4288 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4289 int delta;
4290
4291 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4292 http_msg_move_end(&txn->req, delta);
4293 cur_end += delta;
4294 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4295 goto return_bad_req;
4296 }
4297 else {
4298 char *cur_ptr = req->buf->p;
4299 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4300 int delta;
4301
4302 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4303 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4304 http_msg_move_end(&txn->req, delta);
4305 cur_end += delta;
4306 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4307 goto return_bad_req;
4308 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004309 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004310
Willy Tarreau59234e92008-11-30 23:51:27 +01004311 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004312 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004313 * Note that doing so might move headers in the request, but
4314 * the fields will stay coherent and the URI will not move.
4315 * This should only be performed in the backend.
4316 */
Bertrand Paquetc89b09b2016-04-06 11:58:31 +02004317 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004318 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004319
Willy Tarreau59234e92008-11-30 23:51:27 +01004320 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004321 * 8: the appsession cookie was looked up very early in 1.2,
4322 * so let's do the same now.
4323 */
4324
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004325 /* It needs to look into the URI unless persistence must be ignored */
4326 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004327 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 +01004328 }
4329
William Lallemanda73203e2012-03-12 12:48:57 +01004330 /* add unique-id if "header-unique-id" is specified */
4331
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004332 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4333 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4334 goto return_bad_req;
4335 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004336 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004337 }
William Lallemanda73203e2012-03-12 12:48:57 +01004338
4339 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004340 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4341 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004342 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004343 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004344 goto return_bad_req;
4345 }
4346
Cyril Bontéb21570a2009-11-29 20:04:48 +01004347 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004348 * 9: add X-Forwarded-For if either the frontend or the backend
4349 * asks for it.
4350 */
4351 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004352 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004353 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004354 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4355 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004356 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004357 /* The header is set to be added only if none is present
4358 * and we found it, so don't do anything.
4359 */
4360 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004361 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004362 /* Add an X-Forwarded-For header unless the source IP is
4363 * in the 'except' network range.
4364 */
4365 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004366 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004367 != s->fe->except_net.s_addr) &&
4368 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004369 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004370 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004371 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004372 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004373 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004374
4375 /* Note: we rely on the backend to get the header name to be used for
4376 * x-forwarded-for, because the header is really meant for the backends.
4377 * However, if the backend did not specify any option, we have to rely
4378 * on the frontend's header name.
4379 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004380 if (s->be->fwdfor_hdr_len) {
4381 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004382 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004383 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004384 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004385 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004386 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004387 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 +01004388
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004389 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004390 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004391 }
4392 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004393 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004394 /* FIXME: for the sake of completeness, we should also support
4395 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004396 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004397 int len;
4398 char pn[INET6_ADDRSTRLEN];
4399 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004400 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004401 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004402
Willy Tarreau59234e92008-11-30 23:51:27 +01004403 /* Note: we rely on the backend to get the header name to be used for
4404 * x-forwarded-for, because the header is really meant for the backends.
4405 * However, if the backend did not specify any option, we have to rely
4406 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004407 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004408 if (s->be->fwdfor_hdr_len) {
4409 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004410 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004411 } else {
4412 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004413 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004414 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004415 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004416
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004417 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004418 goto return_bad_req;
4419 }
4420 }
4421
4422 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004423 * 10: add X-Original-To if either the frontend or the backend
4424 * asks for it.
4425 */
4426 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4427
4428 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004429 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004430 /* Add an X-Original-To header unless the destination IP is
4431 * in the 'except' network range.
4432 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004433 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004434
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004435 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004436 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004437 (((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 +02004438 != s->fe->except_to.s_addr) &&
4439 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004440 (((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 +02004441 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004442 int len;
4443 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004444 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004445
4446 /* Note: we rely on the backend to get the header name to be used for
4447 * x-original-to, because the header is really meant for the backends.
4448 * However, if the backend did not specify any option, we have to rely
4449 * on the frontend's header name.
4450 */
4451 if (s->be->orgto_hdr_len) {
4452 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004453 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004454 } else {
4455 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004456 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004457 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004458 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 +02004459
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004460 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004461 goto return_bad_req;
4462 }
4463 }
4464 }
4465
Willy Tarreau50fc7772012-11-11 22:19:57 +01004466 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4467 * If an "Upgrade" token is found, the header is left untouched in order not to have
4468 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4469 * "Upgrade" is present in the Connection header.
4470 */
4471 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4472 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004473 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4474 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004475 unsigned int want_flags = 0;
4476
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004477 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004478 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004479 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4480 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004481 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004482 want_flags |= TX_CON_CLO_SET;
4483 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004484 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004485 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4486 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004487 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004488 want_flags |= TX_CON_KAL_SET;
4489 }
4490
4491 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004492 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004493 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004494
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004495
Willy Tarreau522d6c02009-12-06 18:49:18 +01004496 /* If we have no server assigned yet and we're balancing on url_param
4497 * with a POST request, we may be interested in checking the body for
4498 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004499 */
4500 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4501 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004502 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004503 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004504 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004505 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004506
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004507 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004508 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004509#ifdef TCP_QUICKACK
4510 /* We expect some data from the client. Unless we know for sure
4511 * we already have a full request, we have to re-enable quick-ack
4512 * in case we previously disabled it, otherwise we might cause
4513 * the client to delay further data.
4514 */
4515 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004516 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004517 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004518 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004519 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004520#endif
4521 }
Willy Tarreau03945942009-12-22 16:50:27 +01004522
Willy Tarreau59234e92008-11-30 23:51:27 +01004523 /*************************************************************
4524 * OK, that's finished for the headers. We have done what we *
4525 * could. Let's switch to the DATA state. *
4526 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004527 req->analyse_exp = TICK_ETERNITY;
4528 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004529
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004530 /* if the server closes the connection, we want to immediately react
4531 * and close the socket to save packets and syscalls.
4532 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004533 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4534 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004535
Willy Tarreau59234e92008-11-30 23:51:27 +01004536 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004537 /* OK let's go on with the BODY now */
4538 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004539
Willy Tarreau59234e92008-11-30 23:51:27 +01004540 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004541 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004542 /* we detected a parsing error. We want to archive this request
4543 * in the dedicated proxy area for later troubleshooting.
4544 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004545 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004546 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004547
Willy Tarreau59234e92008-11-30 23:51:27 +01004548 txn->req.msg_state = HTTP_MSG_ERROR;
4549 txn->status = 400;
4550 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004551 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004552
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004553 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004554 if (s->listener->counters)
4555 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004556
Willy Tarreau59234e92008-11-30 23:51:27 +01004557 if (!(s->flags & SN_ERR_MASK))
4558 s->flags |= SN_ERR_PRXCOND;
4559 if (!(s->flags & SN_FINST_MASK))
4560 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004561 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004562}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004563
Willy Tarreau60b85b02008-11-30 23:28:40 +01004564/* This function is an analyser which processes the HTTP tarpit. It always
4565 * returns zero, at the beginning because it prevents any other processing
4566 * from occurring, and at the end because it terminates the request.
4567 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004568int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004569{
4570 struct http_txn *txn = &s->txn;
4571
4572 /* This connection is being tarpitted. The CLIENT side has
4573 * already set the connect expiration date to the right
4574 * timeout. We just have to check that the client is still
4575 * there and that the timeout has not expired.
4576 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004577 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004578 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004579 !tick_is_expired(req->analyse_exp, now_ms))
4580 return 0;
4581
4582 /* We will set the queue timer to the time spent, just for
4583 * logging purposes. We fake a 500 server error, so that the
4584 * attacker will not suspect his connection has been tarpitted.
4585 * It will not cause trouble to the logs because we can exclude
4586 * the tarpitted connections by filtering on the 'PT' status flags.
4587 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004588 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4589
4590 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004591 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004592 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004593
4594 req->analysers = 0;
4595 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004596
Willy Tarreau60b85b02008-11-30 23:28:40 +01004597 if (!(s->flags & SN_ERR_MASK))
4598 s->flags |= SN_ERR_PRXCOND;
4599 if (!(s->flags & SN_FINST_MASK))
4600 s->flags |= SN_FINST_T;
4601 return 0;
4602}
4603
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004604/* This function is an analyser which waits for the HTTP request body. It waits
4605 * for either the buffer to be full, or the full advertised contents to have
4606 * reached the buffer. It must only be called after the standard HTTP request
4607 * processing has occurred, because it expects the request to be parsed and will
4608 * look for the Expect header. It may send a 100-Continue interim response. It
4609 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4610 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4611 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004612 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004613int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004614{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004615 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004616 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004617
4618 /* We have to parse the HTTP request body to find any required data.
4619 * "balance url_param check_post" should have been the only way to get
4620 * into this. We were brought here after HTTP header analysis, so all
4621 * related structures are ready.
4622 */
4623
Willy Tarreau890988f2014-04-10 11:59:33 +02004624 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4625 /* This is the first call */
4626 if (msg->msg_state < HTTP_MSG_BODY)
4627 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004628
Willy Tarreau890988f2014-04-10 11:59:33 +02004629 if (msg->msg_state < HTTP_MSG_100_SENT) {
4630 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4631 * send an HTTP/1.1 100 Continue intermediate response.
4632 */
4633 if (msg->flags & HTTP_MSGF_VER_11) {
4634 struct hdr_ctx ctx;
4635 ctx.idx = 0;
4636 /* Expect is allowed in 1.1, look for it */
4637 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4638 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4639 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4640 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004641 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004642 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004643 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004644
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004645 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004646 * req->buf->p still points to the beginning of the message. We
4647 * must save the body in msg->next because it survives buffer
4648 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004649 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004650 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004651
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004652 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004653 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4654 else
4655 msg->msg_state = HTTP_MSG_DATA;
4656 }
4657
Willy Tarreau890988f2014-04-10 11:59:33 +02004658 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4659 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreau5b7f9242015-05-01 23:05:14 +02004660 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004661 goto missing_data;
4662
4663 /* OK we have everything we need now */
4664 goto http_end;
4665 }
4666
4667 /* OK here we're parsing a chunked-encoded message */
4668
Willy Tarreau522d6c02009-12-06 18:49:18 +01004669 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004670 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004671 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004672 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004673 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004674 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004675
Willy Tarreau115acb92009-12-26 13:56:06 +01004676 if (!ret)
4677 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004678 else if (ret < 0) {
4679 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004680 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004681 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004682 }
4683
Willy Tarreaud98cf932009-12-27 22:54:55 +01004684 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau5b7f9242015-05-01 23:05:14 +02004685 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4686 * for at least a whole chunk or the whole content length bytes after
4687 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004688 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004689 if (msg->msg_state == HTTP_MSG_TRAILERS)
4690 goto http_end;
4691
Willy Tarreau5b7f9242015-05-01 23:05:14 +02004692 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004693 goto http_end;
4694
4695 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004696 /* we get here if we need to wait for more data. If the buffer is full,
4697 * we have the maximum we can expect.
4698 */
4699 if (buffer_full(req->buf, global.tune.maxrewrite))
4700 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004701
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004702 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004703 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004704 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004705
4706 if (!(s->flags & SN_ERR_MASK))
4707 s->flags |= SN_ERR_CLITO;
4708 if (!(s->flags & SN_FINST_MASK))
4709 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004710 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004711 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004712
4713 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004714 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004715 /* Not enough data. We'll re-use the http-request
4716 * timeout here. Ideally, we should set the timeout
4717 * relative to the accept() date. We just set the
4718 * request timeout once at the beginning of the
4719 * request.
4720 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004721 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004722 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004723 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004724 return 0;
4725 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004726
4727 http_end:
4728 /* The situation will not evolve, so let's give up on the analysis. */
4729 s->logs.tv_request = now; /* update the request timer to reflect full request */
4730 req->analysers &= ~an_bit;
4731 req->analyse_exp = TICK_ETERNITY;
4732 return 1;
4733
4734 return_bad_req: /* let's centralize all bad requests */
4735 txn->req.msg_state = HTTP_MSG_ERROR;
4736 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004737 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004738
Willy Tarreau79ebac62010-06-07 13:47:49 +02004739 if (!(s->flags & SN_ERR_MASK))
4740 s->flags |= SN_ERR_PRXCOND;
4741 if (!(s->flags & SN_FINST_MASK))
4742 s->flags |= SN_FINST_R;
4743
Willy Tarreau522d6c02009-12-06 18:49:18 +01004744 return_err_msg:
4745 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004746 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004747 if (s->listener->counters)
4748 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004749 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004750}
4751
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004752/* send a server's name with an outgoing request over an established connection.
4753 * Note: this function is designed to be called once the request has been scheduled
4754 * for being forwarded. This is the reason why it rewinds the buffer before
4755 * proceeding.
4756 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004757int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004758
4759 struct hdr_ctx ctx;
4760
Mark Lamourinec2247f02012-01-04 13:02:01 -05004761 char *hdr_name = be->server_id_hdr_name;
4762 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004763 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004764 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004765 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004766
William Lallemandd9e90662012-01-30 17:27:17 +01004767 ctx.idx = 0;
4768
Willy Tarreau211cdec2014-04-17 20:18:08 +02004769 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004770 if (old_o) {
4771 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004772 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004773 txn->req.next += old_o;
4774 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004775 }
4776
Willy Tarreau9b28e032012-10-12 23:49:43 +02004777 old_i = chn->buf->i;
4778 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 -05004779 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004780 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004781 }
4782
4783 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004784 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004785 memcpy(hdr_val, hdr_name, hdr_name_len);
4786 hdr_val += hdr_name_len;
4787 *hdr_val++ = ':';
4788 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004789 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4790 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004791
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004792 if (old_o) {
4793 /* If this was a forwarded request, we must readjust the amount of
4794 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004795 * variations. Note that the current state is >= HTTP_MSG_BODY,
4796 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004797 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004798 old_o += chn->buf->i - old_i;
4799 b_adv(chn->buf, old_o);
4800 txn->req.next -= old_o;
4801 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004802 }
4803
Mark Lamourinec2247f02012-01-04 13:02:01 -05004804 return 0;
4805}
4806
Willy Tarreau610ecce2010-01-04 21:15:02 +01004807/* Terminate current transaction and prepare a new one. This is very tricky
4808 * right now but it works.
4809 */
4810void http_end_txn_clean_session(struct session *s)
4811{
Willy Tarreau068621e2013-12-23 15:11:25 +01004812 int prev_status = s->txn.status;
4813
Willy Tarreau610ecce2010-01-04 21:15:02 +01004814 /* FIXME: We need a more portable way of releasing a backend's and a
4815 * server's connections. We need a safer way to reinitialize buffer
4816 * flags. We also need a more accurate method for computing per-request
4817 * data.
4818 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004819
Willy Tarreau4213a112013-12-15 10:25:42 +01004820 /* unless we're doing keep-alive, we want to quickly close the connection
4821 * to the server.
4822 */
4823 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4824 !si_conn_ready(s->req->cons)) {
4825 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4826 si_shutr(s->req->cons);
4827 si_shutw(s->req->cons);
4828 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004829
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004830 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004831 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004832 if (unlikely(s->srv_conn))
4833 sess_change_server(s, NULL);
4834 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004835
4836 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4837 session_process_counters(s);
4838
4839 if (s->txn.status) {
4840 int n;
4841
4842 n = s->txn.status / 100;
4843 if (n < 1 || n > 5)
4844 n = 0;
4845
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004846 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004847 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004848 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004849 s->fe->fe_counters.p.http.comp_rsp++;
4850 }
Willy Tarreau24657792010-02-26 10:30:28 +01004851 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004852 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004853 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004854 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004855 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004856 s->be->be_counters.p.http.comp_rsp++;
4857 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004858 }
4859
4860 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004861 s->logs.bytes_in -= s->req->buf->i;
4862 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004863
4864 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004865 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004866 !(s->flags & SN_MONITOR) &&
4867 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4868 s->do_log(s);
4869 }
4870
Willy Tarreauc177ea72014-06-25 15:36:04 +02004871 /* stop tracking content-based counters */
4872 session_stop_content_counters(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004873 session_update_time_stats(s);
4874
Willy Tarreau610ecce2010-01-04 21:15:02 +01004875 s->logs.accept_date = date; /* user-visible date for logging */
4876 s->logs.tv_accept = now; /* corrected date for internal use */
4877 tv_zero(&s->logs.tv_request);
4878 s->logs.t_queue = -1;
4879 s->logs.t_connect = -1;
4880 s->logs.t_data = -1;
4881 s->logs.t_close = 0;
4882 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4883 s->logs.srv_queue_size = 0; /* we will get this number soon */
4884
Willy Tarreau9b28e032012-10-12 23:49:43 +02004885 s->logs.bytes_in = s->req->total = s->req->buf->i;
4886 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004887
4888 if (s->pend_pos)
4889 pendconn_free(s->pend_pos);
4890
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004891 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004892 if (s->flags & SN_CURR_SESS) {
4893 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004894 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004895 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004896 if (may_dequeue_tasks(objt_server(s->target), s->be))
4897 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004898 }
4899
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004900 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004901
Willy Tarreau4213a112013-12-15 10:25:42 +01004902 /* only release our endpoint if we don't intend to reuse the
4903 * connection.
4904 */
4905 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4906 !si_conn_ready(s->req->cons)) {
4907 si_release_endpoint(s->req->cons);
4908 }
4909
Willy Tarreau610ecce2010-01-04 21:15:02 +01004910 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004911 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004912 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004913 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004914 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreaub4d05092014-09-01 20:35:55 +02004915 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);
4916 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 +02004917 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 +01004918 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Cyril Bonté17f77072014-10-22 22:30:13 +02004919 s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004920
Willy Tarreau610ecce2010-01-04 21:15:02 +01004921 s->txn.meth = 0;
4922 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004923 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004924
4925 if (prev_status == 401 || prev_status == 407) {
4926 /* In HTTP keep-alive mode, if we receive a 401, we still have
4927 * a chance of being able to send the visitor again to the same
4928 * server over the same connection. This is required by some
4929 * broken protocols such as NTLM, and anyway whenever there is
4930 * an opportunity for sending the challenge to the proper place,
4931 * it's better to do it (at least it helps with debugging).
4932 */
4933 s->txn.flags |= TX_PREFER_LAST;
4934 }
4935
Willy Tarreauee55dc02010-06-01 10:56:34 +02004936 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004937 s->req->cons->flags |= SI_FL_INDEP_STR;
4938
Willy Tarreau96e31212011-05-30 18:10:30 +02004939 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004940 s->req->flags |= CF_NEVER_WAIT;
4941 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004942 }
4943
Willy Tarreau610ecce2010-01-04 21:15:02 +01004944 /* if the request buffer is not empty, it means we're
4945 * about to process another request, so send pending
4946 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004947 * Just don't do this if the buffer is close to be full,
4948 * because the request will wait for it to flush a little
4949 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004950 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004951 if (s->req->buf->i) {
4952 if (s->rep->buf->o &&
4953 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4954 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004955 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004956 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004957
Willy Tarreau3de8e7a2015-11-25 20:11:11 +01004958 /* we're removing the analysers, we MUST re-enable events detection.
4959 * We don't enable close on the response channel since it's either
4960 * already closed, or in keep-alive with an idle connection handler.
4961 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004962 channel_auto_read(s->req);
4963 channel_auto_close(s->req);
4964 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004965
Willy Tarreau27375622013-12-17 00:00:28 +01004966 /* we're in keep-alive with an idle connection, monitor it */
4967 si_idle_conn(s->req->cons);
4968
Willy Tarreau342b11c2010-11-24 16:22:09 +01004969 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004970 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004971}
4972
4973
4974/* This function updates the request state machine according to the response
4975 * state machine and buffer flags. It returns 1 if it changes anything (flag
4976 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4977 * it is only used to find when a request/response couple is complete. Both
4978 * this function and its equivalent should loop until both return zero. It
4979 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4980 */
4981int http_sync_req_state(struct session *s)
4982{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004983 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004984 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004985 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004986 unsigned int old_state = txn->req.msg_state;
4987
Willy Tarreau610ecce2010-01-04 21:15:02 +01004988 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4989 return 0;
4990
4991 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004992 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004993 * We can shut the read side unless we want to abort_on_close,
4994 * or we have a POST request. The issue with POST requests is
4995 * that some browsers still send a CRLF after the request, and
4996 * this CRLF must be read so that it does not remain in the kernel
4997 * buffers, otherwise a close could cause an RST on some systems
4998 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004999 * Note that if we're using keep-alive on the client side, we'd
5000 * rather poll now and keep the polling enabled for the whole
5001 * session's life than enabling/disabling it between each
5002 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005003 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005004 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5005 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5006 !(s->be->options & PR_O_ABRT_CLOSE) &&
5007 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005008 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005009
Willy Tarreau40f151a2012-12-20 12:10:09 +01005010 /* if the server closes the connection, we want to immediately react
5011 * and close the socket to save packets and syscalls.
5012 */
5013 chn->cons->flags |= SI_FL_NOHALF;
5014
Willy Tarreauc0d56132015-11-18 11:59:55 +01005015 /* In any case we've finished parsing the request so we must
5016 * disable Nagle when sending data because 1) we're not going
5017 * to shut this side, and 2) the server is waiting for us to
5018 * send pending data.
5019 */
5020 chn->flags |= CF_NEVER_WAIT;
5021
Willy Tarreau610ecce2010-01-04 21:15:02 +01005022 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5023 goto wait_other_side;
5024
5025 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5026 /* The server has not finished to respond, so we
5027 * don't want to move in order not to upset it.
5028 */
5029 goto wait_other_side;
5030 }
5031
5032 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5033 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005034 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005035 txn->req.msg_state = HTTP_MSG_TUNNEL;
5036 goto wait_other_side;
5037 }
5038
5039 /* When we get here, it means that both the request and the
5040 * response have finished receiving. Depending on the connection
5041 * mode, we'll have to wait for the last bytes to leave in either
5042 * direction, and sometimes for a close to be effective.
5043 */
5044
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005045 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5046 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005047 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5048 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005049 }
5050 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5051 /* Option forceclose is set, or either side wants to close,
5052 * let's enforce it now that we're not expecting any new
5053 * data to come. The caller knows the session is complete
5054 * once both states are CLOSED.
5055 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005056 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5057 channel_shutr_now(chn);
5058 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005059 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005060 }
5061 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005062 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5063 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005064 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005065 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5066 channel_auto_read(chn);
5067 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005068 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005069 }
5070
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005071 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005072 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005073 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005074
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005075 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005076 txn->req.msg_state = HTTP_MSG_CLOSING;
5077 goto http_msg_closing;
5078 }
5079 else {
5080 txn->req.msg_state = HTTP_MSG_CLOSED;
5081 goto http_msg_closed;
5082 }
5083 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005084 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005085 }
5086
5087 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5088 http_msg_closing:
5089 /* nothing else to forward, just waiting for the output buffer
5090 * to be empty and for the shutw_now to take effect.
5091 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005092 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005093 txn->req.msg_state = HTTP_MSG_CLOSED;
5094 goto http_msg_closed;
5095 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005096 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005097 txn->req.msg_state = HTTP_MSG_ERROR;
5098 goto wait_other_side;
5099 }
5100 }
5101
5102 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5103 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005104 /* see above in MSG_DONE why we only do this in these states */
5105 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5106 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5107 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005108 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005109 goto wait_other_side;
5110 }
5111
5112 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005113 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005114}
5115
5116
5117/* This function updates the response state machine according to the request
5118 * state machine and buffer flags. It returns 1 if it changes anything (flag
5119 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5120 * it is only used to find when a request/response couple is complete. Both
5121 * this function and its equivalent should loop until both return zero. It
5122 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5123 */
5124int http_sync_res_state(struct session *s)
5125{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005126 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005127 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005128 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005129 unsigned int old_state = txn->rsp.msg_state;
5130
Willy Tarreau610ecce2010-01-04 21:15:02 +01005131 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5132 return 0;
5133
5134 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5135 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005136 * still monitor the server connection for a possible close
5137 * while the request is being uploaded, so we don't disable
5138 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005139 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005140 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005141
5142 if (txn->req.msg_state == HTTP_MSG_ERROR)
5143 goto wait_other_side;
5144
5145 if (txn->req.msg_state < HTTP_MSG_DONE) {
5146 /* The client seems to still be sending data, probably
5147 * because we got an error response during an upload.
5148 * We have the choice of either breaking the connection
5149 * or letting it pass through. Let's do the later.
5150 */
5151 goto wait_other_side;
5152 }
5153
5154 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5155 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005156 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005157 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005158 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005159 goto wait_other_side;
5160 }
5161
5162 /* When we get here, it means that both the request and the
5163 * response have finished receiving. Depending on the connection
5164 * mode, we'll have to wait for the last bytes to leave in either
5165 * direction, and sometimes for a close to be effective.
5166 */
5167
5168 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5169 /* Server-close mode : shut read and wait for the request
5170 * side to close its output buffer. The caller will detect
5171 * when we're in DONE and the other is in CLOSED and will
5172 * catch that for the final cleanup.
5173 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005174 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5175 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005176 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005177 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5178 /* Option forceclose is set, or either side wants to close,
5179 * let's enforce it now that we're not expecting any new
5180 * data to come. The caller knows the session is complete
5181 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005182 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005183 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5184 channel_shutr_now(chn);
5185 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005186 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005187 }
5188 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005189 /* The last possible modes are keep-alive and tunnel. Tunnel will
5190 * need to forward remaining data. Keep-alive will need to monitor
5191 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005192 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005193 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005194 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005195 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5196 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005197 }
5198
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005199 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005200 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005201 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005202 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5203 goto http_msg_closing;
5204 }
5205 else {
5206 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5207 goto http_msg_closed;
5208 }
5209 }
5210 goto wait_other_side;
5211 }
5212
5213 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5214 http_msg_closing:
5215 /* nothing else to forward, just waiting for the output buffer
5216 * to be empty and for the shutw_now to take effect.
5217 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005218 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005219 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5220 goto http_msg_closed;
5221 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005222 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005223 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005224 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005225 if (objt_server(s->target))
5226 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005227 goto wait_other_side;
5228 }
5229 }
5230
5231 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5232 http_msg_closed:
5233 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005234 bi_erase(chn);
5235 channel_auto_close(chn);
5236 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005237 goto wait_other_side;
5238 }
5239
5240 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005241 /* We force the response to leave immediately if we're waiting for the
5242 * other side, since there is no pending shutdown to push it out.
5243 */
5244 if (!channel_is_empty(chn))
5245 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005246 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005247}
5248
5249
5250/* Resync the request and response state machines. Return 1 if either state
5251 * changes.
5252 */
5253int http_resync_states(struct session *s)
5254{
5255 struct http_txn *txn = &s->txn;
5256 int old_req_state = txn->req.msg_state;
5257 int old_res_state = txn->rsp.msg_state;
5258
Willy Tarreau610ecce2010-01-04 21:15:02 +01005259 http_sync_req_state(s);
5260 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005261 if (!http_sync_res_state(s))
5262 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005263 if (!http_sync_req_state(s))
5264 break;
5265 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005266
Willy Tarreau610ecce2010-01-04 21:15:02 +01005267 /* OK, both state machines agree on a compatible state.
5268 * There are a few cases we're interested in :
5269 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5270 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5271 * directions, so let's simply disable both analysers.
5272 * - HTTP_MSG_CLOSED on the response only means we must abort the
5273 * request.
5274 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5275 * with server-close mode means we've completed one request and we
5276 * must re-initialize the server connection.
5277 */
5278
5279 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5280 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5281 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5282 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5283 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005284 channel_auto_close(s->req);
5285 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005286 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005287 channel_auto_close(s->rep);
5288 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005289 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005290 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5291 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005292 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005293 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005294 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005295 channel_auto_close(s->rep);
5296 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01005297 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005298 channel_abort(s->req);
5299 channel_auto_close(s->req);
5300 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005301 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005302 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005303 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5304 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005305 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005306 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5307 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5308 /* server-close/keep-alive: terminate this transaction,
5309 * possibly killing the server connection and reinitialize
Willy Tarreauc72d85b2016-05-02 16:07:07 +02005310 * a fresh-new transaction, but only once we're sure there's
5311 * enough room in the request and response buffer to process
5312 * another request. The request buffer must not hold any
5313 * pending output data and the request buffer must not have
5314 * output data occupying the reserve.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005315 */
Willy Tarreauc72d85b2016-05-02 16:07:07 +02005316 if (s->req->buf->o)
5317 s->req->flags |= CF_WAKE_WRITE;
5318 else if (channel_congested(s->rep))
5319 s->rep->flags |= CF_WAKE_WRITE;
5320 else
5321 http_end_txn_clean_session(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005322 }
5323
Willy Tarreau610ecce2010-01-04 21:15:02 +01005324 return txn->req.msg_state != old_req_state ||
5325 txn->rsp.msg_state != old_res_state;
5326}
5327
Willy Tarreaud98cf932009-12-27 22:54:55 +01005328/* This function is an analyser which forwards request body (including chunk
5329 * sizes if any). It is called as soon as we must forward, even if we forward
5330 * zero byte. The only situation where it must not be called is when we're in
5331 * tunnel mode and we want to forward till the close. It's used both to forward
5332 * remaining data and to resync after end of body. It expects the msg_state to
5333 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5334 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005335 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005336 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005337 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005338int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005339{
5340 struct http_txn *txn = &s->txn;
5341 struct http_msg *msg = &s->txn.req;
5342
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005343 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5344 return 0;
5345
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005346 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005347 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005348 /* Output closed while we were sending data. We must abort and
5349 * wake the other side up.
5350 */
5351 msg->msg_state = HTTP_MSG_ERROR;
5352 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005353 return 1;
5354 }
5355
Willy Tarreaud98cf932009-12-27 22:54:55 +01005356 /* Note that we don't have to send 100-continue back because we don't
5357 * need the data to complete our job, and it's up to the server to
5358 * decide whether to return 100, 417 or anything else in return of
5359 * an "Expect: 100-continue" header.
5360 */
5361
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005362 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005363 /* we have msg->sov which points to the first byte of message
5364 * body, and req->buf.p still points to the beginning of the
5365 * message. We forward the headers now, as we don't need them
5366 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005367 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005368 b_adv(req->buf, msg->sov);
5369 msg->next -= msg->sov;
5370 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005371
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005372 /* The previous analysers guarantee that the state is somewhere
5373 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5374 * msg->next are always correct.
5375 */
5376 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5377 if (msg->flags & HTTP_MSGF_TE_CHNK)
5378 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5379 else
5380 msg->msg_state = HTTP_MSG_DATA;
5381 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005382 }
5383
Willy Tarreau7ba23542014-04-17 21:50:00 +02005384 /* Some post-connect processing might want us to refrain from starting to
5385 * forward data. Currently, the only reason for this is "balance url_param"
5386 * whichs need to parse/process the request after we've enabled forwarding.
5387 */
5388 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5389 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5390 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005391 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005392 goto missing_data;
5393 }
5394 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5395 }
5396
Willy Tarreau80a92c02014-03-12 10:41:13 +01005397 /* in most states, we should abort in case of early close */
5398 channel_auto_close(req);
5399
Willy Tarreauefdf0942014-04-24 20:08:57 +02005400 if (req->to_forward) {
5401 /* We can't process the buffer's contents yet */
5402 req->flags |= CF_WAKE_WRITE;
5403 goto missing_data;
5404 }
5405
Willy Tarreaud98cf932009-12-27 22:54:55 +01005406 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005407 if (msg->msg_state == HTTP_MSG_DATA) {
5408 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005409 /* we may have some pending data starting at req->buf->p */
5410 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005411 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005412 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005413 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005414 msg->next += msg->chunk_len;
5415 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005416
5417 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005418 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005419 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005420 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005421 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005422 }
5423 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005424 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005425 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005426 * TRAILERS state.
5427 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005428 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005429
Willy Tarreau54d23df2012-10-25 19:04:45 +02005430 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005431 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005432 else if (ret < 0) {
5433 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005434 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005435 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005436 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005437 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005438 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005439 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005440 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005441 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005442 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005443
5444 if (ret == 0)
5445 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005446 else if (ret < 0) {
5447 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005448 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005449 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005450 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005451 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005452 /* we're in MSG_CHUNK_SIZE now */
5453 }
5454 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005455 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005456
5457 if (ret == 0)
5458 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005459 else if (ret < 0) {
5460 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005461 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005462 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005463 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005464 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005465 /* we're in HTTP_MSG_DONE now */
5466 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005467 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005468 int old_state = msg->msg_state;
5469
Willy Tarreau610ecce2010-01-04 21:15:02 +01005470 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005471
5472 /* we may have some pending data starting at req->buf->p
5473 * such as last chunk of data or trailers.
5474 */
5475 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005476 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005477 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005478 msg->next = 0;
5479
Willy Tarreau294e4672015-05-11 18:30:33 +02005480 /* we don't want to forward closes on DONE except in
5481 * tunnel mode.
5482 */
5483 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005484 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005485 if (http_resync_states(s)) {
5486 /* some state changes occurred, maybe the analyser
5487 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005488 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005489 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005490 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005491 /* request errors are most likely due to
5492 * the server aborting the transfer.
5493 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005494 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005495 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005496 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005497 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005498 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005499 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005500 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005501 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005502
5503 /* If "option abortonclose" is set on the backend, we
5504 * want to monitor the client's connection and forward
5505 * any shutdown notification to the server, which will
5506 * decide whether to close or to go on processing the
Willy Tarreau294e4672015-05-11 18:30:33 +02005507 * request. We only do that in tunnel mode, and not in
5508 * other modes since it can be abused to exhaust source
5509 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005510 */
5511 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005512 channel_auto_read(req);
Willy Tarreau294e4672015-05-11 18:30:33 +02005513 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5514 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5515 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005516 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005517 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005518 else if (s->txn.meth == HTTP_METH_POST) {
5519 /* POST requests may require to read extra CRLF
5520 * sent by broken browsers and which could cause
5521 * an RST to be sent upon close on some systems
5522 * (eg: Linux).
5523 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005524 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005525 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005526
Willy Tarreau610ecce2010-01-04 21:15:02 +01005527 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005528 }
5529 }
5530
Willy Tarreaud98cf932009-12-27 22:54:55 +01005531 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005532 /* we may have some pending data starting at req->buf->p */
5533 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005534 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005535 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5536
Willy Tarreaubed410e2014-04-22 08:19:34 +02005537 msg->next = 0;
5538 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5539
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005540 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005541 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005542 if (!(s->flags & SN_ERR_MASK))
5543 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005544 if (!(s->flags & SN_FINST_MASK)) {
5545 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5546 s->flags |= SN_FINST_H;
5547 else
5548 s->flags |= SN_FINST_D;
5549 }
5550
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005551 s->fe->fe_counters.cli_aborts++;
5552 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005553 if (objt_server(s->target))
5554 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005555
5556 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005557 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005558
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005559 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005560 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005561 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005562
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005563 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005564 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005565 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005566 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005567 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005568
Willy Tarreau5c620922011-05-11 19:56:11 +02005569 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005570 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005571 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005572 * modes are already handled by the stream sock layer. We must not do
5573 * this in content-length mode because it could present the MSG_MORE
5574 * flag with the last block of forwarded data, which would cause an
5575 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005576 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005577 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005578 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005579
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005580 return 0;
5581
Willy Tarreaud98cf932009-12-27 22:54:55 +01005582 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005583 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005584 if (s->listener->counters)
5585 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005586
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005587 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005588 /* we may have some pending data starting at req->buf->p */
5589 b_adv(req->buf, msg->next);
5590 msg->next = 0;
5591
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005592 txn->req.msg_state = HTTP_MSG_ERROR;
5593 if (txn->status) {
5594 /* Note: we don't send any error if some data were already sent */
5595 stream_int_retnclose(req->prod, NULL);
5596 } else {
5597 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005598 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005599 }
5600 req->analysers = 0;
5601 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005602
5603 if (!(s->flags & SN_ERR_MASK))
5604 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005605 if (!(s->flags & SN_FINST_MASK)) {
5606 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5607 s->flags |= SN_FINST_H;
5608 else
5609 s->flags |= SN_FINST_D;
5610 }
5611 return 0;
5612
5613 aborted_xfer:
5614 txn->req.msg_state = HTTP_MSG_ERROR;
5615 if (txn->status) {
5616 /* Note: we don't send any error if some data were already sent */
5617 stream_int_retnclose(req->prod, NULL);
5618 } else {
5619 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005620 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005621 }
5622 req->analysers = 0;
5623 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5624
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005625 s->fe->fe_counters.srv_aborts++;
5626 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005627 if (objt_server(s->target))
5628 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005629
5630 if (!(s->flags & SN_ERR_MASK))
5631 s->flags |= SN_ERR_SRVCL;
5632 if (!(s->flags & SN_FINST_MASK)) {
5633 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5634 s->flags |= SN_FINST_H;
5635 else
5636 s->flags |= SN_FINST_D;
5637 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005638 return 0;
5639}
5640
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005641/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5642 * processing can continue on next analysers, or zero if it either needs more
5643 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5644 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5645 * when it has nothing left to do, and may remove any analyser when it wants to
5646 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005647 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005648int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005649{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005650 struct http_txn *txn = &s->txn;
5651 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005652 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005653 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005654 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005655 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005656
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005657 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 +02005658 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005659 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005660 rep,
5661 rep->rex, rep->wex,
5662 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005663 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005664 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005665
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005666 /*
5667 * Now parse the partial (or complete) lines.
5668 * We will check the response syntax, and also join multi-line
5669 * headers. An index of all the lines will be elaborated while
5670 * parsing.
5671 *
5672 * For the parsing, we use a 28 states FSM.
5673 *
5674 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005675 * rep->buf->p = beginning of response
5676 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5677 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005678 * msg->eol = end of current header or line (LF or CRLF)
5679 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005680 */
5681
Willy Tarreau628c40c2014-04-24 19:11:26 +02005682 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005683 /* There's a protected area at the end of the buffer for rewriting
5684 * purposes. We don't want to start to parse the request if the
5685 * protected area is affected, because we may have to move processed
5686 * data later, which is much more complicated.
5687 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005688 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005689 if (unlikely(!channel_reserved(rep))) {
5690 /* some data has still not left the buffer, wake us once that's done */
5691 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5692 goto abort_response;
5693 channel_dont_close(rep);
5694 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005695 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005696 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005697 }
5698
Willy Tarreau379357a2013-06-08 12:55:46 +02005699 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5700 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5701 buffer_slow_realign(rep->buf);
5702
Willy Tarreau9b28e032012-10-12 23:49:43 +02005703 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005704 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005705 }
5706
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005707 /* 1: we might have to print this header in debug mode */
5708 if (unlikely((global.mode & MODE_DEBUG) &&
5709 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau8767b132014-10-21 19:36:09 +02005710 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005711 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005712
Willy Tarreau9b28e032012-10-12 23:49:43 +02005713 sol = rep->buf->p;
5714 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005715 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005716
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005717 sol += hdr_idx_first_pos(&txn->hdr_idx);
5718 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005719
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005720 while (cur_idx) {
5721 eol = sol + txn->hdr_idx.v[cur_idx].len;
5722 debug_hdr("srvhdr", s, sol, eol);
5723 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5724 cur_idx = txn->hdr_idx.v[cur_idx].next;
5725 }
5726 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005727
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005728 /*
5729 * Now we quickly check if we have found a full valid response.
5730 * If not so, we check the FD and buffer states before leaving.
5731 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005732 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005733 * responses are checked first.
5734 *
5735 * Depending on whether the client is still there or not, we
5736 * may send an error response back or not. Note that normally
5737 * we should only check for HTTP status there, and check I/O
5738 * errors somewhere else.
5739 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005740
Willy Tarreau655dce92009-11-08 13:10:58 +01005741 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005742 /* Invalid response */
5743 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5744 /* we detected a parsing error. We want to archive this response
5745 * in the dedicated proxy area for later troubleshooting.
5746 */
5747 hdr_response_bad:
5748 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005749 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005750
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005751 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005752 if (objt_server(s->target)) {
5753 objt_server(s->target)->counters.failed_resp++;
5754 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005755 }
Willy Tarreau64648412010-03-05 10:41:54 +01005756 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005757 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005758 rep->analysers = 0;
5759 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005760 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005761 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005762 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005763
5764 if (!(s->flags & SN_ERR_MASK))
5765 s->flags |= SN_ERR_PRXCOND;
5766 if (!(s->flags & SN_FINST_MASK))
5767 s->flags |= SN_FINST_H;
5768
5769 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005770 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005771
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005772 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005773 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005774 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005775 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005776 goto hdr_response_bad;
5777 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005778
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005779 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005780 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005781 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005782 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005783 else if (txn->flags & TX_NOT_FIRST)
5784 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005785
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005786 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005787 if (objt_server(s->target)) {
5788 objt_server(s->target)->counters.failed_resp++;
5789 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005790 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005791
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005792 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005793 rep->analysers = 0;
5794 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005795 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005796 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005797 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005798
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005799 if (!(s->flags & SN_ERR_MASK))
5800 s->flags |= SN_ERR_SRVCL;
5801 if (!(s->flags & SN_FINST_MASK))
5802 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005803 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005804 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005805
Willy Tarreau2a4f5112014-06-23 15:22:31 +02005806 /* read timeout : return a 504 to the client. */
5807 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005808 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005809 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005810
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005811 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005812 if (objt_server(s->target)) {
5813 objt_server(s->target)->counters.failed_resp++;
5814 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005815 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005816
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005817 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005818 rep->analysers = 0;
5819 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005820 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005821 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005822 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005823
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005824 if (!(s->flags & SN_ERR_MASK))
5825 s->flags |= SN_ERR_SRVTO;
5826 if (!(s->flags & SN_FINST_MASK))
5827 s->flags |= SN_FINST_H;
5828 return 0;
5829 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005830
Willy Tarreauf003d372012-11-26 13:35:37 +01005831 /* client abort with an abortonclose */
5832 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5833 s->fe->fe_counters.cli_aborts++;
5834 s->be->be_counters.cli_aborts++;
5835 if (objt_server(s->target))
5836 objt_server(s->target)->counters.cli_aborts++;
5837
5838 rep->analysers = 0;
5839 channel_auto_close(rep);
5840
5841 txn->status = 400;
5842 bi_erase(rep);
5843 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5844
5845 if (!(s->flags & SN_ERR_MASK))
5846 s->flags |= SN_ERR_CLICL;
5847 if (!(s->flags & SN_FINST_MASK))
5848 s->flags |= SN_FINST_H;
5849
5850 /* process_session() will take care of the error */
5851 return 0;
5852 }
5853
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005854 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005855 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005856 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005857 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005858 else if (txn->flags & TX_NOT_FIRST)
5859 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005860
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005861 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005862 if (objt_server(s->target)) {
5863 objt_server(s->target)->counters.failed_resp++;
5864 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005865 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005866
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005867 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005868 rep->analysers = 0;
5869 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005870 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005871 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005872 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005873
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005874 if (!(s->flags & SN_ERR_MASK))
5875 s->flags |= SN_ERR_SRVCL;
5876 if (!(s->flags & SN_FINST_MASK))
5877 s->flags |= SN_FINST_H;
5878 return 0;
5879 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005880
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005881 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005882 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005883 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005884 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005885 else if (txn->flags & TX_NOT_FIRST)
5886 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005887
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005888 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005889 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005890 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005891
5892 if (!(s->flags & SN_ERR_MASK))
5893 s->flags |= SN_ERR_CLICL;
5894 if (!(s->flags & SN_FINST_MASK))
5895 s->flags |= SN_FINST_H;
5896
5897 /* process_session() will take care of the error */
5898 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005899 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005900
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005901 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005902 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005903 return 0;
5904 }
5905
5906 /* More interesting part now : we know that we have a complete
5907 * response which at least looks like HTTP. We have an indicator
5908 * of each header's length, so we can parse them quickly.
5909 */
5910
5911 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005912 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005913
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005914 /*
5915 * 1: get the status code
5916 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005917 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005918 if (n < 1 || n > 5)
5919 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005920 /* when the client triggers a 4xx from the server, it's most often due
5921 * to a missing object or permission. These events should be tracked
5922 * because if they happen often, it may indicate a brute force or a
5923 * vulnerability scan.
5924 */
5925 if (n == 4)
5926 session_inc_http_err_ctr(s);
5927
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005928 if (objt_server(s->target))
5929 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005930
Willy Tarreau55645552015-05-01 13:26:00 +02005931 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5932 * exactly one digit "." one digit. This check may be disabled using
5933 * option accept-invalid-http-response.
5934 */
5935 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5936 if (msg->sl.st.v_l != 8) {
5937 msg->err_pos = 0;
5938 goto hdr_response_bad;
5939 }
5940
5941 if (rep->buf->p[4] != '/' ||
5942 !isdigit((unsigned char)rep->buf->p[5]) ||
5943 rep->buf->p[6] != '.' ||
5944 !isdigit((unsigned char)rep->buf->p[7])) {
5945 msg->err_pos = 4;
5946 goto hdr_response_bad;
5947 }
5948 }
5949
Willy Tarreau5b154472009-12-21 20:11:07 +01005950 /* check if the response is HTTP/1.1 or above */
5951 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005952 ((rep->buf->p[5] > '1') ||
5953 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005954 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005955
5956 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005957 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 +01005958
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005959 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005960 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005961
Willy Tarreau9b28e032012-10-12 23:49:43 +02005962 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005963
Willy Tarreau39650402010-03-15 19:44:39 +01005964 /* Adjust server's health based on status code. Note: status codes 501
5965 * and 505 are triggered on demand by client request, so we must not
5966 * count them as server failures.
5967 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005968 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005969 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005970 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005971 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005972 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005973 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005974
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005975 /*
5976 * 2: check for cacheability.
5977 */
5978
5979 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005980 case 100:
5981 /*
5982 * We may be facing a 100-continue response, in which case this
5983 * is not the right response, and we're waiting for the next one.
5984 * Let's allow this response to go to the client and wait for the
5985 * next one.
5986 */
5987 hdr_idx_init(&txn->hdr_idx);
5988 msg->next -= channel_forward(rep, msg->next);
5989 msg->msg_state = HTTP_MSG_RPBEFORE;
5990 txn->status = 0;
5991 s->logs.t_data = -1; /* was not a response yet */
5992 goto next_one;
5993
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005994 case 200:
5995 case 203:
5996 case 206:
5997 case 300:
5998 case 301:
5999 case 410:
6000 /* RFC2616 @13.4:
6001 * "A response received with a status code of
6002 * 200, 203, 206, 300, 301 or 410 MAY be stored
6003 * by a cache (...) unless a cache-control
6004 * directive prohibits caching."
6005 *
6006 * RFC2616 @9.5: POST method :
6007 * "Responses to this method are not cacheable,
6008 * unless the response includes appropriate
6009 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006010 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006011 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006012 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006013 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6014 break;
6015 default:
6016 break;
6017 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006018
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006019 /*
6020 * 3: we may need to capture headers
6021 */
6022 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01006023 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006024 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006025 txn->rsp.cap, s->fe->rsp_cap);
6026
Willy Tarreau4db603d2015-05-01 10:05:17 +02006027 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6028 * by RFC7230#3.3.3 :
6029 *
6030 * The length of a message body is determined by one of the following
6031 * (in order of precedence):
6032 *
6033 * 1. Any response to a HEAD request and any response with a 1xx
6034 * (Informational), 204 (No Content), or 304 (Not Modified) status
6035 * code is always terminated by the first empty line after the
6036 * header fields, regardless of the header fields present in the
6037 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006038 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006039 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6040 * the connection will become a tunnel immediately after the empty
6041 * line that concludes the header fields. A client MUST ignore any
6042 * Content-Length or Transfer-Encoding header fields received in
6043 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006044 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006045 * 3. If a Transfer-Encoding header field is present and the chunked
6046 * transfer coding (Section 4.1) is the final encoding, the message
6047 * body length is determined by reading and decoding the chunked
6048 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006049 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006050 * If a Transfer-Encoding header field is present in a response and
6051 * the chunked transfer coding is not the final encoding, the
6052 * message body length is determined by reading the connection until
6053 * it is closed by the server. If a Transfer-Encoding header field
6054 * is present in a request and the chunked transfer coding is not
6055 * the final encoding, the message body length cannot be determined
6056 * reliably; the server MUST respond with the 400 (Bad Request)
6057 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006058 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006059 * If a message is received with both a Transfer-Encoding and a
6060 * Content-Length header field, the Transfer-Encoding overrides the
6061 * Content-Length. Such a message might indicate an attempt to
6062 * perform request smuggling (Section 9.5) or response splitting
6063 * (Section 9.4) and ought to be handled as an error. A sender MUST
6064 * remove the received Content-Length field prior to forwarding such
6065 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006066 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006067 * 4. If a message is received without Transfer-Encoding and with
6068 * either multiple Content-Length header fields having differing
6069 * field-values or a single Content-Length header field having an
6070 * invalid value, then the message framing is invalid and the
6071 * recipient MUST treat it as an unrecoverable error. If this is a
6072 * request message, the server MUST respond with a 400 (Bad Request)
6073 * status code and then close the connection. If this is a response
6074 * message received by a proxy, the proxy MUST close the connection
6075 * to the server, discard the received response, and send a 502 (Bad
6076 * Gateway) response to the client. If this is a response message
6077 * received by a user agent, the user agent MUST close the
6078 * connection to the server and discard the received response.
6079 *
6080 * 5. If a valid Content-Length header field is present without
6081 * Transfer-Encoding, its decimal value defines the expected message
6082 * body length in octets. If the sender closes the connection or
6083 * the recipient times out before the indicated number of octets are
6084 * received, the recipient MUST consider the message to be
6085 * incomplete and close the connection.
6086 *
6087 * 6. If this is a request message and none of the above are true, then
6088 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006089 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006090 * 7. Otherwise, this is a response message without a declared message
6091 * body length, so the message body length is determined by the
6092 * number of octets received prior to the server closing the
6093 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006094 */
6095
6096 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006097 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006098 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006099 * FIXME: should we parse anyway and return an error on chunked encoding ?
6100 */
6101 if (txn->meth == HTTP_METH_HEAD ||
6102 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006103 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006104 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006105 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006106 goto skip_content_length;
6107 }
6108
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006109 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006110 ctx.idx = 0;
Willy Tarreaue77bc172015-05-01 10:06:30 +02006111 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006112 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006113 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6114 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006115 /* bad transfer-encoding (chunked followed by something else) */
6116 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006117 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006118 break;
6119 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006120 }
6121
Willy Tarreaudfa3d922015-04-30 10:57:51 +02006122 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006123 ctx.idx = 0;
Willy Tarreau660418d2015-05-01 10:25:45 +02006124 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreaudfa3d922015-04-30 10:57:51 +02006125 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6126 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6127 }
Willy Tarreau660418d2015-05-01 10:25:45 +02006128 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006129 signed long long cl;
6130
Willy Tarreauad14f752011-09-02 20:33:27 +02006131 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006132 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006133 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006134 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006135
Willy Tarreauad14f752011-09-02 20:33:27 +02006136 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006137 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006138 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006139 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006140
Willy Tarreauad14f752011-09-02 20:33:27 +02006141 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006142 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006143 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006144 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006145
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006146 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006147 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006148 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006149 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006150
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006151 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006152 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006153 }
6154
William Lallemand82fe75c2012-10-23 10:25:10 +02006155 if (s->fe->comp || s->be->comp)
6156 select_compression_response_header(s, rep->buf);
6157
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006158skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006159 /* Now we have to check if we need to modify the Connection header.
6160 * This is more difficult on the response than it is on the request,
6161 * because we can have two different HTTP versions and we don't know
6162 * how the client will interprete a response. For instance, let's say
6163 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6164 * HTTP/1.1 response without any header. Maybe it will bound itself to
6165 * HTTP/1.0 because it only knows about it, and will consider the lack
6166 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6167 * the lack of header as a keep-alive. Thus we will use two flags
6168 * indicating how a request MAY be understood by the client. In case
6169 * of multiple possibilities, we'll fix the header to be explicit. If
6170 * ambiguous cases such as both close and keepalive are seen, then we
6171 * will fall back to explicit close. Note that we won't take risks with
6172 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006173 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006174 */
6175
Willy Tarreaudc008c52010-02-01 16:20:08 +01006176 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6177 txn->status == 101)) {
6178 /* Either we've established an explicit tunnel, or we're
6179 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006180 * to understand the next protocols. We have to switch to tunnel
6181 * mode, so that we transfer the request and responses then let
6182 * this protocol pass unmodified. When we later implement specific
6183 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006184 * header which contains information about that protocol for
6185 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006186 */
6187 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6188 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006189 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6190 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006191 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6192 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006193 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006194
Willy Tarreau70dffda2014-01-30 03:07:23 +01006195 /* this situation happens when combining pretend-keepalive with httpclose. */
6196 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006197 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6198 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006199 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6200
Willy Tarreau60466522010-01-18 19:08:45 +01006201 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006202 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006203 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6204 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006205
Willy Tarreau60466522010-01-18 19:08:45 +01006206 /* now adjust header transformations depending on current state */
6207 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6208 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6209 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006210 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006211 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006212 }
Willy Tarreau60466522010-01-18 19:08:45 +01006213 else { /* SCL / KAL */
6214 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006215 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006216 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006217 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006218
Willy Tarreau60466522010-01-18 19:08:45 +01006219 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006220 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006221
Willy Tarreau60466522010-01-18 19:08:45 +01006222 /* Some keep-alive responses are converted to Server-close if
6223 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006224 */
Willy Tarreau60466522010-01-18 19:08:45 +01006225 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6226 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006227 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006228 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006229 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006230 }
6231
Willy Tarreau7959a552013-09-23 16:44:27 +02006232 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006233 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006234
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006235 /* end of job, return OK */
6236 rep->analysers &= ~an_bit;
6237 rep->analyse_exp = TICK_ETERNITY;
6238 channel_auto_close(rep);
6239 return 1;
6240
6241 abort_keep_alive:
6242 /* A keep-alive request to the server failed on a network error.
6243 * The client is required to retry. We need to close without returning
6244 * any other information so that the client retries.
6245 */
6246 txn->status = 0;
6247 rep->analysers = 0;
6248 s->req->analysers = 0;
6249 channel_auto_close(rep);
6250 s->logs.logwait = 0;
6251 s->logs.level = 0;
6252 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
6253 bi_erase(rep);
6254 stream_int_retnclose(rep->cons, NULL);
6255 return 0;
6256}
6257
6258/* This function performs all the processing enabled for the current response.
6259 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6260 * and updates s->rep->analysers. It might make sense to explode it into several
6261 * other functions. It works like process_request (see indications above).
6262 */
6263int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6264{
6265 struct http_txn *txn = &s->txn;
6266 struct http_msg *msg = &txn->rsp;
6267 struct proxy *cur_proxy;
6268 struct cond_wordlist *wl;
6269 struct http_res_rule *http_res_last_rule = NULL;
6270
6271 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6272 now_ms, __FUNCTION__,
6273 s,
6274 rep,
6275 rep->rex, rep->wex,
6276 rep->flags,
6277 rep->buf->i,
6278 rep->analysers);
6279
6280 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6281 return 0;
6282
6283 rep->analysers &= ~an_bit;
6284 rep->analyse_exp = TICK_ETERNITY;
6285
Willy Tarreau70730dd2014-04-24 18:06:27 +02006286 /* The stats applet needs to adjust the Connection header but we don't
6287 * apply any filter there.
6288 */
6289 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6290 goto skip_filters;
6291
Willy Tarreau58975672014-04-24 21:13:57 +02006292 /*
6293 * We will have to evaluate the filters.
6294 * As opposed to version 1.2, now they will be evaluated in the
6295 * filters order and not in the header order. This means that
6296 * each filter has to be validated among all headers.
6297 *
6298 * Filters are tried with ->be first, then with ->fe if it is
6299 * different from ->be.
6300 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006301
Willy Tarreau58975672014-04-24 21:13:57 +02006302 cur_proxy = s->be;
6303 while (1) {
6304 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006305
Willy Tarreau58975672014-04-24 21:13:57 +02006306 /* evaluate http-response rules */
6307 if (!http_res_last_rule)
6308 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 +02006309
Willy Tarreau58975672014-04-24 21:13:57 +02006310 /* try headers filters */
6311 if (rule_set->rsp_exp != NULL) {
6312 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6313 return_bad_resp:
6314 if (objt_server(s->target)) {
6315 objt_server(s->target)->counters.failed_resp++;
6316 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006317 }
Willy Tarreau58975672014-04-24 21:13:57 +02006318 s->be->be_counters.failed_resp++;
6319 return_srv_prx_502:
6320 rep->analysers = 0;
6321 txn->status = 502;
6322 s->logs.t_data = -1; /* was not a valid response */
6323 rep->prod->flags |= SI_FL_NOLINGER;
6324 bi_erase(rep);
6325 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6326 if (!(s->flags & SN_ERR_MASK))
6327 s->flags |= SN_ERR_PRXCOND;
6328 if (!(s->flags & SN_FINST_MASK))
6329 s->flags |= SN_FINST_H;
6330 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006331 }
Willy Tarreau58975672014-04-24 21:13:57 +02006332 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006333
Willy Tarreau58975672014-04-24 21:13:57 +02006334 /* has the response been denied ? */
6335 if (txn->flags & TX_SVDENY) {
6336 if (objt_server(s->target))
6337 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006338
Willy Tarreau58975672014-04-24 21:13:57 +02006339 s->be->be_counters.denied_resp++;
6340 s->fe->fe_counters.denied_resp++;
6341 if (s->listener->counters)
6342 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006343
Willy Tarreau58975672014-04-24 21:13:57 +02006344 goto return_srv_prx_502;
6345 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006346
Willy Tarreau58975672014-04-24 21:13:57 +02006347 /* add response headers from the rule sets in the same order */
6348 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006349 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006350 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006351 if (wl->cond) {
6352 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6353 ret = acl_pass(ret);
6354 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6355 ret = !ret;
6356 if (!ret)
6357 continue;
6358 }
6359 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6360 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006361 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006362
Willy Tarreau58975672014-04-24 21:13:57 +02006363 /* check whether we're already working on the frontend */
6364 if (cur_proxy == s->fe)
6365 break;
6366 cur_proxy = s->fe;
6367 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006368
Willy Tarreau58975672014-04-24 21:13:57 +02006369 /* OK that's all we can do for 1xx responses */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006370 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006371 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006372
Willy Tarreau58975672014-04-24 21:13:57 +02006373 /*
6374 * Now check for a server cookie.
6375 */
6376 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6377 (s->be->options & PR_O_CHK_CACHE))
6378 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006379
Willy Tarreau58975672014-04-24 21:13:57 +02006380 /*
6381 * Check for cache-control or pragma headers if required.
6382 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006383 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 +02006384 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006385
Willy Tarreau58975672014-04-24 21:13:57 +02006386 /*
6387 * Add server cookie in the response if needed
6388 */
6389 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6390 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6391 (!(s->flags & SN_DIRECT) ||
6392 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6393 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6394 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6395 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6396 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6397 !(s->flags & SN_IGNORE_PRST)) {
6398 /* the server is known, it's not the one the client requested, or the
6399 * cookie's last seen date needs to be refreshed. We have to
6400 * insert a set-cookie here, except if we want to insert only on POST
6401 * requests and this one isn't. Note that servers which don't have cookies
6402 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006403 */
Willy Tarreau58975672014-04-24 21:13:57 +02006404 if (!objt_server(s->target)->cookie) {
6405 chunk_printf(&trash,
6406 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6407 s->be->cookie_name);
6408 }
6409 else {
6410 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006411
Willy Tarreau58975672014-04-24 21:13:57 +02006412 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6413 /* emit last_date, which is mandatory */
6414 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6415 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6416 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006417
Willy Tarreau58975672014-04-24 21:13:57 +02006418 if (s->be->cookie_maxlife) {
6419 /* emit first_date, which is either the original one or
6420 * the current date.
6421 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006422 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006423 s30tob64(txn->cookie_first_date ?
6424 txn->cookie_first_date >> 2 :
6425 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006426 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006427 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006428 }
Willy Tarreau58975672014-04-24 21:13:57 +02006429 chunk_appendf(&trash, "; path=/");
6430 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006431
Willy Tarreau58975672014-04-24 21:13:57 +02006432 if (s->be->cookie_domain)
6433 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006434
Willy Tarreau58975672014-04-24 21:13:57 +02006435 if (s->be->ck_opts & PR_CK_HTTPONLY)
6436 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006437
Willy Tarreau58975672014-04-24 21:13:57 +02006438 if (s->be->ck_opts & PR_CK_SECURE)
6439 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006440
Willy Tarreau58975672014-04-24 21:13:57 +02006441 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6442 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006443
Willy Tarreau58975672014-04-24 21:13:57 +02006444 txn->flags &= ~TX_SCK_MASK;
6445 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6446 /* the server did not change, only the date was updated */
6447 txn->flags |= TX_SCK_UPDATED;
6448 else
6449 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006450
Willy Tarreau58975672014-04-24 21:13:57 +02006451 /* Here, we will tell an eventual cache on the client side that we don't
6452 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6453 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6454 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006455 */
Willy Tarreau58975672014-04-24 21:13:57 +02006456 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006457
Willy Tarreau58975672014-04-24 21:13:57 +02006458 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006459
Willy Tarreau58975672014-04-24 21:13:57 +02006460 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6461 "Cache-control: private", 22) < 0))
6462 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006463 }
Willy Tarreau58975672014-04-24 21:13:57 +02006464 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006465
Willy Tarreau58975672014-04-24 21:13:57 +02006466 /*
6467 * Check if result will be cacheable with a cookie.
6468 * We'll block the response if security checks have caught
6469 * nasty things such as a cacheable cookie.
6470 */
6471 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6472 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6473 (s->be->options & PR_O_CHK_CACHE)) {
6474 /* we're in presence of a cacheable response containing
6475 * a set-cookie header. We'll block it as requested by
6476 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006477 */
Willy Tarreau58975672014-04-24 21:13:57 +02006478 if (objt_server(s->target))
6479 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006480
Willy Tarreau58975672014-04-24 21:13:57 +02006481 s->be->be_counters.denied_resp++;
6482 s->fe->fe_counters.denied_resp++;
6483 if (s->listener->counters)
6484 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006485
Willy Tarreau58975672014-04-24 21:13:57 +02006486 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6487 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6488 send_log(s->be, LOG_ALERT,
6489 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6490 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6491 goto return_srv_prx_502;
6492 }
Willy Tarreau03945942009-12-22 16:50:27 +01006493
Willy Tarreau70730dd2014-04-24 18:06:27 +02006494 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006495 /*
6496 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6497 * If an "Upgrade" token is found, the header is left untouched in order
6498 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006499 * if anything but "Upgrade" is present in the Connection header. We don't
6500 * want to touch any 101 response either since it's switching to another
6501 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006502 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006503 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006504 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6505 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6506 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6507 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006508
Willy Tarreau58975672014-04-24 21:13:57 +02006509 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6510 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6511 /* we want a keep-alive response here. Keep-alive header
6512 * required if either side is not 1.1.
6513 */
6514 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6515 want_flags |= TX_CON_KAL_SET;
6516 }
6517 else {
6518 /* we want a close response here. Close header required if
6519 * the server is 1.1, regardless of the client.
6520 */
6521 if (msg->flags & HTTP_MSGF_VER_11)
6522 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006523 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006524
Willy Tarreau58975672014-04-24 21:13:57 +02006525 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6526 http_change_connection_header(txn, msg, want_flags);
6527 }
6528
6529 skip_header_mangling:
6530 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6531 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6532 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006533
Willy Tarreau58975672014-04-24 21:13:57 +02006534 /* if the user wants to log as soon as possible, without counting
6535 * bytes from the server, then this is the right moment. We have
6536 * to temporarily assign bytes_out to log what we currently have.
6537 */
6538 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6539 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6540 s->logs.bytes_out = txn->rsp.eoh;
6541 s->do_log(s);
6542 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006543 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006544 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006545}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006546
Willy Tarreaud98cf932009-12-27 22:54:55 +01006547/* This function is an analyser which forwards response body (including chunk
6548 * sizes if any). It is called as soon as we must forward, even if we forward
6549 * zero byte. The only situation where it must not be called is when we're in
6550 * tunnel mode and we want to forward till the close. It's used both to forward
6551 * remaining data and to resync after end of body. It expects the msg_state to
6552 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6553 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006554 *
6555 * It is capable of compressing response data both in content-length mode and
6556 * in chunked mode. The state machines follows different flows depending on
6557 * whether content-length and chunked modes are used, since there are no
6558 * trailers in content-length :
6559 *
6560 * chk-mode cl-mode
6561 * ,----- BODY -----.
6562 * / \
6563 * V size > 0 V chk-mode
6564 * .--> SIZE -------------> DATA -------------> CRLF
6565 * | | size == 0 | last byte |
6566 * | v final crlf v inspected |
6567 * | TRAILERS -----------> DONE |
6568 * | |
6569 * `----------------------------------------------'
6570 *
6571 * Compression only happens in the DATA state, and must be flushed in final
6572 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6573 * is performed at once on final states for all bytes parsed, or when leaving
6574 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006575 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006576int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006577{
6578 struct http_txn *txn = &s->txn;
6579 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006580 static struct buffer *tmpbuf = NULL;
6581 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006582 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006583
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006584 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6585 return 0;
6586
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006587 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006588 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006589 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006590 /* Output closed while we were sending data. We must abort and
6591 * wake the other side up.
6592 */
6593 msg->msg_state = HTTP_MSG_ERROR;
6594 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006595 return 1;
6596 }
6597
Willy Tarreau4fe41902010-06-07 22:27:41 +02006598 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006599 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006600
Willy Tarreau5bebcd02014-07-10 19:06:10 +02006601 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006602 /* we have msg->sov which points to the first byte of message
6603 * body, and res->buf.p still points to the beginning of the
6604 * message. We forward the headers now, as we don't need them
6605 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006606 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006607 b_adv(res->buf, msg->sov);
6608 msg->next -= msg->sov;
6609 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006610
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006611 /* The previous analysers guarantee that the state is somewhere
6612 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6613 * msg->next are always correct.
6614 */
6615 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6616 if (msg->flags & HTTP_MSGF_TE_CHNK)
6617 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6618 else
6619 msg->msg_state = HTTP_MSG_DATA;
6620 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006621 }
6622
Willy Tarreauefdf0942014-04-24 20:08:57 +02006623 if (res->to_forward) {
6624 /* We can't process the buffer's contents yet */
6625 res->flags |= CF_WAKE_WRITE;
6626 goto missing_data;
6627 }
6628
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006629 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6630 /* We need a compression buffer in the DATA state to put the
6631 * output of compressed data, and in CRLF state to let the
6632 * TRAILERS state finish the job of removing the trailing CRLF.
6633 */
6634 if (unlikely(tmpbuf == NULL)) {
6635 /* this is the first time we need the compression buffer */
6636 tmpbuf = pool_alloc2(pool2_buffer);
6637 if (tmpbuf == NULL)
6638 goto aborted_xfer; /* no memory */
6639 }
6640
6641 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006642 if (ret < 0) {
6643 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006644 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006645 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006646 compressing = 1;
6647 }
6648
Willy Tarreaud98cf932009-12-27 22:54:55 +01006649 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006650 switch (msg->msg_state - HTTP_MSG_DATA) {
6651 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006652 /* we may have some pending data starting at res->buf->p */
6653 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006654 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006655 if (ret < 0)
6656 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006657
Willy Tarreaud5a67832014-04-21 10:54:27 +02006658 if (msg->chunk_len) {
6659 /* input empty or output full */
6660 if (res->buf->i > msg->next)
6661 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006662 goto missing_data;
6663 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006664 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006665 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006666 if (msg->chunk_len > res->buf->i - msg->next) {
6667 /* output full */
6668 res->flags |= CF_WAKE_WRITE;
6669 goto missing_data;
6670 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006671 msg->next += msg->chunk_len;
6672 msg->chunk_len = 0;
6673 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006674
6675 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006676 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006677 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006678 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006679 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006680 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006681 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006682 /* fall through for HTTP_MSG_CHUNK_CRLF */
6683
6684 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6685 /* we want the CRLF after the data */
6686
6687 ret = http_skip_chunk_crlf(msg);
6688 if (ret == 0)
6689 goto missing_data;
6690 else if (ret < 0) {
6691 if (msg->err_pos >= 0)
6692 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6693 goto return_bad_res;
6694 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006695 /* we're in MSG_CHUNK_SIZE now, fall through */
6696
6697 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006698 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006699 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006700 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006701 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006702
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006703 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006704 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006705 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006706 else if (ret < 0) {
6707 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006708 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006709 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006710 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006711 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006712 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006713
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006714 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006715 if (unlikely(compressing)) {
6716 /* we need to flush output contents before syncing FSMs */
6717 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6718 compressing = 0;
6719 }
6720
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006721 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006722 if (ret == 0)
6723 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006724 else if (ret < 0) {
6725 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006726 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006727 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006728 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006729 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006730
6731 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006732 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006733 if (unlikely(compressing)) {
6734 /* we need to flush output contents before syncing FSMs */
6735 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6736 compressing = 0;
6737 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006738
Willy Tarreauc623c172014-04-18 09:53:50 +02006739 /* we may have some pending data starting at res->buf->p
6740 * such as a last chunk of data or trailers.
6741 */
6742 b_adv(res->buf, msg->next);
6743 msg->next = 0;
6744
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006745 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006746 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006747 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6748 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006749 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006750
Willy Tarreau610ecce2010-01-04 21:15:02 +01006751 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006752 /* some state changes occurred, maybe the analyser
6753 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006754 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006755 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006756 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006757 /* response errors are most likely due to
6758 * the client aborting the transfer.
6759 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006760 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006761 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006762 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006763 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006764 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006765 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006766 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006767 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006768 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006769 }
6770 }
6771
Willy Tarreaud98cf932009-12-27 22:54:55 +01006772 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006773 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006774 if (unlikely(compressing)) {
6775 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006776 compressing = 0;
6777 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006778
Willy Tarreauc623c172014-04-18 09:53:50 +02006779 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6780 b_adv(res->buf, msg->next);
6781 msg->next = 0;
6782 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6783 }
6784
Willy Tarreauf003d372012-11-26 13:35:37 +01006785 if (res->flags & CF_SHUTW)
6786 goto aborted_xfer;
6787
6788 /* stop waiting for data if the input is closed before the end. If the
6789 * client side was already closed, it means that the client has aborted,
6790 * so we don't want to count this as a server abort. Otherwise it's a
6791 * server abort.
6792 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006793 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006794 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006795 goto aborted_xfer;
Christopher Fauletbe801672015-06-19 09:00:58 +02006796 /* If we have some pending data, we continue the processing */
6797 if (!buffer_pending(res->buf)) {
6798 if (!(s->flags & SN_ERR_MASK))
6799 s->flags |= SN_ERR_SRVCL;
6800 s->be->be_counters.srv_aborts++;
6801 if (objt_server(s->target))
6802 objt_server(s->target)->counters.srv_aborts++;
6803 goto return_bad_res_stats_ok;
6804 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006805 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006806
Willy Tarreau40dba092010-03-04 18:14:51 +01006807 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006808 if (!s->req->analysers)
6809 goto return_bad_res;
6810
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006811 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006812 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006813 * Similarly, with keep-alive on the client side, we don't want to forward a
6814 * close.
6815 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006816 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006817 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6818 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006819 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006820
Willy Tarreau5c620922011-05-11 19:56:11 +02006821 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006822 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006823 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006824 * modes are already handled by the stream sock layer. We must not do
6825 * this in content-length mode because it could present the MSG_MORE
6826 * flag with the last block of forwarded data, which would cause an
6827 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006828 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006829 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006830 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006831
Willy Tarreaud98cf932009-12-27 22:54:55 +01006832 /* the session handler will take care of timeouts and errors */
6833 return 0;
6834
Willy Tarreau40dba092010-03-04 18:14:51 +01006835 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006836 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006837 if (objt_server(s->target))
6838 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006839
6840 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006841 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006842 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006843 compressing = 0;
6844 }
6845
Willy Tarreauc623c172014-04-18 09:53:50 +02006846 /* we may have some pending data starting at res->buf->p */
6847 if (s->comp_algo == NULL) {
6848 b_adv(res->buf, msg->next);
6849 msg->next = 0;
6850 }
6851
Willy Tarreaud98cf932009-12-27 22:54:55 +01006852 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006853 /* don't send any error message as we're in the body */
6854 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006855 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006856 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006857 if (objt_server(s->target))
6858 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006859
6860 if (!(s->flags & SN_ERR_MASK))
6861 s->flags |= SN_ERR_PRXCOND;
6862 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006863 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006864 return 0;
6865
6866 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006867 if (unlikely(compressing)) {
6868 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6869 compressing = 0;
6870 }
6871
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006872 txn->rsp.msg_state = HTTP_MSG_ERROR;
6873 /* don't send any error message as we're in the body */
6874 stream_int_retnclose(res->cons, NULL);
6875 res->analysers = 0;
6876 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6877
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006878 s->fe->fe_counters.cli_aborts++;
6879 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006880 if (objt_server(s->target))
6881 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006882
6883 if (!(s->flags & SN_ERR_MASK))
6884 s->flags |= SN_ERR_CLICL;
6885 if (!(s->flags & SN_FINST_MASK))
6886 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006887 return 0;
6888}
6889
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006890/* Iterate the same filter through all request headers.
6891 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006892 * Since it can manage the switch to another backend, it updates the per-proxy
6893 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006894 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006895int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006896{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006897 char *cur_ptr, *cur_end, *cur_next;
6898 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006899 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006900 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006901 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006902
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006903 last_hdr = 0;
6904
Willy Tarreau9b28e032012-10-12 23:49:43 +02006905 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006906 old_idx = 0;
6907
6908 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006909 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006910 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006911 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006912 (exp->action == ACT_ALLOW ||
6913 exp->action == ACT_DENY ||
6914 exp->action == ACT_TARPIT))
6915 return 0;
6916
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006917 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006918 if (!cur_idx)
6919 break;
6920
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006921 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006922 cur_ptr = cur_next;
6923 cur_end = cur_ptr + cur_hdr->len;
6924 cur_next = cur_end + cur_hdr->cr + 1;
6925
6926 /* Now we have one header between cur_ptr and cur_end,
6927 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006928 */
6929
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02006930 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006931 switch (exp->action) {
6932 case ACT_SETBE:
6933 /* It is not possible to jump a second time.
6934 * FIXME: should we return an HTTP/500 here so that
6935 * the admin knows there's a problem ?
6936 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006937 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006938 break;
6939
6940 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006941 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006942 last_hdr = 1;
6943 break;
6944
6945 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006946 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006947 last_hdr = 1;
6948 break;
6949
6950 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006951 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006952 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006953 break;
6954
6955 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006956 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006957 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006958 break;
6959
6960 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006961 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6962 if (trash.len < 0)
6963 return -1;
6964
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006965 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006966 /* FIXME: if the user adds a newline in the replacement, the
6967 * index will not be recalculated for now, and the new line
6968 * will not be counted as a new header.
6969 */
6970
6971 cur_end += delta;
6972 cur_next += delta;
6973 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006974 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006975 break;
6976
6977 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006978 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006979 cur_next += delta;
6980
Willy Tarreaufa355d42009-11-29 18:12:29 +01006981 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006982 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6983 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006984 cur_hdr->len = 0;
6985 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006986 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006987 break;
6988
6989 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006990 }
6991
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006992 /* keep the link from this header to next one in case of later
6993 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006994 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006995 old_idx = cur_idx;
6996 }
6997 return 0;
6998}
6999
7000
7001/* Apply the filter to the request line.
7002 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7003 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007004 * Since it can manage the switch to another backend, it updates the per-proxy
7005 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007006 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007007int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007008{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007009 char *cur_ptr, *cur_end;
7010 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007011 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007012 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007013
Willy Tarreau3d300592007-03-18 18:34:41 +01007014 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007015 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007016 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007017 (exp->action == ACT_ALLOW ||
7018 exp->action == ACT_DENY ||
7019 exp->action == ACT_TARPIT))
7020 return 0;
7021 else if (exp->action == ACT_REMOVE)
7022 return 0;
7023
7024 done = 0;
7025
Willy Tarreau9b28e032012-10-12 23:49:43 +02007026 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007027 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007028
7029 /* Now we have the request line between cur_ptr and cur_end */
7030
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007031 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007032 switch (exp->action) {
7033 case ACT_SETBE:
7034 /* It is not possible to jump a second time.
7035 * FIXME: should we return an HTTP/500 here so that
7036 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007037 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007038 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007039 break;
7040
7041 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007042 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007043 done = 1;
7044 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007045
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007046 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007047 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007048 done = 1;
7049 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007050
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007051 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007052 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007053 done = 1;
7054 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007055
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007056 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007057 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007058 done = 1;
7059 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007060
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007061 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007062 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7063 if (trash.len < 0)
7064 return -1;
7065
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007066 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007067 /* FIXME: if the user adds a newline in the replacement, the
7068 * index will not be recalculated for now, and the new line
7069 * will not be counted as a new header.
7070 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007071
Willy Tarreaufa355d42009-11-29 18:12:29 +01007072 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007073 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007074 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007075 HTTP_MSG_RQMETH,
7076 cur_ptr, cur_end + 1,
7077 NULL, NULL);
7078 if (unlikely(!cur_end))
7079 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007080
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007081 /* we have a full request and we know that we have either a CR
7082 * or an LF at <ptr>.
7083 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007084 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7085 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007086 /* there is no point trying this regex on headers */
7087 return 1;
7088 }
7089 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007090 return done;
7091}
Willy Tarreau97de6242006-12-27 17:18:38 +01007092
Willy Tarreau58f10d72006-12-04 02:26:12 +01007093
Willy Tarreau58f10d72006-12-04 02:26:12 +01007094
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007095/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01007096 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007097 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007098 * unparsable request. Since it can manage the switch to another backend, it
7099 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007100 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007101int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007102{
Willy Tarreau6c123b12010-01-28 20:22:06 +01007103 struct http_txn *txn = &s->txn;
7104 struct hdr_exp *exp;
7105
7106 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007107 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007108
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007109 /*
7110 * The interleaving of transformations and verdicts
7111 * makes it difficult to decide to continue or stop
7112 * the evaluation.
7113 */
7114
Willy Tarreau6c123b12010-01-28 20:22:06 +01007115 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7116 break;
7117
Willy Tarreau3d300592007-03-18 18:34:41 +01007118 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007119 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007120 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007121 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007122
7123 /* if this filter had a condition, evaluate it now and skip to
7124 * next filter if the condition does not match.
7125 */
7126 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007127 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007128 ret = acl_pass(ret);
7129 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7130 ret = !ret;
7131
7132 if (!ret)
7133 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007134 }
7135
7136 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007137 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007138 if (unlikely(ret < 0))
7139 return -1;
7140
7141 if (likely(ret == 0)) {
7142 /* The filter did not match the request, it can be
7143 * iterated through all headers.
7144 */
Willy Tarreaua256fae2015-01-30 20:58:58 +01007145 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7146 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007147 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007148 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007149 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007150}
7151
7152
Willy Tarreaua15645d2007-03-18 16:22:39 +01007153
Willy Tarreau58f10d72006-12-04 02:26:12 +01007154/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007155 * Try to retrieve the server associated to the appsession.
7156 * If the server is found, it's assigned to the session.
7157 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007158void manage_client_side_appsession(struct session *s, const char *buf, int len) {
7159 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007160 appsess *asession = NULL;
7161 char *sessid_temp = NULL;
7162
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007163 if (len > s->be->appsession_len) {
7164 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007165 }
7166
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007167 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007168 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007169 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007170 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007171 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007172 }
7173
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007174 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007175 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007176 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007177 return;
7178 }
7179
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007180 memcpy(txn->sessid, buf, len);
7181 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007182 }
7183
7184 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7185 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007186 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007187 return;
7188 }
7189
Cyril Bontéb21570a2009-11-29 20:04:48 +01007190 memcpy(sessid_temp, buf, len);
7191 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007192
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007193 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007194 /* free previously allocated memory */
7195 pool_free2(apools.sessid, sessid_temp);
7196
7197 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007198 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7199 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007200 asession->request_count++;
7201
7202 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007203 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007204
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007205 while (srv) {
7206 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007207 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007208 (s->be->options & PR_O_PERSIST) ||
7209 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007210 /* we found the server and it's usable */
7211 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007212 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007213 s->flags |= SN_DIRECT | SN_ASSIGNED;
7214 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007215
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007216 break;
7217 } else {
7218 txn->flags &= ~TX_CK_MASK;
7219 txn->flags |= TX_CK_DOWN;
7220 }
7221 }
7222 srv = srv->next;
7223 }
7224 }
7225 }
7226}
7227
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007228/* Find the end of a cookie value contained between <s> and <e>. It works the
7229 * same way as with headers above except that the semi-colon also ends a token.
7230 * See RFC2965 for more information. Note that it requires a valid header to
7231 * return a valid result.
7232 */
7233char *find_cookie_value_end(char *s, const char *e)
7234{
7235 int quoted, qdpair;
7236
7237 quoted = qdpair = 0;
7238 for (; s < e; s++) {
7239 if (qdpair) qdpair = 0;
7240 else if (quoted) {
7241 if (*s == '\\') qdpair = 1;
7242 else if (*s == '"') quoted = 0;
7243 }
7244 else if (*s == '"') quoted = 1;
7245 else if (*s == ',' || *s == ';') return s;
7246 }
7247 return s;
7248}
7249
7250/* Delete a value in a header between delimiters <from> and <next> in buffer
7251 * <buf>. The number of characters displaced is returned, and the pointer to
7252 * the first delimiter is updated if required. The function tries as much as
7253 * possible to respect the following principles :
7254 * - replace <from> delimiter by the <next> one unless <from> points to a
7255 * colon, in which case <next> is simply removed
7256 * - set exactly one space character after the new first delimiter, unless
7257 * there are not enough characters in the block being moved to do so.
7258 * - remove unneeded spaces before the previous delimiter and after the new
7259 * one.
7260 *
7261 * It is the caller's responsibility to ensure that :
7262 * - <from> points to a valid delimiter or the colon ;
7263 * - <next> points to a valid delimiter or the final CR/LF ;
7264 * - there are non-space chars before <from> ;
7265 * - there is a CR/LF at or after <next>.
7266 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007267int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007268{
7269 char *prev = *from;
7270
7271 if (*prev == ':') {
7272 /* We're removing the first value, preserve the colon and add a
7273 * space if possible.
7274 */
7275 if (!http_is_crlf[(unsigned char)*next])
7276 next++;
7277 prev++;
7278 if (prev < next)
7279 *prev++ = ' ';
7280
7281 while (http_is_spht[(unsigned char)*next])
7282 next++;
7283 } else {
7284 /* Remove useless spaces before the old delimiter. */
7285 while (http_is_spht[(unsigned char)*(prev-1)])
7286 prev--;
7287 *from = prev;
7288
7289 /* copy the delimiter and if possible a space if we're
7290 * not at the end of the line.
7291 */
7292 if (!http_is_crlf[(unsigned char)*next]) {
7293 *prev++ = *next++;
7294 if (prev + 1 < next)
7295 *prev++ = ' ';
7296 while (http_is_spht[(unsigned char)*next])
7297 next++;
7298 }
7299 }
7300 return buffer_replace2(buf, prev, next, NULL, 0);
7301}
7302
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007303/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007304 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007305 * desirable to call it only when needed. This code is quite complex because
7306 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7307 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007308 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007309void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007310{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007311 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007312 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007313 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007314 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7315 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007316
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007317 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007318 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007319 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007320
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007321 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007322 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007323 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007324
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007325 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007326 hdr_beg = hdr_next;
7327 hdr_end = hdr_beg + cur_hdr->len;
7328 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007329
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007330 /* We have one full header between hdr_beg and hdr_end, and the
7331 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007332 * "Cookie:" headers.
7333 */
7334
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007335 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007336 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007337 old_idx = cur_idx;
7338 continue;
7339 }
7340
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007341 del_from = NULL; /* nothing to be deleted */
7342 preserve_hdr = 0; /* assume we may kill the whole header */
7343
Willy Tarreau58f10d72006-12-04 02:26:12 +01007344 /* Now look for cookies. Conforming to RFC2109, we have to support
7345 * attributes whose name begin with a '$', and associate them with
7346 * the right cookie, if we want to delete this cookie.
7347 * So there are 3 cases for each cookie read :
7348 * 1) it's a special attribute, beginning with a '$' : ignore it.
7349 * 2) it's a server id cookie that we *MAY* want to delete : save
7350 * some pointers on it (last semi-colon, beginning of cookie...)
7351 * 3) it's an application cookie : we *MAY* have to delete a previous
7352 * "special" cookie.
7353 * At the end of loop, if a "special" cookie remains, we may have to
7354 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007355 * *MUST* delete it.
7356 *
7357 * Note: RFC2965 is unclear about the processing of spaces around
7358 * the equal sign in the ATTR=VALUE form. A careful inspection of
7359 * the RFC explicitly allows spaces before it, and not within the
7360 * tokens (attrs or values). An inspection of RFC2109 allows that
7361 * too but section 10.1.3 lets one think that spaces may be allowed
7362 * after the equal sign too, resulting in some (rare) buggy
7363 * implementations trying to do that. So let's do what servers do.
7364 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7365 * allowed quoted strings in values, with any possible character
7366 * after a backslash, including control chars and delimitors, which
7367 * causes parsing to become ambiguous. Browsers also allow spaces
7368 * within values even without quotes.
7369 *
7370 * We have to keep multiple pointers in order to support cookie
7371 * removal at the beginning, middle or end of header without
7372 * corrupting the header. All of these headers are valid :
7373 *
7374 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7375 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7376 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7377 * | | | | | | | | |
7378 * | | | | | | | | hdr_end <--+
7379 * | | | | | | | +--> next
7380 * | | | | | | +----> val_end
7381 * | | | | | +-----------> val_beg
7382 * | | | | +--------------> equal
7383 * | | | +----------------> att_end
7384 * | | +---------------------> att_beg
7385 * | +--------------------------> prev
7386 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007387 */
7388
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007389 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7390 /* Iterate through all cookies on this line */
7391
7392 /* find att_beg */
7393 att_beg = prev + 1;
7394 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7395 att_beg++;
7396
7397 /* find att_end : this is the first character after the last non
7398 * space before the equal. It may be equal to hdr_end.
7399 */
7400 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007401
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007402 while (equal < hdr_end) {
7403 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007404 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007405 if (http_is_spht[(unsigned char)*equal++])
7406 continue;
7407 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007408 }
7409
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007410 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7411 * is between <att_beg> and <equal>, both may be identical.
7412 */
7413
7414 /* look for end of cookie if there is an equal sign */
7415 if (equal < hdr_end && *equal == '=') {
7416 /* look for the beginning of the value */
7417 val_beg = equal + 1;
7418 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7419 val_beg++;
7420
7421 /* find the end of the value, respecting quotes */
7422 next = find_cookie_value_end(val_beg, hdr_end);
7423
7424 /* make val_end point to the first white space or delimitor after the value */
7425 val_end = next;
7426 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7427 val_end--;
7428 } else {
7429 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007430 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007431
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007432 /* We have nothing to do with attributes beginning with '$'. However,
7433 * they will automatically be removed if a header before them is removed,
7434 * since they're supposed to be linked together.
7435 */
7436 if (*att_beg == '$')
7437 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007438
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007439 /* Ignore cookies with no equal sign */
7440 if (equal == next) {
7441 /* This is not our cookie, so we must preserve it. But if we already
7442 * scheduled another cookie for removal, we cannot remove the
7443 * complete header, but we can remove the previous block itself.
7444 */
7445 preserve_hdr = 1;
7446 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007447 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007448 val_end += delta;
7449 next += delta;
7450 hdr_end += delta;
7451 hdr_next += delta;
7452 cur_hdr->len += delta;
7453 http_msg_move_end(&txn->req, delta);
7454 prev = del_from;
7455 del_from = NULL;
7456 }
7457 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007458 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007459
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007460 /* if there are spaces around the equal sign, we need to
7461 * strip them otherwise we'll get trouble for cookie captures,
7462 * or even for rewrites. Since this happens extremely rarely,
7463 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007464 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007465 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7466 int stripped_before = 0;
7467 int stripped_after = 0;
7468
7469 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007470 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007471 equal += stripped_before;
7472 val_beg += stripped_before;
7473 }
7474
7475 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007476 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007477 val_beg += stripped_after;
7478 stripped_before += stripped_after;
7479 }
7480
7481 val_end += stripped_before;
7482 next += stripped_before;
7483 hdr_end += stripped_before;
7484 hdr_next += stripped_before;
7485 cur_hdr->len += stripped_before;
7486 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007487 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007488 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007489
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007490 /* First, let's see if we want to capture this cookie. We check
7491 * that we don't already have a client side cookie, because we
7492 * can only capture one. Also as an optimisation, we ignore
7493 * cookies shorter than the declared name.
7494 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007495 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7496 (val_end - att_beg >= s->fe->capture_namelen) &&
7497 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007498 int log_len = val_end - att_beg;
7499
7500 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7501 Alert("HTTP logging : out of memory.\n");
7502 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007503 if (log_len > s->fe->capture_len)
7504 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007505 memcpy(txn->cli_cookie, att_beg, log_len);
7506 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007507 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007508 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007509
Willy Tarreaubca99692010-10-06 19:25:55 +02007510 /* Persistence cookies in passive, rewrite or insert mode have the
7511 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007512 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007513 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007514 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007515 * For cookies in prefix mode, the form is :
7516 *
7517 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007518 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007519 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7520 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7521 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007522 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007523
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007524 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7525 * have the server ID between val_beg and delim, and the original cookie between
7526 * delim+1 and val_end. Otherwise, delim==val_end :
7527 *
7528 * Cookie: NAME=SRV; # in all but prefix modes
7529 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7530 * | || || | |+-> next
7531 * | || || | +--> val_end
7532 * | || || +---------> delim
7533 * | || |+------------> val_beg
7534 * | || +-------------> att_end = equal
7535 * | |+-----------------> att_beg
7536 * | +------------------> prev
7537 * +-------------------------> hdr_beg
7538 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007539
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007540 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007541 for (delim = val_beg; delim < val_end; delim++)
7542 if (*delim == COOKIE_DELIM)
7543 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007544 } else {
7545 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007546 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007547 /* Now check if the cookie contains a date field, which would
7548 * appear after a vertical bar ('|') just after the server name
7549 * and before the delimiter.
7550 */
7551 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7552 if (vbar1) {
7553 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007554 * right is the last seen date. It is a base64 encoded
7555 * 30-bit value representing the UNIX date since the
7556 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007557 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007558 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007559 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007560 if (val_end - vbar1 >= 5) {
7561 val = b64tos30(vbar1);
7562 if (val > 0)
7563 txn->cookie_last_date = val << 2;
7564 }
7565 /* look for a second vertical bar */
7566 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7567 if (vbar1 && (val_end - vbar1 > 5)) {
7568 val = b64tos30(vbar1 + 1);
7569 if (val > 0)
7570 txn->cookie_first_date = val << 2;
7571 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007572 }
7573 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007574
Willy Tarreauf64d1412010-10-07 20:06:11 +02007575 /* if the cookie has an expiration date and the proxy wants to check
7576 * it, then we do that now. We first check if the cookie is too old,
7577 * then only if it has expired. We detect strict overflow because the
7578 * time resolution here is not great (4 seconds). Cookies with dates
7579 * in the future are ignored if their offset is beyond one day. This
7580 * allows an admin to fix timezone issues without expiring everyone
7581 * and at the same time avoids keeping unwanted side effects for too
7582 * long.
7583 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007584 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7585 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007586 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007587 txn->flags &= ~TX_CK_MASK;
7588 txn->flags |= TX_CK_OLD;
7589 delim = val_beg; // let's pretend we have not found the cookie
7590 txn->cookie_first_date = 0;
7591 txn->cookie_last_date = 0;
7592 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007593 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7594 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007595 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007596 txn->flags &= ~TX_CK_MASK;
7597 txn->flags |= TX_CK_EXPIRED;
7598 delim = val_beg; // let's pretend we have not found the cookie
7599 txn->cookie_first_date = 0;
7600 txn->cookie_last_date = 0;
7601 }
7602
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007603 /* Here, we'll look for the first running server which supports the cookie.
7604 * This allows to share a same cookie between several servers, for example
7605 * to dedicate backup servers to specific servers only.
7606 * However, to prevent clients from sticking to cookie-less backup server
7607 * when they have incidentely learned an empty cookie, we simply ignore
7608 * empty cookies and mark them as invalid.
7609 * The same behaviour is applied when persistence must be ignored.
7610 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007611 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007612 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007613
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007614 while (srv) {
7615 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7616 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007617 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007618 (s->be->options & PR_O_PERSIST) ||
7619 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007620 /* we found the server and we can use it */
7621 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007622 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007623 s->flags |= SN_DIRECT | SN_ASSIGNED;
7624 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007625 break;
7626 } else {
7627 /* we found a server, but it's down,
7628 * mark it as such and go on in case
7629 * another one is available.
7630 */
7631 txn->flags &= ~TX_CK_MASK;
7632 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007633 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007634 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007635 srv = srv->next;
7636 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007637
Willy Tarreauf64d1412010-10-07 20:06:11 +02007638 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007639 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007640 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007641 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007642 txn->flags |= TX_CK_UNUSED;
7643 else
7644 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007645 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007646
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007647 /* depending on the cookie mode, we may have to either :
7648 * - delete the complete cookie if we're in insert+indirect mode, so that
7649 * the server never sees it ;
7650 * - remove the server id from the cookie value, and tag the cookie as an
7651 * application cookie so that it does not get accidentely removed later,
7652 * if we're in cookie prefix mode
7653 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007654 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007655 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007656
Willy Tarreau9b28e032012-10-12 23:49:43 +02007657 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007658 val_end += delta;
7659 next += delta;
7660 hdr_end += delta;
7661 hdr_next += delta;
7662 cur_hdr->len += delta;
7663 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007664
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007665 del_from = NULL;
7666 preserve_hdr = 1; /* we want to keep this cookie */
7667 }
7668 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007669 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007670 del_from = prev;
7671 }
7672 } else {
7673 /* This is not our cookie, so we must preserve it. But if we already
7674 * scheduled another cookie for removal, we cannot remove the
7675 * complete header, but we can remove the previous block itself.
7676 */
7677 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007678
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007679 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007680 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007681 if (att_beg >= del_from)
7682 att_beg += delta;
7683 if (att_end >= del_from)
7684 att_end += delta;
7685 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007686 val_end += delta;
7687 next += delta;
7688 hdr_end += delta;
7689 hdr_next += delta;
7690 cur_hdr->len += delta;
7691 http_msg_move_end(&txn->req, delta);
7692 prev = del_from;
7693 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007694 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007695 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007696
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007697 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007698 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007699 int cmp_len, value_len;
7700 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007701
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007702 if (s->be->options2 & PR_O2_AS_PFX) {
7703 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7704 value_begin = att_beg + s->be->appsession_name_len;
7705 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007706 } else {
7707 cmp_len = att_end - att_beg;
7708 value_begin = val_beg;
7709 value_len = val_end - val_beg;
7710 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007711
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007712 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007713 if (cmp_len == s->be->appsession_name_len &&
7714 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7715 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007716 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007717 }
7718
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007719 /* continue with next cookie on this header line */
7720 att_beg = next;
7721 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007722
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007723 /* There are no more cookies on this line.
7724 * We may still have one (or several) marked for deletion at the
7725 * end of the line. We must do this now in two ways :
7726 * - if some cookies must be preserved, we only delete from the
7727 * mark to the end of line ;
7728 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007729 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007730 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007731 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007732 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007733 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007734 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007735 cur_hdr->len += delta;
7736 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007737 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007738
7739 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007740 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7741 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007742 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007743 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007744 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007745 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007746 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007747 }
7748
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007749 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007750 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007751 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007752}
7753
7754
Willy Tarreaua15645d2007-03-18 16:22:39 +01007755/* Iterate the same filter through all response headers contained in <rtr>.
7756 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7757 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007758int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007759{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007760 char *cur_ptr, *cur_end, *cur_next;
7761 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007762 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007763 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007764 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007765
7766 last_hdr = 0;
7767
Willy Tarreau9b28e032012-10-12 23:49:43 +02007768 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007769 old_idx = 0;
7770
7771 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007772 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007773 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007774 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007775 (exp->action == ACT_ALLOW ||
7776 exp->action == ACT_DENY))
7777 return 0;
7778
7779 cur_idx = txn->hdr_idx.v[old_idx].next;
7780 if (!cur_idx)
7781 break;
7782
7783 cur_hdr = &txn->hdr_idx.v[cur_idx];
7784 cur_ptr = cur_next;
7785 cur_end = cur_ptr + cur_hdr->len;
7786 cur_next = cur_end + cur_hdr->cr + 1;
7787
7788 /* Now we have one header between cur_ptr and cur_end,
7789 * and the next header starts at cur_next.
7790 */
7791
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007792 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007793 switch (exp->action) {
7794 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007795 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007796 last_hdr = 1;
7797 break;
7798
7799 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007800 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007801 last_hdr = 1;
7802 break;
7803
7804 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007805 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7806 if (trash.len < 0)
7807 return -1;
7808
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007809 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007810 /* FIXME: if the user adds a newline in the replacement, the
7811 * index will not be recalculated for now, and the new line
7812 * will not be counted as a new header.
7813 */
7814
7815 cur_end += delta;
7816 cur_next += delta;
7817 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007818 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007819 break;
7820
7821 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007822 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007823 cur_next += delta;
7824
Willy Tarreaufa355d42009-11-29 18:12:29 +01007825 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007826 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7827 txn->hdr_idx.used--;
7828 cur_hdr->len = 0;
7829 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007830 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007831 break;
7832
7833 }
7834 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007835
7836 /* keep the link from this header to next one in case of later
7837 * removal of next header.
7838 */
7839 old_idx = cur_idx;
7840 }
7841 return 0;
7842}
7843
7844
7845/* Apply the filter to the status line in the response buffer <rtr>.
7846 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7847 * or -1 if a replacement resulted in an invalid status line.
7848 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007849int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007850{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007851 char *cur_ptr, *cur_end;
7852 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007853 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007854 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007855
7856
Willy Tarreau3d300592007-03-18 18:34:41 +01007857 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007858 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007859 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007860 (exp->action == ACT_ALLOW ||
7861 exp->action == ACT_DENY))
7862 return 0;
7863 else if (exp->action == ACT_REMOVE)
7864 return 0;
7865
7866 done = 0;
7867
Willy Tarreau9b28e032012-10-12 23:49:43 +02007868 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007869 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007870
7871 /* Now we have the status line between cur_ptr and cur_end */
7872
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007873 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007874 switch (exp->action) {
7875 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007876 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007877 done = 1;
7878 break;
7879
7880 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007881 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007882 done = 1;
7883 break;
7884
7885 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007886 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7887 if (trash.len < 0)
7888 return -1;
7889
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007890 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007891 /* FIXME: if the user adds a newline in the replacement, the
7892 * index will not be recalculated for now, and the new line
7893 * will not be counted as a new header.
7894 */
7895
Willy Tarreaufa355d42009-11-29 18:12:29 +01007896 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007897 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007898 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007899 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007900 cur_ptr, cur_end + 1,
7901 NULL, NULL);
7902 if (unlikely(!cur_end))
7903 return -1;
7904
7905 /* we have a full respnse and we know that we have either a CR
7906 * or an LF at <ptr>.
7907 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007908 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007909 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007910 /* there is no point trying this regex on headers */
7911 return 1;
7912 }
7913 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007914 return done;
7915}
7916
7917
7918
7919/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007920 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007921 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7922 * unparsable response.
7923 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007924int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007925{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007926 struct http_txn *txn = &s->txn;
7927 struct hdr_exp *exp;
7928
7929 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007930 int ret;
7931
7932 /*
7933 * The interleaving of transformations and verdicts
7934 * makes it difficult to decide to continue or stop
7935 * the evaluation.
7936 */
7937
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007938 if (txn->flags & TX_SVDENY)
7939 break;
7940
Willy Tarreau3d300592007-03-18 18:34:41 +01007941 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007942 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7943 exp->action == ACT_PASS)) {
7944 exp = exp->next;
7945 continue;
7946 }
7947
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007948 /* if this filter had a condition, evaluate it now and skip to
7949 * next filter if the condition does not match.
7950 */
7951 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007952 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007953 ret = acl_pass(ret);
7954 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7955 ret = !ret;
7956 if (!ret)
7957 continue;
7958 }
7959
Willy Tarreaua15645d2007-03-18 16:22:39 +01007960 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007961 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007962 if (unlikely(ret < 0))
7963 return -1;
7964
7965 if (likely(ret == 0)) {
7966 /* The filter did not match the response, it can be
7967 * iterated through all headers.
7968 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007969 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7970 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007971 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007972 }
7973 return 0;
7974}
7975
7976
Willy Tarreaua15645d2007-03-18 16:22:39 +01007977/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007978 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007979 * desirable to call it only when needed. This function is also used when we
7980 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007981 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007982void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007983{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007984 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007985 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007986 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007987 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007988 char *hdr_beg, *hdr_end, *hdr_next;
7989 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007990
Willy Tarreaua15645d2007-03-18 16:22:39 +01007991 /* Iterate through the headers.
7992 * we start with the start line.
7993 */
7994 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007995 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007996
7997 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7998 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007999 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008000
8001 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008002 hdr_beg = hdr_next;
8003 hdr_end = hdr_beg + cur_hdr->len;
8004 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008005
Willy Tarreau24581ba2010-08-31 22:39:35 +02008006 /* We have one full header between hdr_beg and hdr_end, and the
8007 * next header starts at hdr_next. We're only interested in
8008 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008009 */
8010
Willy Tarreau24581ba2010-08-31 22:39:35 +02008011 is_cookie2 = 0;
8012 prev = hdr_beg + 10;
8013 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008014 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008015 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8016 if (!val) {
8017 old_idx = cur_idx;
8018 continue;
8019 }
8020 is_cookie2 = 1;
8021 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008022 }
8023
Willy Tarreau24581ba2010-08-31 22:39:35 +02008024 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8025 * <prev> points to the colon.
8026 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008027 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008028
Willy Tarreau24581ba2010-08-31 22:39:35 +02008029 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8030 * check-cache is enabled) and we are not interested in checking
8031 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008032 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008033 if (s->be->cookie_name == NULL &&
8034 s->be->appsession_name == NULL &&
8035 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008036 return;
8037
Willy Tarreau24581ba2010-08-31 22:39:35 +02008038 /* OK so now we know we have to process this response cookie.
8039 * The format of the Set-Cookie header is slightly different
8040 * from the format of the Cookie header in that it does not
8041 * support the comma as a cookie delimiter (thus the header
8042 * cannot be folded) because the Expires attribute described in
8043 * the original Netscape's spec may contain an unquoted date
8044 * with a comma inside. We have to live with this because
8045 * many browsers don't support Max-Age and some browsers don't
8046 * support quoted strings. However the Set-Cookie2 header is
8047 * clean.
8048 *
8049 * We have to keep multiple pointers in order to support cookie
8050 * removal at the beginning, middle or end of header without
8051 * corrupting the header (in case of set-cookie2). A special
8052 * pointer, <scav> points to the beginning of the set-cookie-av
8053 * fields after the first semi-colon. The <next> pointer points
8054 * either to the end of line (set-cookie) or next unquoted comma
8055 * (set-cookie2). All of these headers are valid :
8056 *
8057 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8058 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8059 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8060 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8061 * | | | | | | | | | |
8062 * | | | | | | | | +-> next hdr_end <--+
8063 * | | | | | | | +------------> scav
8064 * | | | | | | +--------------> val_end
8065 * | | | | | +--------------------> val_beg
8066 * | | | | +----------------------> equal
8067 * | | | +------------------------> att_end
8068 * | | +----------------------------> att_beg
8069 * | +------------------------------> prev
8070 * +-----------------------------------------> hdr_beg
8071 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008072
Willy Tarreau24581ba2010-08-31 22:39:35 +02008073 for (; prev < hdr_end; prev = next) {
8074 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008075
Willy Tarreau24581ba2010-08-31 22:39:35 +02008076 /* find att_beg */
8077 att_beg = prev + 1;
8078 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8079 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008080
Willy Tarreau24581ba2010-08-31 22:39:35 +02008081 /* find att_end : this is the first character after the last non
8082 * space before the equal. It may be equal to hdr_end.
8083 */
8084 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008085
Willy Tarreau24581ba2010-08-31 22:39:35 +02008086 while (equal < hdr_end) {
8087 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8088 break;
8089 if (http_is_spht[(unsigned char)*equal++])
8090 continue;
8091 att_end = equal;
8092 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008093
Willy Tarreau24581ba2010-08-31 22:39:35 +02008094 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8095 * is between <att_beg> and <equal>, both may be identical.
8096 */
8097
8098 /* look for end of cookie if there is an equal sign */
8099 if (equal < hdr_end && *equal == '=') {
8100 /* look for the beginning of the value */
8101 val_beg = equal + 1;
8102 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8103 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008104
Willy Tarreau24581ba2010-08-31 22:39:35 +02008105 /* find the end of the value, respecting quotes */
8106 next = find_cookie_value_end(val_beg, hdr_end);
8107
8108 /* make val_end point to the first white space or delimitor after the value */
8109 val_end = next;
8110 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8111 val_end--;
8112 } else {
8113 /* <equal> points to next comma, semi-colon or EOL */
8114 val_beg = val_end = next = equal;
8115 }
8116
8117 if (next < hdr_end) {
8118 /* Set-Cookie2 supports multiple cookies, and <next> points to
8119 * a colon or semi-colon before the end. So skip all attr-value
8120 * pairs and look for the next comma. For Set-Cookie, since
8121 * commas are permitted in values, skip to the end.
8122 */
8123 if (is_cookie2)
8124 next = find_hdr_value_end(next, hdr_end);
8125 else
8126 next = hdr_end;
8127 }
8128
8129 /* Now everything is as on the diagram above */
8130
8131 /* Ignore cookies with no equal sign */
8132 if (equal == val_end)
8133 continue;
8134
8135 /* If there are spaces around the equal sign, we need to
8136 * strip them otherwise we'll get trouble for cookie captures,
8137 * or even for rewrites. Since this happens extremely rarely,
8138 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008139 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008140 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8141 int stripped_before = 0;
8142 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008143
Willy Tarreau24581ba2010-08-31 22:39:35 +02008144 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008145 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008146 equal += stripped_before;
8147 val_beg += stripped_before;
8148 }
8149
8150 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008151 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008152 val_beg += stripped_after;
8153 stripped_before += stripped_after;
8154 }
8155
8156 val_end += stripped_before;
8157 next += stripped_before;
8158 hdr_end += stripped_before;
8159 hdr_next += stripped_before;
8160 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008161 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008162 }
8163
8164 /* First, let's see if we want to capture this cookie. We check
8165 * that we don't already have a server side cookie, because we
8166 * can only capture one. Also as an optimisation, we ignore
8167 * cookies shorter than the declared name.
8168 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008169 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008170 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008171 (val_end - att_beg >= s->fe->capture_namelen) &&
8172 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008173 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008174 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008175 Alert("HTTP logging : out of memory.\n");
8176 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008177 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008178 if (log_len > s->fe->capture_len)
8179 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008180 memcpy(txn->srv_cookie, att_beg, log_len);
8181 txn->srv_cookie[log_len] = 0;
8182 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008183 }
8184
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008185 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008186 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008187 if (!(s->flags & SN_IGNORE_PRST) &&
8188 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8189 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008190 /* assume passive cookie by default */
8191 txn->flags &= ~TX_SCK_MASK;
8192 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008193
8194 /* If the cookie is in insert mode on a known server, we'll delete
8195 * this occurrence because we'll insert another one later.
8196 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008197 * a direct access.
8198 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008199 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008200 /* The "preserve" flag was set, we don't want to touch the
8201 * server's cookie.
8202 */
8203 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008204 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8205 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008206 /* this cookie must be deleted */
8207 if (*prev == ':' && next == hdr_end) {
8208 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008209 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008210 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8211 txn->hdr_idx.used--;
8212 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008213 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008214 hdr_next += delta;
8215 http_msg_move_end(&txn->rsp, delta);
8216 /* note: while both invalid now, <next> and <hdr_end>
8217 * are still equal, so the for() will stop as expected.
8218 */
8219 } else {
8220 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008221 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008222 next = prev;
8223 hdr_end += delta;
8224 hdr_next += delta;
8225 cur_hdr->len += delta;
8226 http_msg_move_end(&txn->rsp, delta);
8227 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008228 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008229 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008230 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008231 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008232 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008233 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008234 * with this server since we know it.
8235 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008236 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008237 next += delta;
8238 hdr_end += delta;
8239 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008240 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008241 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008242
Willy Tarreauf1348312010-10-07 15:54:11 +02008243 txn->flags &= ~TX_SCK_MASK;
8244 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008245 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008246 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008247 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008248 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008249 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008250 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008251 next += delta;
8252 hdr_end += delta;
8253 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008254 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008255 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008256
Willy Tarreau827aee92011-03-10 16:55:02 +01008257 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008258 txn->flags &= ~TX_SCK_MASK;
8259 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008260 }
8261 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008262 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008263 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008264 int cmp_len, value_len;
8265 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008266
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008267 if (s->be->options2 & PR_O2_AS_PFX) {
8268 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8269 value_begin = att_beg + s->be->appsession_name_len;
8270 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008271 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008272 cmp_len = att_end - att_beg;
8273 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008274 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008275 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008276
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008277 if ((cmp_len == s->be->appsession_name_len) &&
8278 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008279 /* free a possibly previously allocated memory */
8280 pool_free2(apools.sessid, txn->sessid);
8281
Cyril Bontéb21570a2009-11-29 20:04:48 +01008282 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008283 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008284 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008285 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008286 return;
8287 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008288 memcpy(txn->sessid, value_begin, value_len);
8289 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008290 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008291 }
8292 /* that's done for this cookie, check the next one on the same
8293 * line when next != hdr_end (only if is_cookie2).
8294 */
8295 }
8296 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008297 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008298 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008299
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008300 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008301 appsess *asession = NULL;
8302 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008303 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008304 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008305 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008306 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8307 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008308 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008309 return;
8310 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008311 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8312
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008313 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8314 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008315 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8316 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008317 return;
8318 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008319 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8320 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008321
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008322 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008323 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008324 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008325 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8326 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008327 return;
8328 }
8329 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008330 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008331
8332 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008333 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008334 }
8335
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008336 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008337 asession->request_count++;
8338 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008339}
8340
8341
Willy Tarreaua15645d2007-03-18 16:22:39 +01008342/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008343 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008344 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008345void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008346{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008347 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008348 char *p1, *p2;
8349
8350 char *cur_ptr, *cur_end, *cur_next;
8351 int cur_idx;
8352
Willy Tarreau5df51872007-11-25 16:20:08 +01008353 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008354 return;
8355
8356 /* Iterate through the headers.
8357 * we start with the start line.
8358 */
8359 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008360 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008361
8362 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8363 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008364 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008365
8366 cur_hdr = &txn->hdr_idx.v[cur_idx];
8367 cur_ptr = cur_next;
8368 cur_end = cur_ptr + cur_hdr->len;
8369 cur_next = cur_end + cur_hdr->cr + 1;
8370
8371 /* We have one full header between cur_ptr and cur_end, and the
8372 * next header starts at cur_next. We're only interested in
8373 * "Cookie:" headers.
8374 */
8375
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008376 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8377 if (val) {
8378 if ((cur_end - (cur_ptr + val) >= 8) &&
8379 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8380 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8381 return;
8382 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008383 }
8384
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008385 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8386 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008387 continue;
8388
8389 /* OK, right now we know we have a cache-control header at cur_ptr */
8390
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008391 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008392
8393 if (p1 >= cur_end) /* no more info */
8394 continue;
8395
8396 /* p1 is at the beginning of the value */
8397 p2 = p1;
8398
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008399 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008400 p2++;
8401
8402 /* we have a complete value between p1 and p2 */
8403 if (p2 < cur_end && *p2 == '=') {
8404 /* we have something of the form no-cache="set-cookie" */
8405 if ((cur_end - p1 >= 21) &&
8406 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8407 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008408 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008409 continue;
8410 }
8411
8412 /* OK, so we know that either p2 points to the end of string or to a comma */
8413 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008414 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008415 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8416 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8417 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008418 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008419 return;
8420 }
8421
8422 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008423 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008424 continue;
8425 }
8426 }
8427}
8428
8429
Willy Tarreau58f10d72006-12-04 02:26:12 +01008430/*
8431 * Try to retrieve a known appsession in the URI, then the associated server.
8432 * If the server is found, it's assigned to the session.
8433 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008434void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008435{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008436 char *end_params, *first_param, *cur_param, *next_param;
8437 char separator;
8438 int value_len;
8439
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008440 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008441
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008442 if (s->be->appsession_name == NULL ||
8443 (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 +01008444 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008445 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008446
Cyril Bontéb21570a2009-11-29 20:04:48 +01008447 first_param = NULL;
8448 switch (mode) {
8449 case PR_O2_AS_M_PP:
8450 first_param = memchr(begin, ';', len);
8451 break;
8452 case PR_O2_AS_M_QS:
8453 first_param = memchr(begin, '?', len);
8454 break;
8455 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008456
Cyril Bontéb21570a2009-11-29 20:04:48 +01008457 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008458 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008459 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008460
Cyril Bontéb21570a2009-11-29 20:04:48 +01008461 switch (mode) {
8462 case PR_O2_AS_M_PP:
8463 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8464 end_params = (char *) begin + len;
8465 }
8466 separator = ';';
8467 break;
8468 case PR_O2_AS_M_QS:
8469 end_params = (char *) begin + len;
8470 separator = '&';
8471 break;
8472 default:
8473 /* unknown mode, shouldn't happen */
8474 return;
8475 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008476
Cyril Bontéb21570a2009-11-29 20:04:48 +01008477 cur_param = next_param = end_params;
8478 while (cur_param > first_param) {
8479 cur_param--;
8480 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8481 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008482 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8483 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8484 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008485 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008486 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8487 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008488 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008489 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008490 }
8491 break;
8492 }
8493 next_param = cur_param;
8494 }
8495 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008496#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008497 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008498 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008499#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008500}
8501
Willy Tarreaub2513902006-12-17 14:52:38 +01008502/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008503 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008504 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008505 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008506 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008507 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008508 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008509 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008510 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008511int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008512{
8513 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008514 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008515 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008516
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008517 if (!uri_auth)
8518 return 0;
8519
Cyril Bonté70be45d2010-10-12 00:14:35 +02008520 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008521 return 0;
8522
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008523 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008524 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008525 return 0;
8526
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008527 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008528 return 0;
8529
Willy Tarreaub2513902006-12-17 14:52:38 +01008530 return 1;
8531}
8532
Willy Tarreau4076a152009-04-02 15:18:36 +02008533/*
8534 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008535 * By default it tries to report the error position as msg->err_pos. However if
8536 * this one is not set, it will then report msg->next, which is the last known
8537 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008538 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008539 */
8540void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008541 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008542 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008543{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008544 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008545 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008546
Willy Tarreau9b28e032012-10-12 23:49:43 +02008547 es->len = MIN(chn->buf->i, sizeof(es->buf));
8548 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008549 len1 = MIN(len1, es->len);
8550 len2 = es->len - len1; /* remaining data if buffer wraps */
8551
Willy Tarreau9b28e032012-10-12 23:49:43 +02008552 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008553 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008554 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008555
Willy Tarreau4076a152009-04-02 15:18:36 +02008556 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008557 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008558 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008559 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008560
Willy Tarreau4076a152009-04-02 15:18:36 +02008561 es->when = date; // user-visible date
8562 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008563 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008564 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008565 if (objt_conn(s->req->prod->end))
8566 es->src = __objt_conn(s->req->prod->end)->addr.from;
8567 else
8568 memset(&es->src, 0, sizeof(es->src));
8569
Willy Tarreau078272e2010-12-12 12:46:33 +01008570 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008571 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008572 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008573 es->s_flags = s->flags;
8574 es->t_flags = s->txn.flags;
8575 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008576 es->b_out = chn->buf->o;
8577 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008578 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008579 es->m_clen = msg->chunk_len;
8580 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008581}
Willy Tarreaub2513902006-12-17 14:52:38 +01008582
Willy Tarreau294c4732011-12-16 21:35:50 +01008583/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8584 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8585 * performed over the whole headers. Otherwise it must contain a valid header
8586 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8587 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8588 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8589 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008590 * -1. The value fetch stops at commas, so this function is suited for use with
8591 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008592 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008593 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008594unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008595 struct hdr_idx *idx, int occ,
8596 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008597{
Willy Tarreau294c4732011-12-16 21:35:50 +01008598 struct hdr_ctx local_ctx;
8599 char *ptr_hist[MAX_HDR_HISTORY];
8600 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008601 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008602 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008603
Willy Tarreau294c4732011-12-16 21:35:50 +01008604 if (!ctx) {
8605 local_ctx.idx = 0;
8606 ctx = &local_ctx;
8607 }
8608
Willy Tarreaubce70882009-09-07 11:51:47 +02008609 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008610 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008611 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008612 occ--;
8613 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008614 *vptr = ctx->line + ctx->val;
8615 *vlen = ctx->vlen;
8616 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008617 }
8618 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008619 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008620 }
8621
8622 /* negative occurrence, we scan all the list then walk back */
8623 if (-occ > MAX_HDR_HISTORY)
8624 return 0;
8625
Willy Tarreau294c4732011-12-16 21:35:50 +01008626 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008627 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008628 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8629 len_hist[hist_ptr] = ctx->vlen;
8630 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008631 hist_ptr = 0;
8632 found++;
8633 }
8634 if (-occ > found)
8635 return 0;
8636 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008637 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8638 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8639 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008640 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008641 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008642 if (hist_ptr >= MAX_HDR_HISTORY)
8643 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008644 *vptr = ptr_hist[hist_ptr];
8645 *vlen = len_hist[hist_ptr];
8646 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008647}
8648
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008649/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8650 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8651 * performed over the whole headers. Otherwise it must contain a valid header
8652 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8653 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8654 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8655 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8656 * -1. This function differs from http_get_hdr() in that it only returns full
8657 * line header values and does not stop at commas.
8658 * The return value is 0 if nothing was found, or non-zero otherwise.
8659 */
8660unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8661 struct hdr_idx *idx, int occ,
8662 struct hdr_ctx *ctx, char **vptr, int *vlen)
8663{
8664 struct hdr_ctx local_ctx;
8665 char *ptr_hist[MAX_HDR_HISTORY];
8666 int len_hist[MAX_HDR_HISTORY];
8667 unsigned int hist_ptr;
8668 int found;
8669
8670 if (!ctx) {
8671 local_ctx.idx = 0;
8672 ctx = &local_ctx;
8673 }
8674
8675 if (occ >= 0) {
8676 /* search from the beginning */
8677 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8678 occ--;
8679 if (occ <= 0) {
8680 *vptr = ctx->line + ctx->val;
8681 *vlen = ctx->vlen;
8682 return 1;
8683 }
8684 }
8685 return 0;
8686 }
8687
8688 /* negative occurrence, we scan all the list then walk back */
8689 if (-occ > MAX_HDR_HISTORY)
8690 return 0;
8691
8692 found = hist_ptr = 0;
8693 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8694 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8695 len_hist[hist_ptr] = ctx->vlen;
8696 if (++hist_ptr >= MAX_HDR_HISTORY)
8697 hist_ptr = 0;
8698 found++;
8699 }
8700 if (-occ > found)
8701 return 0;
Nenad Merdanovicbb656432016-03-29 13:14:30 +02008702
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008703 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovicbb656432016-03-29 13:14:30 +02008704 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8705 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8706 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008707 */
Nenad Merdanovicbb656432016-03-29 13:14:30 +02008708 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008709 if (hist_ptr >= MAX_HDR_HISTORY)
8710 hist_ptr -= MAX_HDR_HISTORY;
8711 *vptr = ptr_hist[hist_ptr];
8712 *vlen = len_hist[hist_ptr];
8713 return 1;
8714}
8715
Willy Tarreaubaaee002006-06-26 02:48:02 +02008716/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008717 * Print a debug line with a header. Always stop at the first CR or LF char,
8718 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8719 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008720 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008721void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008722{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008723 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008724 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008725 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008726 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8727 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 +02008728
8729 for (max = 0; start + max < end; max++)
8730 if (start[max] == '\r' || start[max] == '\n')
8731 break;
8732
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008733 UBOUND(max, trash.size - trash.len - 3);
8734 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8735 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008736 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008737}
8738
Willy Tarreau0937bc42009-12-22 15:03:09 +01008739/*
8740 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8741 * the required fields are properly allocated and that we only need to (re)init
8742 * them. This should be used before processing any new request.
8743 */
8744void http_init_txn(struct session *s)
8745{
8746 struct http_txn *txn = &s->txn;
8747 struct proxy *fe = s->fe;
8748
8749 txn->flags = 0;
8750 txn->status = -1;
8751
Willy Tarreauf64d1412010-10-07 20:06:11 +02008752 txn->cookie_first_date = 0;
8753 txn->cookie_last_date = 0;
8754
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008755 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008756 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008757 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008758 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008759 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008760 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008761 txn->req.chunk_len = 0LL;
8762 txn->req.body_len = 0LL;
8763 txn->rsp.chunk_len = 0LL;
8764 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008765 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8766 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008767 txn->req.chn = s->req;
8768 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008769
8770 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008771
8772 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8773 if (fe->options2 & PR_O2_REQBUG_OK)
8774 txn->req.err_pos = -1; /* let buggy requests pass */
8775
Willy Tarreau46023632010-01-07 22:51:47 +01008776 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008777 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8778
Willy Tarreau46023632010-01-07 22:51:47 +01008779 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008780 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8781
8782 if (txn->hdr_idx.v)
8783 hdr_idx_init(&txn->hdr_idx);
8784}
8785
8786/* to be used at the end of a transaction */
8787void http_end_txn(struct session *s)
8788{
8789 struct http_txn *txn = &s->txn;
8790
Willy Tarreau75195602014-03-11 15:48:55 +01008791 /* release any possible compression context */
8792 if (s->flags & SN_COMP_READY)
8793 s->comp_algo->end(&s->comp_ctx);
8794 s->comp_algo = NULL;
8795 s->flags &= ~SN_COMP_READY;
8796
Willy Tarreau0937bc42009-12-22 15:03:09 +01008797 /* these ones will have been dynamically allocated */
8798 pool_free2(pool2_requri, txn->uri);
8799 pool_free2(pool2_capture, txn->cli_cookie);
8800 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008801 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008802 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008803
William Lallemanda73203e2012-03-12 12:48:57 +01008804 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008805 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008806 txn->uri = NULL;
8807 txn->srv_cookie = NULL;
8808 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008809
8810 if (txn->req.cap) {
8811 struct cap_hdr *h;
8812 for (h = s->fe->req_cap; h; h = h->next)
8813 pool_free2(h->pool, txn->req.cap[h->index]);
8814 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8815 }
8816
8817 if (txn->rsp.cap) {
8818 struct cap_hdr *h;
8819 for (h = s->fe->rsp_cap; h; h = h->next)
8820 pool_free2(h->pool, txn->rsp.cap[h->index]);
8821 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8822 }
8823
Willy Tarreau0937bc42009-12-22 15:03:09 +01008824}
8825
8826/* to be used at the end of a transaction to prepare a new one */
8827void http_reset_txn(struct session *s)
8828{
8829 http_end_txn(s);
8830 http_init_txn(s);
8831
8832 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008833 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008834 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008835 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008836 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008837 /* re-init store persistence */
8838 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008839 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008840
Willy Tarreau0937bc42009-12-22 15:03:09 +01008841 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008842
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008843 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008844
Willy Tarreau739cfba2010-01-25 23:11:14 +01008845 /* We must trim any excess data from the response buffer, because we
8846 * may have blocked an invalid response from a server that we don't
8847 * want to accidentely forward once we disable the analysers, nor do
8848 * we want those data to come along with next response. A typical
8849 * example of such data would be from a buggy server responding to
8850 * a HEAD with some data, or sending more than the advertised
8851 * content-length.
8852 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008853 if (unlikely(s->rep->buf->i))
8854 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008855
Willy Tarreau0937bc42009-12-22 15:03:09 +01008856 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008857 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008858
Willy Tarreaud04e8582010-05-31 12:31:35 +02008859 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008860 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008861
8862 s->req->rex = TICK_ETERNITY;
8863 s->req->wex = TICK_ETERNITY;
8864 s->req->analyse_exp = TICK_ETERNITY;
8865 s->rep->rex = TICK_ETERNITY;
8866 s->rep->wex = TICK_ETERNITY;
8867 s->rep->analyse_exp = TICK_ETERNITY;
8868}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008869
Sasha Pachev218f0642014-06-16 12:05:59 -06008870void free_http_res_rules(struct list *r)
8871{
8872 struct http_res_rule *tr, *pr;
8873
8874 list_for_each_entry_safe(pr, tr, r, list) {
8875 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008876 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008877 free(pr);
8878 }
8879}
8880
8881void free_http_req_rules(struct list *r)
8882{
Willy Tarreauff011f22011-01-06 17:51:27 +01008883 struct http_req_rule *tr, *pr;
8884
8885 list_for_each_entry_safe(pr, tr, r, list) {
8886 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008887 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008888 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008889
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008890 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008891 free(pr);
8892 }
8893}
8894
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008895/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008896struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8897{
8898 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008899 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008900 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008901 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008902
8903 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8904 if (!rule) {
8905 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008906 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008907 }
8908
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008909 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008910 rule->action = HTTP_REQ_ACT_ALLOW;
8911 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008912 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008913 rule->action = HTTP_REQ_ACT_DENY;
8914 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008915 } else if (!strcmp(args[0], "tarpit")) {
8916 rule->action = HTTP_REQ_ACT_TARPIT;
8917 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008918 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008919 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008920 cur_arg = 1;
8921
8922 while(*args[cur_arg]) {
8923 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008924 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008925 cur_arg+=2;
8926 continue;
8927 } else
8928 break;
8929 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008930 } else if (!strcmp(args[0], "set-nice")) {
8931 rule->action = HTTP_REQ_ACT_SET_NICE;
8932 cur_arg = 1;
8933
8934 if (!*args[cur_arg] ||
8935 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8936 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8937 file, linenum, args[0]);
8938 goto out_err;
8939 }
8940 rule->arg.nice = atoi(args[cur_arg]);
8941 if (rule->arg.nice < -1024)
8942 rule->arg.nice = -1024;
8943 else if (rule->arg.nice > 1024)
8944 rule->arg.nice = 1024;
8945 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008946 } else if (!strcmp(args[0], "set-tos")) {
8947#ifdef IP_TOS
8948 char *err;
8949 rule->action = HTTP_REQ_ACT_SET_TOS;
8950 cur_arg = 1;
8951
8952 if (!*args[cur_arg] ||
8953 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8954 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8955 file, linenum, args[0]);
8956 goto out_err;
8957 }
8958
8959 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8960 if (err && *err != '\0') {
8961 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8962 file, linenum, err, args[0]);
8963 goto out_err;
8964 }
8965 cur_arg++;
8966#else
8967 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8968 goto out_err;
8969#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008970 } else if (!strcmp(args[0], "set-mark")) {
8971#ifdef SO_MARK
8972 char *err;
8973 rule->action = HTTP_REQ_ACT_SET_MARK;
8974 cur_arg = 1;
8975
8976 if (!*args[cur_arg] ||
8977 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8978 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8979 file, linenum, args[0]);
8980 goto out_err;
8981 }
8982
8983 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8984 if (err && *err != '\0') {
8985 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8986 file, linenum, err, args[0]);
8987 goto out_err;
8988 }
8989 cur_arg++;
8990 global.last_checks |= LSTCHK_NETADM;
8991#else
8992 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8993 goto out_err;
8994#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008995 } else if (!strcmp(args[0], "set-log-level")) {
8996 rule->action = HTTP_REQ_ACT_SET_LOGL;
8997 cur_arg = 1;
8998
8999 if (!*args[cur_arg] ||
9000 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9001 bad_log_level:
9002 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9003 file, linenum, args[0]);
9004 goto out_err;
9005 }
9006 if (strcmp(args[cur_arg], "silent") == 0)
9007 rule->arg.loglevel = -1;
9008 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9009 goto bad_log_level;
9010 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009011 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9012 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9013 cur_arg = 1;
9014
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009015 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9016 (*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 +01009017 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9018 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009019 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009020 }
9021
9022 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9023 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9024 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009025
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009026 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009027 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009028 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9029 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009030 free(proxy->conf.lfs_file);
9031 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9032 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009033 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009034 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9035 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009036 cur_arg = 1;
9037
9038 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann8f249f42014-06-24 11:10:00 +02009039 (*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 -06009040 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9041 file, linenum, args[0]);
9042 goto out_err;
9043 }
9044
9045 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9046 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9047 LIST_INIT(&rule->arg.hdr_add.fmt);
9048
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009049 error = NULL;
9050 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9051 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9052 args[cur_arg + 1], error);
9053 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009054 goto out_err;
9055 }
9056
9057 proxy->conf.args.ctx = ARGC_HRQ;
9058 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9059 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9060 file, linenum);
9061
9062 free(proxy->conf.lfs_file);
9063 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9064 proxy->conf.lfs_line = proxy->conf.args.line;
9065 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009066 } else if (strcmp(args[0], "del-header") == 0) {
9067 rule->action = HTTP_REQ_ACT_DEL_HDR;
9068 cur_arg = 1;
9069
9070 if (!*args[cur_arg] ||
9071 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9072 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9073 file, linenum, args[0]);
9074 goto out_err;
9075 }
9076
9077 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9078 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9079
9080 proxy->conf.args.ctx = ARGC_HRQ;
9081 free(proxy->conf.lfs_file);
9082 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9083 proxy->conf.lfs_line = proxy->conf.args.line;
9084 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009085 } else if (strcmp(args[0], "redirect") == 0) {
9086 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009087 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009088
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009089 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009090 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9091 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9092 goto out_err;
9093 }
9094
9095 /* this redirect rule might already contain a parsed condition which
9096 * we'll pass to the http-request rule.
9097 */
9098 rule->action = HTTP_REQ_ACT_REDIR;
9099 rule->arg.redir = redir;
9100 rule->cond = redir->cond;
9101 redir->cond = NULL;
9102 cur_arg = 2;
9103 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009104 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9105 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9106 rule->action = HTTP_REQ_ACT_ADD_ACL;
9107 /*
9108 * '+ 8' for 'add-acl('
9109 * '- 9' for 'add-acl(' + trailing ')'
9110 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009111 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009112
9113 cur_arg = 1;
9114
9115 if (!*args[cur_arg] ||
9116 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9117 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9118 file, linenum, args[0]);
9119 goto out_err;
9120 }
9121
9122 LIST_INIT(&rule->arg.map.key);
9123 proxy->conf.args.ctx = ARGC_HRQ;
9124 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9125 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9126 file, linenum);
9127 free(proxy->conf.lfs_file);
9128 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9129 proxy->conf.lfs_line = proxy->conf.args.line;
9130 cur_arg += 1;
9131 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9132 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9133 rule->action = HTTP_REQ_ACT_DEL_ACL;
9134 /*
9135 * '+ 8' for 'del-acl('
9136 * '- 9' for 'del-acl(' + trailing ')'
9137 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009138 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009139
9140 cur_arg = 1;
9141
9142 if (!*args[cur_arg] ||
9143 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9144 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9145 file, linenum, args[0]);
9146 goto out_err;
9147 }
9148
9149 LIST_INIT(&rule->arg.map.key);
9150 proxy->conf.args.ctx = ARGC_HRQ;
9151 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9152 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9153 file, linenum);
9154 free(proxy->conf.lfs_file);
9155 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9156 proxy->conf.lfs_line = proxy->conf.args.line;
9157 cur_arg += 1;
9158 } else if (strncmp(args[0], "del-map", 7) == 0) {
9159 /* http-request del-map(<reference (map name)>) <key pattern> */
9160 rule->action = HTTP_REQ_ACT_DEL_MAP;
9161 /*
9162 * '+ 8' for 'del-map('
9163 * '- 9' for 'del-map(' + trailing ')'
9164 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009165 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009166
9167 cur_arg = 1;
9168
9169 if (!*args[cur_arg] ||
9170 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9171 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9172 file, linenum, args[0]);
9173 goto out_err;
9174 }
9175
9176 LIST_INIT(&rule->arg.map.key);
9177 proxy->conf.args.ctx = ARGC_HRQ;
9178 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9179 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9180 file, linenum);
9181 free(proxy->conf.lfs_file);
9182 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9183 proxy->conf.lfs_line = proxy->conf.args.line;
9184 cur_arg += 1;
9185 } else if (strncmp(args[0], "set-map", 7) == 0) {
9186 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9187 rule->action = HTTP_REQ_ACT_SET_MAP;
9188 /*
9189 * '+ 8' for 'set-map('
9190 * '- 9' for 'set-map(' + trailing ')'
9191 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009192 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009193
9194 cur_arg = 1;
9195
9196 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9197 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9198 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9199 file, linenum, args[0]);
9200 goto out_err;
9201 }
9202
9203 LIST_INIT(&rule->arg.map.key);
9204 LIST_INIT(&rule->arg.map.value);
9205 proxy->conf.args.ctx = ARGC_HRQ;
9206
9207 /* key pattern */
9208 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9209 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9210 file, linenum);
9211
9212 /* value pattern */
9213 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9214 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9215 file, linenum);
9216 free(proxy->conf.lfs_file);
9217 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9218 proxy->conf.lfs_line = proxy->conf.args.line;
9219
9220 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009221 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9222 char *errmsg = NULL;
9223 cur_arg = 1;
9224 /* try in the module list */
9225 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9226 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9227 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9228 free(errmsg);
9229 goto out_err;
9230 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009231 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009232 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 +01009233 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009234 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009235 }
9236
9237 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9238 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009239 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009240
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009241 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9242 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9243 file, linenum, args[0], errmsg);
9244 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009245 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009246 }
9247 rule->cond = cond;
9248 }
9249 else if (*args[cur_arg]) {
9250 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9251 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9252 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009253 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009254 }
9255
9256 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009257 out_err:
9258 free(rule);
9259 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009260}
9261
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009262/* parse an "http-respose" rule */
9263struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9264{
9265 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009266 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009267 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009268 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009269
9270 rule = calloc(1, sizeof(*rule));
9271 if (!rule) {
9272 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9273 goto out_err;
9274 }
9275
9276 if (!strcmp(args[0], "allow")) {
9277 rule->action = HTTP_RES_ACT_ALLOW;
9278 cur_arg = 1;
9279 } else if (!strcmp(args[0], "deny")) {
9280 rule->action = HTTP_RES_ACT_DENY;
9281 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009282 } else if (!strcmp(args[0], "set-nice")) {
9283 rule->action = HTTP_RES_ACT_SET_NICE;
9284 cur_arg = 1;
9285
9286 if (!*args[cur_arg] ||
9287 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9288 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9289 file, linenum, args[0]);
9290 goto out_err;
9291 }
9292 rule->arg.nice = atoi(args[cur_arg]);
9293 if (rule->arg.nice < -1024)
9294 rule->arg.nice = -1024;
9295 else if (rule->arg.nice > 1024)
9296 rule->arg.nice = 1024;
9297 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009298 } else if (!strcmp(args[0], "set-tos")) {
9299#ifdef IP_TOS
9300 char *err;
9301 rule->action = HTTP_RES_ACT_SET_TOS;
9302 cur_arg = 1;
9303
9304 if (!*args[cur_arg] ||
9305 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9306 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9307 file, linenum, args[0]);
9308 goto out_err;
9309 }
9310
9311 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9312 if (err && *err != '\0') {
9313 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9314 file, linenum, err, args[0]);
9315 goto out_err;
9316 }
9317 cur_arg++;
9318#else
9319 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9320 goto out_err;
9321#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009322 } else if (!strcmp(args[0], "set-mark")) {
9323#ifdef SO_MARK
9324 char *err;
9325 rule->action = HTTP_RES_ACT_SET_MARK;
9326 cur_arg = 1;
9327
9328 if (!*args[cur_arg] ||
9329 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9330 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9331 file, linenum, args[0]);
9332 goto out_err;
9333 }
9334
9335 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9336 if (err && *err != '\0') {
9337 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9338 file, linenum, err, args[0]);
9339 goto out_err;
9340 }
9341 cur_arg++;
9342 global.last_checks |= LSTCHK_NETADM;
9343#else
9344 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9345 goto out_err;
9346#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009347 } else if (!strcmp(args[0], "set-log-level")) {
9348 rule->action = HTTP_RES_ACT_SET_LOGL;
9349 cur_arg = 1;
9350
9351 if (!*args[cur_arg] ||
9352 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9353 bad_log_level:
9354 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9355 file, linenum, args[0]);
9356 goto out_err;
9357 }
9358 if (strcmp(args[cur_arg], "silent") == 0)
9359 rule->arg.loglevel = -1;
9360 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9361 goto bad_log_level;
9362 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009363 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9364 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9365 cur_arg = 1;
9366
9367 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9368 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9369 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9370 file, linenum, args[0]);
9371 goto out_err;
9372 }
9373
9374 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9375 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9376 LIST_INIT(&rule->arg.hdr_add.fmt);
9377
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009378 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009379 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009380 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9381 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009382 free(proxy->conf.lfs_file);
9383 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9384 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009385 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009386 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009387 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009388 cur_arg = 1;
9389
9390 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmanna772b942014-08-08 17:29:06 +02009391 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9392 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009393 file, linenum, args[0]);
9394 goto out_err;
9395 }
9396
9397 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9398 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9399 LIST_INIT(&rule->arg.hdr_add.fmt);
9400
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009401 error = NULL;
9402 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9403 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9404 args[cur_arg + 1], error);
9405 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009406 goto out_err;
9407 }
9408
9409 proxy->conf.args.ctx = ARGC_HRQ;
9410 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9411 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9412 file, linenum);
9413
9414 free(proxy->conf.lfs_file);
9415 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9416 proxy->conf.lfs_line = proxy->conf.args.line;
9417 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009418 } else if (strcmp(args[0], "del-header") == 0) {
9419 rule->action = HTTP_RES_ACT_DEL_HDR;
9420 cur_arg = 1;
9421
9422 if (!*args[cur_arg] ||
9423 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9424 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9425 file, linenum, args[0]);
9426 goto out_err;
9427 }
9428
9429 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9430 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9431
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009432 proxy->conf.args.ctx = ARGC_HRS;
9433 free(proxy->conf.lfs_file);
9434 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9435 proxy->conf.lfs_line = proxy->conf.args.line;
9436 cur_arg += 1;
9437 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9438 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9439 rule->action = HTTP_RES_ACT_ADD_ACL;
9440 /*
9441 * '+ 8' for 'add-acl('
9442 * '- 9' for 'add-acl(' + trailing ')'
9443 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009444 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009445
9446 cur_arg = 1;
9447
9448 if (!*args[cur_arg] ||
9449 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9450 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9451 file, linenum, args[0]);
9452 goto out_err;
9453 }
9454
9455 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009456 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009457 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9458 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9459 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009460 free(proxy->conf.lfs_file);
9461 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9462 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009463
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009464 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009465 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9466 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9467 rule->action = HTTP_RES_ACT_DEL_ACL;
9468 /*
9469 * '+ 8' for 'del-acl('
9470 * '- 9' for 'del-acl(' + trailing ')'
9471 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009472 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009473
9474 cur_arg = 1;
9475
9476 if (!*args[cur_arg] ||
9477 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9478 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9479 file, linenum, args[0]);
9480 goto out_err;
9481 }
9482
9483 LIST_INIT(&rule->arg.map.key);
9484 proxy->conf.args.ctx = ARGC_HRS;
9485 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9486 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9487 file, linenum);
9488 free(proxy->conf.lfs_file);
9489 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9490 proxy->conf.lfs_line = proxy->conf.args.line;
9491 cur_arg += 1;
9492 } else if (strncmp(args[0], "del-map", 7) == 0) {
9493 /* http-response del-map(<reference (map name)>) <key pattern> */
9494 rule->action = HTTP_RES_ACT_DEL_MAP;
9495 /*
9496 * '+ 8' for 'del-map('
9497 * '- 9' for 'del-map(' + trailing ')'
9498 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009499 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009500
9501 cur_arg = 1;
9502
9503 if (!*args[cur_arg] ||
9504 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9505 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9506 file, linenum, args[0]);
9507 goto out_err;
9508 }
9509
9510 LIST_INIT(&rule->arg.map.key);
9511 proxy->conf.args.ctx = ARGC_HRS;
9512 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9513 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9514 file, linenum);
9515 free(proxy->conf.lfs_file);
9516 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9517 proxy->conf.lfs_line = proxy->conf.args.line;
9518 cur_arg += 1;
9519 } else if (strncmp(args[0], "set-map", 7) == 0) {
9520 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9521 rule->action = HTTP_RES_ACT_SET_MAP;
9522 /*
9523 * '+ 8' for 'set-map('
9524 * '- 9' for 'set-map(' + trailing ')'
9525 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009526 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009527
9528 cur_arg = 1;
9529
9530 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9531 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9532 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9533 file, linenum, args[0]);
9534 goto out_err;
9535 }
9536
9537 LIST_INIT(&rule->arg.map.key);
9538 LIST_INIT(&rule->arg.map.value);
9539
9540 proxy->conf.args.ctx = ARGC_HRS;
9541
9542 /* key pattern */
9543 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9544 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9545 file, linenum);
9546
9547 /* value pattern */
9548 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9549 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9550 file, linenum);
9551
9552 free(proxy->conf.lfs_file);
9553 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9554 proxy->conf.lfs_line = proxy->conf.args.line;
9555
9556 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009557 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9558 char *errmsg = NULL;
9559 cur_arg = 1;
9560 /* try in the module list */
9561 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9562 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9563 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9564 free(errmsg);
9565 goto out_err;
9566 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009567 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009568 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 +02009569 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9570 goto out_err;
9571 }
9572
9573 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9574 struct acl_cond *cond;
9575 char *errmsg = NULL;
9576
9577 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9578 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9579 file, linenum, args[0], errmsg);
9580 free(errmsg);
9581 goto out_err;
9582 }
9583 rule->cond = cond;
9584 }
9585 else if (*args[cur_arg]) {
9586 Alert("parsing [%s:%d]: 'http-response %s' expects"
9587 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9588 file, linenum, args[0], args[cur_arg]);
9589 goto out_err;
9590 }
9591
9592 return rule;
9593 out_err:
9594 free(rule);
9595 return NULL;
9596}
9597
Willy Tarreau4baae242012-12-27 12:00:31 +01009598/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009599 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9600 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009601 */
9602struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009603 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009604{
9605 struct redirect_rule *rule;
9606 int cur_arg;
9607 int type = REDIRECT_TYPE_NONE;
9608 int code = 302;
9609 const char *destination = NULL;
9610 const char *cookie = NULL;
9611 int cookie_set = 0;
9612 unsigned int flags = REDIRECT_FLAG_NONE;
9613 struct acl_cond *cond = NULL;
9614
9615 cur_arg = 0;
9616 while (*(args[cur_arg])) {
9617 if (strcmp(args[cur_arg], "location") == 0) {
9618 if (!*args[cur_arg + 1])
9619 goto missing_arg;
9620
9621 type = REDIRECT_TYPE_LOCATION;
9622 cur_arg++;
9623 destination = args[cur_arg];
9624 }
9625 else if (strcmp(args[cur_arg], "prefix") == 0) {
9626 if (!*args[cur_arg + 1])
9627 goto missing_arg;
9628
9629 type = REDIRECT_TYPE_PREFIX;
9630 cur_arg++;
9631 destination = args[cur_arg];
9632 }
9633 else if (strcmp(args[cur_arg], "scheme") == 0) {
9634 if (!*args[cur_arg + 1])
9635 goto missing_arg;
9636
9637 type = REDIRECT_TYPE_SCHEME;
9638 cur_arg++;
9639 destination = args[cur_arg];
9640 }
9641 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9642 if (!*args[cur_arg + 1])
9643 goto missing_arg;
9644
9645 cur_arg++;
9646 cookie = args[cur_arg];
9647 cookie_set = 1;
9648 }
9649 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9650 if (!*args[cur_arg + 1])
9651 goto missing_arg;
9652
9653 cur_arg++;
9654 cookie = args[cur_arg];
9655 cookie_set = 0;
9656 }
9657 else if (strcmp(args[cur_arg], "code") == 0) {
9658 if (!*args[cur_arg + 1])
9659 goto missing_arg;
9660
9661 cur_arg++;
9662 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009663 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009664 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009665 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009666 args[cur_arg - 1], args[cur_arg]);
9667 return NULL;
9668 }
9669 }
9670 else if (!strcmp(args[cur_arg],"drop-query")) {
9671 flags |= REDIRECT_FLAG_DROP_QS;
9672 }
9673 else if (!strcmp(args[cur_arg],"append-slash")) {
9674 flags |= REDIRECT_FLAG_APPEND_SLASH;
9675 }
9676 else if (strcmp(args[cur_arg], "if") == 0 ||
9677 strcmp(args[cur_arg], "unless") == 0) {
9678 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9679 if (!cond) {
9680 memprintf(errmsg, "error in condition: %s", *errmsg);
9681 return NULL;
9682 }
9683 break;
9684 }
9685 else {
9686 memprintf(errmsg,
9687 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9688 args[cur_arg]);
9689 return NULL;
9690 }
9691 cur_arg++;
9692 }
9693
9694 if (type == REDIRECT_TYPE_NONE) {
9695 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9696 return NULL;
9697 }
9698
9699 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9700 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009701 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009702
9703 if (!use_fmt) {
9704 /* old-style static redirect rule */
9705 rule->rdr_str = strdup(destination);
9706 rule->rdr_len = strlen(destination);
9707 }
9708 else {
9709 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009710
9711 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9712 * if prefix == "/", we don't want to add anything, otherwise it
9713 * makes it hard for the user to configure a self-redirection.
9714 */
Godbach543b9782014-12-18 15:44:58 +08009715 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009716 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009717 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009718 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9719 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009720 free(curproxy->conf.lfs_file);
9721 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9722 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009723 }
9724 }
9725
Willy Tarreau4baae242012-12-27 12:00:31 +01009726 if (cookie) {
9727 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9728 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9729 */
9730 rule->cookie_len = strlen(cookie);
9731 if (cookie_set) {
9732 rule->cookie_str = malloc(rule->cookie_len + 10);
9733 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9734 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9735 rule->cookie_len += 9;
9736 } else {
9737 rule->cookie_str = malloc(rule->cookie_len + 21);
9738 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9739 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9740 rule->cookie_len += 20;
9741 }
9742 }
9743 rule->type = type;
9744 rule->code = code;
9745 rule->flags = flags;
9746 LIST_INIT(&rule->list);
9747 return rule;
9748
9749 missing_arg:
9750 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9751 return NULL;
9752}
9753
Willy Tarreau8797c062007-05-07 00:55:35 +02009754/************************************************************************/
9755/* The code below is dedicated to ACL parsing and matching */
9756/************************************************************************/
9757
9758
Willy Tarreau14174bc2012-04-16 14:34:04 +02009759/* This function ensures that the prerequisites for an L7 fetch are ready,
9760 * which means that a request or response is ready. If some data is missing,
9761 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009762 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9763 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009764 *
9765 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009766 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9767 * decide whether or not an HTTP message is present ;
9768 * 0 if the requested data cannot be fetched or if it is certain that
9769 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009770 * 1 if an HTTP message is ready
9771 */
9772static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009773smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009774 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009775{
9776 struct http_txn *txn = l7;
9777 struct http_msg *msg = &txn->req;
9778
9779 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9780 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9781 */
9782
9783 if (unlikely(!s || !txn))
9784 return 0;
9785
9786 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009787 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009788
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009789 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009790 if (unlikely(!s->req))
9791 return 0;
9792
Willy Tarreauaae75e32013-03-29 12:31:49 +01009793 /* If the buffer does not leave enough free space at the end,
9794 * we must first realign it.
9795 */
9796 if (s->req->buf->p > s->req->buf->data &&
9797 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9798 buffer_slow_realign(s->req->buf);
9799
Willy Tarreau14174bc2012-04-16 14:34:04 +02009800 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009801 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009802 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009803
9804 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009805 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009806 http_msg_analyzer(msg, &txn->hdr_idx);
9807
9808 /* Still no valid request ? */
9809 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009810 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009811 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009812 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009813 }
9814 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009815 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009816 return 0;
9817 }
9818
9819 /* OK we just got a valid HTTP request. We have some minor
9820 * preparation to perform so that further checks can rely
9821 * on HTTP tests.
9822 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009823
9824 /* If the request was parsed but was too large, we must absolutely
9825 * return an error so that it is not processed. At the moment this
9826 * cannot happen, but if the parsers are to change in the future,
9827 * we want this check to be maintained.
9828 */
9829 if (unlikely(s->req->buf->i + s->req->buf->p >
9830 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9831 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009832 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009833 return 1;
9834 }
9835
Willy Tarreau9b28e032012-10-12 23:49:43 +02009836 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009837 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9838 s->flags |= SN_REDIRECTABLE;
9839
Willy Tarreau506d0502013-07-06 13:29:24 +02009840 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9841 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009842 }
9843
Willy Tarreau506d0502013-07-06 13:29:24 +02009844 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009845 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009846 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009847
9848 /* otherwise everything's ready for the request */
9849 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009850 else {
9851 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009852 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9853 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009854 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009855 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009856 }
9857
9858 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009859 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009860 return 1;
9861}
Willy Tarreau8797c062007-05-07 00:55:35 +02009862
Willy Tarreaua4ba9db2014-06-25 16:56:41 +02009863/* Note: these functinos *do* modify the sample. Even in case of success, at
9864 * least the type and uint value are modified.
9865 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009866#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009867 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 +02009868
Willy Tarreau24e32d82012-04-23 23:55:44 +02009869#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009870 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 +02009871
Willy Tarreau8797c062007-05-07 00:55:35 +02009872
9873/* 1. Check on METHOD
9874 * We use the pre-parsed method if it is known, and store its number as an
9875 * integer. If it is unknown, we use the pointer and the length.
9876 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009877static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009878{
9879 int len, meth;
9880
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009881 len = strlen(text);
9882 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009883
9884 pattern->val.i = meth;
9885 if (meth == HTTP_METH_OTHER) {
Willy Tarreau71196f32014-08-29 15:15:50 +02009886 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009887 pattern->len = len;
9888 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009889 else {
9890 pattern->ptr.str = NULL;
9891 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009892 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009893 return 1;
9894}
9895
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009896/* This function fetches the method of current HTTP request and stores
9897 * it in the global pattern struct as a chunk. There are two possibilities :
9898 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9899 * in <len> and <ptr> is NULL ;
9900 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9901 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009902 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009903 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009904static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009905smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009906 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009907{
9908 int meth;
9909 struct http_txn *txn = l7;
9910
Willy Tarreau24e32d82012-04-23 23:55:44 +02009911 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009912
Willy Tarreau8797c062007-05-07 00:55:35 +02009913 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009914 smp->type = SMP_T_METH;
9915 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009916 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009917 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9918 /* ensure the indexes are not affected */
9919 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009920 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009921 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9922 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009923 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009924 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009925 return 1;
9926}
9927
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009928/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009929static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009930{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009931 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009932 struct pattern_list *lst;
9933 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009934
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009935 list_for_each_entry(lst, &expr->patterns, list) {
9936 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009937
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009938 /* well-known method */
9939 if (pattern->val.i != HTTP_METH_OTHER) {
9940 if (smp->data.meth.meth == pattern->val.i)
9941 return pattern;
9942 else
9943 continue;
9944 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009945
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009946 /* Other method, we must compare the strings */
9947 if (pattern->len != smp->data.meth.str.len)
9948 continue;
9949
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009950 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreauc62a0c62014-08-28 20:42:57 +02009951 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
9952 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009953 return pattern;
9954 }
9955 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009956}
9957
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009958static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009959smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009960 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009961{
9962 struct http_txn *txn = l7;
9963 char *ptr;
9964 int len;
9965
Willy Tarreauc0239e02012-04-16 14:42:55 +02009966 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009967
Willy Tarreau8797c062007-05-07 00:55:35 +02009968 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009969 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009970
9971 while ((len-- > 0) && (*ptr++ != '/'));
9972 if (len <= 0)
9973 return 0;
9974
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009975 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009976 smp->data.str.str = ptr;
9977 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009978
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009979 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009980 return 1;
9981}
9982
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009983static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009984smp_fetch_stver(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.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009997 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009998
9999 while ((len-- > 0) && (*ptr++ != '/'));
10000 if (len <= 0)
10001 return 0;
10002
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010003 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010004 smp->data.str.str = ptr;
10005 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010006
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010007 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010008 return 1;
10009}
10010
10011/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010012static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010013smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010014 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +020010015{
10016 struct http_txn *txn = l7;
10017 char *ptr;
10018 int len;
10019
Willy Tarreauc0239e02012-04-16 14:42:55 +020010020 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010021
Willy Tarreauf26b2522012-12-14 08:33:14 +010010022 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10023 return 0;
10024
Willy Tarreau8797c062007-05-07 00:55:35 +020010025 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010026 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010027
Willy Tarreauf853c462012-04-23 18:53:56 +020010028 smp->type = SMP_T_UINT;
10029 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010030 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010031 return 1;
10032}
10033
10034/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010035static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010036smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010037 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +020010038{
10039 struct http_txn *txn = l7;
10040
Willy Tarreauc0239e02012-04-16 14:42:55 +020010041 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010042
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010043 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010044 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010045 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010046 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010047 return 1;
10048}
10049
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010050static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010051smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010052 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010053{
10054 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010055 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010056
Willy Tarreauc0239e02012-04-16 14:42:55 +020010057 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010058
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010059 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 +020010060 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010061 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010062
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010063 smp->type = SMP_T_IPV4;
10064 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010065 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010066 return 1;
10067}
10068
10069static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010070smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010071 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010072{
10073 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010074 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010075
Willy Tarreauc0239e02012-04-16 14:42:55 +020010076 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010077
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010078 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 +020010079 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10080 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010081
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010082 smp->type = SMP_T_UINT;
10083 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010084 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010085 return 1;
10086}
10087
Willy Tarreau185b5c42012-04-26 15:11:51 +020010088/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10089 * Accepts an optional argument of type string containing the header field name,
10090 * and an optional argument of type signed or unsigned integer to request an
10091 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010092 * headers are considered from the first one. It does not stop on commas and
10093 * returns full lines instead (useful for User-Agent or Date for example).
10094 */
10095static int
10096smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010097 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010098{
10099 struct http_txn *txn = l7;
10100 struct hdr_idx *idx = &txn->hdr_idx;
10101 struct hdr_ctx *ctx = smp->ctx.a[0];
10102 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10103 int occ = 0;
10104 const char *name_str = NULL;
10105 int name_len = 0;
10106
10107 if (!ctx) {
10108 /* first call */
10109 ctx = &static_hdr_ctx;
10110 ctx->idx = 0;
10111 smp->ctx.a[0] = ctx;
10112 }
10113
10114 if (args) {
10115 if (args[0].type != ARGT_STR)
10116 return 0;
10117 name_str = args[0].data.str.str;
10118 name_len = args[0].data.str.len;
10119
10120 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10121 occ = args[1].data.uint;
10122 }
10123
10124 CHECK_HTTP_MESSAGE_FIRST();
10125
10126 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10127 /* search for header from the beginning */
10128 ctx->idx = 0;
10129
10130 if (!occ && !(opt & SMP_OPT_ITERATE))
10131 /* no explicit occurrence and single fetch => last header by default */
10132 occ = -1;
10133
10134 if (!occ)
10135 /* prepare to report multiple occurrences for ACL fetches */
10136 smp->flags |= SMP_F_NOT_LAST;
10137
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010138 smp->type = SMP_T_STR;
10139 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010140 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10141 return 1;
10142
10143 smp->flags &= ~SMP_F_NOT_LAST;
10144 return 0;
10145}
10146
10147/* 6. Check on HTTP header count. The number of occurrences is returned.
10148 * Accepts exactly 1 argument of type string. It does not stop on commas and
10149 * returns full lines instead (useful for User-Agent or Date for example).
10150 */
10151static int
10152smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010153 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010154{
10155 struct http_txn *txn = l7;
10156 struct hdr_idx *idx = &txn->hdr_idx;
10157 struct hdr_ctx ctx;
10158 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10159 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010160 const char *name = NULL;
10161 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010162
Willy Tarreau29437342015-04-01 19:16:09 +020010163 if (args && args->type == ARGT_STR) {
10164 name = args->data.str.str;
10165 len = args->data.str.len;
10166 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010167
10168 CHECK_HTTP_MESSAGE_FIRST();
10169
10170 ctx.idx = 0;
10171 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010172 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010173 cnt++;
10174
10175 smp->type = SMP_T_UINT;
10176 smp->data.uint = cnt;
10177 smp->flags = SMP_F_VOL_HDR;
10178 return 1;
10179}
10180
10181/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10182 * Accepts an optional argument of type string containing the header field name,
10183 * and an optional argument of type signed or unsigned integer to request an
10184 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010185 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010186 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010187static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010188smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010189 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010190{
10191 struct http_txn *txn = l7;
10192 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010193 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010194 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010195 int occ = 0;
10196 const char *name_str = NULL;
10197 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010198
Willy Tarreaua890d072013-04-02 12:01:06 +020010199 if (!ctx) {
10200 /* first call */
10201 ctx = &static_hdr_ctx;
10202 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010203 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010204 }
10205
Willy Tarreau185b5c42012-04-26 15:11:51 +020010206 if (args) {
10207 if (args[0].type != ARGT_STR)
10208 return 0;
10209 name_str = args[0].data.str.str;
10210 name_len = args[0].data.str.len;
10211
10212 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10213 occ = args[1].data.uint;
10214 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010215
Willy Tarreaue333ec92012-04-16 16:26:40 +020010216 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010217
Willy Tarreau185b5c42012-04-26 15:11:51 +020010218 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010219 /* search for header from the beginning */
10220 ctx->idx = 0;
10221
Willy Tarreau185b5c42012-04-26 15:11:51 +020010222 if (!occ && !(opt & SMP_OPT_ITERATE))
10223 /* no explicit occurrence and single fetch => last header by default */
10224 occ = -1;
10225
10226 if (!occ)
10227 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010228 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010229
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010230 smp->type = SMP_T_STR;
10231 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010232 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 +020010233 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010234
Willy Tarreau37406352012-04-23 16:16:37 +020010235 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010236 return 0;
10237}
10238
Willy Tarreauc11416f2007-06-17 16:58:38 +020010239/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010240 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010241 */
10242static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010243smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010244 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010245{
10246 struct http_txn *txn = l7;
10247 struct hdr_idx *idx = &txn->hdr_idx;
10248 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010249 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010250 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010251 const char *name = NULL;
10252 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010253
Willy Tarreau29437342015-04-01 19:16:09 +020010254 if (args && args->type == ARGT_STR) {
10255 name = args->data.str.str;
10256 len = args->data.str.len;
10257 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010258
Willy Tarreaue333ec92012-04-16 16:26:40 +020010259 CHECK_HTTP_MESSAGE_FIRST();
10260
Willy Tarreau33a7e692007-06-10 19:45:56 +020010261 ctx.idx = 0;
10262 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010263 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010264 cnt++;
10265
Willy Tarreauf853c462012-04-23 18:53:56 +020010266 smp->type = SMP_T_UINT;
10267 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010268 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010269 return 1;
10270}
10271
Willy Tarreau185b5c42012-04-26 15:11:51 +020010272/* Fetch an HTTP header's integer value. The integer value is returned. It
10273 * takes a mandatory argument of type string and an optional one of type int
10274 * to designate a specific occurrence. It returns an unsigned integer, which
10275 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010276 */
10277static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010278smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010279 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010280{
Willy Tarreauef38c392013-07-22 16:29:32 +020010281 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010282
Willy Tarreauf853c462012-04-23 18:53:56 +020010283 if (ret > 0) {
10284 smp->type = SMP_T_UINT;
10285 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10286 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010287
Willy Tarreaud53e2422012-04-16 17:21:11 +020010288 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010289}
10290
Cyril Bonté69fa9922012-10-25 00:01:06 +020010291/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10292 * and an optional one of type int to designate a specific occurrence.
10293 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010294 */
10295static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010296smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010297 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +020010298{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010299 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010300
Willy Tarreauef38c392013-07-22 16:29:32 +020010301 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010302 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10303 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010304 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010305 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010306 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010307 if (smp->data.str.len < temp->size - 1) {
10308 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10309 temp->str[smp->data.str.len] = '\0';
10310 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10311 smp->type = SMP_T_IPV6;
10312 break;
10313 }
10314 }
10315 }
10316
Willy Tarreaud53e2422012-04-16 17:21:11 +020010317 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010318 if (!(smp->flags & SMP_F_NOT_LAST))
10319 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010320 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010321 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010322}
10323
Willy Tarreau737b0c12007-06-10 21:28:46 +020010324/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10325 * the first '/' after the possible hostname, and ends before the possible '?'.
10326 */
10327static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010328smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010329 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010330{
10331 struct http_txn *txn = l7;
10332 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010333
Willy Tarreauc0239e02012-04-16 14:42:55 +020010334 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010335
Willy Tarreau9b28e032012-10-12 23:49:43 +020010336 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010337 ptr = http_get_path(txn);
10338 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010339 return 0;
10340
10341 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010342 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010343 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010344
10345 while (ptr < end && *ptr != '?')
10346 ptr++;
10347
Willy Tarreauf853c462012-04-23 18:53:56 +020010348 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010349 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010350 return 1;
10351}
10352
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010353/* This produces a concatenation of the first occurrence of the Host header
10354 * followed by the path component if it begins with a slash ('/'). This means
10355 * that '*' will not be added, resulting in exactly the first Host entry.
10356 * If no Host header is found, then the path is returned as-is. The returned
10357 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010358 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010359 */
10360static int
10361smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010362 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010363{
10364 struct http_txn *txn = l7;
10365 char *ptr, *end, *beg;
10366 struct hdr_ctx ctx;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010367 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010368
10369 CHECK_HTTP_MESSAGE_FIRST();
10370
10371 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010372 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +020010373 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010374
10375 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010376 temp = get_trash_chunk();
10377 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010378 smp->type = SMP_T_STR;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010379 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010380 smp->data.str.len = ctx.vlen;
10381
10382 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010383 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010384 beg = http_get_path(txn);
10385 if (!beg)
10386 beg = end;
10387
10388 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10389
10390 if (beg < ptr && *beg == '/') {
10391 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10392 smp->data.str.len += ptr - beg;
10393 }
10394
10395 smp->flags = SMP_F_VOL_1ST;
10396 return 1;
10397}
10398
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010399/* This produces a 32-bit hash of the concatenation of the first occurrence of
10400 * the Host header followed by the path component if it begins with a slash ('/').
10401 * This means that '*' will not be added, resulting in exactly the first Host
10402 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010403 * is hashed using the path hash followed by a full avalanche hash and provides a
10404 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010405 * high-traffic sites without having to store whole paths.
10406 */
10407static int
10408smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010409 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010410{
10411 struct http_txn *txn = l7;
10412 struct hdr_ctx ctx;
10413 unsigned int hash = 0;
10414 char *ptr, *beg, *end;
10415 int len;
10416
10417 CHECK_HTTP_MESSAGE_FIRST();
10418
10419 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010420 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010421 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10422 ptr = ctx.line + ctx.val;
10423 len = ctx.vlen;
10424 while (len--)
10425 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10426 }
10427
10428 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010429 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010430 beg = http_get_path(txn);
10431 if (!beg)
10432 beg = end;
10433
10434 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10435
10436 if (beg < ptr && *beg == '/') {
10437 while (beg < ptr)
10438 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10439 }
10440 hash = full_hash(hash);
10441
10442 smp->type = SMP_T_UINT;
10443 smp->data.uint = hash;
10444 smp->flags = SMP_F_VOL_1ST;
10445 return 1;
10446}
10447
Willy Tarreau4a550602012-12-09 14:53:32 +010010448/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010449 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10450 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10451 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010452 * that in environments where IPv6 is insignificant, truncating the output to
10453 * 8 bytes would still work.
10454 */
10455static int
10456smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010457 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +010010458{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010459 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010460 struct connection *cli_conn = objt_conn(l4->si[0].end);
10461
10462 if (!cli_conn)
10463 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010464
Willy Tarreauef38c392013-07-22 16:29:32 +020010465 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +010010466 return 0;
10467
Willy Tarreau47ca5452012-12-23 20:22:19 +010010468 temp = get_trash_chunk();
Willy Tarreau0dff81c2014-07-15 21:34:06 +020010469 *(unsigned int *)temp->str = htonl(smp->data.uint);
10470 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010471
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010472 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010473 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010474 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010475 temp->len += 4;
10476 break;
10477 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010478 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010479 temp->len += 16;
10480 break;
10481 default:
10482 return 0;
10483 }
10484
10485 smp->data.str = *temp;
10486 smp->type = SMP_T_BIN;
10487 return 1;
10488}
10489
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010490static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010491smp_fetch_proto_http(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)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010493{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010494 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10495 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10496 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010497
Willy Tarreau24e32d82012-04-23 23:55:44 +020010498 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010499
Willy Tarreauf853c462012-04-23 18:53:56 +020010500 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010501 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010502 return 1;
10503}
10504
Willy Tarreau7f18e522010-10-22 20:04:13 +020010505/* return a valid test if the current request is the first one on the connection */
10506static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010507smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010508 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010509{
10510 if (!s)
10511 return 0;
10512
Willy Tarreauf853c462012-04-23 18:53:56 +020010513 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010514 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010515 return 1;
10516}
10517
Willy Tarreau34db1082012-04-19 17:16:54 +020010518/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010519static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010520smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010521 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010522{
10523
Willy Tarreau24e32d82012-04-23 23:55:44 +020010524 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010525 return 0;
10526
Willy Tarreauc0239e02012-04-16 14:42:55 +020010527 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010528
Willy Tarreauc0239e02012-04-16 14:42:55 +020010529 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010530 return 0;
10531
Willy Tarreauf853c462012-04-23 18:53:56 +020010532 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010533 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010534 return 1;
10535}
Willy Tarreau8797c062007-05-07 00:55:35 +020010536
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010537/* Accepts exactly 1 argument of type userlist */
10538static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010539smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010540 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010541{
10542
10543 if (!args || args->type != ARGT_USR)
10544 return 0;
10545
10546 CHECK_HTTP_MESSAGE_FIRST();
10547
10548 if (!get_http_auth(l4))
10549 return 0;
10550
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010551 /* if the user does not belong to the userlist or has a wrong password,
10552 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010553 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010554 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010555 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10556 return 0;
10557
10558 /* pat_match_auth() will need the user list */
10559 smp->ctx.a[0] = args->data.usr;
10560
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010561 smp->type = SMP_T_STR;
10562 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010563 smp->data.str.str = l4->txn.auth.user;
10564 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010565
10566 return 1;
10567}
10568
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010569/* Try to find the next occurrence of a cookie name in a cookie header value.
10570 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10571 * the cookie value is returned into *value and *value_l, and the function
10572 * returns a pointer to the next pointer to search from if the value was found.
10573 * Otherwise if the cookie was not found, NULL is returned and neither value
10574 * nor value_l are touched. The input <hdr> string should first point to the
10575 * header's value, and the <hdr_end> pointer must point to the first character
10576 * not part of the value. <list> must be non-zero if value may represent a list
10577 * of values (cookie headers). This makes it faster to abort parsing when no
10578 * list is expected.
10579 */
10580static char *
10581extract_cookie_value(char *hdr, const char *hdr_end,
10582 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010583 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010584{
10585 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10586 char *next;
10587
10588 /* we search at least a cookie name followed by an equal, and more
10589 * generally something like this :
10590 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10591 */
10592 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10593 /* Iterate through all cookies on this line */
10594
10595 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10596 att_beg++;
10597
10598 /* find att_end : this is the first character after the last non
10599 * space before the equal. It may be equal to hdr_end.
10600 */
10601 equal = att_end = att_beg;
10602
10603 while (equal < hdr_end) {
10604 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10605 break;
10606 if (http_is_spht[(unsigned char)*equal++])
10607 continue;
10608 att_end = equal;
10609 }
10610
10611 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10612 * is between <att_beg> and <equal>, both may be identical.
10613 */
10614
10615 /* look for end of cookie if there is an equal sign */
10616 if (equal < hdr_end && *equal == '=') {
10617 /* look for the beginning of the value */
10618 val_beg = equal + 1;
10619 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10620 val_beg++;
10621
10622 /* find the end of the value, respecting quotes */
10623 next = find_cookie_value_end(val_beg, hdr_end);
10624
10625 /* make val_end point to the first white space or delimitor after the value */
10626 val_end = next;
10627 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10628 val_end--;
10629 } else {
10630 val_beg = val_end = next = equal;
10631 }
10632
10633 /* We have nothing to do with attributes beginning with '$'. However,
10634 * they will automatically be removed if a header before them is removed,
10635 * since they're supposed to be linked together.
10636 */
10637 if (*att_beg == '$')
10638 continue;
10639
10640 /* Ignore cookies with no equal sign */
10641 if (equal == next)
10642 continue;
10643
10644 /* Now we have the cookie name between att_beg and att_end, and
10645 * its value between val_beg and val_end.
10646 */
10647
10648 if (att_end - att_beg == cookie_name_l &&
10649 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10650 /* let's return this value and indicate where to go on from */
10651 *value = val_beg;
10652 *value_l = val_end - val_beg;
10653 return next + 1;
10654 }
10655
10656 /* Set-Cookie headers only have the name in the first attr=value part */
10657 if (!list)
10658 break;
10659 }
10660
10661 return NULL;
10662}
10663
William Lallemanda43ba4e2014-01-28 18:14:25 +010010664/* Fetch a captured HTTP request header. The index is the position of
10665 * the "capture" option in the configuration file
10666 */
10667static int
10668smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10669 const struct arg *args, struct sample *smp, const char *kw)
10670{
10671 struct proxy *fe = l4->fe;
10672 struct http_txn *txn = l7;
10673 int idx;
10674
10675 if (!args || args->type != ARGT_UINT)
10676 return 0;
10677
10678 idx = args->data.uint;
10679
10680 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10681 return 0;
10682
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010683 smp->type = SMP_T_STR;
10684 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010685 smp->data.str.str = txn->req.cap[idx];
10686 smp->data.str.len = strlen(txn->req.cap[idx]);
10687
10688 return 1;
10689}
10690
10691/* Fetch a captured HTTP response header. The index is the position of
10692 * the "capture" option in the configuration file
10693 */
10694static int
10695smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10696 const struct arg *args, struct sample *smp, const char *kw)
10697{
10698 struct proxy *fe = l4->fe;
10699 struct http_txn *txn = l7;
10700 int idx;
10701
10702 if (!args || args->type != ARGT_UINT)
10703 return 0;
10704
10705 idx = args->data.uint;
10706
10707 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10708 return 0;
10709
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010710 smp->type = SMP_T_STR;
10711 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010712 smp->data.str.str = txn->rsp.cap[idx];
10713 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10714
10715 return 1;
10716}
10717
William Lallemand65ad6e12014-01-31 15:08:02 +010010718/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10719static int
10720smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10721 const struct arg *args, struct sample *smp, const char *kw)
10722{
10723 struct chunk *temp;
10724 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010725 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010726
10727 if (!txn->uri)
10728 return 0;
10729
William Lallemand96a77852014-02-05 00:30:02 +010010730 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010731
William Lallemand96a77852014-02-05 00:30:02 +010010732 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10733 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010734
William Lallemand96a77852014-02-05 00:30:02 +010010735 temp = get_trash_chunk();
10736 temp->str = txn->uri;
10737 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010738 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010739 smp->type = SMP_T_STR;
10740 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010741
10742 return 1;
10743
10744}
10745
10746/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10747static int
10748smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10749 const struct arg *args, struct sample *smp, const char *kw)
10750{
10751 struct chunk *temp;
10752 struct http_txn *txn = l7;
10753 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010754
10755 if (!txn->uri)
10756 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010757
William Lallemand65ad6e12014-01-31 15:08:02 +010010758 ptr = txn->uri;
10759
10760 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10761 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010762
William Lallemand65ad6e12014-01-31 15:08:02 +010010763 if (!*ptr)
10764 return 0;
10765
10766 ptr++; /* skip the space */
10767
10768 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010769 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010770 if (!ptr)
10771 return 0;
10772 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10773 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010774
10775 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010776 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010777 smp->type = SMP_T_STR;
10778 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010779
10780 return 1;
10781}
10782
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010783/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10784 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10785 */
10786static int
10787smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10788 const struct arg *args, struct sample *smp, const char *kw)
10789{
10790 struct http_txn *txn = l7;
10791
10792 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10793 return 0;
10794
10795 if (txn->req.flags & HTTP_MSGF_VER_11)
10796 smp->data.str.str = "HTTP/1.1";
10797 else
10798 smp->data.str.str = "HTTP/1.0";
10799
10800 smp->data.str.len = 8;
10801 smp->type = SMP_T_STR;
10802 smp->flags = SMP_F_CONST;
10803 return 1;
10804
10805}
10806
10807/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10808 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10809 */
10810static int
10811smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10812 const struct arg *args, struct sample *smp, const char *kw)
10813{
10814 struct http_txn *txn = l7;
10815
10816 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10817 return 0;
10818
10819 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10820 smp->data.str.str = "HTTP/1.1";
10821 else
10822 smp->data.str.str = "HTTP/1.0";
10823
10824 smp->data.str.len = 8;
10825 smp->type = SMP_T_STR;
10826 smp->flags = SMP_F_CONST;
10827 return 1;
10828
10829}
10830
William Lallemand65ad6e12014-01-31 15:08:02 +010010831
Willy Tarreaue333ec92012-04-16 16:26:40 +020010832/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010833 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010834 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010835 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010836 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010837 * Accepts exactly 1 argument of type string. If the input options indicate
10838 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010839 * The returned sample is of type CSTR. Can be used to parse cookies in other
10840 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010841 */
William Lallemand07c8b242014-05-02 17:11:07 +020010842int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010843 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010844{
10845 struct http_txn *txn = l7;
10846 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010847 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010848 const struct http_msg *msg;
10849 const char *hdr_name;
10850 int hdr_name_len;
10851 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010852 int occ = 0;
10853 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010854
Willy Tarreau24e32d82012-04-23 23:55:44 +020010855 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010856 return 0;
10857
Willy Tarreaua890d072013-04-02 12:01:06 +020010858 if (!ctx) {
10859 /* first call */
10860 ctx = &static_hdr_ctx;
10861 ctx->idx = 0;
10862 smp->ctx.a[2] = ctx;
10863 }
10864
Willy Tarreaue333ec92012-04-16 16:26:40 +020010865 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010866
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010867 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010868 msg = &txn->req;
10869 hdr_name = "Cookie";
10870 hdr_name_len = 6;
10871 } else {
10872 msg = &txn->rsp;
10873 hdr_name = "Set-Cookie";
10874 hdr_name_len = 10;
10875 }
10876
Willy Tarreau28376d62012-04-26 21:26:10 +020010877 if (!occ && !(opt & SMP_OPT_ITERATE))
10878 /* no explicit occurrence and single fetch => last cookie by default */
10879 occ = -1;
10880
10881 /* OK so basically here, either we want only one value and it's the
10882 * last one, or we want to iterate over all of them and we fetch the
10883 * next one.
10884 */
10885
Willy Tarreau9b28e032012-10-12 23:49:43 +020010886 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010887 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010888 /* search for the header from the beginning, we must first initialize
10889 * the search parameters.
10890 */
Willy Tarreau37406352012-04-23 16:16:37 +020010891 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010892 ctx->idx = 0;
10893 }
10894
Willy Tarreau28376d62012-04-26 21:26:10 +020010895 smp->flags |= SMP_F_VOL_HDR;
10896
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010897 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010898 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10899 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010900 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10901 goto out;
10902
Willy Tarreau24e32d82012-04-23 23:55:44 +020010903 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010904 continue;
10905
Willy Tarreau37406352012-04-23 16:16:37 +020010906 smp->ctx.a[0] = ctx->line + ctx->val;
10907 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010908 }
10909
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010910 smp->type = SMP_T_STR;
10911 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010912 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010913 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010914 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010915 &smp->data.str.str,
10916 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010917 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010918 found = 1;
10919 if (occ >= 0) {
10920 /* one value was returned into smp->data.str.{str,len} */
10921 smp->flags |= SMP_F_NOT_LAST;
10922 return 1;
10923 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010924 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010925 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010926 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010927 /* all cookie headers and values were scanned. If we're looking for the
10928 * last occurrence, we may return it now.
10929 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010930 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010931 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010932 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010933}
10934
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010935/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010936 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010937 * multiple cookies may be parsed on the same line. The returned sample is of
10938 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010939 */
10940static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010941smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010942 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010943{
10944 struct http_txn *txn = l7;
10945 struct hdr_idx *idx = &txn->hdr_idx;
10946 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010947 const struct http_msg *msg;
10948 const char *hdr_name;
10949 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010950 int cnt;
10951 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010952 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010953
Willy Tarreau24e32d82012-04-23 23:55:44 +020010954 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010955 return 0;
10956
Willy Tarreaue333ec92012-04-16 16:26:40 +020010957 CHECK_HTTP_MESSAGE_FIRST();
10958
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010959 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010960 msg = &txn->req;
10961 hdr_name = "Cookie";
10962 hdr_name_len = 6;
10963 } else {
10964 msg = &txn->rsp;
10965 hdr_name = "Set-Cookie";
10966 hdr_name_len = 10;
10967 }
10968
Willy Tarreau9b28e032012-10-12 23:49:43 +020010969 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010970 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010971 ctx.idx = 0;
10972 cnt = 0;
10973
10974 while (1) {
10975 /* Note: val_beg == NULL every time we need to fetch a new header */
10976 if (!val_beg) {
10977 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10978 break;
10979
Willy Tarreau24e32d82012-04-23 23:55:44 +020010980 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010981 continue;
10982
10983 val_beg = ctx.line + ctx.val;
10984 val_end = val_beg + ctx.vlen;
10985 }
10986
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010987 smp->type = SMP_T_STR;
10988 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010989 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010990 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010991 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010992 &smp->data.str.str,
10993 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010994 cnt++;
10995 }
10996 }
10997
Willy Tarreaub169eba2013-12-16 15:14:43 +010010998 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010999 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011000 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011001 return 1;
11002}
11003
Willy Tarreau51539362012-05-08 12:46:28 +020011004/* Fetch an cookie's integer value. The integer value is returned. It
11005 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11006 */
11007static int
11008smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011009 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020011010{
Willy Tarreauef38c392013-07-22 16:29:32 +020011011 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020011012
11013 if (ret > 0) {
11014 smp->type = SMP_T_UINT;
11015 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11016 }
11017
11018 return ret;
11019}
11020
Willy Tarreau8797c062007-05-07 00:55:35 +020011021/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011022/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011023/************************************************************************/
11024
David Cournapeau16023ee2010-12-23 20:55:41 +090011025/*
11026 * Given a path string and its length, find the position of beginning of the
11027 * query string. Returns NULL if no query string is found in the path.
11028 *
11029 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11030 *
11031 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11032 */
bedis4c75cca2012-10-05 08:38:24 +020011033static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011034{
11035 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011036
bedis4c75cca2012-10-05 08:38:24 +020011037 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011038 return p ? p + 1 : NULL;
11039}
11040
bedis4c75cca2012-10-05 08:38:24 +020011041static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011042{
bedis4c75cca2012-10-05 08:38:24 +020011043 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011044}
11045
11046/*
11047 * Given a url parameter, find the starting position of the first occurence,
11048 * or NULL if the parameter is not found.
11049 *
11050 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11051 * the function will return query_string+8.
11052 */
11053static char*
11054find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011055 char* url_param_name, size_t url_param_name_l,
11056 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011057{
11058 char *pos, *last;
11059
11060 pos = query_string;
11061 last = query_string + query_string_l - url_param_name_l - 1;
11062
11063 while (pos <= last) {
11064 if (pos[url_param_name_l] == '=') {
11065 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11066 return pos;
11067 pos += url_param_name_l + 1;
11068 }
bedis4c75cca2012-10-05 08:38:24 +020011069 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011070 pos++;
11071 pos++;
11072 }
11073 return NULL;
11074}
11075
11076/*
Cyril Bonté0f836e12015-11-26 21:39:56 +010011077 * Given a url parameter name and a query string, find the next value.
11078 * An empty url_param_name matches the first available parameter.
11079 * If the parameter is found, 1 is returned and *value / *value_l are updated
11080 * to respectively provide a pointer to the value and its length.
11081 * Otherwise, 0 is returned and value/value_l are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011082 */
11083static int
11084find_url_param_value(char* path, size_t path_l,
11085 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011086 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011087{
11088 char *query_string, *qs_end;
11089 char *arg_start;
11090 char *value_start, *value_end;
11091
bedis4c75cca2012-10-05 08:38:24 +020011092 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011093 if (!query_string)
11094 return 0;
11095
11096 qs_end = path + path_l;
11097 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011098 url_param_name, url_param_name_l,
11099 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011100 if (!arg_start)
11101 return 0;
11102
11103 value_start = arg_start + url_param_name_l + 1;
11104 value_end = value_start;
11105
bedis4c75cca2012-10-05 08:38:24 +020011106 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011107 value_end++;
11108
11109 *value = value_start;
11110 *value_l = value_end - value_start;
Cyril Bonté0f836e12015-11-26 21:39:56 +010011111 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011112}
11113
11114static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011115smp_fetch_url_param(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)
David Cournapeau16023ee2010-12-23 20:55:41 +090011117{
bedis4c75cca2012-10-05 08:38:24 +020011118 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090011119 struct http_txn *txn = l7;
11120 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011121
bedis4c75cca2012-10-05 08:38:24 +020011122 if (!args || args[0].type != ARGT_STR ||
11123 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011124 return 0;
11125
11126 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011127
bedis4c75cca2012-10-05 08:38:24 +020011128 if (args[1].type)
11129 delim = *args[1].data.str.str;
11130
Willy Tarreau9b28e032012-10-12 23:49:43 +020011131 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011132 args->data.str.str, args->data.str.len,
11133 &smp->data.str.str, &smp->data.str.len,
11134 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011135 return 0;
11136
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011137 smp->type = SMP_T_STR;
11138 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011139 return 1;
11140}
11141
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011142/* Return the signed integer value for the specified url parameter (see url_param
11143 * above).
11144 */
11145static int
11146smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011147 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011148{
Willy Tarreauef38c392013-07-22 16:29:32 +020011149 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011150
11151 if (ret > 0) {
11152 smp->type = SMP_T_UINT;
11153 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11154 }
11155
11156 return ret;
11157}
11158
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011159/* This produces a 32-bit hash of the concatenation of the first occurrence of
11160 * the Host header followed by the path component if it begins with a slash ('/').
11161 * This means that '*' will not be added, resulting in exactly the first Host
11162 * entry. If no Host header is found, then the path is used. The resulting value
11163 * is hashed using the url hash followed by a full avalanche hash and provides a
11164 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11165 * high-traffic sites without having to store whole paths.
11166 * this differs from the base32 functions in that it includes the url parameters
11167 * as well as the path
11168 */
11169static int
11170smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011171 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011172{
11173 struct http_txn *txn = l7;
11174 struct hdr_ctx ctx;
11175 unsigned int hash = 0;
11176 char *ptr, *beg, *end;
11177 int len;
11178
11179 CHECK_HTTP_MESSAGE_FIRST();
11180
11181 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011182 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011183 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11184 ptr = ctx.line + ctx.val;
11185 len = ctx.vlen;
11186 while (len--)
11187 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11188 }
11189
11190 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011191 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 +000011192 beg = http_get_path(txn);
11193 if (!beg)
11194 beg = end;
11195
11196 for (ptr = beg; ptr < end ; ptr++);
11197
11198 if (beg < ptr && *beg == '/') {
11199 while (beg < ptr)
11200 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11201 }
11202 hash = full_hash(hash);
11203
11204 smp->type = SMP_T_UINT;
11205 smp->data.uint = hash;
11206 smp->flags = SMP_F_VOL_1ST;
11207 return 1;
11208}
11209
11210/* This concatenates the source address with the 32-bit hash of the Host and
11211 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11212 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11213 * on the source address length. The URL hash is stored before the address so
11214 * that in environments where IPv6 is insignificant, truncating the output to
11215 * 8 bytes would still work.
11216 */
11217static int
11218smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011219 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011220{
11221 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011222 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011223
Dragan Dosenb3d91b82016-06-16 11:23:01 +020011224 if (!cli_conn)
11225 return 0;
11226
Willy Tarreaue155ec22013-11-18 18:33:22 +010011227 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011228 return 0;
11229
11230 temp = get_trash_chunk();
Dragan Dosen9410d752016-06-16 11:08:08 +020011231 *(unsigned int *)temp->str = htonl(smp->data.sint);
11232 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011233
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011234 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011235 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011236 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011237 temp->len += 4;
11238 break;
11239 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011240 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011241 temp->len += 16;
11242 break;
11243 default:
11244 return 0;
11245 }
11246
11247 smp->data.str = *temp;
11248 smp->type = SMP_T_BIN;
11249 return 1;
11250}
11251
Willy Tarreau185b5c42012-04-26 15:11:51 +020011252/* This function is used to validate the arguments passed to any "hdr" fetch
11253 * keyword. These keywords support an optional positive or negative occurrence
11254 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11255 * is assumed that the types are already the correct ones. Returns 0 on error,
11256 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11257 * error message in case of error, that the caller is responsible for freeing.
11258 * The initial location must either be freeable or NULL.
11259 */
11260static int val_hdr(struct arg *arg, char **err_msg)
11261{
11262 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011263 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011264 return 0;
11265 }
11266 return 1;
11267}
11268
Willy Tarreau276fae92013-07-25 14:36:01 +020011269/* takes an UINT value on input supposed to represent the time since EPOCH,
11270 * adds an optional offset found in args[0] and emits a string representing
11271 * the date in RFC-1123/5322 format.
11272 */
11273static int sample_conv_http_date(const struct arg *args, struct sample *smp)
11274{
Cyril Bontéf980b362016-01-22 19:40:28 +010011275 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011276 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11277 struct chunk *temp;
11278 struct tm *tm;
11279 time_t curr_date = smp->data.uint;
11280
11281 /* add offset */
11282 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11283 curr_date += args[0].data.sint;
11284
11285 tm = gmtime(&curr_date);
Thierry FOURNIER95558722015-07-08 00:15:20 +020011286 if (!tm)
11287 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011288
11289 temp = get_trash_chunk();
11290 temp->len = snprintf(temp->str, temp->size - temp->len,
11291 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11292 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11293 tm->tm_hour, tm->tm_min, tm->tm_sec);
11294
11295 smp->data.str = *temp;
11296 smp->type = SMP_T_STR;
11297 return 1;
11298}
11299
Thierry FOURNIERad903512014-04-11 17:51:01 +020011300/* Match language range with language tag. RFC2616 14.4:
11301 *
11302 * A language-range matches a language-tag if it exactly equals
11303 * the tag, or if it exactly equals a prefix of the tag such
11304 * that the first tag character following the prefix is "-".
11305 *
11306 * Return 1 if the strings match, else return 0.
11307 */
11308static inline int language_range_match(const char *range, int range_len,
11309 const char *tag, int tag_len)
11310{
11311 const char *end = range + range_len;
11312 const char *tend = tag + tag_len;
11313 while (range < end) {
11314 if (*range == '-' && tag == tend)
11315 return 1;
11316 if (*range != *tag || tag == tend)
11317 return 0;
11318 range++;
11319 tag++;
11320 }
11321 /* Return true only if the last char of the tag is matched. */
11322 return tag == tend;
11323}
11324
11325/* Arguments: The list of expected value, the number of parts returned and the separator */
11326static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
11327{
11328 const char *al = smp->data.str.str;
11329 const char *end = al + smp->data.str.len;
11330 const char *token;
11331 int toklen;
11332 int qvalue;
11333 const char *str;
11334 const char *w;
11335 int best_q = 0;
11336
11337 /* Set the constant to the sample, because the output of the
11338 * function will be peek in the constant configuration string.
11339 */
11340 smp->flags |= SMP_F_CONST;
11341 smp->data.str.size = 0;
11342 smp->data.str.str = "";
11343 smp->data.str.len = 0;
11344
11345 /* Parse the accept language */
11346 while (1) {
11347
11348 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011349 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011350 al++;
11351 if (al >= end)
11352 break;
11353
11354 /* Start of the fisrt word. */
11355 token = al;
11356
11357 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011358 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011359 al++;
11360 if (al == token)
11361 goto expect_comma;
11362
11363 /* Length of the token. */
11364 toklen = al - token;
11365 qvalue = 1000;
11366
11367 /* Check if the token exists in the list. If the token not exists,
11368 * jump to the next token.
11369 */
11370 str = args[0].data.str.str;
11371 w = str;
11372 while (1) {
11373 if (*str == ';' || *str == '\0') {
11374 if (language_range_match(token, toklen, w, str-w))
11375 goto look_for_q;
11376 if (*str == '\0')
11377 goto expect_comma;
11378 w = str + 1;
11379 }
11380 str++;
11381 }
11382 goto expect_comma;
11383
11384look_for_q:
11385
11386 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011387 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011388 al++;
11389 if (al >= end)
11390 goto process_value;
11391
11392 /* If ',' is found, process the result */
11393 if (*al == ',')
11394 goto process_value;
11395
11396 /* If the character is different from ';', look
11397 * for the end of the header part in best effort.
11398 */
11399 if (*al != ';')
11400 goto expect_comma;
11401
11402 /* Assumes that the char is ';', now expect "q=". */
11403 al++;
11404
11405 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011406 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011407 al++;
11408 if (al >= end)
11409 goto process_value;
11410
11411 /* Expect 'q'. If no 'q', continue in best effort */
11412 if (*al != 'q')
11413 goto process_value;
11414 al++;
11415
11416 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011417 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011418 al++;
11419 if (al >= end)
11420 goto process_value;
11421
11422 /* Expect '='. If no '=', continue in best effort */
11423 if (*al != '=')
11424 goto process_value;
11425 al++;
11426
11427 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011428 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011429 al++;
11430 if (al >= end)
11431 goto process_value;
11432
11433 /* Parse the q value. */
11434 qvalue = parse_qvalue(al, &al);
11435
11436process_value:
11437
11438 /* If the new q value is the best q value, then store the associated
11439 * language in the response. If qvalue is the biggest value (1000),
11440 * break the process.
11441 */
11442 if (qvalue > best_q) {
11443 smp->data.str.str = (char *)w;
11444 smp->data.str.len = str - w;
11445 if (qvalue >= 1000)
11446 break;
11447 best_q = qvalue;
11448 }
11449
11450expect_comma:
11451
11452 /* Expect comma or end. If the end is detected, quit the loop. */
11453 while (al < end && *al != ',')
11454 al++;
11455 if (al >= end)
11456 break;
11457
11458 /* Comma is found, jump it and restart the analyzer. */
11459 al++;
11460 }
11461
11462 /* Set default value if required. */
11463 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11464 smp->data.str.str = args[1].data.str.str;
11465 smp->data.str.len = args[1].data.str.len;
11466 }
11467
11468 /* Return true only if a matching language was found. */
11469 return smp->data.str.len != 0;
11470}
11471
William Lallemand73025dd2014-04-24 14:38:37 +020011472/*
11473 * Return the struct http_req_action_kw associated to a keyword.
11474 */
11475struct http_req_action_kw *action_http_req_custom(const char *kw)
11476{
11477 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11478 struct http_req_action_kw_list *kw_list;
11479 int i;
11480
11481 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11482 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11483 if (!strcmp(kw, kw_list->kw[i].kw))
11484 return &kw_list->kw[i];
11485 }
11486 }
11487 }
11488 return NULL;
11489}
11490
11491/*
11492 * Return the struct http_res_action_kw associated to a keyword.
11493 */
11494struct http_res_action_kw *action_http_res_custom(const char *kw)
11495{
11496 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11497 struct http_res_action_kw_list *kw_list;
11498 int i;
11499
11500 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11501 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11502 if (!strcmp(kw, kw_list->kw[i].kw))
11503 return &kw_list->kw[i];
11504 }
11505 }
11506 }
11507 return NULL;
11508}
11509
Willy Tarreau4a568972010-05-12 08:08:50 +020011510/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011511/* All supported ACL keywords must be declared here. */
11512/************************************************************************/
11513
11514/* Note: must not be declared <const> as its list will be overwritten.
11515 * Please take care of keeping this list alphabetically sorted.
11516 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011517static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011518 { "base", "base", PAT_MATCH_STR },
11519 { "base_beg", "base", PAT_MATCH_BEG },
11520 { "base_dir", "base", PAT_MATCH_DIR },
11521 { "base_dom", "base", PAT_MATCH_DOM },
11522 { "base_end", "base", PAT_MATCH_END },
11523 { "base_len", "base", PAT_MATCH_LEN },
11524 { "base_reg", "base", PAT_MATCH_REG },
11525 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011526
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011527 { "cook", "req.cook", PAT_MATCH_STR },
11528 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11529 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11530 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11531 { "cook_end", "req.cook", PAT_MATCH_END },
11532 { "cook_len", "req.cook", PAT_MATCH_LEN },
11533 { "cook_reg", "req.cook", PAT_MATCH_REG },
11534 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011535
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011536 { "hdr", "req.hdr", PAT_MATCH_STR },
11537 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11538 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11539 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11540 { "hdr_end", "req.hdr", PAT_MATCH_END },
11541 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11542 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11543 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011544
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011545 /* these two declarations uses strings with list storage (in place
11546 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11547 * and delete functions are relative to the list management. The parse
11548 * and match method are related to the corresponding fetch methods. This
11549 * is very particular ACL declaration mode.
11550 */
11551 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11552 { "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 +020011553
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011554 { "path", "path", PAT_MATCH_STR },
11555 { "path_beg", "path", PAT_MATCH_BEG },
11556 { "path_dir", "path", PAT_MATCH_DIR },
11557 { "path_dom", "path", PAT_MATCH_DOM },
11558 { "path_end", "path", PAT_MATCH_END },
11559 { "path_len", "path", PAT_MATCH_LEN },
11560 { "path_reg", "path", PAT_MATCH_REG },
11561 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011562
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011563 { "req_ver", "req.ver", PAT_MATCH_STR },
11564 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011565
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011566 { "scook", "res.cook", PAT_MATCH_STR },
11567 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11568 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11569 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11570 { "scook_end", "res.cook", PAT_MATCH_END },
11571 { "scook_len", "res.cook", PAT_MATCH_LEN },
11572 { "scook_reg", "res.cook", PAT_MATCH_REG },
11573 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011574
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011575 { "shdr", "res.hdr", PAT_MATCH_STR },
11576 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11577 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11578 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11579 { "shdr_end", "res.hdr", PAT_MATCH_END },
11580 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11581 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11582 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011583
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011584 { "url", "url", PAT_MATCH_STR },
11585 { "url_beg", "url", PAT_MATCH_BEG },
11586 { "url_dir", "url", PAT_MATCH_DIR },
11587 { "url_dom", "url", PAT_MATCH_DOM },
11588 { "url_end", "url", PAT_MATCH_END },
11589 { "url_len", "url", PAT_MATCH_LEN },
11590 { "url_reg", "url", PAT_MATCH_REG },
11591 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011592
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011593 { "urlp", "urlp", PAT_MATCH_STR },
11594 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11595 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11596 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11597 { "urlp_end", "urlp", PAT_MATCH_END },
11598 { "urlp_len", "urlp", PAT_MATCH_LEN },
11599 { "urlp_reg", "urlp", PAT_MATCH_REG },
11600 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011601
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011602 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011603}};
11604
11605/************************************************************************/
11606/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011607/************************************************************************/
11608/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011609static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011610 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011611 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11612 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11613
William Lallemanda43ba4e2014-01-28 18:14:25 +010011614 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011615 { "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 +020011616
11617 /* retrieve these captures from the HTTP logs */
11618 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11619 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11620 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11621
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011622 { "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 +020011623 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011624
Willy Tarreau409bcde2013-01-08 00:31:00 +010011625 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11626 * are only here to match the ACL's name, are request-only and are used
11627 * for ACL compatibility only.
11628 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011629 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11630 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011631 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11632 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11633
11634 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11635 * only here to match the ACL's name, are request-only and are used for
11636 * ACL compatibility only.
11637 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011638 { "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 +010011639 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11640 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11641 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11642
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011643 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011644 { "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 +010011645 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011646 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011647 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011648
11649 /* HTTP protocol on the request path */
11650 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011651 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011652
11653 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011654 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11655 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011656
11657 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011658 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11659 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011660
Willy Tarreau18ed2562013-01-14 15:56:36 +010011661 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011662 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011663 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11664 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11665
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011666 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011667 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011668 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011669 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11670 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11671 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11672
11673 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011674 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011675 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11676 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11677
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011678 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011679 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011680 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011681 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11682 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11683 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11684
Willy Tarreau409bcde2013-01-08 00:31:00 +010011685 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011686 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011687 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11688 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011689 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011690
11691 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011692 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011693 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11694 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11695 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11696
11697 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011698 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011699 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11700 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011701 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11702 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011703 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11704 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011705 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11706 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011707}};
11708
Willy Tarreau8797c062007-05-07 00:55:35 +020011709
Willy Tarreau276fae92013-07-25 14:36:01 +020011710/* Note: must not be declared <const> as its list will be overwritten */
11711static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011712 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11713 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011714 { NULL, NULL, 0, 0, 0 },
11715}};
11716
Willy Tarreau8797c062007-05-07 00:55:35 +020011717__attribute__((constructor))
11718static void __http_protocol_init(void)
11719{
11720 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011721 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011722 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020011723}
11724
11725
Willy Tarreau58f10d72006-12-04 02:26:12 +010011726/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011727 * Local variables:
11728 * c-indent-level: 8
11729 * c-basic-offset: 8
11730 * End:
11731 */