blob: ba14e7eb285a4db3d4adb9676dc92d42bbeb8e7e [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;
3911 memcpy(trash.str + trash.len, "\r\n", 2);
3912 trash.len += 2;
3913 }
3914
3915 /* add end of headers and the keep-alive/close status.
3916 * We may choose to set keep-alive if the Location begins
3917 * with a slash, because the client will come back to the
3918 * same server.
3919 */
3920 txn->status = rule->code;
3921 /* let's log the request time */
3922 s->logs.tv_request = now;
3923
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003924 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003925 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3926 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3927 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3928 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3929 /* keep-alive possible */
3930 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3931 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3932 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3933 trash.len += 30;
3934 } else {
3935 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3936 trash.len += 24;
3937 }
3938 }
3939 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3940 trash.len += 4;
3941 bo_inject(txn->rsp.chn, trash.str, trash.len);
3942 /* "eat" the request */
3943 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02003944 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003945 msg->sov = 0;
3946 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3947 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3948 txn->req.msg_state = HTTP_MSG_CLOSED;
3949 txn->rsp.msg_state = HTTP_MSG_DONE;
3950 } else {
3951 /* keep-alive not possible */
3952 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3953 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3954 trash.len += 29;
3955 } else {
3956 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3957 trash.len += 23;
3958 }
3959 stream_int_retnclose(txn->req.chn->prod, &trash);
3960 txn->req.chn->analysers = 0;
3961 }
3962
3963 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003964 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003965 if (!(s->flags & SN_FINST_MASK))
3966 s->flags |= SN_FINST_R;
3967
3968 return 1;
3969}
3970
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003971/* This stream analyser runs all HTTP request processing which is common to
3972 * frontends and backends, which means blocking ACLs, filters, connection-close,
3973 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003974 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003975 * either needs more data or wants to immediately abort the request (eg: deny,
3976 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003977 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003978int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003979{
Willy Tarreaud787e662009-07-07 10:14:51 +02003980 struct http_txn *txn = &s->txn;
3981 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003982 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003983 struct cond_wordlist *wl;
Willy Tarreau0b748332014-04-29 00:13:29 +02003984 enum rule_result verdict;
Willy Tarreaud787e662009-07-07 10:14:51 +02003985
Willy Tarreau655dce92009-11-08 13:10:58 +01003986 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003987 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003988 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003989 return 0;
3990 }
3991
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003992 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02003993 now_ms, __FUNCTION__,
3994 s,
3995 req,
3996 req->rex, req->wex,
3997 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003998 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003999 req->analysers);
4000
Willy Tarreau65410832014-04-28 21:25:43 +02004001 /* just in case we have some per-backend tracking */
4002 session_inc_be_http_req_ctr(s);
4003
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004004 /* evaluate http-request rules */
Willy Tarreau0b748332014-04-29 00:13:29 +02004005 if (!LIST_ISEMPTY(&px->http_req_rules)) {
4006 verdict = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01004007
Willy Tarreau0b748332014-04-29 00:13:29 +02004008 switch (verdict) {
4009 case HTTP_RULE_RES_CONT:
4010 case HTTP_RULE_RES_STOP: /* nothing to do */
4011 break;
Willy Tarreau52542592014-04-28 18:33:26 +02004012
Willy Tarreau0b748332014-04-29 00:13:29 +02004013 case HTTP_RULE_RES_DENY: /* deny or tarpit */
4014 if (txn->flags & TX_CLTARPIT)
4015 goto tarpit;
4016 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004017
Willy Tarreau0b748332014-04-29 00:13:29 +02004018 case HTTP_RULE_RES_ABRT: /* abort request, response already sent. Eg: auth */
4019 goto return_prx_cond;
Willy Tarreau52542592014-04-28 18:33:26 +02004020
Willy Tarreau0b748332014-04-29 00:13:29 +02004021 case HTTP_RULE_RES_DONE: /* OK, but terminate request processing (eg: redirect) */
Willy Tarreau52542592014-04-28 18:33:26 +02004022 goto done;
4023
Willy Tarreau0b748332014-04-29 00:13:29 +02004024 case HTTP_RULE_RES_BADREQ: /* failed with a bad request */
4025 goto return_bad_req;
4026 }
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004027 }
4028
Willy Tarreau52542592014-04-28 18:33:26 +02004029 /* OK at this stage, we know that the request was accepted according to
4030 * the http-request rules, we can check for the stats. Note that the
4031 * URI is detected *before* the req* rules in order not to be affected
4032 * by a possible reqrep, while they are processed *after* so that a
4033 * reqdeny can still block them. This clearly needs to change in 1.6!
4034 */
4035 if (stats_check_uri(s->rep->prod, txn, px)) {
4036 s->target = &http_stats_applet.obj_type;
4037 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
4038 txn->status = 500;
4039 s->logs.tv_request = now;
4040 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01004041
Willy Tarreau52542592014-04-28 18:33:26 +02004042 if (!(s->flags & SN_ERR_MASK))
4043 s->flags |= SN_ERR_RESOURCE;
4044 goto return_prx_cond;
4045 }
4046
4047 /* parse the whole stats request and extract the relevant information */
4048 http_handle_stats(s, req);
Willy Tarreau0b748332014-04-29 00:13:29 +02004049 verdict = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
4050 /* not all actions implemented: deny, allow, auth */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004051
Willy Tarreau0b748332014-04-29 00:13:29 +02004052 if (verdict == HTTP_RULE_RES_DENY) /* stats http-request deny */
4053 goto deny;
Willy Tarreau52542592014-04-28 18:33:26 +02004054
Willy Tarreau0b748332014-04-29 00:13:29 +02004055 if (verdict == HTTP_RULE_RES_ABRT) /* stats auth / stats http-request auth */
4056 goto return_prx_cond;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01004057 }
4058
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004059 /* evaluate the req* rules except reqadd */
4060 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01004061 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004062 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01004063
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004064 if (txn->flags & TX_CLDENY)
4065 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02004066
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004067 if (txn->flags & TX_CLTARPIT)
4068 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004069 }
Willy Tarreau06619262006-12-17 08:37:22 +01004070
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004071 /* add request headers from the rule sets in the same order */
4072 list_for_each_entry(wl, &px->req_add, list) {
4073 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004074 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004075 ret = acl_pass(ret);
4076 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4077 ret = !ret;
4078 if (!ret)
4079 continue;
4080 }
4081
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004082 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004083 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01004084 }
4085
Willy Tarreau52542592014-04-28 18:33:26 +02004086
4087 /* Proceed with the stats now. */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01004088 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01004089 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01004090 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
4091 s->fe->fe_counters.intercepted_req++;
4092
4093 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
4094 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
4095 if (!(s->flags & SN_FINST_MASK))
4096 s->flags |= SN_FINST_R;
4097
Willy Tarreau70730dd2014-04-24 18:06:27 +02004098 /* we may want to compress the stats page */
4099 if (s->fe->comp || s->be->comp)
4100 select_compression_request_header(s, req->buf);
4101
4102 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreaude8ca962014-11-20 22:23:10 +01004103 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) | AN_REQ_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004104 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004105 }
Willy Tarreaub2513902006-12-17 14:52:38 +01004106
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004107 /* check whether we have some ACLs set to redirect this request */
4108 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01004109 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01004110 int ret;
4111
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02004112 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01004113 ret = acl_pass(ret);
4114 if (rule->cond->pol == ACL_COND_UNLESS)
4115 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01004116 if (!ret)
4117 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01004118 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01004119 if (!http_apply_redirect_rule(rule, s, txn))
4120 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004121 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004122 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004123
Willy Tarreau2be39392010-01-03 17:24:51 +01004124 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
4125 * If this happens, then the data will not come immediately, so we must
4126 * send all what we have without waiting. Note that due to the small gain
4127 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004128 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01004129 * itself once used.
4130 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004131 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01004132
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004133 done: /* done with this analyser, continue with next ones that the calling
4134 * points will have set, if any.
4135 */
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004136 req->analyse_exp = TICK_ETERNITY;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004137 done_without_exp: /* done with this analyser, but dont reset the analyse_exp. */
4138 req->analysers &= ~an_bit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004139 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02004140
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004141 tarpit:
4142 /* When a connection is tarpitted, we use the tarpit timeout,
4143 * which may be the same as the connect timeout if unspecified.
4144 * If unset, then set it to zero because we really want it to
4145 * eventually expire. We build the tarpit as an analyser.
4146 */
4147 channel_erase(s->req);
4148
4149 /* wipe the request out so that we can drop the connection early
4150 * if the client closes first.
4151 */
4152 channel_dont_connect(req);
4153 req->analysers = 0; /* remove switching rules etc... */
Bertrand Paquetc89b09b2016-04-06 11:58:31 +02004154
4155 /* Allow cookie logging
4156 */
4157 if (s->be->cookie_name || s->fe->capture_name)
4158 manage_client_side_cookies(s, req);
4159
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004160 req->analysers |= AN_REQ_HTTP_TARPIT;
4161 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4162 if (!req->analyse_exp)
4163 req->analyse_exp = tick_add(now_ms, 0);
4164 session_inc_http_err_ctr(s);
4165 s->fe->fe_counters.denied_req++;
4166 if (s->fe != s->be)
4167 s->be->be_counters.denied_req++;
4168 if (s->listener->counters)
4169 s->listener->counters->denied_req++;
Thierry FOURNIERfc566b52014-08-22 06:55:26 +02004170 goto done_without_exp;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004171
4172 deny: /* this request was blocked (denied) */
Bertrand Paquetc89b09b2016-04-06 11:58:31 +02004173
4174 /* Allow cookie logging
4175 */
4176 if (s->be->cookie_name || s->fe->capture_name)
4177 manage_client_side_cookies(s, req);
4178
Willy Tarreau0b748332014-04-29 00:13:29 +02004179 txn->flags |= TX_CLDENY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02004180 txn->status = 403;
4181 s->logs.tv_request = now;
4182 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4183 session_inc_http_err_ctr(s);
4184 s->fe->fe_counters.denied_req++;
4185 if (s->fe != s->be)
4186 s->be->be_counters.denied_req++;
4187 if (s->listener->counters)
4188 s->listener->counters->denied_req++;
4189 goto return_prx_cond;
4190
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004191 return_bad_req:
4192 /* We centralize bad requests processing here */
4193 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4194 /* we detected a parsing error. We want to archive this request
4195 * in the dedicated proxy area for later troubleshooting.
4196 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004197 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004198 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004199
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004200 txn->req.msg_state = HTTP_MSG_ERROR;
4201 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004202 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004203
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004204 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004205 if (s->listener->counters)
4206 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004207
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004208 return_prx_cond:
4209 if (!(s->flags & SN_ERR_MASK))
4210 s->flags |= SN_ERR_PRXCOND;
4211 if (!(s->flags & SN_FINST_MASK))
4212 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004213
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004214 req->analysers = 0;
4215 req->analyse_exp = TICK_ETERNITY;
4216 return 0;
4217}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004218
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004219/* This function performs all the processing enabled for the current request.
4220 * It returns 1 if the processing can continue on next analysers, or zero if it
4221 * needs more data, encounters an error, or wants to immediately abort the
4222 * request. It relies on buffers flags, and updates s->req->analysers.
4223 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004224int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004225{
4226 struct http_txn *txn = &s->txn;
4227 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004228 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004229
Willy Tarreau655dce92009-11-08 13:10:58 +01004230 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004231 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004232 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004233 return 0;
4234 }
4235
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004236 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 +02004237 now_ms, __FUNCTION__,
4238 s,
4239 req,
4240 req->rex, req->wex,
4241 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004242 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004243 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004244
William Lallemand82fe75c2012-10-23 10:25:10 +02004245 if (s->fe->comp || s->be->comp)
4246 select_compression_request_header(s, req->buf);
4247
Willy Tarreau59234e92008-11-30 23:51:27 +01004248 /*
4249 * Right now, we know that we have processed the entire headers
4250 * and that unwanted requests have been filtered out. We can do
4251 * whatever we want with the remaining request. Also, now we
4252 * may have separate values for ->fe, ->be.
4253 */
Willy Tarreau06619262006-12-17 08:37:22 +01004254
Willy Tarreau59234e92008-11-30 23:51:27 +01004255 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004256 * If HTTP PROXY is set we simply get remote server address parsing
4257 * incoming request. Note that this requires that a connection is
4258 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004259 */
4260 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004261 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004262 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004263
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004264 /* Note that for now we don't reuse existing proxy connections */
4265 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004266 txn->req.msg_state = HTTP_MSG_ERROR;
4267 txn->status = 500;
4268 req->analysers = 0;
4269 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4270
4271 if (!(s->flags & SN_ERR_MASK))
4272 s->flags |= SN_ERR_RESOURCE;
4273 if (!(s->flags & SN_FINST_MASK))
4274 s->flags |= SN_FINST_R;
4275
4276 return 0;
4277 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004278
4279 path = http_get_path(txn);
4280 url2sa(req->buf->p + msg->sl.rq.u,
4281 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004282 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004283 /* if the path was found, we have to remove everything between
4284 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4285 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4286 * u_l characters by a single "/".
4287 */
4288 if (path) {
4289 char *cur_ptr = req->buf->p;
4290 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4291 int delta;
4292
4293 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4294 http_msg_move_end(&txn->req, delta);
4295 cur_end += delta;
4296 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4297 goto return_bad_req;
4298 }
4299 else {
4300 char *cur_ptr = req->buf->p;
4301 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4302 int delta;
4303
4304 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4305 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4306 http_msg_move_end(&txn->req, delta);
4307 cur_end += delta;
4308 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4309 goto return_bad_req;
4310 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004311 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004312
Willy Tarreau59234e92008-11-30 23:51:27 +01004313 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004314 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004315 * Note that doing so might move headers in the request, but
4316 * the fields will stay coherent and the URI will not move.
4317 * This should only be performed in the backend.
4318 */
Bertrand Paquetc89b09b2016-04-06 11:58:31 +02004319 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004320 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004321
Willy Tarreau59234e92008-11-30 23:51:27 +01004322 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004323 * 8: the appsession cookie was looked up very early in 1.2,
4324 * so let's do the same now.
4325 */
4326
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004327 /* It needs to look into the URI unless persistence must be ignored */
4328 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004329 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 +01004330 }
4331
William Lallemanda73203e2012-03-12 12:48:57 +01004332 /* add unique-id if "header-unique-id" is specified */
4333
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004334 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4335 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4336 goto return_bad_req;
4337 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004338 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004339 }
William Lallemanda73203e2012-03-12 12:48:57 +01004340
4341 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004342 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4343 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004344 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004345 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004346 goto return_bad_req;
4347 }
4348
Cyril Bontéb21570a2009-11-29 20:04:48 +01004349 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004350 * 9: add X-Forwarded-For if either the frontend or the backend
4351 * asks for it.
4352 */
4353 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004354 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004355 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004356 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4357 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004358 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004359 /* The header is set to be added only if none is present
4360 * and we found it, so don't do anything.
4361 */
4362 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004363 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004364 /* Add an X-Forwarded-For header unless the source IP is
4365 * in the 'except' network range.
4366 */
4367 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004368 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004369 != s->fe->except_net.s_addr) &&
4370 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004371 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004372 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004373 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004374 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004375 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004376
4377 /* Note: we rely on the backend to get the header name to be used for
4378 * x-forwarded-for, because the header is really meant for the backends.
4379 * However, if the backend did not specify any option, we have to rely
4380 * on the frontend's header name.
4381 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004382 if (s->be->fwdfor_hdr_len) {
4383 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004384 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004385 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004386 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004387 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004388 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004389 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 +01004390
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004391 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004392 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004393 }
4394 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004395 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004396 /* FIXME: for the sake of completeness, we should also support
4397 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004398 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004399 int len;
4400 char pn[INET6_ADDRSTRLEN];
4401 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004402 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004403 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004404
Willy Tarreau59234e92008-11-30 23:51:27 +01004405 /* Note: we rely on the backend to get the header name to be used for
4406 * x-forwarded-for, because the header is really meant for the backends.
4407 * However, if the backend did not specify any option, we have to rely
4408 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004409 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004410 if (s->be->fwdfor_hdr_len) {
4411 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004412 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004413 } else {
4414 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004415 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004416 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004417 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004418
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004419 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004420 goto return_bad_req;
4421 }
4422 }
4423
4424 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004425 * 10: add X-Original-To if either the frontend or the backend
4426 * asks for it.
4427 */
4428 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4429
4430 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004431 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004432 /* Add an X-Original-To header unless the destination IP is
4433 * in the 'except' network range.
4434 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004435 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004436
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004437 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004438 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004439 (((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 +02004440 != s->fe->except_to.s_addr) &&
4441 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004442 (((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 +02004443 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004444 int len;
4445 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004446 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004447
4448 /* Note: we rely on the backend to get the header name to be used for
4449 * x-original-to, because the header is really meant for the backends.
4450 * However, if the backend did not specify any option, we have to rely
4451 * on the frontend's header name.
4452 */
4453 if (s->be->orgto_hdr_len) {
4454 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004455 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004456 } else {
4457 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004458 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004459 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004460 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 +02004461
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004462 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004463 goto return_bad_req;
4464 }
4465 }
4466 }
4467
Willy Tarreau50fc7772012-11-11 22:19:57 +01004468 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4469 * If an "Upgrade" token is found, the header is left untouched in order not to have
4470 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4471 * "Upgrade" is present in the Connection header.
4472 */
4473 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4474 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004475 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4476 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004477 unsigned int want_flags = 0;
4478
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004479 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004480 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004481 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4482 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004483 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004484 want_flags |= TX_CON_CLO_SET;
4485 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004486 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004487 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4488 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004489 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004490 want_flags |= TX_CON_KAL_SET;
4491 }
4492
4493 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004494 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004495 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004496
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004497
Willy Tarreau522d6c02009-12-06 18:49:18 +01004498 /* If we have no server assigned yet and we're balancing on url_param
4499 * with a POST request, we may be interested in checking the body for
4500 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004501 */
4502 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4503 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004504 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004505 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004506 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004507 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004508
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004509 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004510 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004511#ifdef TCP_QUICKACK
4512 /* We expect some data from the client. Unless we know for sure
4513 * we already have a full request, we have to re-enable quick-ack
4514 * in case we previously disabled it, otherwise we might cause
4515 * the client to delay further data.
4516 */
4517 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004518 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004519 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004520 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004521 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004522#endif
4523 }
Willy Tarreau03945942009-12-22 16:50:27 +01004524
Willy Tarreau59234e92008-11-30 23:51:27 +01004525 /*************************************************************
4526 * OK, that's finished for the headers. We have done what we *
4527 * could. Let's switch to the DATA state. *
4528 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004529 req->analyse_exp = TICK_ETERNITY;
4530 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004531
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004532 /* if the server closes the connection, we want to immediately react
4533 * and close the socket to save packets and syscalls.
4534 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004535 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4536 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004537
Willy Tarreau59234e92008-11-30 23:51:27 +01004538 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004539 /* OK let's go on with the BODY now */
4540 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004541
Willy Tarreau59234e92008-11-30 23:51:27 +01004542 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004543 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004544 /* we detected a parsing error. We want to archive this request
4545 * in the dedicated proxy area for later troubleshooting.
4546 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004547 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004548 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004549
Willy Tarreau59234e92008-11-30 23:51:27 +01004550 txn->req.msg_state = HTTP_MSG_ERROR;
4551 txn->status = 400;
4552 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004553 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004554
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004555 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004556 if (s->listener->counters)
4557 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004558
Willy Tarreau59234e92008-11-30 23:51:27 +01004559 if (!(s->flags & SN_ERR_MASK))
4560 s->flags |= SN_ERR_PRXCOND;
4561 if (!(s->flags & SN_FINST_MASK))
4562 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004563 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004564}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004565
Willy Tarreau60b85b02008-11-30 23:28:40 +01004566/* This function is an analyser which processes the HTTP tarpit. It always
4567 * returns zero, at the beginning because it prevents any other processing
4568 * from occurring, and at the end because it terminates the request.
4569 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004570int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004571{
4572 struct http_txn *txn = &s->txn;
4573
4574 /* This connection is being tarpitted. The CLIENT side has
4575 * already set the connect expiration date to the right
4576 * timeout. We just have to check that the client is still
4577 * there and that the timeout has not expired.
4578 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004579 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004580 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004581 !tick_is_expired(req->analyse_exp, now_ms))
4582 return 0;
4583
4584 /* We will set the queue timer to the time spent, just for
4585 * logging purposes. We fake a 500 server error, so that the
4586 * attacker will not suspect his connection has been tarpitted.
4587 * It will not cause trouble to the logs because we can exclude
4588 * the tarpitted connections by filtering on the 'PT' status flags.
4589 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004590 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4591
4592 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004593 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004594 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004595
4596 req->analysers = 0;
4597 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004598
Willy Tarreau60b85b02008-11-30 23:28:40 +01004599 if (!(s->flags & SN_ERR_MASK))
4600 s->flags |= SN_ERR_PRXCOND;
4601 if (!(s->flags & SN_FINST_MASK))
4602 s->flags |= SN_FINST_T;
4603 return 0;
4604}
4605
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004606/* This function is an analyser which waits for the HTTP request body. It waits
4607 * for either the buffer to be full, or the full advertised contents to have
4608 * reached the buffer. It must only be called after the standard HTTP request
4609 * processing has occurred, because it expects the request to be parsed and will
4610 * look for the Expect header. It may send a 100-Continue interim response. It
4611 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4612 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4613 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004614 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004615int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004616{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004617 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004618 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004619
4620 /* We have to parse the HTTP request body to find any required data.
4621 * "balance url_param check_post" should have been the only way to get
4622 * into this. We were brought here after HTTP header analysis, so all
4623 * related structures are ready.
4624 */
4625
Willy Tarreau890988f2014-04-10 11:59:33 +02004626 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4627 /* This is the first call */
4628 if (msg->msg_state < HTTP_MSG_BODY)
4629 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004630
Willy Tarreau890988f2014-04-10 11:59:33 +02004631 if (msg->msg_state < HTTP_MSG_100_SENT) {
4632 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4633 * send an HTTP/1.1 100 Continue intermediate response.
4634 */
4635 if (msg->flags & HTTP_MSGF_VER_11) {
4636 struct hdr_ctx ctx;
4637 ctx.idx = 0;
4638 /* Expect is allowed in 1.1, look for it */
4639 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4640 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4641 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4642 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004643 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004644 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004645 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004646
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004647 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004648 * req->buf->p still points to the beginning of the message. We
4649 * must save the body in msg->next because it survives buffer
4650 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004651 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004652 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004653
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004654 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004655 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4656 else
4657 msg->msg_state = HTTP_MSG_DATA;
4658 }
4659
Willy Tarreau890988f2014-04-10 11:59:33 +02004660 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4661 /* We're in content-length mode, we just have to wait for enough data. */
Willy Tarreau5b7f9242015-05-01 23:05:14 +02004662 if (http_body_bytes(msg) < msg->body_len)
Willy Tarreau890988f2014-04-10 11:59:33 +02004663 goto missing_data;
4664
4665 /* OK we have everything we need now */
4666 goto http_end;
4667 }
4668
4669 /* OK here we're parsing a chunked-encoded message */
4670
Willy Tarreau522d6c02009-12-06 18:49:18 +01004671 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004672 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004673 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004674 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004675 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004676 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004677
Willy Tarreau115acb92009-12-26 13:56:06 +01004678 if (!ret)
4679 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004680 else if (ret < 0) {
4681 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004682 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004683 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004684 }
4685
Willy Tarreaud98cf932009-12-27 22:54:55 +01004686 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau5b7f9242015-05-01 23:05:14 +02004687 * We have the first data byte is in msg->sov + msg->sol. We're waiting
4688 * for at least a whole chunk or the whole content length bytes after
4689 * msg->sov + msg->sol.
Willy Tarreaud34af782008-11-30 23:36:37 +01004690 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004691 if (msg->msg_state == HTTP_MSG_TRAILERS)
4692 goto http_end;
4693
Willy Tarreau5b7f9242015-05-01 23:05:14 +02004694 if (http_body_bytes(msg) >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004695 goto http_end;
4696
4697 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004698 /* we get here if we need to wait for more data. If the buffer is full,
4699 * we have the maximum we can expect.
4700 */
4701 if (buffer_full(req->buf, global.tune.maxrewrite))
4702 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004703
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004704 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004705 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004706 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004707
4708 if (!(s->flags & SN_ERR_MASK))
4709 s->flags |= SN_ERR_CLITO;
4710 if (!(s->flags & SN_FINST_MASK))
4711 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004712 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004713 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004714
4715 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004716 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004717 /* Not enough data. We'll re-use the http-request
4718 * timeout here. Ideally, we should set the timeout
4719 * relative to the accept() date. We just set the
4720 * request timeout once at the beginning of the
4721 * request.
4722 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004723 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004724 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004725 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004726 return 0;
4727 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004728
4729 http_end:
4730 /* The situation will not evolve, so let's give up on the analysis. */
4731 s->logs.tv_request = now; /* update the request timer to reflect full request */
4732 req->analysers &= ~an_bit;
4733 req->analyse_exp = TICK_ETERNITY;
4734 return 1;
4735
4736 return_bad_req: /* let's centralize all bad requests */
4737 txn->req.msg_state = HTTP_MSG_ERROR;
4738 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004739 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004740
Willy Tarreau79ebac62010-06-07 13:47:49 +02004741 if (!(s->flags & SN_ERR_MASK))
4742 s->flags |= SN_ERR_PRXCOND;
4743 if (!(s->flags & SN_FINST_MASK))
4744 s->flags |= SN_FINST_R;
4745
Willy Tarreau522d6c02009-12-06 18:49:18 +01004746 return_err_msg:
4747 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004748 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004749 if (s->listener->counters)
4750 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004751 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004752}
4753
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004754/* send a server's name with an outgoing request over an established connection.
4755 * Note: this function is designed to be called once the request has been scheduled
4756 * for being forwarded. This is the reason why it rewinds the buffer before
4757 * proceeding.
4758 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004759int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004760
4761 struct hdr_ctx ctx;
4762
Mark Lamourinec2247f02012-01-04 13:02:01 -05004763 char *hdr_name = be->server_id_hdr_name;
4764 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004765 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004766 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004767 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004768
William Lallemandd9e90662012-01-30 17:27:17 +01004769 ctx.idx = 0;
4770
Willy Tarreau211cdec2014-04-17 20:18:08 +02004771 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004772 if (old_o) {
4773 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004774 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004775 txn->req.next += old_o;
4776 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004777 }
4778
Willy Tarreau9b28e032012-10-12 23:49:43 +02004779 old_i = chn->buf->i;
4780 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 -05004781 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004782 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004783 }
4784
4785 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004786 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004787 memcpy(hdr_val, hdr_name, hdr_name_len);
4788 hdr_val += hdr_name_len;
4789 *hdr_val++ = ':';
4790 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004791 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4792 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004793
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004794 if (old_o) {
4795 /* If this was a forwarded request, we must readjust the amount of
4796 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004797 * variations. Note that the current state is >= HTTP_MSG_BODY,
4798 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004799 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004800 old_o += chn->buf->i - old_i;
4801 b_adv(chn->buf, old_o);
4802 txn->req.next -= old_o;
4803 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004804 }
4805
Mark Lamourinec2247f02012-01-04 13:02:01 -05004806 return 0;
4807}
4808
Willy Tarreau610ecce2010-01-04 21:15:02 +01004809/* Terminate current transaction and prepare a new one. This is very tricky
4810 * right now but it works.
4811 */
4812void http_end_txn_clean_session(struct session *s)
4813{
Willy Tarreau068621e2013-12-23 15:11:25 +01004814 int prev_status = s->txn.status;
4815
Willy Tarreau610ecce2010-01-04 21:15:02 +01004816 /* FIXME: We need a more portable way of releasing a backend's and a
4817 * server's connections. We need a safer way to reinitialize buffer
4818 * flags. We also need a more accurate method for computing per-request
4819 * data.
4820 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004821
Willy Tarreau4213a112013-12-15 10:25:42 +01004822 /* unless we're doing keep-alive, we want to quickly close the connection
4823 * to the server.
4824 */
4825 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4826 !si_conn_ready(s->req->cons)) {
4827 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4828 si_shutr(s->req->cons);
4829 si_shutw(s->req->cons);
4830 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004831
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004832 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004833 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004834 if (unlikely(s->srv_conn))
4835 sess_change_server(s, NULL);
4836 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004837
4838 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4839 session_process_counters(s);
4840
4841 if (s->txn.status) {
4842 int n;
4843
4844 n = s->txn.status / 100;
4845 if (n < 1 || n > 5)
4846 n = 0;
4847
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004848 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004849 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004850 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004851 s->fe->fe_counters.p.http.comp_rsp++;
4852 }
Willy Tarreau24657792010-02-26 10:30:28 +01004853 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004854 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004855 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004856 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004857 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004858 s->be->be_counters.p.http.comp_rsp++;
4859 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004860 }
4861
4862 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004863 s->logs.bytes_in -= s->req->buf->i;
4864 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004865
4866 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004867 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004868 !(s->flags & SN_MONITOR) &&
4869 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4870 s->do_log(s);
4871 }
4872
Willy Tarreauc177ea72014-06-25 15:36:04 +02004873 /* stop tracking content-based counters */
4874 session_stop_content_counters(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02004875 session_update_time_stats(s);
4876
Willy Tarreau610ecce2010-01-04 21:15:02 +01004877 s->logs.accept_date = date; /* user-visible date for logging */
4878 s->logs.tv_accept = now; /* corrected date for internal use */
4879 tv_zero(&s->logs.tv_request);
4880 s->logs.t_queue = -1;
4881 s->logs.t_connect = -1;
4882 s->logs.t_data = -1;
4883 s->logs.t_close = 0;
4884 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4885 s->logs.srv_queue_size = 0; /* we will get this number soon */
4886
Willy Tarreau9b28e032012-10-12 23:49:43 +02004887 s->logs.bytes_in = s->req->total = s->req->buf->i;
4888 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004889
4890 if (s->pend_pos)
4891 pendconn_free(s->pend_pos);
4892
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004893 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004894 if (s->flags & SN_CURR_SESS) {
4895 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004896 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004897 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004898 if (may_dequeue_tasks(objt_server(s->target), s->be))
4899 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004900 }
4901
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004902 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004903
Willy Tarreau4213a112013-12-15 10:25:42 +01004904 /* only release our endpoint if we don't intend to reuse the
4905 * connection.
4906 */
4907 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4908 !si_conn_ready(s->req->cons)) {
4909 si_release_endpoint(s->req->cons);
4910 }
4911
Willy Tarreau610ecce2010-01-04 21:15:02 +01004912 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004913 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004914 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004915 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004916 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreaub4d05092014-09-01 20:35:55 +02004917 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);
4918 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 +02004919 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 +01004920 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Cyril Bonté17f77072014-10-22 22:30:13 +02004921 s->flags &= ~(SN_ERR_MASK|SN_FINST_MASK|SN_REDISP);
Willy Tarreau543db622012-11-15 16:41:22 +01004922
Willy Tarreau610ecce2010-01-04 21:15:02 +01004923 s->txn.meth = 0;
4924 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004925 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004926
4927 if (prev_status == 401 || prev_status == 407) {
4928 /* In HTTP keep-alive mode, if we receive a 401, we still have
4929 * a chance of being able to send the visitor again to the same
4930 * server over the same connection. This is required by some
4931 * broken protocols such as NTLM, and anyway whenever there is
4932 * an opportunity for sending the challenge to the proper place,
4933 * it's better to do it (at least it helps with debugging).
4934 */
4935 s->txn.flags |= TX_PREFER_LAST;
4936 }
4937
Willy Tarreauee55dc02010-06-01 10:56:34 +02004938 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004939 s->req->cons->flags |= SI_FL_INDEP_STR;
4940
Willy Tarreau96e31212011-05-30 18:10:30 +02004941 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004942 s->req->flags |= CF_NEVER_WAIT;
4943 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004944 }
4945
Willy Tarreau610ecce2010-01-04 21:15:02 +01004946 /* if the request buffer is not empty, it means we're
4947 * about to process another request, so send pending
4948 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004949 * Just don't do this if the buffer is close to be full,
4950 * because the request will wait for it to flush a little
4951 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004952 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004953 if (s->req->buf->i) {
4954 if (s->rep->buf->o &&
4955 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4956 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004957 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004958 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004959
Willy Tarreau3de8e7a2015-11-25 20:11:11 +01004960 /* we're removing the analysers, we MUST re-enable events detection.
4961 * We don't enable close on the response channel since it's either
4962 * already closed, or in keep-alive with an idle connection handler.
4963 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004964 channel_auto_read(s->req);
4965 channel_auto_close(s->req);
4966 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004967
Willy Tarreau27375622013-12-17 00:00:28 +01004968 /* we're in keep-alive with an idle connection, monitor it */
4969 si_idle_conn(s->req->cons);
4970
Willy Tarreau342b11c2010-11-24 16:22:09 +01004971 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004972 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004973}
4974
4975
4976/* This function updates the request state machine according to the response
4977 * state machine and buffer flags. It returns 1 if it changes anything (flag
4978 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4979 * it is only used to find when a request/response couple is complete. Both
4980 * this function and its equivalent should loop until both return zero. It
4981 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4982 */
4983int http_sync_req_state(struct session *s)
4984{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004985 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004986 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004987 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004988 unsigned int old_state = txn->req.msg_state;
4989
Willy Tarreau610ecce2010-01-04 21:15:02 +01004990 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4991 return 0;
4992
4993 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004994 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004995 * We can shut the read side unless we want to abort_on_close,
4996 * or we have a POST request. The issue with POST requests is
4997 * that some browsers still send a CRLF after the request, and
4998 * this CRLF must be read so that it does not remain in the kernel
4999 * buffers, otherwise a close could cause an RST on some systems
5000 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01005001 * Note that if we're using keep-alive on the client side, we'd
5002 * rather poll now and keep the polling enabled for the whole
5003 * session's life than enabling/disabling it between each
5004 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01005005 */
Willy Tarreau3988d932013-12-27 23:03:08 +01005006 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5007 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5008 !(s->be->options & PR_O_ABRT_CLOSE) &&
5009 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005010 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005011
Willy Tarreau40f151a2012-12-20 12:10:09 +01005012 /* if the server closes the connection, we want to immediately react
5013 * and close the socket to save packets and syscalls.
5014 */
5015 chn->cons->flags |= SI_FL_NOHALF;
5016
Willy Tarreauc0d56132015-11-18 11:59:55 +01005017 /* In any case we've finished parsing the request so we must
5018 * disable Nagle when sending data because 1) we're not going
5019 * to shut this side, and 2) the server is waiting for us to
5020 * send pending data.
5021 */
5022 chn->flags |= CF_NEVER_WAIT;
5023
Willy Tarreau610ecce2010-01-04 21:15:02 +01005024 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
5025 goto wait_other_side;
5026
5027 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
5028 /* The server has not finished to respond, so we
5029 * don't want to move in order not to upset it.
5030 */
5031 goto wait_other_side;
5032 }
5033
5034 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
5035 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005036 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005037 txn->req.msg_state = HTTP_MSG_TUNNEL;
5038 goto wait_other_side;
5039 }
5040
5041 /* When we get here, it means that both the request and the
5042 * response have finished receiving. Depending on the connection
5043 * mode, we'll have to wait for the last bytes to leave in either
5044 * direction, and sometimes for a close to be effective.
5045 */
5046
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005047 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5048 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005049 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
5050 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005051 }
5052 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5053 /* Option forceclose is set, or either side wants to close,
5054 * let's enforce it now that we're not expecting any new
5055 * data to come. The caller knows the session is complete
5056 * once both states are CLOSED.
5057 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005058 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5059 channel_shutr_now(chn);
5060 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005061 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005062 }
5063 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005064 /* The last possible modes are keep-alive and tunnel. Tunnel mode
5065 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005066 */
Willy Tarreau4213a112013-12-15 10:25:42 +01005067 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
5068 channel_auto_read(chn);
5069 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau4213a112013-12-15 10:25:42 +01005070 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005071 }
5072
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005073 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005074 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005075 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005076
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005077 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005078 txn->req.msg_state = HTTP_MSG_CLOSING;
5079 goto http_msg_closing;
5080 }
5081 else {
5082 txn->req.msg_state = HTTP_MSG_CLOSED;
5083 goto http_msg_closed;
5084 }
5085 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005086 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005087 }
5088
5089 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
5090 http_msg_closing:
5091 /* nothing else to forward, just waiting for the output buffer
5092 * to be empty and for the shutw_now to take effect.
5093 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005094 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005095 txn->req.msg_state = HTTP_MSG_CLOSED;
5096 goto http_msg_closed;
5097 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005098 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005099 txn->req.msg_state = HTTP_MSG_ERROR;
5100 goto wait_other_side;
5101 }
5102 }
5103
5104 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
5105 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01005106 /* see above in MSG_DONE why we only do this in these states */
5107 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
5108 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
5109 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01005110 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005111 goto wait_other_side;
5112 }
5113
5114 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005115 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005116}
5117
5118
5119/* This function updates the response state machine according to the request
5120 * state machine and buffer flags. It returns 1 if it changes anything (flag
5121 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
5122 * it is only used to find when a request/response couple is complete. Both
5123 * this function and its equivalent should loop until both return zero. It
5124 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
5125 */
5126int http_sync_res_state(struct session *s)
5127{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005128 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005129 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005130 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005131 unsigned int old_state = txn->rsp.msg_state;
5132
Willy Tarreau610ecce2010-01-04 21:15:02 +01005133 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
5134 return 0;
5135
5136 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
5137 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01005138 * still monitor the server connection for a possible close
5139 * while the request is being uploaded, so we don't disable
5140 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005141 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005142 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005143
5144 if (txn->req.msg_state == HTTP_MSG_ERROR)
5145 goto wait_other_side;
5146
5147 if (txn->req.msg_state < HTTP_MSG_DONE) {
5148 /* The client seems to still be sending data, probably
5149 * because we got an error response during an upload.
5150 * We have the choice of either breaking the connection
5151 * or letting it pass through. Let's do the later.
5152 */
5153 goto wait_other_side;
5154 }
5155
5156 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
5157 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005158 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005159 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02005160 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005161 goto wait_other_side;
5162 }
5163
5164 /* When we get here, it means that both the request and the
5165 * response have finished receiving. Depending on the connection
5166 * mode, we'll have to wait for the last bytes to leave in either
5167 * direction, and sometimes for a close to be effective.
5168 */
5169
5170 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5171 /* Server-close mode : shut read and wait for the request
5172 * side to close its output buffer. The caller will detect
5173 * when we're in DONE and the other is in CLOSED and will
5174 * catch that for the final cleanup.
5175 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005176 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5177 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005178 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005179 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5180 /* Option forceclose is set, or either side wants to close,
5181 * let's enforce it now that we're not expecting any new
5182 * data to come. The caller knows the session is complete
5183 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005184 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005185 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5186 channel_shutr_now(chn);
5187 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005188 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005189 }
5190 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005191 /* The last possible modes are keep-alive and tunnel. Tunnel will
5192 * need to forward remaining data. Keep-alive will need to monitor
5193 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005194 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005195 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005196 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005197 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5198 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005199 }
5200
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005201 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005202 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005203 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005204 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5205 goto http_msg_closing;
5206 }
5207 else {
5208 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5209 goto http_msg_closed;
5210 }
5211 }
5212 goto wait_other_side;
5213 }
5214
5215 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5216 http_msg_closing:
5217 /* nothing else to forward, just waiting for the output buffer
5218 * to be empty and for the shutw_now to take effect.
5219 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005220 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005221 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5222 goto http_msg_closed;
5223 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005224 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005225 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005226 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005227 if (objt_server(s->target))
5228 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005229 goto wait_other_side;
5230 }
5231 }
5232
5233 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5234 http_msg_closed:
5235 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005236 bi_erase(chn);
5237 channel_auto_close(chn);
5238 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005239 goto wait_other_side;
5240 }
5241
5242 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005243 /* We force the response to leave immediately if we're waiting for the
5244 * other side, since there is no pending shutdown to push it out.
5245 */
5246 if (!channel_is_empty(chn))
5247 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005248 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005249}
5250
5251
5252/* Resync the request and response state machines. Return 1 if either state
5253 * changes.
5254 */
5255int http_resync_states(struct session *s)
5256{
5257 struct http_txn *txn = &s->txn;
5258 int old_req_state = txn->req.msg_state;
5259 int old_res_state = txn->rsp.msg_state;
5260
Willy Tarreau610ecce2010-01-04 21:15:02 +01005261 http_sync_req_state(s);
5262 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005263 if (!http_sync_res_state(s))
5264 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005265 if (!http_sync_req_state(s))
5266 break;
5267 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005268
Willy Tarreau610ecce2010-01-04 21:15:02 +01005269 /* OK, both state machines agree on a compatible state.
5270 * There are a few cases we're interested in :
5271 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5272 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5273 * directions, so let's simply disable both analysers.
5274 * - HTTP_MSG_CLOSED on the response only means we must abort the
5275 * request.
5276 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5277 * with server-close mode means we've completed one request and we
5278 * must re-initialize the server connection.
5279 */
5280
5281 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5282 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5283 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5284 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5285 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005286 channel_auto_close(s->req);
5287 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005288 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005289 channel_auto_close(s->rep);
5290 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005291 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005292 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5293 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005294 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005295 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005296 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005297 channel_auto_close(s->rep);
5298 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01005299 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005300 channel_abort(s->req);
5301 channel_auto_close(s->req);
5302 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005303 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005304 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005305 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5306 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005307 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005308 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5309 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5310 /* server-close/keep-alive: terminate this transaction,
5311 * possibly killing the server connection and reinitialize
Willy Tarreauc72d85b2016-05-02 16:07:07 +02005312 * a fresh-new transaction, but only once we're sure there's
5313 * enough room in the request and response buffer to process
5314 * another request. The request buffer must not hold any
5315 * pending output data and the request buffer must not have
5316 * output data occupying the reserve.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005317 */
Willy Tarreauc72d85b2016-05-02 16:07:07 +02005318 if (s->req->buf->o)
5319 s->req->flags |= CF_WAKE_WRITE;
5320 else if (channel_congested(s->rep))
5321 s->rep->flags |= CF_WAKE_WRITE;
5322 else
5323 http_end_txn_clean_session(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005324 }
5325
Willy Tarreau610ecce2010-01-04 21:15:02 +01005326 return txn->req.msg_state != old_req_state ||
5327 txn->rsp.msg_state != old_res_state;
5328}
5329
Willy Tarreaud98cf932009-12-27 22:54:55 +01005330/* This function is an analyser which forwards request body (including chunk
5331 * sizes if any). It is called as soon as we must forward, even if we forward
5332 * zero byte. The only situation where it must not be called is when we're in
5333 * tunnel mode and we want to forward till the close. It's used both to forward
5334 * remaining data and to resync after end of body. It expects the msg_state to
5335 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5336 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005337 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005338 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005339 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005340int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005341{
5342 struct http_txn *txn = &s->txn;
5343 struct http_msg *msg = &s->txn.req;
5344
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005345 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5346 return 0;
5347
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005348 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005349 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005350 /* Output closed while we were sending data. We must abort and
5351 * wake the other side up.
5352 */
5353 msg->msg_state = HTTP_MSG_ERROR;
5354 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005355 return 1;
5356 }
5357
Willy Tarreaud98cf932009-12-27 22:54:55 +01005358 /* Note that we don't have to send 100-continue back because we don't
5359 * need the data to complete our job, and it's up to the server to
5360 * decide whether to return 100, 417 or anything else in return of
5361 * an "Expect: 100-continue" header.
5362 */
5363
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005364 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005365 /* we have msg->sov which points to the first byte of message
5366 * body, and req->buf.p still points to the beginning of the
5367 * message. We forward the headers now, as we don't need them
5368 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005369 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005370 b_adv(req->buf, msg->sov);
5371 msg->next -= msg->sov;
5372 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005373
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005374 /* The previous analysers guarantee that the state is somewhere
5375 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5376 * msg->next are always correct.
5377 */
5378 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5379 if (msg->flags & HTTP_MSGF_TE_CHNK)
5380 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5381 else
5382 msg->msg_state = HTTP_MSG_DATA;
5383 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005384 }
5385
Willy Tarreau7ba23542014-04-17 21:50:00 +02005386 /* Some post-connect processing might want us to refrain from starting to
5387 * forward data. Currently, the only reason for this is "balance url_param"
5388 * whichs need to parse/process the request after we've enabled forwarding.
5389 */
5390 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5391 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5392 channel_auto_connect(req);
Willy Tarreau644c1012014-04-30 18:11:11 +02005393 req->flags |= CF_WAKE_CONNECT;
Willy Tarreau7ba23542014-04-17 21:50:00 +02005394 goto missing_data;
5395 }
5396 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5397 }
5398
Willy Tarreau80a92c02014-03-12 10:41:13 +01005399 /* in most states, we should abort in case of early close */
5400 channel_auto_close(req);
5401
Willy Tarreauefdf0942014-04-24 20:08:57 +02005402 if (req->to_forward) {
5403 /* We can't process the buffer's contents yet */
5404 req->flags |= CF_WAKE_WRITE;
5405 goto missing_data;
5406 }
5407
Willy Tarreaud98cf932009-12-27 22:54:55 +01005408 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005409 if (msg->msg_state == HTTP_MSG_DATA) {
5410 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005411 /* we may have some pending data starting at req->buf->p */
5412 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005413 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005414 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005415 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005416 msg->next += msg->chunk_len;
5417 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005418
5419 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005420 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005421 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005422 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005423 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005424 }
5425 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005426 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005427 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005428 * TRAILERS state.
5429 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005430 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005431
Willy Tarreau54d23df2012-10-25 19:04:45 +02005432 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005433 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005434 else if (ret < 0) {
5435 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005436 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005437 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005438 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005439 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005440 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005441 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005442 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005443 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005444 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005445
5446 if (ret == 0)
5447 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005448 else if (ret < 0) {
5449 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005450 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005451 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005452 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005453 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005454 /* we're in MSG_CHUNK_SIZE now */
5455 }
5456 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005457 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005458
5459 if (ret == 0)
5460 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005461 else if (ret < 0) {
5462 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005463 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005464 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005465 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005466 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005467 /* we're in HTTP_MSG_DONE now */
5468 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005469 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005470 int old_state = msg->msg_state;
5471
Willy Tarreau610ecce2010-01-04 21:15:02 +01005472 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005473
5474 /* we may have some pending data starting at req->buf->p
5475 * such as last chunk of data or trailers.
5476 */
5477 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005478 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005479 msg->sov -= msg->next;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005480 msg->next = 0;
5481
Willy Tarreau294e4672015-05-11 18:30:33 +02005482 /* we don't want to forward closes on DONE except in
5483 * tunnel mode.
5484 */
5485 if ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005486 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005487 if (http_resync_states(s)) {
5488 /* some state changes occurred, maybe the analyser
5489 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005490 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005491 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005492 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005493 /* request errors are most likely due to
5494 * the server aborting the transfer.
5495 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005496 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005497 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005498 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005499 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005500 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005501 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005502 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005503 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005504
5505 /* If "option abortonclose" is set on the backend, we
5506 * want to monitor the client's connection and forward
5507 * any shutdown notification to the server, which will
5508 * decide whether to close or to go on processing the
Willy Tarreau294e4672015-05-11 18:30:33 +02005509 * request. We only do that in tunnel mode, and not in
5510 * other modes since it can be abused to exhaust source
5511 * ports.
Willy Tarreau5c54c712010-07-17 08:02:58 +02005512 */
5513 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005514 channel_auto_read(req);
Willy Tarreau294e4672015-05-11 18:30:33 +02005515 if ((req->flags & (CF_SHUTR|CF_READ_NULL)) &&
5516 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN))
5517 s->si[1].flags |= SI_FL_NOLINGER;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005518 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005519 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005520 else if (s->txn.meth == HTTP_METH_POST) {
5521 /* POST requests may require to read extra CRLF
5522 * sent by broken browsers and which could cause
5523 * an RST to be sent upon close on some systems
5524 * (eg: Linux).
5525 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005526 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005527 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005528
Willy Tarreau610ecce2010-01-04 21:15:02 +01005529 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005530 }
5531 }
5532
Willy Tarreaud98cf932009-12-27 22:54:55 +01005533 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005534 /* we may have some pending data starting at req->buf->p */
5535 b_adv(req->buf, msg->next);
Willy Tarreaub4d05092014-09-01 20:35:55 +02005536 if (unlikely(!(s->req->flags & CF_WROTE_DATA)))
Willy Tarreau5bebcd02014-07-10 19:06:10 +02005537 msg->sov -= msg->next + MIN(msg->chunk_len, req->buf->i);
5538
Willy Tarreaubed410e2014-04-22 08:19:34 +02005539 msg->next = 0;
5540 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5541
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005542 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005543 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005544 if (!(s->flags & SN_ERR_MASK))
5545 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005546 if (!(s->flags & SN_FINST_MASK)) {
5547 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5548 s->flags |= SN_FINST_H;
5549 else
5550 s->flags |= SN_FINST_D;
5551 }
5552
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005553 s->fe->fe_counters.cli_aborts++;
5554 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005555 if (objt_server(s->target))
5556 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005557
5558 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005559 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005560
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005561 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005562 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005563 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005564
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005565 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005566 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005567 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005568 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005569 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005570
Willy Tarreau5c620922011-05-11 19:56:11 +02005571 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005572 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005573 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005574 * modes are already handled by the stream sock layer. We must not do
5575 * this in content-length mode because it could present the MSG_MORE
5576 * flag with the last block of forwarded data, which would cause an
5577 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005578 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005579 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005580 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005581
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005582 return 0;
5583
Willy Tarreaud98cf932009-12-27 22:54:55 +01005584 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005585 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005586 if (s->listener->counters)
5587 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005588
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005589 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005590 /* we may have some pending data starting at req->buf->p */
5591 b_adv(req->buf, msg->next);
5592 msg->next = 0;
5593
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005594 txn->req.msg_state = HTTP_MSG_ERROR;
5595 if (txn->status) {
5596 /* Note: we don't send any error if some data were already sent */
5597 stream_int_retnclose(req->prod, NULL);
5598 } else {
5599 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005600 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005601 }
5602 req->analysers = 0;
5603 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005604
5605 if (!(s->flags & SN_ERR_MASK))
5606 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005607 if (!(s->flags & SN_FINST_MASK)) {
5608 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5609 s->flags |= SN_FINST_H;
5610 else
5611 s->flags |= SN_FINST_D;
5612 }
5613 return 0;
5614
5615 aborted_xfer:
5616 txn->req.msg_state = HTTP_MSG_ERROR;
5617 if (txn->status) {
5618 /* Note: we don't send any error if some data were already sent */
5619 stream_int_retnclose(req->prod, NULL);
5620 } else {
5621 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005622 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005623 }
5624 req->analysers = 0;
5625 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5626
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005627 s->fe->fe_counters.srv_aborts++;
5628 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005629 if (objt_server(s->target))
5630 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005631
5632 if (!(s->flags & SN_ERR_MASK))
5633 s->flags |= SN_ERR_SRVCL;
5634 if (!(s->flags & SN_FINST_MASK)) {
5635 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5636 s->flags |= SN_FINST_H;
5637 else
5638 s->flags |= SN_FINST_D;
5639 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005640 return 0;
5641}
5642
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005643/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5644 * processing can continue on next analysers, or zero if it either needs more
5645 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5646 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5647 * when it has nothing left to do, and may remove any analyser when it wants to
5648 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005649 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005650int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005651{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005652 struct http_txn *txn = &s->txn;
5653 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005654 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005655 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005656 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005657 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005658
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005659 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 +02005660 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005661 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005662 rep,
5663 rep->rex, rep->wex,
5664 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005665 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005666 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005667
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005668 /*
5669 * Now parse the partial (or complete) lines.
5670 * We will check the response syntax, and also join multi-line
5671 * headers. An index of all the lines will be elaborated while
5672 * parsing.
5673 *
5674 * For the parsing, we use a 28 states FSM.
5675 *
5676 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005677 * rep->buf->p = beginning of response
5678 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5679 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005680 * msg->eol = end of current header or line (LF or CRLF)
5681 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005682 */
5683
Willy Tarreau628c40c2014-04-24 19:11:26 +02005684 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005685 /* There's a protected area at the end of the buffer for rewriting
5686 * purposes. We don't want to start to parse the request if the
5687 * protected area is affected, because we may have to move processed
5688 * data later, which is much more complicated.
5689 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005690 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005691 if (unlikely(!channel_reserved(rep))) {
5692 /* some data has still not left the buffer, wake us once that's done */
5693 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5694 goto abort_response;
5695 channel_dont_close(rep);
5696 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005697 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005698 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005699 }
5700
Willy Tarreau379357a2013-06-08 12:55:46 +02005701 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5702 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5703 buffer_slow_realign(rep->buf);
5704
Willy Tarreau9b28e032012-10-12 23:49:43 +02005705 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005706 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005707 }
5708
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005709 /* 1: we might have to print this header in debug mode */
5710 if (unlikely((global.mode & MODE_DEBUG) &&
5711 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau8767b132014-10-21 19:36:09 +02005712 msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005713 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005714
Willy Tarreau9b28e032012-10-12 23:49:43 +02005715 sol = rep->buf->p;
5716 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005717 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005718
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005719 sol += hdr_idx_first_pos(&txn->hdr_idx);
5720 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005721
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005722 while (cur_idx) {
5723 eol = sol + txn->hdr_idx.v[cur_idx].len;
5724 debug_hdr("srvhdr", s, sol, eol);
5725 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5726 cur_idx = txn->hdr_idx.v[cur_idx].next;
5727 }
5728 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005729
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005730 /*
5731 * Now we quickly check if we have found a full valid response.
5732 * If not so, we check the FD and buffer states before leaving.
5733 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005734 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005735 * responses are checked first.
5736 *
5737 * Depending on whether the client is still there or not, we
5738 * may send an error response back or not. Note that normally
5739 * we should only check for HTTP status there, and check I/O
5740 * errors somewhere else.
5741 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005742
Willy Tarreau655dce92009-11-08 13:10:58 +01005743 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005744 /* Invalid response */
5745 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5746 /* we detected a parsing error. We want to archive this response
5747 * in the dedicated proxy area for later troubleshooting.
5748 */
5749 hdr_response_bad:
5750 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005751 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005752
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005753 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005754 if (objt_server(s->target)) {
5755 objt_server(s->target)->counters.failed_resp++;
5756 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005757 }
Willy Tarreau64648412010-03-05 10:41:54 +01005758 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005759 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005760 rep->analysers = 0;
5761 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005762 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005763 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005764 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005765
5766 if (!(s->flags & SN_ERR_MASK))
5767 s->flags |= SN_ERR_PRXCOND;
5768 if (!(s->flags & SN_FINST_MASK))
5769 s->flags |= SN_FINST_H;
5770
5771 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005772 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005773
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005774 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005775 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005776 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005777 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005778 goto hdr_response_bad;
5779 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005780
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005781 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005782 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005783 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005784 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005785 else if (txn->flags & TX_NOT_FIRST)
5786 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005787
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005788 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005789 if (objt_server(s->target)) {
5790 objt_server(s->target)->counters.failed_resp++;
5791 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005792 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005793
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005794 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005795 rep->analysers = 0;
5796 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005797 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005798 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005799 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005800
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005801 if (!(s->flags & SN_ERR_MASK))
5802 s->flags |= SN_ERR_SRVCL;
5803 if (!(s->flags & SN_FINST_MASK))
5804 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005805 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005806 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005807
Willy Tarreau2a4f5112014-06-23 15:22:31 +02005808 /* read timeout : return a 504 to the client. */
5809 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005810 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005811 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005812
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005813 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005814 if (objt_server(s->target)) {
5815 objt_server(s->target)->counters.failed_resp++;
5816 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005817 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005818
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005819 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005820 rep->analysers = 0;
5821 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005822 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005823 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005824 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005825
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005826 if (!(s->flags & SN_ERR_MASK))
5827 s->flags |= SN_ERR_SRVTO;
5828 if (!(s->flags & SN_FINST_MASK))
5829 s->flags |= SN_FINST_H;
5830 return 0;
5831 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005832
Willy Tarreauf003d372012-11-26 13:35:37 +01005833 /* client abort with an abortonclose */
5834 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5835 s->fe->fe_counters.cli_aborts++;
5836 s->be->be_counters.cli_aborts++;
5837 if (objt_server(s->target))
5838 objt_server(s->target)->counters.cli_aborts++;
5839
5840 rep->analysers = 0;
5841 channel_auto_close(rep);
5842
5843 txn->status = 400;
5844 bi_erase(rep);
5845 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5846
5847 if (!(s->flags & SN_ERR_MASK))
5848 s->flags |= SN_ERR_CLICL;
5849 if (!(s->flags & SN_FINST_MASK))
5850 s->flags |= SN_FINST_H;
5851
5852 /* process_session() will take care of the error */
5853 return 0;
5854 }
5855
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005856 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005857 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005858 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005859 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005860 else if (txn->flags & TX_NOT_FIRST)
5861 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005862
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005863 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005864 if (objt_server(s->target)) {
5865 objt_server(s->target)->counters.failed_resp++;
5866 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005867 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005868
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005869 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005870 rep->analysers = 0;
5871 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005872 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005873 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005874 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005875
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005876 if (!(s->flags & SN_ERR_MASK))
5877 s->flags |= SN_ERR_SRVCL;
5878 if (!(s->flags & SN_FINST_MASK))
5879 s->flags |= SN_FINST_H;
5880 return 0;
5881 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005882
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005883 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005884 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005885 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005886 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005887 else if (txn->flags & TX_NOT_FIRST)
5888 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005889
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005890 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005891 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005892 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005893
5894 if (!(s->flags & SN_ERR_MASK))
5895 s->flags |= SN_ERR_CLICL;
5896 if (!(s->flags & SN_FINST_MASK))
5897 s->flags |= SN_FINST_H;
5898
5899 /* process_session() will take care of the error */
5900 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005901 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005902
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005903 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005904 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005905 return 0;
5906 }
5907
5908 /* More interesting part now : we know that we have a complete
5909 * response which at least looks like HTTP. We have an indicator
5910 * of each header's length, so we can parse them quickly.
5911 */
5912
5913 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005914 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005915
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005916 /*
5917 * 1: get the status code
5918 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005919 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005920 if (n < 1 || n > 5)
5921 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005922 /* when the client triggers a 4xx from the server, it's most often due
5923 * to a missing object or permission. These events should be tracked
5924 * because if they happen often, it may indicate a brute force or a
5925 * vulnerability scan.
5926 */
5927 if (n == 4)
5928 session_inc_http_err_ctr(s);
5929
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005930 if (objt_server(s->target))
5931 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005932
Willy Tarreau55645552015-05-01 13:26:00 +02005933 /* RFC7230#2.6 has enforced the format of the HTTP version string to be
5934 * exactly one digit "." one digit. This check may be disabled using
5935 * option accept-invalid-http-response.
5936 */
5937 if (!(s->be->options2 & PR_O2_RSPBUG_OK)) {
5938 if (msg->sl.st.v_l != 8) {
5939 msg->err_pos = 0;
5940 goto hdr_response_bad;
5941 }
5942
5943 if (rep->buf->p[4] != '/' ||
5944 !isdigit((unsigned char)rep->buf->p[5]) ||
5945 rep->buf->p[6] != '.' ||
5946 !isdigit((unsigned char)rep->buf->p[7])) {
5947 msg->err_pos = 4;
5948 goto hdr_response_bad;
5949 }
5950 }
5951
Willy Tarreau5b154472009-12-21 20:11:07 +01005952 /* check if the response is HTTP/1.1 or above */
5953 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005954 ((rep->buf->p[5] > '1') ||
5955 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005956 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005957
5958 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005959 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 +01005960
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005961 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005962 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005963
Willy Tarreau9b28e032012-10-12 23:49:43 +02005964 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005965
Willy Tarreau39650402010-03-15 19:44:39 +01005966 /* Adjust server's health based on status code. Note: status codes 501
5967 * and 505 are triggered on demand by client request, so we must not
5968 * count them as server failures.
5969 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005970 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005971 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005972 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005973 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005974 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005975 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005976
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005977 /*
5978 * 2: check for cacheability.
5979 */
5980
5981 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005982 case 100:
5983 /*
5984 * We may be facing a 100-continue response, in which case this
5985 * is not the right response, and we're waiting for the next one.
5986 * Let's allow this response to go to the client and wait for the
5987 * next one.
5988 */
5989 hdr_idx_init(&txn->hdr_idx);
5990 msg->next -= channel_forward(rep, msg->next);
5991 msg->msg_state = HTTP_MSG_RPBEFORE;
5992 txn->status = 0;
5993 s->logs.t_data = -1; /* was not a response yet */
5994 goto next_one;
5995
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005996 case 200:
5997 case 203:
5998 case 206:
5999 case 300:
6000 case 301:
6001 case 410:
6002 /* RFC2616 @13.4:
6003 * "A response received with a status code of
6004 * 200, 203, 206, 300, 301 or 410 MAY be stored
6005 * by a cache (...) unless a cache-control
6006 * directive prohibits caching."
6007 *
6008 * RFC2616 @9.5: POST method :
6009 * "Responses to this method are not cacheable,
6010 * unless the response includes appropriate
6011 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006012 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006013 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02006014 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006015 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
6016 break;
6017 default:
6018 break;
6019 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006020
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006021 /*
6022 * 3: we may need to capture headers
6023 */
6024 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01006025 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02006026 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02006027 txn->rsp.cap, s->fe->rsp_cap);
6028
Willy Tarreau4db603d2015-05-01 10:05:17 +02006029 /* 4: determine the transfer-length according to RFC2616 #4.4, updated
6030 * by RFC7230#3.3.3 :
6031 *
6032 * The length of a message body is determined by one of the following
6033 * (in order of precedence):
6034 *
6035 * 1. Any response to a HEAD request and any response with a 1xx
6036 * (Informational), 204 (No Content), or 304 (Not Modified) status
6037 * code is always terminated by the first empty line after the
6038 * header fields, regardless of the header fields present in the
6039 * message, and thus cannot contain a message body.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006040 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006041 * 2. Any 2xx (Successful) response to a CONNECT request implies that
6042 * the connection will become a tunnel immediately after the empty
6043 * line that concludes the header fields. A client MUST ignore any
6044 * Content-Length or Transfer-Encoding header fields received in
6045 * such a message.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006046 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006047 * 3. If a Transfer-Encoding header field is present and the chunked
6048 * transfer coding (Section 4.1) is the final encoding, the message
6049 * body length is determined by reading and decoding the chunked
6050 * data until the transfer coding indicates the data is complete.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006051 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006052 * If a Transfer-Encoding header field is present in a response and
6053 * the chunked transfer coding is not the final encoding, the
6054 * message body length is determined by reading the connection until
6055 * it is closed by the server. If a Transfer-Encoding header field
6056 * is present in a request and the chunked transfer coding is not
6057 * the final encoding, the message body length cannot be determined
6058 * reliably; the server MUST respond with the 400 (Bad Request)
6059 * status code and then close the connection.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006060 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006061 * If a message is received with both a Transfer-Encoding and a
6062 * Content-Length header field, the Transfer-Encoding overrides the
6063 * Content-Length. Such a message might indicate an attempt to
6064 * perform request smuggling (Section 9.5) or response splitting
6065 * (Section 9.4) and ought to be handled as an error. A sender MUST
6066 * remove the received Content-Length field prior to forwarding such
6067 * a message downstream.
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006068 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006069 * 4. If a message is received without Transfer-Encoding and with
6070 * either multiple Content-Length header fields having differing
6071 * field-values or a single Content-Length header field having an
6072 * invalid value, then the message framing is invalid and the
6073 * recipient MUST treat it as an unrecoverable error. If this is a
6074 * request message, the server MUST respond with a 400 (Bad Request)
6075 * status code and then close the connection. If this is a response
6076 * message received by a proxy, the proxy MUST close the connection
6077 * to the server, discard the received response, and send a 502 (Bad
6078 * Gateway) response to the client. If this is a response message
6079 * received by a user agent, the user agent MUST close the
6080 * connection to the server and discard the received response.
6081 *
6082 * 5. If a valid Content-Length header field is present without
6083 * Transfer-Encoding, its decimal value defines the expected message
6084 * body length in octets. If the sender closes the connection or
6085 * the recipient times out before the indicated number of octets are
6086 * received, the recipient MUST consider the message to be
6087 * incomplete and close the connection.
6088 *
6089 * 6. If this is a request message and none of the above are true, then
6090 * the message body length is zero (no message body is present).
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006091 *
Willy Tarreau4db603d2015-05-01 10:05:17 +02006092 * 7. Otherwise, this is a response message without a declared message
6093 * body length, so the message body length is determined by the
6094 * number of octets received prior to the server closing the
6095 * connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006096 */
6097
6098 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01006099 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006100 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006101 * FIXME: should we parse anyway and return an error on chunked encoding ?
6102 */
6103 if (txn->meth == HTTP_METH_HEAD ||
6104 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006105 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006106 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01006107 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006108 goto skip_content_length;
6109 }
6110
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006111 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006112 ctx.idx = 0;
Willy Tarreaue77bc172015-05-01 10:06:30 +02006113 while (http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006114 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006115 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
6116 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006117 /* bad transfer-encoding (chunked followed by something else) */
6118 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006119 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01006120 break;
6121 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006122 }
6123
Willy Tarreaudfa3d922015-04-30 10:57:51 +02006124 /* Chunked responses must have their content-length removed */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006125 ctx.idx = 0;
Willy Tarreau660418d2015-05-01 10:25:45 +02006126 if (use_close_only || (msg->flags & HTTP_MSGF_TE_CHNK)) {
Willy Tarreaudfa3d922015-04-30 10:57:51 +02006127 while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx))
6128 http_remove_header2(msg, &txn->hdr_idx, &ctx);
6129 }
Willy Tarreau660418d2015-05-01 10:25:45 +02006130 else while (http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006131 signed long long cl;
6132
Willy Tarreauad14f752011-09-02 20:33:27 +02006133 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006134 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006135 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006136 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006137
Willy Tarreauad14f752011-09-02 20:33:27 +02006138 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006139 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006140 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02006141 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006142
Willy Tarreauad14f752011-09-02 20:33:27 +02006143 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006144 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006145 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02006146 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006147
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006148 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006149 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006150 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02006151 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006152
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006153 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01006154 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006155 }
6156
William Lallemand82fe75c2012-10-23 10:25:10 +02006157 if (s->fe->comp || s->be->comp)
6158 select_compression_response_header(s, rep->buf);
6159
Willy Tarreaub8c82c22009-10-18 23:45:12 +02006160skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01006161 /* Now we have to check if we need to modify the Connection header.
6162 * This is more difficult on the response than it is on the request,
6163 * because we can have two different HTTP versions and we don't know
6164 * how the client will interprete a response. For instance, let's say
6165 * that the client sends a keep-alive request in HTTP/1.0 and gets an
6166 * HTTP/1.1 response without any header. Maybe it will bound itself to
6167 * HTTP/1.0 because it only knows about it, and will consider the lack
6168 * of header as a close, or maybe it knows HTTP/1.1 and can consider
6169 * the lack of header as a keep-alive. Thus we will use two flags
6170 * indicating how a request MAY be understood by the client. In case
6171 * of multiple possibilities, we'll fix the header to be explicit. If
6172 * ambiguous cases such as both close and keepalive are seen, then we
6173 * will fall back to explicit close. Note that we won't take risks with
6174 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01006175 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01006176 */
6177
Willy Tarreaudc008c52010-02-01 16:20:08 +01006178 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
6179 txn->status == 101)) {
6180 /* Either we've established an explicit tunnel, or we're
6181 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006182 * to understand the next protocols. We have to switch to tunnel
6183 * mode, so that we transfer the request and responses then let
6184 * this protocol pass unmodified. When we later implement specific
6185 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01006186 * header which contains information about that protocol for
6187 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006188 */
6189 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
6190 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01006191 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
6192 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006193 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6194 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006195 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01006196
Willy Tarreau70dffda2014-01-30 03:07:23 +01006197 /* this situation happens when combining pretend-keepalive with httpclose. */
6198 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006199 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6200 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01006201 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
6202
Willy Tarreau60466522010-01-18 19:08:45 +01006203 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006204 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01006205 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
6206 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01006207
Willy Tarreau60466522010-01-18 19:08:45 +01006208 /* now adjust header transformations depending on current state */
6209 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
6210 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
6211 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006212 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006213 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006214 }
Willy Tarreau60466522010-01-18 19:08:45 +01006215 else { /* SCL / KAL */
6216 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006217 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006218 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01006219 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006220
Willy Tarreau60466522010-01-18 19:08:45 +01006221 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006222 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01006223
Willy Tarreau60466522010-01-18 19:08:45 +01006224 /* Some keep-alive responses are converted to Server-close if
6225 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01006226 */
Willy Tarreau60466522010-01-18 19:08:45 +01006227 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
6228 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006229 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01006230 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01006231 }
Willy Tarreau5b154472009-12-21 20:11:07 +01006232 }
6233
Willy Tarreau7959a552013-09-23 16:44:27 +02006234 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006235 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006236
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006237 /* end of job, return OK */
6238 rep->analysers &= ~an_bit;
6239 rep->analyse_exp = TICK_ETERNITY;
6240 channel_auto_close(rep);
6241 return 1;
6242
6243 abort_keep_alive:
6244 /* A keep-alive request to the server failed on a network error.
6245 * The client is required to retry. We need to close without returning
6246 * any other information so that the client retries.
6247 */
6248 txn->status = 0;
6249 rep->analysers = 0;
6250 s->req->analysers = 0;
6251 channel_auto_close(rep);
6252 s->logs.logwait = 0;
6253 s->logs.level = 0;
6254 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
6255 bi_erase(rep);
6256 stream_int_retnclose(rep->cons, NULL);
6257 return 0;
6258}
6259
6260/* This function performs all the processing enabled for the current response.
6261 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6262 * and updates s->rep->analysers. It might make sense to explode it into several
6263 * other functions. It works like process_request (see indications above).
6264 */
6265int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6266{
6267 struct http_txn *txn = &s->txn;
6268 struct http_msg *msg = &txn->rsp;
6269 struct proxy *cur_proxy;
6270 struct cond_wordlist *wl;
6271 struct http_res_rule *http_res_last_rule = NULL;
6272
6273 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6274 now_ms, __FUNCTION__,
6275 s,
6276 rep,
6277 rep->rex, rep->wex,
6278 rep->flags,
6279 rep->buf->i,
6280 rep->analysers);
6281
6282 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6283 return 0;
6284
6285 rep->analysers &= ~an_bit;
6286 rep->analyse_exp = TICK_ETERNITY;
6287
Willy Tarreau70730dd2014-04-24 18:06:27 +02006288 /* The stats applet needs to adjust the Connection header but we don't
6289 * apply any filter there.
6290 */
6291 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6292 goto skip_filters;
6293
Willy Tarreau58975672014-04-24 21:13:57 +02006294 /*
6295 * We will have to evaluate the filters.
6296 * As opposed to version 1.2, now they will be evaluated in the
6297 * filters order and not in the header order. This means that
6298 * each filter has to be validated among all headers.
6299 *
6300 * Filters are tried with ->be first, then with ->fe if it is
6301 * different from ->be.
6302 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006303
Willy Tarreau58975672014-04-24 21:13:57 +02006304 cur_proxy = s->be;
6305 while (1) {
6306 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006307
Willy Tarreau58975672014-04-24 21:13:57 +02006308 /* evaluate http-response rules */
6309 if (!http_res_last_rule)
6310 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 +02006311
Willy Tarreau58975672014-04-24 21:13:57 +02006312 /* try headers filters */
6313 if (rule_set->rsp_exp != NULL) {
6314 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6315 return_bad_resp:
6316 if (objt_server(s->target)) {
6317 objt_server(s->target)->counters.failed_resp++;
6318 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006319 }
Willy Tarreau58975672014-04-24 21:13:57 +02006320 s->be->be_counters.failed_resp++;
6321 return_srv_prx_502:
6322 rep->analysers = 0;
6323 txn->status = 502;
6324 s->logs.t_data = -1; /* was not a valid response */
6325 rep->prod->flags |= SI_FL_NOLINGER;
6326 bi_erase(rep);
6327 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6328 if (!(s->flags & SN_ERR_MASK))
6329 s->flags |= SN_ERR_PRXCOND;
6330 if (!(s->flags & SN_FINST_MASK))
6331 s->flags |= SN_FINST_H;
6332 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006333 }
Willy Tarreau58975672014-04-24 21:13:57 +02006334 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006335
Willy Tarreau58975672014-04-24 21:13:57 +02006336 /* has the response been denied ? */
6337 if (txn->flags & TX_SVDENY) {
6338 if (objt_server(s->target))
6339 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006340
Willy Tarreau58975672014-04-24 21:13:57 +02006341 s->be->be_counters.denied_resp++;
6342 s->fe->fe_counters.denied_resp++;
6343 if (s->listener->counters)
6344 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006345
Willy Tarreau58975672014-04-24 21:13:57 +02006346 goto return_srv_prx_502;
6347 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006348
Willy Tarreau58975672014-04-24 21:13:57 +02006349 /* add response headers from the rule sets in the same order */
6350 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006351 if (txn->status < 200 && txn->status != 101)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006352 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006353 if (wl->cond) {
6354 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6355 ret = acl_pass(ret);
6356 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6357 ret = !ret;
6358 if (!ret)
6359 continue;
6360 }
6361 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6362 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006363 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006364
Willy Tarreau58975672014-04-24 21:13:57 +02006365 /* check whether we're already working on the frontend */
6366 if (cur_proxy == s->fe)
6367 break;
6368 cur_proxy = s->fe;
6369 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006370
Willy Tarreau58975672014-04-24 21:13:57 +02006371 /* OK that's all we can do for 1xx responses */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006372 if (unlikely(txn->status < 200 && txn->status != 101))
Willy Tarreau58975672014-04-24 21:13:57 +02006373 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006374
Willy Tarreau58975672014-04-24 21:13:57 +02006375 /*
6376 * Now check for a server cookie.
6377 */
6378 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6379 (s->be->options & PR_O_CHK_CACHE))
6380 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006381
Willy Tarreau58975672014-04-24 21:13:57 +02006382 /*
6383 * Check for cache-control or pragma headers if required.
6384 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006385 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 +02006386 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006387
Willy Tarreau58975672014-04-24 21:13:57 +02006388 /*
6389 * Add server cookie in the response if needed
6390 */
6391 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6392 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6393 (!(s->flags & SN_DIRECT) ||
6394 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6395 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6396 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6397 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6398 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6399 !(s->flags & SN_IGNORE_PRST)) {
6400 /* the server is known, it's not the one the client requested, or the
6401 * cookie's last seen date needs to be refreshed. We have to
6402 * insert a set-cookie here, except if we want to insert only on POST
6403 * requests and this one isn't. Note that servers which don't have cookies
6404 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006405 */
Willy Tarreau58975672014-04-24 21:13:57 +02006406 if (!objt_server(s->target)->cookie) {
6407 chunk_printf(&trash,
6408 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6409 s->be->cookie_name);
6410 }
6411 else {
6412 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006413
Willy Tarreau58975672014-04-24 21:13:57 +02006414 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6415 /* emit last_date, which is mandatory */
6416 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6417 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6418 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006419
Willy Tarreau58975672014-04-24 21:13:57 +02006420 if (s->be->cookie_maxlife) {
6421 /* emit first_date, which is either the original one or
6422 * the current date.
6423 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006424 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006425 s30tob64(txn->cookie_first_date ?
6426 txn->cookie_first_date >> 2 :
6427 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006428 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006429 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006430 }
Willy Tarreau58975672014-04-24 21:13:57 +02006431 chunk_appendf(&trash, "; path=/");
6432 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006433
Willy Tarreau58975672014-04-24 21:13:57 +02006434 if (s->be->cookie_domain)
6435 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006436
Willy Tarreau58975672014-04-24 21:13:57 +02006437 if (s->be->ck_opts & PR_CK_HTTPONLY)
6438 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006439
Willy Tarreau58975672014-04-24 21:13:57 +02006440 if (s->be->ck_opts & PR_CK_SECURE)
6441 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006442
Willy Tarreau58975672014-04-24 21:13:57 +02006443 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6444 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006445
Willy Tarreau58975672014-04-24 21:13:57 +02006446 txn->flags &= ~TX_SCK_MASK;
6447 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6448 /* the server did not change, only the date was updated */
6449 txn->flags |= TX_SCK_UPDATED;
6450 else
6451 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006452
Willy Tarreau58975672014-04-24 21:13:57 +02006453 /* Here, we will tell an eventual cache on the client side that we don't
6454 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6455 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6456 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006457 */
Willy Tarreau58975672014-04-24 21:13:57 +02006458 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006459
Willy Tarreau58975672014-04-24 21:13:57 +02006460 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006461
Willy Tarreau58975672014-04-24 21:13:57 +02006462 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6463 "Cache-control: private", 22) < 0))
6464 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006465 }
Willy Tarreau58975672014-04-24 21:13:57 +02006466 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006467
Willy Tarreau58975672014-04-24 21:13:57 +02006468 /*
6469 * Check if result will be cacheable with a cookie.
6470 * We'll block the response if security checks have caught
6471 * nasty things such as a cacheable cookie.
6472 */
6473 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6474 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6475 (s->be->options & PR_O_CHK_CACHE)) {
6476 /* we're in presence of a cacheable response containing
6477 * a set-cookie header. We'll block it as requested by
6478 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006479 */
Willy Tarreau58975672014-04-24 21:13:57 +02006480 if (objt_server(s->target))
6481 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006482
Willy Tarreau58975672014-04-24 21:13:57 +02006483 s->be->be_counters.denied_resp++;
6484 s->fe->fe_counters.denied_resp++;
6485 if (s->listener->counters)
6486 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006487
Willy Tarreau58975672014-04-24 21:13:57 +02006488 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6489 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6490 send_log(s->be, LOG_ALERT,
6491 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6492 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6493 goto return_srv_prx_502;
6494 }
Willy Tarreau03945942009-12-22 16:50:27 +01006495
Willy Tarreau70730dd2014-04-24 18:06:27 +02006496 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006497 /*
6498 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6499 * If an "Upgrade" token is found, the header is left untouched in order
6500 * not to have to deal with some client bugs : some of them fail an upgrade
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006501 * if anything but "Upgrade" is present in the Connection header. We don't
6502 * want to touch any 101 response either since it's switching to another
6503 * protocol.
Willy Tarreau58975672014-04-24 21:13:57 +02006504 */
Willy Tarreau0cb4b892014-09-16 10:40:38 +02006505 if ((txn->status != 101) && !(txn->flags & TX_HDR_CONN_UPG) &&
Willy Tarreau58975672014-04-24 21:13:57 +02006506 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6507 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6508 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6509 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006510
Willy Tarreau58975672014-04-24 21:13:57 +02006511 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6512 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6513 /* we want a keep-alive response here. Keep-alive header
6514 * required if either side is not 1.1.
6515 */
6516 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6517 want_flags |= TX_CON_KAL_SET;
6518 }
6519 else {
6520 /* we want a close response here. Close header required if
6521 * the server is 1.1, regardless of the client.
6522 */
6523 if (msg->flags & HTTP_MSGF_VER_11)
6524 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006525 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006526
Willy Tarreau58975672014-04-24 21:13:57 +02006527 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6528 http_change_connection_header(txn, msg, want_flags);
6529 }
6530
6531 skip_header_mangling:
6532 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6533 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6534 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006535
Willy Tarreau58975672014-04-24 21:13:57 +02006536 /* if the user wants to log as soon as possible, without counting
6537 * bytes from the server, then this is the right moment. We have
6538 * to temporarily assign bytes_out to log what we currently have.
6539 */
6540 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6541 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6542 s->logs.bytes_out = txn->rsp.eoh;
6543 s->do_log(s);
6544 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006545 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006546 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006547}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006548
Willy Tarreaud98cf932009-12-27 22:54:55 +01006549/* This function is an analyser which forwards response body (including chunk
6550 * sizes if any). It is called as soon as we must forward, even if we forward
6551 * zero byte. The only situation where it must not be called is when we're in
6552 * tunnel mode and we want to forward till the close. It's used both to forward
6553 * remaining data and to resync after end of body. It expects the msg_state to
6554 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6555 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006556 *
6557 * It is capable of compressing response data both in content-length mode and
6558 * in chunked mode. The state machines follows different flows depending on
6559 * whether content-length and chunked modes are used, since there are no
6560 * trailers in content-length :
6561 *
6562 * chk-mode cl-mode
6563 * ,----- BODY -----.
6564 * / \
6565 * V size > 0 V chk-mode
6566 * .--> SIZE -------------> DATA -------------> CRLF
6567 * | | size == 0 | last byte |
6568 * | v final crlf v inspected |
6569 * | TRAILERS -----------> DONE |
6570 * | |
6571 * `----------------------------------------------'
6572 *
6573 * Compression only happens in the DATA state, and must be flushed in final
6574 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6575 * is performed at once on final states for all bytes parsed, or when leaving
6576 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006577 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006578int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006579{
6580 struct http_txn *txn = &s->txn;
6581 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006582 static struct buffer *tmpbuf = NULL;
6583 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006584 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006585
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006586 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6587 return 0;
6588
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006589 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006590 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006591 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006592 /* Output closed while we were sending data. We must abort and
6593 * wake the other side up.
6594 */
6595 msg->msg_state = HTTP_MSG_ERROR;
6596 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006597 return 1;
6598 }
6599
Willy Tarreau4fe41902010-06-07 22:27:41 +02006600 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006601 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006602
Willy Tarreau5bebcd02014-07-10 19:06:10 +02006603 if (msg->sov > 0) {
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006604 /* we have msg->sov which points to the first byte of message
6605 * body, and res->buf.p still points to the beginning of the
6606 * message. We forward the headers now, as we don't need them
6607 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006608 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006609 b_adv(res->buf, msg->sov);
6610 msg->next -= msg->sov;
6611 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006612
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006613 /* The previous analysers guarantee that the state is somewhere
6614 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6615 * msg->next are always correct.
6616 */
6617 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6618 if (msg->flags & HTTP_MSGF_TE_CHNK)
6619 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6620 else
6621 msg->msg_state = HTTP_MSG_DATA;
6622 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006623 }
6624
Willy Tarreauefdf0942014-04-24 20:08:57 +02006625 if (res->to_forward) {
6626 /* We can't process the buffer's contents yet */
6627 res->flags |= CF_WAKE_WRITE;
6628 goto missing_data;
6629 }
6630
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006631 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6632 /* We need a compression buffer in the DATA state to put the
6633 * output of compressed data, and in CRLF state to let the
6634 * TRAILERS state finish the job of removing the trailing CRLF.
6635 */
6636 if (unlikely(tmpbuf == NULL)) {
6637 /* this is the first time we need the compression buffer */
6638 tmpbuf = pool_alloc2(pool2_buffer);
6639 if (tmpbuf == NULL)
6640 goto aborted_xfer; /* no memory */
6641 }
6642
6643 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006644 if (ret < 0) {
6645 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006646 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006647 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006648 compressing = 1;
6649 }
6650
Willy Tarreaud98cf932009-12-27 22:54:55 +01006651 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006652 switch (msg->msg_state - HTTP_MSG_DATA) {
6653 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006654 /* we may have some pending data starting at res->buf->p */
6655 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006656 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006657 if (ret < 0)
6658 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006659
Willy Tarreaud5a67832014-04-21 10:54:27 +02006660 if (msg->chunk_len) {
6661 /* input empty or output full */
6662 if (res->buf->i > msg->next)
6663 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006664 goto missing_data;
6665 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006666 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006667 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006668 if (msg->chunk_len > res->buf->i - msg->next) {
6669 /* output full */
6670 res->flags |= CF_WAKE_WRITE;
6671 goto missing_data;
6672 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006673 msg->next += msg->chunk_len;
6674 msg->chunk_len = 0;
6675 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006676
6677 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006678 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006679 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006680 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006681 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006682 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006683 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006684 /* fall through for HTTP_MSG_CHUNK_CRLF */
6685
6686 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6687 /* we want the CRLF after the data */
6688
6689 ret = http_skip_chunk_crlf(msg);
6690 if (ret == 0)
6691 goto missing_data;
6692 else if (ret < 0) {
6693 if (msg->err_pos >= 0)
6694 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6695 goto return_bad_res;
6696 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006697 /* we're in MSG_CHUNK_SIZE now, fall through */
6698
6699 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006700 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006701 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006702 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006703 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006704
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006705 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006706 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006707 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006708 else if (ret < 0) {
6709 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006710 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006711 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006712 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006713 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006714 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006715
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006716 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006717 if (unlikely(compressing)) {
6718 /* we need to flush output contents before syncing FSMs */
6719 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6720 compressing = 0;
6721 }
6722
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006723 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006724 if (ret == 0)
6725 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006726 else if (ret < 0) {
6727 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006728 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006729 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006730 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006731 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006732
6733 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006734 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006735 if (unlikely(compressing)) {
6736 /* we need to flush output contents before syncing FSMs */
6737 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6738 compressing = 0;
6739 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006740
Willy Tarreauc623c172014-04-18 09:53:50 +02006741 /* we may have some pending data starting at res->buf->p
6742 * such as a last chunk of data or trailers.
6743 */
6744 b_adv(res->buf, msg->next);
6745 msg->next = 0;
6746
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006747 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006748 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006749 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6750 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006751 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006752
Willy Tarreau610ecce2010-01-04 21:15:02 +01006753 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006754 /* some state changes occurred, maybe the analyser
6755 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006756 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006757 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006758 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006759 /* response errors are most likely due to
6760 * the client aborting the transfer.
6761 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006762 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006763 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006764 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006765 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006766 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006767 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006768 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006769 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006770 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006771 }
6772 }
6773
Willy Tarreaud98cf932009-12-27 22:54:55 +01006774 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006775 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006776 if (unlikely(compressing)) {
6777 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006778 compressing = 0;
6779 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006780
Willy Tarreauc623c172014-04-18 09:53:50 +02006781 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6782 b_adv(res->buf, msg->next);
6783 msg->next = 0;
6784 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6785 }
6786
Willy Tarreauf003d372012-11-26 13:35:37 +01006787 if (res->flags & CF_SHUTW)
6788 goto aborted_xfer;
6789
6790 /* stop waiting for data if the input is closed before the end. If the
6791 * client side was already closed, it means that the client has aborted,
6792 * so we don't want to count this as a server abort. Otherwise it's a
6793 * server abort.
6794 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006795 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006796 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006797 goto aborted_xfer;
Christopher Fauletbe801672015-06-19 09:00:58 +02006798 /* If we have some pending data, we continue the processing */
6799 if (!buffer_pending(res->buf)) {
6800 if (!(s->flags & SN_ERR_MASK))
6801 s->flags |= SN_ERR_SRVCL;
6802 s->be->be_counters.srv_aborts++;
6803 if (objt_server(s->target))
6804 objt_server(s->target)->counters.srv_aborts++;
6805 goto return_bad_res_stats_ok;
6806 }
Willy Tarreau40dba092010-03-04 18:14:51 +01006807 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006808
Willy Tarreau40dba092010-03-04 18:14:51 +01006809 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006810 if (!s->req->analysers)
6811 goto return_bad_res;
6812
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006813 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006814 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006815 * Similarly, with keep-alive on the client side, we don't want to forward a
6816 * close.
6817 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006818 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006819 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6820 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006821 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006822
Willy Tarreau5c620922011-05-11 19:56:11 +02006823 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006824 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006825 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006826 * modes are already handled by the stream sock layer. We must not do
6827 * this in content-length mode because it could present the MSG_MORE
6828 * flag with the last block of forwarded data, which would cause an
6829 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006830 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006831 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006832 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006833
Willy Tarreaud98cf932009-12-27 22:54:55 +01006834 /* the session handler will take care of timeouts and errors */
6835 return 0;
6836
Willy Tarreau40dba092010-03-04 18:14:51 +01006837 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006838 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006839 if (objt_server(s->target))
6840 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006841
6842 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006843 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006844 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006845 compressing = 0;
6846 }
6847
Willy Tarreauc623c172014-04-18 09:53:50 +02006848 /* we may have some pending data starting at res->buf->p */
6849 if (s->comp_algo == NULL) {
6850 b_adv(res->buf, msg->next);
6851 msg->next = 0;
6852 }
6853
Willy Tarreaud98cf932009-12-27 22:54:55 +01006854 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006855 /* don't send any error message as we're in the body */
6856 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006857 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006858 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006859 if (objt_server(s->target))
6860 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006861
6862 if (!(s->flags & SN_ERR_MASK))
6863 s->flags |= SN_ERR_PRXCOND;
6864 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006865 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006866 return 0;
6867
6868 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006869 if (unlikely(compressing)) {
6870 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6871 compressing = 0;
6872 }
6873
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006874 txn->rsp.msg_state = HTTP_MSG_ERROR;
6875 /* don't send any error message as we're in the body */
6876 stream_int_retnclose(res->cons, NULL);
6877 res->analysers = 0;
6878 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6879
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006880 s->fe->fe_counters.cli_aborts++;
6881 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006882 if (objt_server(s->target))
6883 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006884
6885 if (!(s->flags & SN_ERR_MASK))
6886 s->flags |= SN_ERR_CLICL;
6887 if (!(s->flags & SN_FINST_MASK))
6888 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006889 return 0;
6890}
6891
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006892/* Iterate the same filter through all request headers.
6893 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006894 * Since it can manage the switch to another backend, it updates the per-proxy
6895 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006896 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006897int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006898{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006899 char *cur_ptr, *cur_end, *cur_next;
6900 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006901 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006902 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006903 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006904
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006905 last_hdr = 0;
6906
Willy Tarreau9b28e032012-10-12 23:49:43 +02006907 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006908 old_idx = 0;
6909
6910 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006911 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006912 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006913 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006914 (exp->action == ACT_ALLOW ||
6915 exp->action == ACT_DENY ||
6916 exp->action == ACT_TARPIT))
6917 return 0;
6918
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006919 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006920 if (!cur_idx)
6921 break;
6922
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006923 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006924 cur_ptr = cur_next;
6925 cur_end = cur_ptr + cur_hdr->len;
6926 cur_next = cur_end + cur_hdr->cr + 1;
6927
6928 /* Now we have one header between cur_ptr and cur_end,
6929 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006930 */
6931
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02006932 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006933 switch (exp->action) {
6934 case ACT_SETBE:
6935 /* It is not possible to jump a second time.
6936 * FIXME: should we return an HTTP/500 here so that
6937 * the admin knows there's a problem ?
6938 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006939 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006940 break;
6941
6942 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006943 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006944 last_hdr = 1;
6945 break;
6946
6947 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006948 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006949 last_hdr = 1;
6950 break;
6951
6952 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006953 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006954 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006955 break;
6956
6957 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006958 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006959 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006960 break;
6961
6962 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06006963 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
6964 if (trash.len < 0)
6965 return -1;
6966
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006967 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006968 /* FIXME: if the user adds a newline in the replacement, the
6969 * index will not be recalculated for now, and the new line
6970 * will not be counted as a new header.
6971 */
6972
6973 cur_end += delta;
6974 cur_next += delta;
6975 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006976 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006977 break;
6978
6979 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006980 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006981 cur_next += delta;
6982
Willy Tarreaufa355d42009-11-29 18:12:29 +01006983 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006984 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6985 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006986 cur_hdr->len = 0;
6987 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006988 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006989 break;
6990
6991 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006992 }
6993
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006994 /* keep the link from this header to next one in case of later
6995 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006996 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006997 old_idx = cur_idx;
6998 }
6999 return 0;
7000}
7001
7002
7003/* Apply the filter to the request line.
7004 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7005 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007006 * Since it can manage the switch to another backend, it updates the per-proxy
7007 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007008 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007009int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007010{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007011 char *cur_ptr, *cur_end;
7012 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007013 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007014 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007015
Willy Tarreau3d300592007-03-18 18:34:41 +01007016 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007017 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007018 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007019 (exp->action == ACT_ALLOW ||
7020 exp->action == ACT_DENY ||
7021 exp->action == ACT_TARPIT))
7022 return 0;
7023 else if (exp->action == ACT_REMOVE)
7024 return 0;
7025
7026 done = 0;
7027
Willy Tarreau9b28e032012-10-12 23:49:43 +02007028 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007029 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007030
7031 /* Now we have the request line between cur_ptr and cur_end */
7032
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007033 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007034 switch (exp->action) {
7035 case ACT_SETBE:
7036 /* It is not possible to jump a second time.
7037 * FIXME: should we return an HTTP/500 here so that
7038 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01007039 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007040 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007041 break;
7042
7043 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007044 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007045 done = 1;
7046 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007047
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007048 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007049 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007050 done = 1;
7051 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007052
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007053 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007054 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007055 done = 1;
7056 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007057
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007058 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01007059 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007060 done = 1;
7061 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01007062
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007063 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007064 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7065 if (trash.len < 0)
7066 return -1;
7067
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007068 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007069 /* FIXME: if the user adds a newline in the replacement, the
7070 * index will not be recalculated for now, and the new line
7071 * will not be counted as a new header.
7072 */
Willy Tarreaua496b602006-12-17 23:15:24 +01007073
Willy Tarreaufa355d42009-11-29 18:12:29 +01007074 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007075 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007076 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007077 HTTP_MSG_RQMETH,
7078 cur_ptr, cur_end + 1,
7079 NULL, NULL);
7080 if (unlikely(!cur_end))
7081 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01007082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007083 /* we have a full request and we know that we have either a CR
7084 * or an LF at <ptr>.
7085 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007086 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
7087 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007088 /* there is no point trying this regex on headers */
7089 return 1;
7090 }
7091 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007092 return done;
7093}
Willy Tarreau97de6242006-12-27 17:18:38 +01007094
Willy Tarreau58f10d72006-12-04 02:26:12 +01007095
Willy Tarreau58f10d72006-12-04 02:26:12 +01007096
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007097/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01007098 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007099 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01007100 * unparsable request. Since it can manage the switch to another backend, it
7101 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007102 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007103int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007104{
Willy Tarreau6c123b12010-01-28 20:22:06 +01007105 struct http_txn *txn = &s->txn;
7106 struct hdr_exp *exp;
7107
7108 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007109 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007110
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007111 /*
7112 * The interleaving of transformations and verdicts
7113 * makes it difficult to decide to continue or stop
7114 * the evaluation.
7115 */
7116
Willy Tarreau6c123b12010-01-28 20:22:06 +01007117 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
7118 break;
7119
Willy Tarreau3d300592007-03-18 18:34:41 +01007120 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007121 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01007122 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007123 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01007124
7125 /* if this filter had a condition, evaluate it now and skip to
7126 * next filter if the condition does not match.
7127 */
7128 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007129 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01007130 ret = acl_pass(ret);
7131 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7132 ret = !ret;
7133
7134 if (!ret)
7135 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007136 }
7137
7138 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01007139 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007140 if (unlikely(ret < 0))
7141 return -1;
7142
7143 if (likely(ret == 0)) {
7144 /* The filter did not match the request, it can be
7145 * iterated through all headers.
7146 */
Willy Tarreaua256fae2015-01-30 20:58:58 +01007147 if (unlikely(apply_filter_to_req_headers(s, req, exp) < 0))
7148 return -1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007149 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007150 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007151 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007152}
7153
7154
Willy Tarreaua15645d2007-03-18 16:22:39 +01007155
Willy Tarreau58f10d72006-12-04 02:26:12 +01007156/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007157 * Try to retrieve the server associated to the appsession.
7158 * If the server is found, it's assigned to the session.
7159 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007160void manage_client_side_appsession(struct session *s, const char *buf, int len) {
7161 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007162 appsess *asession = NULL;
7163 char *sessid_temp = NULL;
7164
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007165 if (len > s->be->appsession_len) {
7166 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007167 }
7168
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007169 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007170 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007171 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007172 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007173 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007174 }
7175
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007176 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007177 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007178 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007179 return;
7180 }
7181
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007182 memcpy(txn->sessid, buf, len);
7183 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007184 }
7185
7186 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
7187 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007188 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007189 return;
7190 }
7191
Cyril Bontéb21570a2009-11-29 20:04:48 +01007192 memcpy(sessid_temp, buf, len);
7193 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007194
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007195 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007196 /* free previously allocated memory */
7197 pool_free2(apools.sessid, sessid_temp);
7198
7199 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007200 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
7201 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007202 asession->request_count++;
7203
7204 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007205 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007206
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007207 while (srv) {
7208 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007209 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007210 (s->be->options & PR_O_PERSIST) ||
7211 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007212 /* we found the server and it's usable */
7213 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007214 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007215 s->flags |= SN_DIRECT | SN_ASSIGNED;
7216 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01007217
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007218 break;
7219 } else {
7220 txn->flags &= ~TX_CK_MASK;
7221 txn->flags |= TX_CK_DOWN;
7222 }
7223 }
7224 srv = srv->next;
7225 }
7226 }
7227 }
7228}
7229
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007230/* Find the end of a cookie value contained between <s> and <e>. It works the
7231 * same way as with headers above except that the semi-colon also ends a token.
7232 * See RFC2965 for more information. Note that it requires a valid header to
7233 * return a valid result.
7234 */
7235char *find_cookie_value_end(char *s, const char *e)
7236{
7237 int quoted, qdpair;
7238
7239 quoted = qdpair = 0;
7240 for (; s < e; s++) {
7241 if (qdpair) qdpair = 0;
7242 else if (quoted) {
7243 if (*s == '\\') qdpair = 1;
7244 else if (*s == '"') quoted = 0;
7245 }
7246 else if (*s == '"') quoted = 1;
7247 else if (*s == ',' || *s == ';') return s;
7248 }
7249 return s;
7250}
7251
7252/* Delete a value in a header between delimiters <from> and <next> in buffer
7253 * <buf>. The number of characters displaced is returned, and the pointer to
7254 * the first delimiter is updated if required. The function tries as much as
7255 * possible to respect the following principles :
7256 * - replace <from> delimiter by the <next> one unless <from> points to a
7257 * colon, in which case <next> is simply removed
7258 * - set exactly one space character after the new first delimiter, unless
7259 * there are not enough characters in the block being moved to do so.
7260 * - remove unneeded spaces before the previous delimiter and after the new
7261 * one.
7262 *
7263 * It is the caller's responsibility to ensure that :
7264 * - <from> points to a valid delimiter or the colon ;
7265 * - <next> points to a valid delimiter or the final CR/LF ;
7266 * - there are non-space chars before <from> ;
7267 * - there is a CR/LF at or after <next>.
7268 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007269int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007270{
7271 char *prev = *from;
7272
7273 if (*prev == ':') {
7274 /* We're removing the first value, preserve the colon and add a
7275 * space if possible.
7276 */
7277 if (!http_is_crlf[(unsigned char)*next])
7278 next++;
7279 prev++;
7280 if (prev < next)
7281 *prev++ = ' ';
7282
7283 while (http_is_spht[(unsigned char)*next])
7284 next++;
7285 } else {
7286 /* Remove useless spaces before the old delimiter. */
7287 while (http_is_spht[(unsigned char)*(prev-1)])
7288 prev--;
7289 *from = prev;
7290
7291 /* copy the delimiter and if possible a space if we're
7292 * not at the end of the line.
7293 */
7294 if (!http_is_crlf[(unsigned char)*next]) {
7295 *prev++ = *next++;
7296 if (prev + 1 < next)
7297 *prev++ = ' ';
7298 while (http_is_spht[(unsigned char)*next])
7299 next++;
7300 }
7301 }
7302 return buffer_replace2(buf, prev, next, NULL, 0);
7303}
7304
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007305/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007306 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007307 * desirable to call it only when needed. This code is quite complex because
7308 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7309 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007310 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007311void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007312{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007313 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007314 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007315 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007316 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7317 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007318
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007319 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007320 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007321 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007322
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007323 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007324 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007325 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007326
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007327 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007328 hdr_beg = hdr_next;
7329 hdr_end = hdr_beg + cur_hdr->len;
7330 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007331
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007332 /* We have one full header between hdr_beg and hdr_end, and the
7333 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007334 * "Cookie:" headers.
7335 */
7336
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007337 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007338 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007339 old_idx = cur_idx;
7340 continue;
7341 }
7342
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007343 del_from = NULL; /* nothing to be deleted */
7344 preserve_hdr = 0; /* assume we may kill the whole header */
7345
Willy Tarreau58f10d72006-12-04 02:26:12 +01007346 /* Now look for cookies. Conforming to RFC2109, we have to support
7347 * attributes whose name begin with a '$', and associate them with
7348 * the right cookie, if we want to delete this cookie.
7349 * So there are 3 cases for each cookie read :
7350 * 1) it's a special attribute, beginning with a '$' : ignore it.
7351 * 2) it's a server id cookie that we *MAY* want to delete : save
7352 * some pointers on it (last semi-colon, beginning of cookie...)
7353 * 3) it's an application cookie : we *MAY* have to delete a previous
7354 * "special" cookie.
7355 * At the end of loop, if a "special" cookie remains, we may have to
7356 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007357 * *MUST* delete it.
7358 *
7359 * Note: RFC2965 is unclear about the processing of spaces around
7360 * the equal sign in the ATTR=VALUE form. A careful inspection of
7361 * the RFC explicitly allows spaces before it, and not within the
7362 * tokens (attrs or values). An inspection of RFC2109 allows that
7363 * too but section 10.1.3 lets one think that spaces may be allowed
7364 * after the equal sign too, resulting in some (rare) buggy
7365 * implementations trying to do that. So let's do what servers do.
7366 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7367 * allowed quoted strings in values, with any possible character
7368 * after a backslash, including control chars and delimitors, which
7369 * causes parsing to become ambiguous. Browsers also allow spaces
7370 * within values even without quotes.
7371 *
7372 * We have to keep multiple pointers in order to support cookie
7373 * removal at the beginning, middle or end of header without
7374 * corrupting the header. All of these headers are valid :
7375 *
7376 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7377 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7378 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7379 * | | | | | | | | |
7380 * | | | | | | | | hdr_end <--+
7381 * | | | | | | | +--> next
7382 * | | | | | | +----> val_end
7383 * | | | | | +-----------> val_beg
7384 * | | | | +--------------> equal
7385 * | | | +----------------> att_end
7386 * | | +---------------------> att_beg
7387 * | +--------------------------> prev
7388 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007389 */
7390
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007391 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7392 /* Iterate through all cookies on this line */
7393
7394 /* find att_beg */
7395 att_beg = prev + 1;
7396 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7397 att_beg++;
7398
7399 /* find att_end : this is the first character after the last non
7400 * space before the equal. It may be equal to hdr_end.
7401 */
7402 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007403
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007404 while (equal < hdr_end) {
7405 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007406 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007407 if (http_is_spht[(unsigned char)*equal++])
7408 continue;
7409 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007410 }
7411
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007412 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7413 * is between <att_beg> and <equal>, both may be identical.
7414 */
7415
7416 /* look for end of cookie if there is an equal sign */
7417 if (equal < hdr_end && *equal == '=') {
7418 /* look for the beginning of the value */
7419 val_beg = equal + 1;
7420 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7421 val_beg++;
7422
7423 /* find the end of the value, respecting quotes */
7424 next = find_cookie_value_end(val_beg, hdr_end);
7425
7426 /* make val_end point to the first white space or delimitor after the value */
7427 val_end = next;
7428 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7429 val_end--;
7430 } else {
7431 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007432 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007433
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007434 /* We have nothing to do with attributes beginning with '$'. However,
7435 * they will automatically be removed if a header before them is removed,
7436 * since they're supposed to be linked together.
7437 */
7438 if (*att_beg == '$')
7439 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007440
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007441 /* Ignore cookies with no equal sign */
7442 if (equal == next) {
7443 /* This is not our cookie, so we must preserve it. But if we already
7444 * scheduled another cookie for removal, we cannot remove the
7445 * complete header, but we can remove the previous block itself.
7446 */
7447 preserve_hdr = 1;
7448 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007449 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007450 val_end += delta;
7451 next += delta;
7452 hdr_end += delta;
7453 hdr_next += delta;
7454 cur_hdr->len += delta;
7455 http_msg_move_end(&txn->req, delta);
7456 prev = del_from;
7457 del_from = NULL;
7458 }
7459 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007460 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007461
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007462 /* if there are spaces around the equal sign, we need to
7463 * strip them otherwise we'll get trouble for cookie captures,
7464 * or even for rewrites. Since this happens extremely rarely,
7465 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007466 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007467 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7468 int stripped_before = 0;
7469 int stripped_after = 0;
7470
7471 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007472 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007473 equal += stripped_before;
7474 val_beg += stripped_before;
7475 }
7476
7477 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007478 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007479 val_beg += stripped_after;
7480 stripped_before += stripped_after;
7481 }
7482
7483 val_end += stripped_before;
7484 next += stripped_before;
7485 hdr_end += stripped_before;
7486 hdr_next += stripped_before;
7487 cur_hdr->len += stripped_before;
7488 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007489 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007490 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007491
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007492 /* First, let's see if we want to capture this cookie. We check
7493 * that we don't already have a client side cookie, because we
7494 * can only capture one. Also as an optimisation, we ignore
7495 * cookies shorter than the declared name.
7496 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007497 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7498 (val_end - att_beg >= s->fe->capture_namelen) &&
7499 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007500 int log_len = val_end - att_beg;
7501
7502 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7503 Alert("HTTP logging : out of memory.\n");
7504 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007505 if (log_len > s->fe->capture_len)
7506 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007507 memcpy(txn->cli_cookie, att_beg, log_len);
7508 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007509 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007510 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007511
Willy Tarreaubca99692010-10-06 19:25:55 +02007512 /* Persistence cookies in passive, rewrite or insert mode have the
7513 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007514 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007515 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007516 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007517 * For cookies in prefix mode, the form is :
7518 *
7519 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007520 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007521 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7522 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7523 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007524 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007525
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007526 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7527 * have the server ID between val_beg and delim, and the original cookie between
7528 * delim+1 and val_end. Otherwise, delim==val_end :
7529 *
7530 * Cookie: NAME=SRV; # in all but prefix modes
7531 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7532 * | || || | |+-> next
7533 * | || || | +--> val_end
7534 * | || || +---------> delim
7535 * | || |+------------> val_beg
7536 * | || +-------------> att_end = equal
7537 * | |+-----------------> att_beg
7538 * | +------------------> prev
7539 * +-------------------------> hdr_beg
7540 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007541
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007542 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007543 for (delim = val_beg; delim < val_end; delim++)
7544 if (*delim == COOKIE_DELIM)
7545 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007546 } else {
7547 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007548 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007549 /* Now check if the cookie contains a date field, which would
7550 * appear after a vertical bar ('|') just after the server name
7551 * and before the delimiter.
7552 */
7553 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7554 if (vbar1) {
7555 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007556 * right is the last seen date. It is a base64 encoded
7557 * 30-bit value representing the UNIX date since the
7558 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007559 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007560 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007561 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007562 if (val_end - vbar1 >= 5) {
7563 val = b64tos30(vbar1);
7564 if (val > 0)
7565 txn->cookie_last_date = val << 2;
7566 }
7567 /* look for a second vertical bar */
7568 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7569 if (vbar1 && (val_end - vbar1 > 5)) {
7570 val = b64tos30(vbar1 + 1);
7571 if (val > 0)
7572 txn->cookie_first_date = val << 2;
7573 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007574 }
7575 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007576
Willy Tarreauf64d1412010-10-07 20:06:11 +02007577 /* if the cookie has an expiration date and the proxy wants to check
7578 * it, then we do that now. We first check if the cookie is too old,
7579 * then only if it has expired. We detect strict overflow because the
7580 * time resolution here is not great (4 seconds). Cookies with dates
7581 * in the future are ignored if their offset is beyond one day. This
7582 * allows an admin to fix timezone issues without expiring everyone
7583 * and at the same time avoids keeping unwanted side effects for too
7584 * long.
7585 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007586 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7587 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007588 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007589 txn->flags &= ~TX_CK_MASK;
7590 txn->flags |= TX_CK_OLD;
7591 delim = val_beg; // let's pretend we have not found the cookie
7592 txn->cookie_first_date = 0;
7593 txn->cookie_last_date = 0;
7594 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007595 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7596 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007597 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007598 txn->flags &= ~TX_CK_MASK;
7599 txn->flags |= TX_CK_EXPIRED;
7600 delim = val_beg; // let's pretend we have not found the cookie
7601 txn->cookie_first_date = 0;
7602 txn->cookie_last_date = 0;
7603 }
7604
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007605 /* Here, we'll look for the first running server which supports the cookie.
7606 * This allows to share a same cookie between several servers, for example
7607 * to dedicate backup servers to specific servers only.
7608 * However, to prevent clients from sticking to cookie-less backup server
7609 * when they have incidentely learned an empty cookie, we simply ignore
7610 * empty cookies and mark them as invalid.
7611 * The same behaviour is applied when persistence must be ignored.
7612 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007613 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007614 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007615
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007616 while (srv) {
7617 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7618 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
Willy Tarreau892337c2014-05-13 23:41:20 +02007619 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007620 (s->be->options & PR_O_PERSIST) ||
7621 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007622 /* we found the server and we can use it */
7623 txn->flags &= ~TX_CK_MASK;
Willy Tarreau892337c2014-05-13 23:41:20 +02007624 txn->flags |= (srv->state != SRV_ST_STOPPED) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007625 s->flags |= SN_DIRECT | SN_ASSIGNED;
7626 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007627 break;
7628 } else {
7629 /* we found a server, but it's down,
7630 * mark it as such and go on in case
7631 * another one is available.
7632 */
7633 txn->flags &= ~TX_CK_MASK;
7634 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007635 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007636 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007637 srv = srv->next;
7638 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007639
Willy Tarreauf64d1412010-10-07 20:06:11 +02007640 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007641 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007642 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007643 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007644 txn->flags |= TX_CK_UNUSED;
7645 else
7646 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007647 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007648
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007649 /* depending on the cookie mode, we may have to either :
7650 * - delete the complete cookie if we're in insert+indirect mode, so that
7651 * the server never sees it ;
7652 * - remove the server id from the cookie value, and tag the cookie as an
7653 * application cookie so that it does not get accidentely removed later,
7654 * if we're in cookie prefix mode
7655 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007656 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007657 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007658
Willy Tarreau9b28e032012-10-12 23:49:43 +02007659 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007660 val_end += delta;
7661 next += delta;
7662 hdr_end += delta;
7663 hdr_next += delta;
7664 cur_hdr->len += delta;
7665 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007666
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007667 del_from = NULL;
7668 preserve_hdr = 1; /* we want to keep this cookie */
7669 }
7670 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007671 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007672 del_from = prev;
7673 }
7674 } else {
7675 /* This is not our cookie, so we must preserve it. But if we already
7676 * scheduled another cookie for removal, we cannot remove the
7677 * complete header, but we can remove the previous block itself.
7678 */
7679 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007680
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007681 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007682 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007683 if (att_beg >= del_from)
7684 att_beg += delta;
7685 if (att_end >= del_from)
7686 att_end += delta;
7687 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007688 val_end += delta;
7689 next += delta;
7690 hdr_end += delta;
7691 hdr_next += delta;
7692 cur_hdr->len += delta;
7693 http_msg_move_end(&txn->req, delta);
7694 prev = del_from;
7695 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007696 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007697 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007698
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007699 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007700 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007701 int cmp_len, value_len;
7702 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007703
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007704 if (s->be->options2 & PR_O2_AS_PFX) {
7705 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7706 value_begin = att_beg + s->be->appsession_name_len;
7707 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007708 } else {
7709 cmp_len = att_end - att_beg;
7710 value_begin = val_beg;
7711 value_len = val_end - val_beg;
7712 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007713
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007714 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007715 if (cmp_len == s->be->appsession_name_len &&
7716 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7717 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007718 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007719 }
7720
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007721 /* continue with next cookie on this header line */
7722 att_beg = next;
7723 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007724
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007725 /* There are no more cookies on this line.
7726 * We may still have one (or several) marked for deletion at the
7727 * end of the line. We must do this now in two ways :
7728 * - if some cookies must be preserved, we only delete from the
7729 * mark to the end of line ;
7730 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007731 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007732 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007733 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007734 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007735 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007736 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007737 cur_hdr->len += delta;
7738 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007739 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007740
7741 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007742 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7743 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007744 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007745 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007746 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007747 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007748 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007749 }
7750
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007751 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007752 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007753 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007754}
7755
7756
Willy Tarreaua15645d2007-03-18 16:22:39 +01007757/* Iterate the same filter through all response headers contained in <rtr>.
7758 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7759 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007760int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007761{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007762 char *cur_ptr, *cur_end, *cur_next;
7763 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007764 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007765 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007766 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007767
7768 last_hdr = 0;
7769
Willy Tarreau9b28e032012-10-12 23:49:43 +02007770 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007771 old_idx = 0;
7772
7773 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007774 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007775 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007776 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007777 (exp->action == ACT_ALLOW ||
7778 exp->action == ACT_DENY))
7779 return 0;
7780
7781 cur_idx = txn->hdr_idx.v[old_idx].next;
7782 if (!cur_idx)
7783 break;
7784
7785 cur_hdr = &txn->hdr_idx.v[cur_idx];
7786 cur_ptr = cur_next;
7787 cur_end = cur_ptr + cur_hdr->len;
7788 cur_next = cur_end + cur_hdr->cr + 1;
7789
7790 /* Now we have one header between cur_ptr and cur_end,
7791 * and the next header starts at cur_next.
7792 */
7793
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007794 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007795 switch (exp->action) {
7796 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007797 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007798 last_hdr = 1;
7799 break;
7800
7801 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007802 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007803 last_hdr = 1;
7804 break;
7805
7806 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007807 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7808 if (trash.len < 0)
7809 return -1;
7810
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007811 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007812 /* FIXME: if the user adds a newline in the replacement, the
7813 * index will not be recalculated for now, and the new line
7814 * will not be counted as a new header.
7815 */
7816
7817 cur_end += delta;
7818 cur_next += delta;
7819 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007820 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007821 break;
7822
7823 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007824 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007825 cur_next += delta;
7826
Willy Tarreaufa355d42009-11-29 18:12:29 +01007827 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007828 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7829 txn->hdr_idx.used--;
7830 cur_hdr->len = 0;
7831 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007832 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007833 break;
7834
7835 }
7836 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007837
7838 /* keep the link from this header to next one in case of later
7839 * removal of next header.
7840 */
7841 old_idx = cur_idx;
7842 }
7843 return 0;
7844}
7845
7846
7847/* Apply the filter to the status line in the response buffer <rtr>.
7848 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7849 * or -1 if a replacement resulted in an invalid status line.
7850 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007851int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007852{
Willy Tarreaua15645d2007-03-18 16:22:39 +01007853 char *cur_ptr, *cur_end;
7854 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007855 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007856 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007857
7858
Willy Tarreau3d300592007-03-18 18:34:41 +01007859 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007860 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007861 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007862 (exp->action == ACT_ALLOW ||
7863 exp->action == ACT_DENY))
7864 return 0;
7865 else if (exp->action == ACT_REMOVE)
7866 return 0;
7867
7868 done = 0;
7869
Willy Tarreau9b28e032012-10-12 23:49:43 +02007870 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007871 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007872
7873 /* Now we have the status line between cur_ptr and cur_end */
7874
Thierry FOURNIERc9c2daf2014-06-18 11:53:08 +02007875 if (regex_exec_match2(exp->preg, cur_ptr, cur_end-cur_ptr, MAX_MATCH, pmatch)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007876 switch (exp->action) {
7877 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007878 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007879 done = 1;
7880 break;
7881
7882 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007883 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007884 done = 1;
7885 break;
7886
7887 case ACT_REPLACE:
Sasha Pachevc6002042014-05-26 12:33:48 -06007888 trash.len = exp_replace(trash.str, trash.size, cur_ptr, exp->replace, pmatch);
7889 if (trash.len < 0)
7890 return -1;
7891
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007892 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007893 /* FIXME: if the user adds a newline in the replacement, the
7894 * index will not be recalculated for now, and the new line
7895 * will not be counted as a new header.
7896 */
7897
Willy Tarreaufa355d42009-11-29 18:12:29 +01007898 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007899 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007900 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007901 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007902 cur_ptr, cur_end + 1,
7903 NULL, NULL);
7904 if (unlikely(!cur_end))
7905 return -1;
7906
7907 /* we have a full respnse and we know that we have either a CR
7908 * or an LF at <ptr>.
7909 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007910 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007911 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007912 /* there is no point trying this regex on headers */
7913 return 1;
7914 }
7915 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007916 return done;
7917}
7918
7919
7920
7921/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007922 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007923 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7924 * unparsable response.
7925 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007926int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007927{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007928 struct http_txn *txn = &s->txn;
7929 struct hdr_exp *exp;
7930
7931 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007932 int ret;
7933
7934 /*
7935 * The interleaving of transformations and verdicts
7936 * makes it difficult to decide to continue or stop
7937 * the evaluation.
7938 */
7939
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007940 if (txn->flags & TX_SVDENY)
7941 break;
7942
Willy Tarreau3d300592007-03-18 18:34:41 +01007943 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007944 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7945 exp->action == ACT_PASS)) {
7946 exp = exp->next;
7947 continue;
7948 }
7949
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007950 /* if this filter had a condition, evaluate it now and skip to
7951 * next filter if the condition does not match.
7952 */
7953 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007954 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007955 ret = acl_pass(ret);
7956 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7957 ret = !ret;
7958 if (!ret)
7959 continue;
7960 }
7961
Willy Tarreaua15645d2007-03-18 16:22:39 +01007962 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007963 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007964 if (unlikely(ret < 0))
7965 return -1;
7966
7967 if (likely(ret == 0)) {
7968 /* The filter did not match the response, it can be
7969 * iterated through all headers.
7970 */
Sasha Pachevc6002042014-05-26 12:33:48 -06007971 if (unlikely(apply_filter_to_resp_headers(s, rtr, exp) < 0))
7972 return -1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007973 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007974 }
7975 return 0;
7976}
7977
7978
Willy Tarreaua15645d2007-03-18 16:22:39 +01007979/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007980 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007981 * desirable to call it only when needed. This function is also used when we
7982 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007983 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007984void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007985{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007986 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007987 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007988 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007989 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007990 char *hdr_beg, *hdr_end, *hdr_next;
7991 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007992
Willy Tarreaua15645d2007-03-18 16:22:39 +01007993 /* Iterate through the headers.
7994 * we start with the start line.
7995 */
7996 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007997 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007998
7999 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
8000 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008001 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008002
8003 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02008004 hdr_beg = hdr_next;
8005 hdr_end = hdr_beg + cur_hdr->len;
8006 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008007
Willy Tarreau24581ba2010-08-31 22:39:35 +02008008 /* We have one full header between hdr_beg and hdr_end, and the
8009 * next header starts at hdr_next. We're only interested in
8010 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008011 */
8012
Willy Tarreau24581ba2010-08-31 22:39:35 +02008013 is_cookie2 = 0;
8014 prev = hdr_beg + 10;
8015 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008016 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008017 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
8018 if (!val) {
8019 old_idx = cur_idx;
8020 continue;
8021 }
8022 is_cookie2 = 1;
8023 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008024 }
8025
Willy Tarreau24581ba2010-08-31 22:39:35 +02008026 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
8027 * <prev> points to the colon.
8028 */
Willy Tarreauf1348312010-10-07 15:54:11 +02008029 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008030
Willy Tarreau24581ba2010-08-31 22:39:35 +02008031 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
8032 * check-cache is enabled) and we are not interested in checking
8033 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02008034 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008035 if (s->be->cookie_name == NULL &&
8036 s->be->appsession_name == NULL &&
8037 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008038 return;
8039
Willy Tarreau24581ba2010-08-31 22:39:35 +02008040 /* OK so now we know we have to process this response cookie.
8041 * The format of the Set-Cookie header is slightly different
8042 * from the format of the Cookie header in that it does not
8043 * support the comma as a cookie delimiter (thus the header
8044 * cannot be folded) because the Expires attribute described in
8045 * the original Netscape's spec may contain an unquoted date
8046 * with a comma inside. We have to live with this because
8047 * many browsers don't support Max-Age and some browsers don't
8048 * support quoted strings. However the Set-Cookie2 header is
8049 * clean.
8050 *
8051 * We have to keep multiple pointers in order to support cookie
8052 * removal at the beginning, middle or end of header without
8053 * corrupting the header (in case of set-cookie2). A special
8054 * pointer, <scav> points to the beginning of the set-cookie-av
8055 * fields after the first semi-colon. The <next> pointer points
8056 * either to the end of line (set-cookie) or next unquoted comma
8057 * (set-cookie2). All of these headers are valid :
8058 *
8059 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
8060 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8061 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
8062 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
8063 * | | | | | | | | | |
8064 * | | | | | | | | +-> next hdr_end <--+
8065 * | | | | | | | +------------> scav
8066 * | | | | | | +--------------> val_end
8067 * | | | | | +--------------------> val_beg
8068 * | | | | +----------------------> equal
8069 * | | | +------------------------> att_end
8070 * | | +----------------------------> att_beg
8071 * | +------------------------------> prev
8072 * +-----------------------------------------> hdr_beg
8073 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008074
Willy Tarreau24581ba2010-08-31 22:39:35 +02008075 for (; prev < hdr_end; prev = next) {
8076 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008077
Willy Tarreau24581ba2010-08-31 22:39:35 +02008078 /* find att_beg */
8079 att_beg = prev + 1;
8080 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8081 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008082
Willy Tarreau24581ba2010-08-31 22:39:35 +02008083 /* find att_end : this is the first character after the last non
8084 * space before the equal. It may be equal to hdr_end.
8085 */
8086 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008087
Willy Tarreau24581ba2010-08-31 22:39:35 +02008088 while (equal < hdr_end) {
8089 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
8090 break;
8091 if (http_is_spht[(unsigned char)*equal++])
8092 continue;
8093 att_end = equal;
8094 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008095
Willy Tarreau24581ba2010-08-31 22:39:35 +02008096 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8097 * is between <att_beg> and <equal>, both may be identical.
8098 */
8099
8100 /* look for end of cookie if there is an equal sign */
8101 if (equal < hdr_end && *equal == '=') {
8102 /* look for the beginning of the value */
8103 val_beg = equal + 1;
8104 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8105 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008106
Willy Tarreau24581ba2010-08-31 22:39:35 +02008107 /* find the end of the value, respecting quotes */
8108 next = find_cookie_value_end(val_beg, hdr_end);
8109
8110 /* make val_end point to the first white space or delimitor after the value */
8111 val_end = next;
8112 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8113 val_end--;
8114 } else {
8115 /* <equal> points to next comma, semi-colon or EOL */
8116 val_beg = val_end = next = equal;
8117 }
8118
8119 if (next < hdr_end) {
8120 /* Set-Cookie2 supports multiple cookies, and <next> points to
8121 * a colon or semi-colon before the end. So skip all attr-value
8122 * pairs and look for the next comma. For Set-Cookie, since
8123 * commas are permitted in values, skip to the end.
8124 */
8125 if (is_cookie2)
8126 next = find_hdr_value_end(next, hdr_end);
8127 else
8128 next = hdr_end;
8129 }
8130
8131 /* Now everything is as on the diagram above */
8132
8133 /* Ignore cookies with no equal sign */
8134 if (equal == val_end)
8135 continue;
8136
8137 /* If there are spaces around the equal sign, we need to
8138 * strip them otherwise we'll get trouble for cookie captures,
8139 * or even for rewrites. Since this happens extremely rarely,
8140 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008141 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02008142 if (unlikely(att_end != equal || val_beg > equal + 1)) {
8143 int stripped_before = 0;
8144 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008145
Willy Tarreau24581ba2010-08-31 22:39:35 +02008146 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008147 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008148 equal += stripped_before;
8149 val_beg += stripped_before;
8150 }
8151
8152 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02008153 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008154 val_beg += stripped_after;
8155 stripped_before += stripped_after;
8156 }
8157
8158 val_end += stripped_before;
8159 next += stripped_before;
8160 hdr_end += stripped_before;
8161 hdr_next += stripped_before;
8162 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02008163 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008164 }
8165
8166 /* First, let's see if we want to capture this cookie. We check
8167 * that we don't already have a server side cookie, because we
8168 * can only capture one. Also as an optimisation, we ignore
8169 * cookies shorter than the declared name.
8170 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008171 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01008172 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008173 (val_end - att_beg >= s->fe->capture_namelen) &&
8174 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008175 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02008176 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008177 Alert("HTTP logging : out of memory.\n");
8178 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01008179 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008180 if (log_len > s->fe->capture_len)
8181 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01008182 memcpy(txn->srv_cookie, att_beg, log_len);
8183 txn->srv_cookie[log_len] = 0;
8184 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008185 }
8186
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008187 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008188 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008189 if (!(s->flags & SN_IGNORE_PRST) &&
8190 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
8191 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02008192 /* assume passive cookie by default */
8193 txn->flags &= ~TX_SCK_MASK;
8194 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008195
8196 /* If the cookie is in insert mode on a known server, we'll delete
8197 * this occurrence because we'll insert another one later.
8198 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02008199 * a direct access.
8200 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008201 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02008202 /* The "preserve" flag was set, we don't want to touch the
8203 * server's cookie.
8204 */
8205 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008206 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
8207 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008208 /* this cookie must be deleted */
8209 if (*prev == ':' && next == hdr_end) {
8210 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008211 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008212 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8213 txn->hdr_idx.used--;
8214 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008215 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008216 hdr_next += delta;
8217 http_msg_move_end(&txn->rsp, delta);
8218 /* note: while both invalid now, <next> and <hdr_end>
8219 * are still equal, so the for() will stop as expected.
8220 */
8221 } else {
8222 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008223 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008224 next = prev;
8225 hdr_end += delta;
8226 hdr_next += delta;
8227 cur_hdr->len += delta;
8228 http_msg_move_end(&txn->rsp, delta);
8229 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008230 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008231 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008232 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008233 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008234 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008235 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008236 * with this server since we know it.
8237 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008238 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008239 next += delta;
8240 hdr_end += delta;
8241 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008242 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008243 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008244
Willy Tarreauf1348312010-10-07 15:54:11 +02008245 txn->flags &= ~TX_SCK_MASK;
8246 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008247 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008248 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008249 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008250 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008251 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008252 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008253 next += delta;
8254 hdr_end += delta;
8255 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008256 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008257 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008258
Willy Tarreau827aee92011-03-10 16:55:02 +01008259 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008260 txn->flags &= ~TX_SCK_MASK;
8261 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008262 }
8263 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008264 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008265 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008266 int cmp_len, value_len;
8267 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008268
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008269 if (s->be->options2 & PR_O2_AS_PFX) {
8270 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8271 value_begin = att_beg + s->be->appsession_name_len;
8272 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008273 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008274 cmp_len = att_end - att_beg;
8275 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008276 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008277 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008278
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008279 if ((cmp_len == s->be->appsession_name_len) &&
8280 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008281 /* free a possibly previously allocated memory */
8282 pool_free2(apools.sessid, txn->sessid);
8283
Cyril Bontéb21570a2009-11-29 20:04:48 +01008284 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008285 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008286 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008287 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008288 return;
8289 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008290 memcpy(txn->sessid, value_begin, value_len);
8291 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008292 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008293 }
8294 /* that's done for this cookie, check the next one on the same
8295 * line when next != hdr_end (only if is_cookie2).
8296 */
8297 }
8298 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008299 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008300 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008301
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008302 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008303 appsess *asession = NULL;
8304 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008305 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008306 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008307 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008308 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8309 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008310 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008311 return;
8312 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008313 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8314
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008315 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8316 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008317 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8318 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008319 return;
8320 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008321 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8322 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008323
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008324 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008325 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008326 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008327 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8328 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008329 return;
8330 }
8331 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008332 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008333
8334 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008335 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008336 }
8337
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008338 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008339 asession->request_count++;
8340 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008341}
8342
8343
Willy Tarreaua15645d2007-03-18 16:22:39 +01008344/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008345 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008346 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008347void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008348{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008349 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008350 char *p1, *p2;
8351
8352 char *cur_ptr, *cur_end, *cur_next;
8353 int cur_idx;
8354
Willy Tarreau5df51872007-11-25 16:20:08 +01008355 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008356 return;
8357
8358 /* Iterate through the headers.
8359 * we start with the start line.
8360 */
8361 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008362 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008363
8364 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8365 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008366 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008367
8368 cur_hdr = &txn->hdr_idx.v[cur_idx];
8369 cur_ptr = cur_next;
8370 cur_end = cur_ptr + cur_hdr->len;
8371 cur_next = cur_end + cur_hdr->cr + 1;
8372
8373 /* We have one full header between cur_ptr and cur_end, and the
8374 * next header starts at cur_next. We're only interested in
8375 * "Cookie:" headers.
8376 */
8377
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008378 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8379 if (val) {
8380 if ((cur_end - (cur_ptr + val) >= 8) &&
8381 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8382 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8383 return;
8384 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008385 }
8386
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008387 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8388 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008389 continue;
8390
8391 /* OK, right now we know we have a cache-control header at cur_ptr */
8392
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008393 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008394
8395 if (p1 >= cur_end) /* no more info */
8396 continue;
8397
8398 /* p1 is at the beginning of the value */
8399 p2 = p1;
8400
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008401 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008402 p2++;
8403
8404 /* we have a complete value between p1 and p2 */
8405 if (p2 < cur_end && *p2 == '=') {
8406 /* we have something of the form no-cache="set-cookie" */
8407 if ((cur_end - p1 >= 21) &&
8408 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8409 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008410 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008411 continue;
8412 }
8413
8414 /* OK, so we know that either p2 points to the end of string or to a comma */
8415 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008416 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008417 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8418 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8419 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008420 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008421 return;
8422 }
8423
8424 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008425 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008426 continue;
8427 }
8428 }
8429}
8430
8431
Willy Tarreau58f10d72006-12-04 02:26:12 +01008432/*
8433 * Try to retrieve a known appsession in the URI, then the associated server.
8434 * If the server is found, it's assigned to the session.
8435 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008436void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008437{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008438 char *end_params, *first_param, *cur_param, *next_param;
8439 char separator;
8440 int value_len;
8441
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008442 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008443
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008444 if (s->be->appsession_name == NULL ||
8445 (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 +01008446 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008447 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008448
Cyril Bontéb21570a2009-11-29 20:04:48 +01008449 first_param = NULL;
8450 switch (mode) {
8451 case PR_O2_AS_M_PP:
8452 first_param = memchr(begin, ';', len);
8453 break;
8454 case PR_O2_AS_M_QS:
8455 first_param = memchr(begin, '?', len);
8456 break;
8457 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008458
Cyril Bontéb21570a2009-11-29 20:04:48 +01008459 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008460 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008461 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008462
Cyril Bontéb21570a2009-11-29 20:04:48 +01008463 switch (mode) {
8464 case PR_O2_AS_M_PP:
8465 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8466 end_params = (char *) begin + len;
8467 }
8468 separator = ';';
8469 break;
8470 case PR_O2_AS_M_QS:
8471 end_params = (char *) begin + len;
8472 separator = '&';
8473 break;
8474 default:
8475 /* unknown mode, shouldn't happen */
8476 return;
8477 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008478
Cyril Bontéb21570a2009-11-29 20:04:48 +01008479 cur_param = next_param = end_params;
8480 while (cur_param > first_param) {
8481 cur_param--;
8482 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8483 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008484 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8485 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8486 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008487 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008488 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8489 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008490 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008491 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008492 }
8493 break;
8494 }
8495 next_param = cur_param;
8496 }
8497 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008498#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008499 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008500 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008501#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008502}
8503
Willy Tarreaub2513902006-12-17 14:52:38 +01008504/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008505 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008506 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008507 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008508 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008509 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008510 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008511 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008512 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008513int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008514{
8515 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008516 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008517 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008518
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008519 if (!uri_auth)
8520 return 0;
8521
Cyril Bonté70be45d2010-10-12 00:14:35 +02008522 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008523 return 0;
8524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008525 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008526 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008527 return 0;
8528
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008529 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008530 return 0;
8531
Willy Tarreaub2513902006-12-17 14:52:38 +01008532 return 1;
8533}
8534
Willy Tarreau4076a152009-04-02 15:18:36 +02008535/*
8536 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008537 * By default it tries to report the error position as msg->err_pos. However if
8538 * this one is not set, it will then report msg->next, which is the last known
8539 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008540 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008541 */
8542void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008543 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008544 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008545{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008546 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008547 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008548
Willy Tarreau9b28e032012-10-12 23:49:43 +02008549 es->len = MIN(chn->buf->i, sizeof(es->buf));
8550 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008551 len1 = MIN(len1, es->len);
8552 len2 = es->len - len1; /* remaining data if buffer wraps */
8553
Willy Tarreau9b28e032012-10-12 23:49:43 +02008554 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008555 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008556 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008557
Willy Tarreau4076a152009-04-02 15:18:36 +02008558 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008559 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008560 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008561 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008562
Willy Tarreau4076a152009-04-02 15:18:36 +02008563 es->when = date; // user-visible date
8564 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008565 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008566 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008567 if (objt_conn(s->req->prod->end))
8568 es->src = __objt_conn(s->req->prod->end)->addr.from;
8569 else
8570 memset(&es->src, 0, sizeof(es->src));
8571
Willy Tarreau078272e2010-12-12 12:46:33 +01008572 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008573 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008574 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008575 es->s_flags = s->flags;
8576 es->t_flags = s->txn.flags;
8577 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008578 es->b_out = chn->buf->o;
8579 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008580 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008581 es->m_clen = msg->chunk_len;
8582 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008583}
Willy Tarreaub2513902006-12-17 14:52:38 +01008584
Willy Tarreau294c4732011-12-16 21:35:50 +01008585/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8586 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8587 * performed over the whole headers. Otherwise it must contain a valid header
8588 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8589 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8590 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8591 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008592 * -1. The value fetch stops at commas, so this function is suited for use with
8593 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008594 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008595 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008596unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008597 struct hdr_idx *idx, int occ,
8598 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008599{
Willy Tarreau294c4732011-12-16 21:35:50 +01008600 struct hdr_ctx local_ctx;
8601 char *ptr_hist[MAX_HDR_HISTORY];
8602 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008603 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008604 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008605
Willy Tarreau294c4732011-12-16 21:35:50 +01008606 if (!ctx) {
8607 local_ctx.idx = 0;
8608 ctx = &local_ctx;
8609 }
8610
Willy Tarreaubce70882009-09-07 11:51:47 +02008611 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008612 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008613 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008614 occ--;
8615 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008616 *vptr = ctx->line + ctx->val;
8617 *vlen = ctx->vlen;
8618 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008619 }
8620 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008621 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008622 }
8623
8624 /* negative occurrence, we scan all the list then walk back */
8625 if (-occ > MAX_HDR_HISTORY)
8626 return 0;
8627
Willy Tarreau294c4732011-12-16 21:35:50 +01008628 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008629 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008630 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8631 len_hist[hist_ptr] = ctx->vlen;
8632 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008633 hist_ptr = 0;
8634 found++;
8635 }
8636 if (-occ > found)
8637 return 0;
8638 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008639 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8640 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8641 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008642 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008643 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008644 if (hist_ptr >= MAX_HDR_HISTORY)
8645 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008646 *vptr = ptr_hist[hist_ptr];
8647 *vlen = len_hist[hist_ptr];
8648 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008649}
8650
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008651/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8652 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8653 * performed over the whole headers. Otherwise it must contain a valid header
8654 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8655 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8656 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8657 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8658 * -1. This function differs from http_get_hdr() in that it only returns full
8659 * line header values and does not stop at commas.
8660 * The return value is 0 if nothing was found, or non-zero otherwise.
8661 */
8662unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8663 struct hdr_idx *idx, int occ,
8664 struct hdr_ctx *ctx, char **vptr, int *vlen)
8665{
8666 struct hdr_ctx local_ctx;
8667 char *ptr_hist[MAX_HDR_HISTORY];
8668 int len_hist[MAX_HDR_HISTORY];
8669 unsigned int hist_ptr;
8670 int found;
8671
8672 if (!ctx) {
8673 local_ctx.idx = 0;
8674 ctx = &local_ctx;
8675 }
8676
8677 if (occ >= 0) {
8678 /* search from the beginning */
8679 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8680 occ--;
8681 if (occ <= 0) {
8682 *vptr = ctx->line + ctx->val;
8683 *vlen = ctx->vlen;
8684 return 1;
8685 }
8686 }
8687 return 0;
8688 }
8689
8690 /* negative occurrence, we scan all the list then walk back */
8691 if (-occ > MAX_HDR_HISTORY)
8692 return 0;
8693
8694 found = hist_ptr = 0;
8695 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8696 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8697 len_hist[hist_ptr] = ctx->vlen;
8698 if (++hist_ptr >= MAX_HDR_HISTORY)
8699 hist_ptr = 0;
8700 found++;
8701 }
8702 if (-occ > found)
8703 return 0;
Nenad Merdanovicbb656432016-03-29 13:14:30 +02008704
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008705 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Nenad Merdanovicbb656432016-03-29 13:14:30 +02008706 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8707 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8708 * to remain in the 0..9 range.
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008709 */
Nenad Merdanovicbb656432016-03-29 13:14:30 +02008710 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008711 if (hist_ptr >= MAX_HDR_HISTORY)
8712 hist_ptr -= MAX_HDR_HISTORY;
8713 *vptr = ptr_hist[hist_ptr];
8714 *vlen = len_hist[hist_ptr];
8715 return 1;
8716}
8717
Willy Tarreaubaaee002006-06-26 02:48:02 +02008718/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008719 * Print a debug line with a header. Always stop at the first CR or LF char,
8720 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8721 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008722 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008723void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008724{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008725 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008726 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008727 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008728 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8729 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 +02008730
8731 for (max = 0; start + max < end; max++)
8732 if (start[max] == '\r' || start[max] == '\n')
8733 break;
8734
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008735 UBOUND(max, trash.size - trash.len - 3);
8736 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8737 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008738 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008739}
8740
Willy Tarreau0937bc42009-12-22 15:03:09 +01008741/*
8742 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8743 * the required fields are properly allocated and that we only need to (re)init
8744 * them. This should be used before processing any new request.
8745 */
8746void http_init_txn(struct session *s)
8747{
8748 struct http_txn *txn = &s->txn;
8749 struct proxy *fe = s->fe;
8750
8751 txn->flags = 0;
8752 txn->status = -1;
8753
Willy Tarreauf64d1412010-10-07 20:06:11 +02008754 txn->cookie_first_date = 0;
8755 txn->cookie_last_date = 0;
8756
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008757 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008758 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008759 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008760 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008761 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008762 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008763 txn->req.chunk_len = 0LL;
8764 txn->req.body_len = 0LL;
8765 txn->rsp.chunk_len = 0LL;
8766 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008767 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8768 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008769 txn->req.chn = s->req;
8770 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008771
8772 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008773
8774 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8775 if (fe->options2 & PR_O2_REQBUG_OK)
8776 txn->req.err_pos = -1; /* let buggy requests pass */
8777
Willy Tarreau46023632010-01-07 22:51:47 +01008778 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008779 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8780
Willy Tarreau46023632010-01-07 22:51:47 +01008781 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008782 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8783
8784 if (txn->hdr_idx.v)
8785 hdr_idx_init(&txn->hdr_idx);
8786}
8787
8788/* to be used at the end of a transaction */
8789void http_end_txn(struct session *s)
8790{
8791 struct http_txn *txn = &s->txn;
8792
Willy Tarreau75195602014-03-11 15:48:55 +01008793 /* release any possible compression context */
8794 if (s->flags & SN_COMP_READY)
8795 s->comp_algo->end(&s->comp_ctx);
8796 s->comp_algo = NULL;
8797 s->flags &= ~SN_COMP_READY;
8798
Willy Tarreau0937bc42009-12-22 15:03:09 +01008799 /* these ones will have been dynamically allocated */
8800 pool_free2(pool2_requri, txn->uri);
8801 pool_free2(pool2_capture, txn->cli_cookie);
8802 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008803 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008804 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008805
William Lallemanda73203e2012-03-12 12:48:57 +01008806 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008807 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008808 txn->uri = NULL;
8809 txn->srv_cookie = NULL;
8810 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008811
8812 if (txn->req.cap) {
8813 struct cap_hdr *h;
8814 for (h = s->fe->req_cap; h; h = h->next)
8815 pool_free2(h->pool, txn->req.cap[h->index]);
8816 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8817 }
8818
8819 if (txn->rsp.cap) {
8820 struct cap_hdr *h;
8821 for (h = s->fe->rsp_cap; h; h = h->next)
8822 pool_free2(h->pool, txn->rsp.cap[h->index]);
8823 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8824 }
8825
Willy Tarreau0937bc42009-12-22 15:03:09 +01008826}
8827
8828/* to be used at the end of a transaction to prepare a new one */
8829void http_reset_txn(struct session *s)
8830{
8831 http_end_txn(s);
8832 http_init_txn(s);
8833
8834 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008835 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008836 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008837 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008838 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008839 /* re-init store persistence */
8840 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008841 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008842
Willy Tarreau0937bc42009-12-22 15:03:09 +01008843 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008844
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008845 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008846
Willy Tarreau739cfba2010-01-25 23:11:14 +01008847 /* We must trim any excess data from the response buffer, because we
8848 * may have blocked an invalid response from a server that we don't
8849 * want to accidentely forward once we disable the analysers, nor do
8850 * we want those data to come along with next response. A typical
8851 * example of such data would be from a buggy server responding to
8852 * a HEAD with some data, or sending more than the advertised
8853 * content-length.
8854 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008855 if (unlikely(s->rep->buf->i))
8856 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008857
Willy Tarreau0937bc42009-12-22 15:03:09 +01008858 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008859 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008860
Willy Tarreaud04e8582010-05-31 12:31:35 +02008861 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008862 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008863
8864 s->req->rex = TICK_ETERNITY;
8865 s->req->wex = TICK_ETERNITY;
8866 s->req->analyse_exp = TICK_ETERNITY;
8867 s->rep->rex = TICK_ETERNITY;
8868 s->rep->wex = TICK_ETERNITY;
8869 s->rep->analyse_exp = TICK_ETERNITY;
8870}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008871
Sasha Pachev218f0642014-06-16 12:05:59 -06008872void free_http_res_rules(struct list *r)
8873{
8874 struct http_res_rule *tr, *pr;
8875
8876 list_for_each_entry_safe(pr, tr, r, list) {
8877 LIST_DEL(&pr->list);
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008878 regex_free(&pr->arg.hdr_add.re);
Sasha Pachev218f0642014-06-16 12:05:59 -06008879 free(pr);
8880 }
8881}
8882
8883void free_http_req_rules(struct list *r)
8884{
Willy Tarreauff011f22011-01-06 17:51:27 +01008885 struct http_req_rule *tr, *pr;
8886
8887 list_for_each_entry_safe(pr, tr, r, list) {
8888 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008889 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008890 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008891
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008892 regex_free(&pr->arg.hdr_add.re);
Willy Tarreauff011f22011-01-06 17:51:27 +01008893 free(pr);
8894 }
8895}
8896
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008897/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008898struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8899{
8900 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008901 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008902 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02008903 char *error;
Willy Tarreauff011f22011-01-06 17:51:27 +01008904
8905 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8906 if (!rule) {
8907 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008908 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008909 }
8910
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008911 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008912 rule->action = HTTP_REQ_ACT_ALLOW;
8913 cur_arg = 1;
Willy Tarreau5bd67592014-04-28 22:00:46 +02008914 } else if (!strcmp(args[0], "deny") || !strcmp(args[0], "block")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008915 rule->action = HTTP_REQ_ACT_DENY;
8916 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008917 } else if (!strcmp(args[0], "tarpit")) {
8918 rule->action = HTTP_REQ_ACT_TARPIT;
8919 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008920 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008921 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008922 cur_arg = 1;
8923
8924 while(*args[cur_arg]) {
8925 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008926 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008927 cur_arg+=2;
8928 continue;
8929 } else
8930 break;
8931 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008932 } else if (!strcmp(args[0], "set-nice")) {
8933 rule->action = HTTP_REQ_ACT_SET_NICE;
8934 cur_arg = 1;
8935
8936 if (!*args[cur_arg] ||
8937 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8938 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8939 file, linenum, args[0]);
8940 goto out_err;
8941 }
8942 rule->arg.nice = atoi(args[cur_arg]);
8943 if (rule->arg.nice < -1024)
8944 rule->arg.nice = -1024;
8945 else if (rule->arg.nice > 1024)
8946 rule->arg.nice = 1024;
8947 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008948 } else if (!strcmp(args[0], "set-tos")) {
8949#ifdef IP_TOS
8950 char *err;
8951 rule->action = HTTP_REQ_ACT_SET_TOS;
8952 cur_arg = 1;
8953
8954 if (!*args[cur_arg] ||
8955 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8956 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8957 file, linenum, args[0]);
8958 goto out_err;
8959 }
8960
8961 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8962 if (err && *err != '\0') {
8963 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8964 file, linenum, err, args[0]);
8965 goto out_err;
8966 }
8967 cur_arg++;
8968#else
8969 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8970 goto out_err;
8971#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008972 } else if (!strcmp(args[0], "set-mark")) {
8973#ifdef SO_MARK
8974 char *err;
8975 rule->action = HTTP_REQ_ACT_SET_MARK;
8976 cur_arg = 1;
8977
8978 if (!*args[cur_arg] ||
8979 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8980 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8981 file, linenum, args[0]);
8982 goto out_err;
8983 }
8984
8985 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8986 if (err && *err != '\0') {
8987 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8988 file, linenum, err, args[0]);
8989 goto out_err;
8990 }
8991 cur_arg++;
8992 global.last_checks |= LSTCHK_NETADM;
8993#else
8994 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8995 goto out_err;
8996#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008997 } else if (!strcmp(args[0], "set-log-level")) {
8998 rule->action = HTTP_REQ_ACT_SET_LOGL;
8999 cur_arg = 1;
9000
9001 if (!*args[cur_arg] ||
9002 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9003 bad_log_level:
9004 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
9005 file, linenum, args[0]);
9006 goto out_err;
9007 }
9008 if (strcmp(args[cur_arg], "silent") == 0)
9009 rule->arg.loglevel = -1;
9010 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
9011 goto bad_log_level;
9012 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009013 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9014 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
9015 cur_arg = 1;
9016
Willy Tarreau8d1c5162013-04-03 14:13:58 +02009017 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9018 (*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 +01009019 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9020 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009021 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009022 }
9023
9024 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9025 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9026 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02009027
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009028 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009029 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009030 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9031 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009032 free(proxy->conf.lfs_file);
9033 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9034 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01009035 cur_arg += 2;
Willy Tarreaub8543922014-06-17 18:58:26 +02009036 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
9037 rule->action = args[0][8] == 'h' ? HTTP_REQ_ACT_REPLACE_HDR : HTTP_REQ_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009038 cur_arg = 1;
9039
9040 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmann8f249f42014-06-24 11:10:00 +02009041 (*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 -06009042 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 3 arguments.\n",
9043 file, linenum, args[0]);
9044 goto out_err;
9045 }
9046
9047 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9048 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9049 LIST_INIT(&rule->arg.hdr_add.fmt);
9050
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009051 error = NULL;
9052 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9053 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9054 args[cur_arg + 1], error);
9055 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009056 goto out_err;
9057 }
9058
9059 proxy->conf.args.ctx = ARGC_HRQ;
9060 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9061 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9062 file, linenum);
9063
9064 free(proxy->conf.lfs_file);
9065 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9066 proxy->conf.lfs_line = proxy->conf.args.line;
9067 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009068 } else if (strcmp(args[0], "del-header") == 0) {
9069 rule->action = HTTP_REQ_ACT_DEL_HDR;
9070 cur_arg = 1;
9071
9072 if (!*args[cur_arg] ||
9073 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9074 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9075 file, linenum, args[0]);
9076 goto out_err;
9077 }
9078
9079 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9080 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9081
9082 proxy->conf.args.ctx = ARGC_HRQ;
9083 free(proxy->conf.lfs_file);
9084 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9085 proxy->conf.lfs_line = proxy->conf.args.line;
9086 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009087 } else if (strcmp(args[0], "redirect") == 0) {
9088 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01009089 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009090
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009091 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01009092 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9093 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9094 goto out_err;
9095 }
9096
9097 /* this redirect rule might already contain a parsed condition which
9098 * we'll pass to the http-request rule.
9099 */
9100 rule->action = HTTP_REQ_ACT_REDIR;
9101 rule->arg.redir = redir;
9102 rule->cond = redir->cond;
9103 redir->cond = NULL;
9104 cur_arg = 2;
9105 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009106 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9107 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9108 rule->action = HTTP_REQ_ACT_ADD_ACL;
9109 /*
9110 * '+ 8' for 'add-acl('
9111 * '- 9' for 'add-acl(' + trailing ')'
9112 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009113 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009114
9115 cur_arg = 1;
9116
9117 if (!*args[cur_arg] ||
9118 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9119 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9120 file, linenum, args[0]);
9121 goto out_err;
9122 }
9123
9124 LIST_INIT(&rule->arg.map.key);
9125 proxy->conf.args.ctx = ARGC_HRQ;
9126 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9127 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9128 file, linenum);
9129 free(proxy->conf.lfs_file);
9130 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9131 proxy->conf.lfs_line = proxy->conf.args.line;
9132 cur_arg += 1;
9133 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9134 /* http-request del-acl(<reference (acl name)>) <key pattern> */
9135 rule->action = HTTP_REQ_ACT_DEL_ACL;
9136 /*
9137 * '+ 8' for 'del-acl('
9138 * '- 9' for 'del-acl(' + trailing ')'
9139 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009140 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009141
9142 cur_arg = 1;
9143
9144 if (!*args[cur_arg] ||
9145 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9146 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9147 file, linenum, args[0]);
9148 goto out_err;
9149 }
9150
9151 LIST_INIT(&rule->arg.map.key);
9152 proxy->conf.args.ctx = ARGC_HRQ;
9153 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9154 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9155 file, linenum);
9156 free(proxy->conf.lfs_file);
9157 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9158 proxy->conf.lfs_line = proxy->conf.args.line;
9159 cur_arg += 1;
9160 } else if (strncmp(args[0], "del-map", 7) == 0) {
9161 /* http-request del-map(<reference (map name)>) <key pattern> */
9162 rule->action = HTTP_REQ_ACT_DEL_MAP;
9163 /*
9164 * '+ 8' for 'del-map('
9165 * '- 9' for 'del-map(' + trailing ')'
9166 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009167 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009168
9169 cur_arg = 1;
9170
9171 if (!*args[cur_arg] ||
9172 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9173 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
9174 file, linenum, args[0]);
9175 goto out_err;
9176 }
9177
9178 LIST_INIT(&rule->arg.map.key);
9179 proxy->conf.args.ctx = ARGC_HRQ;
9180 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9181 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9182 file, linenum);
9183 free(proxy->conf.lfs_file);
9184 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9185 proxy->conf.lfs_line = proxy->conf.args.line;
9186 cur_arg += 1;
9187 } else if (strncmp(args[0], "set-map", 7) == 0) {
9188 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
9189 rule->action = HTTP_REQ_ACT_SET_MAP;
9190 /*
9191 * '+ 8' for 'set-map('
9192 * '- 9' for 'set-map(' + trailing ')'
9193 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009194 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009195
9196 cur_arg = 1;
9197
9198 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9199 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9200 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
9201 file, linenum, args[0]);
9202 goto out_err;
9203 }
9204
9205 LIST_INIT(&rule->arg.map.key);
9206 LIST_INIT(&rule->arg.map.value);
9207 proxy->conf.args.ctx = ARGC_HRQ;
9208
9209 /* key pattern */
9210 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9211 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9212 file, linenum);
9213
9214 /* value pattern */
9215 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9216 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9217 file, linenum);
9218 free(proxy->conf.lfs_file);
9219 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9220 proxy->conf.lfs_line = proxy->conf.args.line;
9221
9222 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009223 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
9224 char *errmsg = NULL;
9225 cur_arg = 1;
9226 /* try in the module list */
9227 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9228 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
9229 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9230 free(errmsg);
9231 goto out_err;
9232 }
Willy Tarreauff011f22011-01-06 17:51:27 +01009233 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009234 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 +01009235 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01009236 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009237 }
9238
9239 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9240 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009241 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009242
Willy Tarreaub7451bb2012-04-27 12:38:15 +02009243 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9244 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
9245 file, linenum, args[0], errmsg);
9246 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009247 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009248 }
9249 rule->cond = cond;
9250 }
9251 else if (*args[cur_arg]) {
9252 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
9253 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9254 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01009255 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01009256 }
9257
9258 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009259 out_err:
9260 free(rule);
9261 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009262}
9263
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009264/* parse an "http-respose" rule */
9265struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9266{
9267 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009268 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009269 int cur_arg;
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009270 char *error;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009271
9272 rule = calloc(1, sizeof(*rule));
9273 if (!rule) {
9274 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9275 goto out_err;
9276 }
9277
9278 if (!strcmp(args[0], "allow")) {
9279 rule->action = HTTP_RES_ACT_ALLOW;
9280 cur_arg = 1;
9281 } else if (!strcmp(args[0], "deny")) {
9282 rule->action = HTTP_RES_ACT_DENY;
9283 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009284 } else if (!strcmp(args[0], "set-nice")) {
9285 rule->action = HTTP_RES_ACT_SET_NICE;
9286 cur_arg = 1;
9287
9288 if (!*args[cur_arg] ||
9289 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9290 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9291 file, linenum, args[0]);
9292 goto out_err;
9293 }
9294 rule->arg.nice = atoi(args[cur_arg]);
9295 if (rule->arg.nice < -1024)
9296 rule->arg.nice = -1024;
9297 else if (rule->arg.nice > 1024)
9298 rule->arg.nice = 1024;
9299 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009300 } else if (!strcmp(args[0], "set-tos")) {
9301#ifdef IP_TOS
9302 char *err;
9303 rule->action = HTTP_RES_ACT_SET_TOS;
9304 cur_arg = 1;
9305
9306 if (!*args[cur_arg] ||
9307 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9308 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9309 file, linenum, args[0]);
9310 goto out_err;
9311 }
9312
9313 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9314 if (err && *err != '\0') {
9315 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9316 file, linenum, err, args[0]);
9317 goto out_err;
9318 }
9319 cur_arg++;
9320#else
9321 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9322 goto out_err;
9323#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009324 } else if (!strcmp(args[0], "set-mark")) {
9325#ifdef SO_MARK
9326 char *err;
9327 rule->action = HTTP_RES_ACT_SET_MARK;
9328 cur_arg = 1;
9329
9330 if (!*args[cur_arg] ||
9331 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9332 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9333 file, linenum, args[0]);
9334 goto out_err;
9335 }
9336
9337 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9338 if (err && *err != '\0') {
9339 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9340 file, linenum, err, args[0]);
9341 goto out_err;
9342 }
9343 cur_arg++;
9344 global.last_checks |= LSTCHK_NETADM;
9345#else
9346 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9347 goto out_err;
9348#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009349 } else if (!strcmp(args[0], "set-log-level")) {
9350 rule->action = HTTP_RES_ACT_SET_LOGL;
9351 cur_arg = 1;
9352
9353 if (!*args[cur_arg] ||
9354 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9355 bad_log_level:
9356 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9357 file, linenum, args[0]);
9358 goto out_err;
9359 }
9360 if (strcmp(args[cur_arg], "silent") == 0)
9361 rule->arg.loglevel = -1;
9362 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9363 goto bad_log_level;
9364 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009365 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9366 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9367 cur_arg = 1;
9368
9369 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9370 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9371 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9372 file, linenum, args[0]);
9373 goto out_err;
9374 }
9375
9376 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9377 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9378 LIST_INIT(&rule->arg.hdr_add.fmt);
9379
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009380 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009381 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009382 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9383 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009384 free(proxy->conf.lfs_file);
9385 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9386 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009387 cur_arg += 2;
Sasha Pachev218f0642014-06-16 12:05:59 -06009388 } else if (strcmp(args[0], "replace-header") == 0 || strcmp(args[0], "replace-value") == 0) {
Willy Tarreaub8543922014-06-17 18:58:26 +02009389 rule->action = args[0][8] == 'h' ? HTTP_RES_ACT_REPLACE_HDR : HTTP_RES_ACT_REPLACE_VAL;
Sasha Pachev218f0642014-06-16 12:05:59 -06009390 cur_arg = 1;
9391
9392 if (!*args[cur_arg] || !*args[cur_arg+1] || !*args[cur_arg+2] ||
Baptiste Assmanna772b942014-08-08 17:29:06 +02009393 (*args[cur_arg+3] && strcmp(args[cur_arg+3], "if") != 0 && strcmp(args[cur_arg+3], "unless") != 0)) {
9394 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 3 arguments.\n",
Sasha Pachev218f0642014-06-16 12:05:59 -06009395 file, linenum, args[0]);
9396 goto out_err;
9397 }
9398
9399 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9400 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9401 LIST_INIT(&rule->arg.hdr_add.fmt);
9402
Thierry FOURNIER09af0d62014-06-18 11:35:54 +02009403 error = NULL;
9404 if (!regex_comp(args[cur_arg + 1], &rule->arg.hdr_add.re, 1, 1, &error)) {
9405 Alert("parsing [%s:%d] : '%s' : %s.\n", file, linenum,
9406 args[cur_arg + 1], error);
9407 free(error);
Sasha Pachev218f0642014-06-16 12:05:59 -06009408 goto out_err;
9409 }
9410
9411 proxy->conf.args.ctx = ARGC_HRQ;
9412 parse_logformat_string(args[cur_arg + 2], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
9413 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9414 file, linenum);
9415
9416 free(proxy->conf.lfs_file);
9417 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9418 proxy->conf.lfs_line = proxy->conf.args.line;
9419 cur_arg += 3;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009420 } else if (strcmp(args[0], "del-header") == 0) {
9421 rule->action = HTTP_RES_ACT_DEL_HDR;
9422 cur_arg = 1;
9423
9424 if (!*args[cur_arg] ||
9425 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9426 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9427 file, linenum, args[0]);
9428 goto out_err;
9429 }
9430
9431 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9432 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9433
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009434 proxy->conf.args.ctx = ARGC_HRS;
9435 free(proxy->conf.lfs_file);
9436 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9437 proxy->conf.lfs_line = proxy->conf.args.line;
9438 cur_arg += 1;
9439 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9440 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9441 rule->action = HTTP_RES_ACT_ADD_ACL;
9442 /*
9443 * '+ 8' for 'add-acl('
9444 * '- 9' for 'add-acl(' + trailing ')'
9445 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009446 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009447
9448 cur_arg = 1;
9449
9450 if (!*args[cur_arg] ||
9451 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9452 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9453 file, linenum, args[0]);
9454 goto out_err;
9455 }
9456
9457 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009458 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009459 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9460 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9461 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009462 free(proxy->conf.lfs_file);
9463 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9464 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009465
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009466 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009467 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9468 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9469 rule->action = HTTP_RES_ACT_DEL_ACL;
9470 /*
9471 * '+ 8' for 'del-acl('
9472 * '- 9' for 'del-acl(' + trailing ')'
9473 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009474 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009475
9476 cur_arg = 1;
9477
9478 if (!*args[cur_arg] ||
9479 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9480 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9481 file, linenum, args[0]);
9482 goto out_err;
9483 }
9484
9485 LIST_INIT(&rule->arg.map.key);
9486 proxy->conf.args.ctx = ARGC_HRS;
9487 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9488 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9489 file, linenum);
9490 free(proxy->conf.lfs_file);
9491 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9492 proxy->conf.lfs_line = proxy->conf.args.line;
9493 cur_arg += 1;
9494 } else if (strncmp(args[0], "del-map", 7) == 0) {
9495 /* http-response del-map(<reference (map name)>) <key pattern> */
9496 rule->action = HTTP_RES_ACT_DEL_MAP;
9497 /*
9498 * '+ 8' for 'del-map('
9499 * '- 9' for 'del-map(' + trailing ')'
9500 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009501 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009502
9503 cur_arg = 1;
9504
9505 if (!*args[cur_arg] ||
9506 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9507 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9508 file, linenum, args[0]);
9509 goto out_err;
9510 }
9511
9512 LIST_INIT(&rule->arg.map.key);
9513 proxy->conf.args.ctx = ARGC_HRS;
9514 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9515 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9516 file, linenum);
9517 free(proxy->conf.lfs_file);
9518 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9519 proxy->conf.lfs_line = proxy->conf.args.line;
9520 cur_arg += 1;
9521 } else if (strncmp(args[0], "set-map", 7) == 0) {
9522 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9523 rule->action = HTTP_RES_ACT_SET_MAP;
9524 /*
9525 * '+ 8' for 'set-map('
9526 * '- 9' for 'set-map(' + trailing ')'
9527 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009528 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009529
9530 cur_arg = 1;
9531
9532 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9533 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9534 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9535 file, linenum, args[0]);
9536 goto out_err;
9537 }
9538
9539 LIST_INIT(&rule->arg.map.key);
9540 LIST_INIT(&rule->arg.map.value);
9541
9542 proxy->conf.args.ctx = ARGC_HRS;
9543
9544 /* key pattern */
9545 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9546 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9547 file, linenum);
9548
9549 /* value pattern */
9550 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9551 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9552 file, linenum);
9553
9554 free(proxy->conf.lfs_file);
9555 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9556 proxy->conf.lfs_line = proxy->conf.args.line;
9557
9558 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009559 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9560 char *errmsg = NULL;
9561 cur_arg = 1;
9562 /* try in the module list */
9563 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9564 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9565 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9566 free(errmsg);
9567 goto out_err;
9568 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009569 } else {
Sasha Pachev218f0642014-06-16 12:05:59 -06009570 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 +02009571 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9572 goto out_err;
9573 }
9574
9575 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9576 struct acl_cond *cond;
9577 char *errmsg = NULL;
9578
9579 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9580 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9581 file, linenum, args[0], errmsg);
9582 free(errmsg);
9583 goto out_err;
9584 }
9585 rule->cond = cond;
9586 }
9587 else if (*args[cur_arg]) {
9588 Alert("parsing [%s:%d]: 'http-response %s' expects"
9589 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9590 file, linenum, args[0], args[cur_arg]);
9591 goto out_err;
9592 }
9593
9594 return rule;
9595 out_err:
9596 free(rule);
9597 return NULL;
9598}
9599
Willy Tarreau4baae242012-12-27 12:00:31 +01009600/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009601 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9602 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009603 */
9604struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009605 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009606{
9607 struct redirect_rule *rule;
9608 int cur_arg;
9609 int type = REDIRECT_TYPE_NONE;
9610 int code = 302;
9611 const char *destination = NULL;
9612 const char *cookie = NULL;
9613 int cookie_set = 0;
9614 unsigned int flags = REDIRECT_FLAG_NONE;
9615 struct acl_cond *cond = NULL;
9616
9617 cur_arg = 0;
9618 while (*(args[cur_arg])) {
9619 if (strcmp(args[cur_arg], "location") == 0) {
9620 if (!*args[cur_arg + 1])
9621 goto missing_arg;
9622
9623 type = REDIRECT_TYPE_LOCATION;
9624 cur_arg++;
9625 destination = args[cur_arg];
9626 }
9627 else if (strcmp(args[cur_arg], "prefix") == 0) {
9628 if (!*args[cur_arg + 1])
9629 goto missing_arg;
9630
9631 type = REDIRECT_TYPE_PREFIX;
9632 cur_arg++;
9633 destination = args[cur_arg];
9634 }
9635 else if (strcmp(args[cur_arg], "scheme") == 0) {
9636 if (!*args[cur_arg + 1])
9637 goto missing_arg;
9638
9639 type = REDIRECT_TYPE_SCHEME;
9640 cur_arg++;
9641 destination = args[cur_arg];
9642 }
9643 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9644 if (!*args[cur_arg + 1])
9645 goto missing_arg;
9646
9647 cur_arg++;
9648 cookie = args[cur_arg];
9649 cookie_set = 1;
9650 }
9651 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9652 if (!*args[cur_arg + 1])
9653 goto missing_arg;
9654
9655 cur_arg++;
9656 cookie = args[cur_arg];
9657 cookie_set = 0;
9658 }
9659 else if (strcmp(args[cur_arg], "code") == 0) {
9660 if (!*args[cur_arg + 1])
9661 goto missing_arg;
9662
9663 cur_arg++;
9664 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009665 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009666 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009667 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009668 args[cur_arg - 1], args[cur_arg]);
9669 return NULL;
9670 }
9671 }
9672 else if (!strcmp(args[cur_arg],"drop-query")) {
9673 flags |= REDIRECT_FLAG_DROP_QS;
9674 }
9675 else if (!strcmp(args[cur_arg],"append-slash")) {
9676 flags |= REDIRECT_FLAG_APPEND_SLASH;
9677 }
9678 else if (strcmp(args[cur_arg], "if") == 0 ||
9679 strcmp(args[cur_arg], "unless") == 0) {
9680 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9681 if (!cond) {
9682 memprintf(errmsg, "error in condition: %s", *errmsg);
9683 return NULL;
9684 }
9685 break;
9686 }
9687 else {
9688 memprintf(errmsg,
9689 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9690 args[cur_arg]);
9691 return NULL;
9692 }
9693 cur_arg++;
9694 }
9695
9696 if (type == REDIRECT_TYPE_NONE) {
9697 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9698 return NULL;
9699 }
9700
9701 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9702 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009703 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009704
9705 if (!use_fmt) {
9706 /* old-style static redirect rule */
9707 rule->rdr_str = strdup(destination);
9708 rule->rdr_len = strlen(destination);
9709 }
9710 else {
9711 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009712
9713 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9714 * if prefix == "/", we don't want to add anything, otherwise it
9715 * makes it hard for the user to configure a self-redirection.
9716 */
Godbach543b9782014-12-18 15:44:58 +08009717 curproxy->conf.args.ctx = ARGC_RDR;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009718 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009719 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009720 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9721 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009722 free(curproxy->conf.lfs_file);
9723 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9724 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009725 }
9726 }
9727
Willy Tarreau4baae242012-12-27 12:00:31 +01009728 if (cookie) {
9729 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9730 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9731 */
9732 rule->cookie_len = strlen(cookie);
9733 if (cookie_set) {
9734 rule->cookie_str = malloc(rule->cookie_len + 10);
9735 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9736 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9737 rule->cookie_len += 9;
9738 } else {
9739 rule->cookie_str = malloc(rule->cookie_len + 21);
9740 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9741 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9742 rule->cookie_len += 20;
9743 }
9744 }
9745 rule->type = type;
9746 rule->code = code;
9747 rule->flags = flags;
9748 LIST_INIT(&rule->list);
9749 return rule;
9750
9751 missing_arg:
9752 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9753 return NULL;
9754}
9755
Willy Tarreau8797c062007-05-07 00:55:35 +02009756/************************************************************************/
9757/* The code below is dedicated to ACL parsing and matching */
9758/************************************************************************/
9759
9760
Willy Tarreau14174bc2012-04-16 14:34:04 +02009761/* This function ensures that the prerequisites for an L7 fetch are ready,
9762 * which means that a request or response is ready. If some data is missing,
9763 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009764 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9765 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009766 *
9767 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009768 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9769 * decide whether or not an HTTP message is present ;
9770 * 0 if the requested data cannot be fetched or if it is certain that
9771 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009772 * 1 if an HTTP message is ready
9773 */
9774static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009775smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009776 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009777{
9778 struct http_txn *txn = l7;
9779 struct http_msg *msg = &txn->req;
9780
9781 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9782 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9783 */
9784
9785 if (unlikely(!s || !txn))
9786 return 0;
9787
9788 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009789 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009790
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009791 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009792 if (unlikely(!s->req))
9793 return 0;
9794
Willy Tarreauaae75e32013-03-29 12:31:49 +01009795 /* If the buffer does not leave enough free space at the end,
9796 * we must first realign it.
9797 */
9798 if (s->req->buf->p > s->req->buf->data &&
9799 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9800 buffer_slow_realign(s->req->buf);
9801
Willy Tarreau14174bc2012-04-16 14:34:04 +02009802 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009803 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009804 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009805
9806 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009807 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009808 http_msg_analyzer(msg, &txn->hdr_idx);
9809
9810 /* Still no valid request ? */
9811 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009812 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009813 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009814 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009815 }
9816 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009817 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009818 return 0;
9819 }
9820
9821 /* OK we just got a valid HTTP request. We have some minor
9822 * preparation to perform so that further checks can rely
9823 * on HTTP tests.
9824 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009825
9826 /* If the request was parsed but was too large, we must absolutely
9827 * return an error so that it is not processed. At the moment this
9828 * cannot happen, but if the parsers are to change in the future,
9829 * we want this check to be maintained.
9830 */
9831 if (unlikely(s->req->buf->i + s->req->buf->p >
9832 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9833 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009834 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009835 return 1;
9836 }
9837
Willy Tarreau9b28e032012-10-12 23:49:43 +02009838 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009839 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9840 s->flags |= SN_REDIRECTABLE;
9841
Willy Tarreau506d0502013-07-06 13:29:24 +02009842 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9843 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009844 }
9845
Willy Tarreau506d0502013-07-06 13:29:24 +02009846 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009847 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009848 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009849
9850 /* otherwise everything's ready for the request */
9851 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009852 else {
9853 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009854 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9855 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009856 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009857 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009858 }
9859
9860 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009861 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009862 return 1;
9863}
Willy Tarreau8797c062007-05-07 00:55:35 +02009864
Willy Tarreaua4ba9db2014-06-25 16:56:41 +02009865/* Note: these functinos *do* modify the sample. Even in case of success, at
9866 * least the type and uint value are modified.
9867 */
Willy Tarreauc0239e02012-04-16 14:42:55 +02009868#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009869 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 +02009870
Willy Tarreau24e32d82012-04-23 23:55:44 +02009871#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009872 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 +02009873
Willy Tarreau8797c062007-05-07 00:55:35 +02009874
9875/* 1. Check on METHOD
9876 * We use the pre-parsed method if it is known, and store its number as an
9877 * integer. If it is unknown, we use the pointer and the length.
9878 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009879static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009880{
9881 int len, meth;
9882
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009883 len = strlen(text);
9884 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009885
9886 pattern->val.i = meth;
9887 if (meth == HTTP_METH_OTHER) {
Willy Tarreau71196f32014-08-29 15:15:50 +02009888 pattern->ptr.str = (char *)text;
Willy Tarreau8797c062007-05-07 00:55:35 +02009889 pattern->len = len;
9890 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009891 else {
9892 pattern->ptr.str = NULL;
9893 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009894 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009895 return 1;
9896}
9897
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009898/* This function fetches the method of current HTTP request and stores
9899 * it in the global pattern struct as a chunk. There are two possibilities :
9900 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9901 * in <len> and <ptr> is NULL ;
9902 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9903 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009904 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009905 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009906static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009907smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009908 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009909{
9910 int meth;
9911 struct http_txn *txn = l7;
9912
Willy Tarreau24e32d82012-04-23 23:55:44 +02009913 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009914
Willy Tarreau8797c062007-05-07 00:55:35 +02009915 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009916 smp->type = SMP_T_METH;
9917 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009918 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009919 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9920 /* ensure the indexes are not affected */
9921 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009922 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009923 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9924 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009925 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009926 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009927 return 1;
9928}
9929
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009930/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009931static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009932{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009933 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009934 struct pattern_list *lst;
9935 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009936
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009937 list_for_each_entry(lst, &expr->patterns, list) {
9938 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009939
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009940 /* well-known method */
9941 if (pattern->val.i != HTTP_METH_OTHER) {
9942 if (smp->data.meth.meth == pattern->val.i)
9943 return pattern;
9944 else
9945 continue;
9946 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009947
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009948 /* Other method, we must compare the strings */
9949 if (pattern->len != smp->data.meth.str.len)
9950 continue;
9951
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009952 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Willy Tarreauc62a0c62014-08-28 20:42:57 +02009953 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0) ||
9954 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) == 0))
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009955 return pattern;
9956 }
9957 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009958}
9959
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009960static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009961smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009962 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009963{
9964 struct http_txn *txn = l7;
9965 char *ptr;
9966 int len;
9967
Willy Tarreauc0239e02012-04-16 14:42:55 +02009968 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009969
Willy Tarreau8797c062007-05-07 00:55:35 +02009970 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009971 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009972
9973 while ((len-- > 0) && (*ptr++ != '/'));
9974 if (len <= 0)
9975 return 0;
9976
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009977 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009978 smp->data.str.str = ptr;
9979 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009980
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009981 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009982 return 1;
9983}
9984
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009985static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009986smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009987 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009988{
9989 struct http_txn *txn = l7;
9990 char *ptr;
9991 int len;
9992
Willy Tarreauc0239e02012-04-16 14:42:55 +02009993 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009994
Willy Tarreauf26b2522012-12-14 08:33:14 +01009995 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9996 return 0;
9997
Willy Tarreau8797c062007-05-07 00:55:35 +02009998 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009999 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +020010000
10001 while ((len-- > 0) && (*ptr++ != '/'));
10002 if (len <= 0)
10003 return 0;
10004
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010005 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010006 smp->data.str.str = ptr;
10007 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +020010008
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010009 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010010 return 1;
10011}
10012
10013/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010014static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010015smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010016 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +020010017{
10018 struct http_txn *txn = l7;
10019 char *ptr;
10020 int len;
10021
Willy Tarreauc0239e02012-04-16 14:42:55 +020010022 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010023
Willy Tarreauf26b2522012-12-14 08:33:14 +010010024 if (txn->rsp.msg_state < HTTP_MSG_BODY)
10025 return 0;
10026
Willy Tarreau8797c062007-05-07 00:55:35 +020010027 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010028 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +020010029
Willy Tarreauf853c462012-04-23 18:53:56 +020010030 smp->type = SMP_T_UINT;
10031 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +020010032 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010033 return 1;
10034}
10035
10036/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +020010037static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010038smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010039 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +020010040{
10041 struct http_txn *txn = l7;
10042
Willy Tarreauc0239e02012-04-16 14:42:55 +020010043 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010044
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010045 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010046 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +020010047 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010048 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +020010049 return 1;
10050}
10051
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010052static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010053smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010054 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010055{
10056 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010057 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010058
Willy Tarreauc0239e02012-04-16 14:42:55 +020010059 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010060
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010061 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &addr, NULL);
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010062 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +010010063 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010064
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010065 smp->type = SMP_T_IPV4;
10066 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +020010067 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010068 return 1;
10069}
10070
10071static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010072smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010073 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010074{
10075 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010076 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010077
Willy Tarreauc0239e02012-04-16 14:42:55 +020010078 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010079
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010010080 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 +020010081 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
10082 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010083
Willy Tarreau4c804ec2013-09-30 14:37:14 +020010084 smp->type = SMP_T_UINT;
10085 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +020010086 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +010010087 return 1;
10088}
10089
Willy Tarreau185b5c42012-04-26 15:11:51 +020010090/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10091 * Accepts an optional argument of type string containing the header field name,
10092 * and an optional argument of type signed or unsigned integer to request an
10093 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010094 * headers are considered from the first one. It does not stop on commas and
10095 * returns full lines instead (useful for User-Agent or Date for example).
10096 */
10097static int
10098smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010099 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010100{
10101 struct http_txn *txn = l7;
10102 struct hdr_idx *idx = &txn->hdr_idx;
10103 struct hdr_ctx *ctx = smp->ctx.a[0];
10104 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10105 int occ = 0;
10106 const char *name_str = NULL;
10107 int name_len = 0;
10108
10109 if (!ctx) {
10110 /* first call */
10111 ctx = &static_hdr_ctx;
10112 ctx->idx = 0;
10113 smp->ctx.a[0] = ctx;
10114 }
10115
10116 if (args) {
10117 if (args[0].type != ARGT_STR)
10118 return 0;
10119 name_str = args[0].data.str.str;
10120 name_len = args[0].data.str.len;
10121
10122 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10123 occ = args[1].data.uint;
10124 }
10125
10126 CHECK_HTTP_MESSAGE_FIRST();
10127
10128 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
10129 /* search for header from the beginning */
10130 ctx->idx = 0;
10131
10132 if (!occ && !(opt & SMP_OPT_ITERATE))
10133 /* no explicit occurrence and single fetch => last header by default */
10134 occ = -1;
10135
10136 if (!occ)
10137 /* prepare to report multiple occurrences for ACL fetches */
10138 smp->flags |= SMP_F_NOT_LAST;
10139
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010140 smp->type = SMP_T_STR;
10141 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010142 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
10143 return 1;
10144
10145 smp->flags &= ~SMP_F_NOT_LAST;
10146 return 0;
10147}
10148
10149/* 6. Check on HTTP header count. The number of occurrences is returned.
10150 * Accepts exactly 1 argument of type string. It does not stop on commas and
10151 * returns full lines instead (useful for User-Agent or Date for example).
10152 */
10153static int
10154smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010155 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010156{
10157 struct http_txn *txn = l7;
10158 struct hdr_idx *idx = &txn->hdr_idx;
10159 struct hdr_ctx ctx;
10160 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
10161 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010162 const char *name = NULL;
10163 int len = 0;
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010164
Willy Tarreau29437342015-04-01 19:16:09 +020010165 if (args && args->type == ARGT_STR) {
10166 name = args->data.str.str;
10167 len = args->data.str.len;
10168 }
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010169
10170 CHECK_HTTP_MESSAGE_FIRST();
10171
10172 ctx.idx = 0;
10173 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010174 while (http_find_full_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010175 cnt++;
10176
10177 smp->type = SMP_T_UINT;
10178 smp->data.uint = cnt;
10179 smp->flags = SMP_F_VOL_HDR;
10180 return 1;
10181}
10182
10183/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
10184 * Accepts an optional argument of type string containing the header field name,
10185 * and an optional argument of type signed or unsigned integer to request an
10186 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +020010187 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010188 */
Willy Tarreau33a7e692007-06-10 19:45:56 +020010189static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010190smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010191 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010192{
10193 struct http_txn *txn = l7;
10194 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010195 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010196 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010197 int occ = 0;
10198 const char *name_str = NULL;
10199 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010200
Willy Tarreaua890d072013-04-02 12:01:06 +020010201 if (!ctx) {
10202 /* first call */
10203 ctx = &static_hdr_ctx;
10204 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +020010205 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010206 }
10207
Willy Tarreau185b5c42012-04-26 15:11:51 +020010208 if (args) {
10209 if (args[0].type != ARGT_STR)
10210 return 0;
10211 name_str = args[0].data.str.str;
10212 name_len = args[0].data.str.len;
10213
10214 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
10215 occ = args[1].data.uint;
10216 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010217
Willy Tarreaue333ec92012-04-16 16:26:40 +020010218 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +020010219
Willy Tarreau185b5c42012-04-26 15:11:51 +020010220 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010221 /* search for header from the beginning */
10222 ctx->idx = 0;
10223
Willy Tarreau185b5c42012-04-26 15:11:51 +020010224 if (!occ && !(opt & SMP_OPT_ITERATE))
10225 /* no explicit occurrence and single fetch => last header by default */
10226 occ = -1;
10227
10228 if (!occ)
10229 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +020010230 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +010010231
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010232 smp->type = SMP_T_STR;
10233 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +020010234 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 +020010235 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010236
Willy Tarreau37406352012-04-23 16:16:37 +020010237 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010238 return 0;
10239}
10240
Willy Tarreauc11416f2007-06-17 16:58:38 +020010241/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +020010242 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +020010243 */
10244static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010245smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010246 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010247{
10248 struct http_txn *txn = l7;
10249 struct hdr_idx *idx = &txn->hdr_idx;
10250 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010251 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010252 int cnt;
Willy Tarreau29437342015-04-01 19:16:09 +020010253 const char *name = NULL;
10254 int len = 0;
Willy Tarreau8797c062007-05-07 00:55:35 +020010255
Willy Tarreau29437342015-04-01 19:16:09 +020010256 if (args && args->type == ARGT_STR) {
10257 name = args->data.str.str;
10258 len = args->data.str.len;
10259 }
Willy Tarreau34db1082012-04-19 17:16:54 +020010260
Willy Tarreaue333ec92012-04-16 16:26:40 +020010261 CHECK_HTTP_MESSAGE_FIRST();
10262
Willy Tarreau33a7e692007-06-10 19:45:56 +020010263 ctx.idx = 0;
10264 cnt = 0;
Willy Tarreau29437342015-04-01 19:16:09 +020010265 while (http_find_header2(name, len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +020010266 cnt++;
10267
Willy Tarreauf853c462012-04-23 18:53:56 +020010268 smp->type = SMP_T_UINT;
10269 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010270 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010271 return 1;
10272}
10273
Willy Tarreau185b5c42012-04-26 15:11:51 +020010274/* Fetch an HTTP header's integer value. The integer value is returned. It
10275 * takes a mandatory argument of type string and an optional one of type int
10276 * to designate a specific occurrence. It returns an unsigned integer, which
10277 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +020010278 */
10279static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010280smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010281 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +020010282{
Willy Tarreauef38c392013-07-22 16:29:32 +020010283 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +020010284
Willy Tarreauf853c462012-04-23 18:53:56 +020010285 if (ret > 0) {
10286 smp->type = SMP_T_UINT;
10287 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10288 }
Willy Tarreau33a7e692007-06-10 19:45:56 +020010289
Willy Tarreaud53e2422012-04-16 17:21:11 +020010290 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010291}
10292
Cyril Bonté69fa9922012-10-25 00:01:06 +020010293/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
10294 * and an optional one of type int to designate a specific occurrence.
10295 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010296 */
10297static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010298smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010299 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +020010300{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010301 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010302
Willy Tarreauef38c392013-07-22 16:29:32 +020010303 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010304 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10305 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010306 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010307 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010308 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010309 if (smp->data.str.len < temp->size - 1) {
10310 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10311 temp->str[smp->data.str.len] = '\0';
10312 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10313 smp->type = SMP_T_IPV6;
10314 break;
10315 }
10316 }
10317 }
10318
Willy Tarreaud53e2422012-04-16 17:21:11 +020010319 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010320 if (!(smp->flags & SMP_F_NOT_LAST))
10321 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010322 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010323 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010324}
10325
Willy Tarreau737b0c12007-06-10 21:28:46 +020010326/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10327 * the first '/' after the possible hostname, and ends before the possible '?'.
10328 */
10329static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010330smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010331 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010332{
10333 struct http_txn *txn = l7;
10334 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010335
Willy Tarreauc0239e02012-04-16 14:42:55 +020010336 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010337
Willy Tarreau9b28e032012-10-12 23:49:43 +020010338 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010339 ptr = http_get_path(txn);
10340 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010341 return 0;
10342
10343 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010344 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010345 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010346
10347 while (ptr < end && *ptr != '?')
10348 ptr++;
10349
Willy Tarreauf853c462012-04-23 18:53:56 +020010350 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010351 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010352 return 1;
10353}
10354
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010355/* This produces a concatenation of the first occurrence of the Host header
10356 * followed by the path component if it begins with a slash ('/'). This means
10357 * that '*' will not be added, resulting in exactly the first Host entry.
10358 * If no Host header is found, then the path is returned as-is. The returned
10359 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010360 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010361 */
10362static int
10363smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010364 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010365{
10366 struct http_txn *txn = l7;
10367 char *ptr, *end, *beg;
10368 struct hdr_ctx ctx;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010369 struct chunk *temp;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010370
10371 CHECK_HTTP_MESSAGE_FIRST();
10372
10373 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010374 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +020010375 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010376
10377 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010378 temp = get_trash_chunk();
10379 memcpy(temp->str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010380 smp->type = SMP_T_STR;
Willy Tarreauc1fbbd42014-06-24 17:27:02 +020010381 smp->data.str.str = temp->str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010382 smp->data.str.len = ctx.vlen;
10383
10384 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010385 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010386 beg = http_get_path(txn);
10387 if (!beg)
10388 beg = end;
10389
10390 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10391
10392 if (beg < ptr && *beg == '/') {
10393 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10394 smp->data.str.len += ptr - beg;
10395 }
10396
10397 smp->flags = SMP_F_VOL_1ST;
10398 return 1;
10399}
10400
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010401/* This produces a 32-bit hash of the concatenation of the first occurrence of
10402 * the Host header followed by the path component if it begins with a slash ('/').
10403 * This means that '*' will not be added, resulting in exactly the first Host
10404 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010405 * is hashed using the path hash followed by a full avalanche hash and provides a
10406 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010407 * high-traffic sites without having to store whole paths.
10408 */
10409static int
10410smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010411 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010412{
10413 struct http_txn *txn = l7;
10414 struct hdr_ctx ctx;
10415 unsigned int hash = 0;
10416 char *ptr, *beg, *end;
10417 int len;
10418
10419 CHECK_HTTP_MESSAGE_FIRST();
10420
10421 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010422 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010423 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10424 ptr = ctx.line + ctx.val;
10425 len = ctx.vlen;
10426 while (len--)
10427 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10428 }
10429
10430 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010431 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010432 beg = http_get_path(txn);
10433 if (!beg)
10434 beg = end;
10435
10436 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10437
10438 if (beg < ptr && *beg == '/') {
10439 while (beg < ptr)
10440 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10441 }
10442 hash = full_hash(hash);
10443
10444 smp->type = SMP_T_UINT;
10445 smp->data.uint = hash;
10446 smp->flags = SMP_F_VOL_1ST;
10447 return 1;
10448}
10449
Willy Tarreau4a550602012-12-09 14:53:32 +010010450/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010451 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10452 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10453 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010454 * that in environments where IPv6 is insignificant, truncating the output to
10455 * 8 bytes would still work.
10456 */
10457static int
10458smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010459 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +010010460{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010461 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010462 struct connection *cli_conn = objt_conn(l4->si[0].end);
10463
10464 if (!cli_conn)
10465 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010466
Willy Tarreauef38c392013-07-22 16:29:32 +020010467 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +010010468 return 0;
10469
Willy Tarreau47ca5452012-12-23 20:22:19 +010010470 temp = get_trash_chunk();
Willy Tarreau0dff81c2014-07-15 21:34:06 +020010471 *(unsigned int *)temp->str = htonl(smp->data.uint);
10472 temp->len += sizeof(unsigned int);
Willy Tarreau4a550602012-12-09 14:53:32 +010010473
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010474 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010475 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010476 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010477 temp->len += 4;
10478 break;
10479 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010480 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010481 temp->len += 16;
10482 break;
10483 default:
10484 return 0;
10485 }
10486
10487 smp->data.str = *temp;
10488 smp->type = SMP_T_BIN;
10489 return 1;
10490}
10491
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010492static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010493smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010494 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010495{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010496 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10497 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10498 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010499
Willy Tarreau24e32d82012-04-23 23:55:44 +020010500 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010501
Willy Tarreauf853c462012-04-23 18:53:56 +020010502 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010503 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010504 return 1;
10505}
10506
Willy Tarreau7f18e522010-10-22 20:04:13 +020010507/* return a valid test if the current request is the first one on the connection */
10508static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010509smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010510 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010511{
10512 if (!s)
10513 return 0;
10514
Willy Tarreauf853c462012-04-23 18:53:56 +020010515 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010516 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010517 return 1;
10518}
10519
Willy Tarreau34db1082012-04-19 17:16:54 +020010520/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010521static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010522smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010523 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010524{
10525
Willy Tarreau24e32d82012-04-23 23:55:44 +020010526 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010527 return 0;
10528
Willy Tarreauc0239e02012-04-16 14:42:55 +020010529 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010530
Willy Tarreauc0239e02012-04-16 14:42:55 +020010531 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010532 return 0;
10533
Willy Tarreauf853c462012-04-23 18:53:56 +020010534 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010535 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010536 return 1;
10537}
Willy Tarreau8797c062007-05-07 00:55:35 +020010538
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010539/* Accepts exactly 1 argument of type userlist */
10540static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010541smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010542 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010543{
10544
10545 if (!args || args->type != ARGT_USR)
10546 return 0;
10547
10548 CHECK_HTTP_MESSAGE_FIRST();
10549
10550 if (!get_http_auth(l4))
10551 return 0;
10552
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010553 /* if the user does not belong to the userlist or has a wrong password,
10554 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010555 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010556 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010557 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10558 return 0;
10559
10560 /* pat_match_auth() will need the user list */
10561 smp->ctx.a[0] = args->data.usr;
10562
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010563 smp->type = SMP_T_STR;
10564 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010565 smp->data.str.str = l4->txn.auth.user;
10566 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010567
10568 return 1;
10569}
10570
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010571/* Try to find the next occurrence of a cookie name in a cookie header value.
10572 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10573 * the cookie value is returned into *value and *value_l, and the function
10574 * returns a pointer to the next pointer to search from if the value was found.
10575 * Otherwise if the cookie was not found, NULL is returned and neither value
10576 * nor value_l are touched. The input <hdr> string should first point to the
10577 * header's value, and the <hdr_end> pointer must point to the first character
10578 * not part of the value. <list> must be non-zero if value may represent a list
10579 * of values (cookie headers). This makes it faster to abort parsing when no
10580 * list is expected.
10581 */
10582static char *
10583extract_cookie_value(char *hdr, const char *hdr_end,
10584 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010585 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010586{
10587 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10588 char *next;
10589
10590 /* we search at least a cookie name followed by an equal, and more
10591 * generally something like this :
10592 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10593 */
10594 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10595 /* Iterate through all cookies on this line */
10596
10597 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10598 att_beg++;
10599
10600 /* find att_end : this is the first character after the last non
10601 * space before the equal. It may be equal to hdr_end.
10602 */
10603 equal = att_end = att_beg;
10604
10605 while (equal < hdr_end) {
10606 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10607 break;
10608 if (http_is_spht[(unsigned char)*equal++])
10609 continue;
10610 att_end = equal;
10611 }
10612
10613 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10614 * is between <att_beg> and <equal>, both may be identical.
10615 */
10616
10617 /* look for end of cookie if there is an equal sign */
10618 if (equal < hdr_end && *equal == '=') {
10619 /* look for the beginning of the value */
10620 val_beg = equal + 1;
10621 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10622 val_beg++;
10623
10624 /* find the end of the value, respecting quotes */
10625 next = find_cookie_value_end(val_beg, hdr_end);
10626
10627 /* make val_end point to the first white space or delimitor after the value */
10628 val_end = next;
10629 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10630 val_end--;
10631 } else {
10632 val_beg = val_end = next = equal;
10633 }
10634
10635 /* We have nothing to do with attributes beginning with '$'. However,
10636 * they will automatically be removed if a header before them is removed,
10637 * since they're supposed to be linked together.
10638 */
10639 if (*att_beg == '$')
10640 continue;
10641
10642 /* Ignore cookies with no equal sign */
10643 if (equal == next)
10644 continue;
10645
10646 /* Now we have the cookie name between att_beg and att_end, and
10647 * its value between val_beg and val_end.
10648 */
10649
10650 if (att_end - att_beg == cookie_name_l &&
10651 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10652 /* let's return this value and indicate where to go on from */
10653 *value = val_beg;
10654 *value_l = val_end - val_beg;
10655 return next + 1;
10656 }
10657
10658 /* Set-Cookie headers only have the name in the first attr=value part */
10659 if (!list)
10660 break;
10661 }
10662
10663 return NULL;
10664}
10665
William Lallemanda43ba4e2014-01-28 18:14:25 +010010666/* Fetch a captured HTTP request header. The index is the position of
10667 * the "capture" option in the configuration file
10668 */
10669static int
10670smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10671 const struct arg *args, struct sample *smp, const char *kw)
10672{
10673 struct proxy *fe = l4->fe;
10674 struct http_txn *txn = l7;
10675 int idx;
10676
10677 if (!args || args->type != ARGT_UINT)
10678 return 0;
10679
10680 idx = args->data.uint;
10681
10682 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10683 return 0;
10684
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010685 smp->type = SMP_T_STR;
10686 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010687 smp->data.str.str = txn->req.cap[idx];
10688 smp->data.str.len = strlen(txn->req.cap[idx]);
10689
10690 return 1;
10691}
10692
10693/* Fetch a captured HTTP response header. The index is the position of
10694 * the "capture" option in the configuration file
10695 */
10696static int
10697smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10698 const struct arg *args, struct sample *smp, const char *kw)
10699{
10700 struct proxy *fe = l4->fe;
10701 struct http_txn *txn = l7;
10702 int idx;
10703
10704 if (!args || args->type != ARGT_UINT)
10705 return 0;
10706
10707 idx = args->data.uint;
10708
10709 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10710 return 0;
10711
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010712 smp->type = SMP_T_STR;
10713 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010714 smp->data.str.str = txn->rsp.cap[idx];
10715 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10716
10717 return 1;
10718}
10719
William Lallemand65ad6e12014-01-31 15:08:02 +010010720/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10721static int
10722smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10723 const struct arg *args, struct sample *smp, const char *kw)
10724{
10725 struct chunk *temp;
10726 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010727 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010728
10729 if (!txn->uri)
10730 return 0;
10731
William Lallemand96a77852014-02-05 00:30:02 +010010732 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010733
William Lallemand96a77852014-02-05 00:30:02 +010010734 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10735 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010736
William Lallemand96a77852014-02-05 00:30:02 +010010737 temp = get_trash_chunk();
10738 temp->str = txn->uri;
10739 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010740 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010741 smp->type = SMP_T_STR;
10742 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010743
10744 return 1;
10745
10746}
10747
10748/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10749static int
10750smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10751 const struct arg *args, struct sample *smp, const char *kw)
10752{
10753 struct chunk *temp;
10754 struct http_txn *txn = l7;
10755 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010756
10757 if (!txn->uri)
10758 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010759
William Lallemand65ad6e12014-01-31 15:08:02 +010010760 ptr = txn->uri;
10761
10762 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10763 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010764
William Lallemand65ad6e12014-01-31 15:08:02 +010010765 if (!*ptr)
10766 return 0;
10767
10768 ptr++; /* skip the space */
10769
10770 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010771 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010772 if (!ptr)
10773 return 0;
10774 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10775 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010776
10777 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010778 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010779 smp->type = SMP_T_STR;
10780 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010781
10782 return 1;
10783}
10784
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010785/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10786 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10787 */
10788static int
10789smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10790 const struct arg *args, struct sample *smp, const char *kw)
10791{
10792 struct http_txn *txn = l7;
10793
10794 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10795 return 0;
10796
10797 if (txn->req.flags & HTTP_MSGF_VER_11)
10798 smp->data.str.str = "HTTP/1.1";
10799 else
10800 smp->data.str.str = "HTTP/1.0";
10801
10802 smp->data.str.len = 8;
10803 smp->type = SMP_T_STR;
10804 smp->flags = SMP_F_CONST;
10805 return 1;
10806
10807}
10808
10809/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10810 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10811 */
10812static int
10813smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10814 const struct arg *args, struct sample *smp, const char *kw)
10815{
10816 struct http_txn *txn = l7;
10817
10818 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10819 return 0;
10820
10821 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10822 smp->data.str.str = "HTTP/1.1";
10823 else
10824 smp->data.str.str = "HTTP/1.0";
10825
10826 smp->data.str.len = 8;
10827 smp->type = SMP_T_STR;
10828 smp->flags = SMP_F_CONST;
10829 return 1;
10830
10831}
10832
William Lallemand65ad6e12014-01-31 15:08:02 +010010833
Willy Tarreaue333ec92012-04-16 16:26:40 +020010834/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010835 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010836 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010837 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010838 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010839 * Accepts exactly 1 argument of type string. If the input options indicate
10840 * that no iterating is desired, then only last value is fetched if any.
William Lallemand07c8b242014-05-02 17:11:07 +020010841 * The returned sample is of type CSTR. Can be used to parse cookies in other
10842 * files.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010843 */
William Lallemand07c8b242014-05-02 17:11:07 +020010844int smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010845 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010846{
10847 struct http_txn *txn = l7;
10848 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010849 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010850 const struct http_msg *msg;
10851 const char *hdr_name;
10852 int hdr_name_len;
10853 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010854 int occ = 0;
10855 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010856
Willy Tarreau24e32d82012-04-23 23:55:44 +020010857 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010858 return 0;
10859
Willy Tarreaua890d072013-04-02 12:01:06 +020010860 if (!ctx) {
10861 /* first call */
10862 ctx = &static_hdr_ctx;
10863 ctx->idx = 0;
10864 smp->ctx.a[2] = ctx;
10865 }
10866
Willy Tarreaue333ec92012-04-16 16:26:40 +020010867 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010868
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010869 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010870 msg = &txn->req;
10871 hdr_name = "Cookie";
10872 hdr_name_len = 6;
10873 } else {
10874 msg = &txn->rsp;
10875 hdr_name = "Set-Cookie";
10876 hdr_name_len = 10;
10877 }
10878
Willy Tarreau28376d62012-04-26 21:26:10 +020010879 if (!occ && !(opt & SMP_OPT_ITERATE))
10880 /* no explicit occurrence and single fetch => last cookie by default */
10881 occ = -1;
10882
10883 /* OK so basically here, either we want only one value and it's the
10884 * last one, or we want to iterate over all of them and we fetch the
10885 * next one.
10886 */
10887
Willy Tarreau9b28e032012-10-12 23:49:43 +020010888 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010889 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010890 /* search for the header from the beginning, we must first initialize
10891 * the search parameters.
10892 */
Willy Tarreau37406352012-04-23 16:16:37 +020010893 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010894 ctx->idx = 0;
10895 }
10896
Willy Tarreau28376d62012-04-26 21:26:10 +020010897 smp->flags |= SMP_F_VOL_HDR;
10898
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010899 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010900 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10901 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010902 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10903 goto out;
10904
Willy Tarreau24e32d82012-04-23 23:55:44 +020010905 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010906 continue;
10907
Willy Tarreau37406352012-04-23 16:16:37 +020010908 smp->ctx.a[0] = ctx->line + ctx->val;
10909 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010910 }
10911
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010912 smp->type = SMP_T_STR;
10913 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010914 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010915 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010916 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010917 &smp->data.str.str,
10918 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010919 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010920 found = 1;
10921 if (occ >= 0) {
10922 /* one value was returned into smp->data.str.{str,len} */
10923 smp->flags |= SMP_F_NOT_LAST;
10924 return 1;
10925 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010926 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010927 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010928 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010929 /* all cookie headers and values were scanned. If we're looking for the
10930 * last occurrence, we may return it now.
10931 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010932 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010933 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010934 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010935}
10936
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010937/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010938 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010939 * multiple cookies may be parsed on the same line. The returned sample is of
10940 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010941 */
10942static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010943smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010944 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010945{
10946 struct http_txn *txn = l7;
10947 struct hdr_idx *idx = &txn->hdr_idx;
10948 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010949 const struct http_msg *msg;
10950 const char *hdr_name;
10951 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010952 int cnt;
10953 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010954 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010955
Willy Tarreau24e32d82012-04-23 23:55:44 +020010956 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010957 return 0;
10958
Willy Tarreaue333ec92012-04-16 16:26:40 +020010959 CHECK_HTTP_MESSAGE_FIRST();
10960
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010961 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010962 msg = &txn->req;
10963 hdr_name = "Cookie";
10964 hdr_name_len = 6;
10965 } else {
10966 msg = &txn->rsp;
10967 hdr_name = "Set-Cookie";
10968 hdr_name_len = 10;
10969 }
10970
Willy Tarreau9b28e032012-10-12 23:49:43 +020010971 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010972 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010973 ctx.idx = 0;
10974 cnt = 0;
10975
10976 while (1) {
10977 /* Note: val_beg == NULL every time we need to fetch a new header */
10978 if (!val_beg) {
10979 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10980 break;
10981
Willy Tarreau24e32d82012-04-23 23:55:44 +020010982 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010983 continue;
10984
10985 val_beg = ctx.line + ctx.val;
10986 val_end = val_beg + ctx.vlen;
10987 }
10988
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010989 smp->type = SMP_T_STR;
10990 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010991 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010992 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010993 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010994 &smp->data.str.str,
10995 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010996 cnt++;
10997 }
10998 }
10999
Willy Tarreaub169eba2013-12-16 15:14:43 +010011000 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020011001 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020011002 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020011003 return 1;
11004}
11005
Willy Tarreau51539362012-05-08 12:46:28 +020011006/* Fetch an cookie's integer value. The integer value is returned. It
11007 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
11008 */
11009static int
11010smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011011 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020011012{
Willy Tarreauef38c392013-07-22 16:29:32 +020011013 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020011014
11015 if (ret > 0) {
11016 smp->type = SMP_T_UINT;
11017 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11018 }
11019
11020 return ret;
11021}
11022
Willy Tarreau8797c062007-05-07 00:55:35 +020011023/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020011024/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020011025/************************************************************************/
11026
David Cournapeau16023ee2010-12-23 20:55:41 +090011027/*
11028 * Given a path string and its length, find the position of beginning of the
11029 * query string. Returns NULL if no query string is found in the path.
11030 *
11031 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
11032 *
11033 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
11034 */
bedis4c75cca2012-10-05 08:38:24 +020011035static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011036{
11037 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020011038
bedis4c75cca2012-10-05 08:38:24 +020011039 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090011040 return p ? p + 1 : NULL;
11041}
11042
bedis4c75cca2012-10-05 08:38:24 +020011043static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011044{
bedis4c75cca2012-10-05 08:38:24 +020011045 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090011046}
11047
11048/*
11049 * Given a url parameter, find the starting position of the first occurence,
11050 * or NULL if the parameter is not found.
11051 *
11052 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
11053 * the function will return query_string+8.
11054 */
11055static char*
11056find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020011057 char* url_param_name, size_t url_param_name_l,
11058 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011059{
11060 char *pos, *last;
11061
11062 pos = query_string;
11063 last = query_string + query_string_l - url_param_name_l - 1;
11064
11065 while (pos <= last) {
11066 if (pos[url_param_name_l] == '=') {
11067 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
11068 return pos;
11069 pos += url_param_name_l + 1;
11070 }
bedis4c75cca2012-10-05 08:38:24 +020011071 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011072 pos++;
11073 pos++;
11074 }
11075 return NULL;
11076}
11077
11078/*
Cyril Bonté0f836e12015-11-26 21:39:56 +010011079 * Given a url parameter name and a query string, find the next value.
11080 * An empty url_param_name matches the first available parameter.
11081 * If the parameter is found, 1 is returned and *value / *value_l are updated
11082 * to respectively provide a pointer to the value and its length.
11083 * Otherwise, 0 is returned and value/value_l are not modified.
David Cournapeau16023ee2010-12-23 20:55:41 +090011084 */
11085static int
11086find_url_param_value(char* path, size_t path_l,
11087 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020011088 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090011089{
11090 char *query_string, *qs_end;
11091 char *arg_start;
11092 char *value_start, *value_end;
11093
bedis4c75cca2012-10-05 08:38:24 +020011094 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011095 if (!query_string)
11096 return 0;
11097
11098 qs_end = path + path_l;
11099 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020011100 url_param_name, url_param_name_l,
11101 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090011102 if (!arg_start)
11103 return 0;
11104
11105 value_start = arg_start + url_param_name_l + 1;
11106 value_end = value_start;
11107
bedis4c75cca2012-10-05 08:38:24 +020011108 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011109 value_end++;
11110
11111 *value = value_start;
11112 *value_l = value_end - value_start;
Cyril Bonté0f836e12015-11-26 21:39:56 +010011113 return 1;
David Cournapeau16023ee2010-12-23 20:55:41 +090011114}
11115
11116static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011117smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011118 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090011119{
bedis4c75cca2012-10-05 08:38:24 +020011120 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090011121 struct http_txn *txn = l7;
11122 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011123
bedis4c75cca2012-10-05 08:38:24 +020011124 if (!args || args[0].type != ARGT_STR ||
11125 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011126 return 0;
11127
11128 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090011129
bedis4c75cca2012-10-05 08:38:24 +020011130 if (args[1].type)
11131 delim = *args[1].data.str.str;
11132
Willy Tarreau9b28e032012-10-12 23:49:43 +020011133 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020011134 args->data.str.str, args->data.str.len,
11135 &smp->data.str.str, &smp->data.str.len,
11136 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090011137 return 0;
11138
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011139 smp->type = SMP_T_STR;
11140 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090011141 return 1;
11142}
11143
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011144/* Return the signed integer value for the specified url parameter (see url_param
11145 * above).
11146 */
11147static int
11148smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020011149 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011150{
Willy Tarreauef38c392013-07-22 16:29:32 +020011151 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020011152
11153 if (ret > 0) {
11154 smp->type = SMP_T_UINT;
11155 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
11156 }
11157
11158 return ret;
11159}
11160
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011161/* This produces a 32-bit hash of the concatenation of the first occurrence of
11162 * the Host header followed by the path component if it begins with a slash ('/').
11163 * This means that '*' will not be added, resulting in exactly the first Host
11164 * entry. If no Host header is found, then the path is used. The resulting value
11165 * is hashed using the url hash followed by a full avalanche hash and provides a
11166 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
11167 * high-traffic sites without having to store whole paths.
11168 * this differs from the base32 functions in that it includes the url parameters
11169 * as well as the path
11170 */
11171static int
11172smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011173 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011174{
11175 struct http_txn *txn = l7;
11176 struct hdr_ctx ctx;
11177 unsigned int hash = 0;
11178 char *ptr, *beg, *end;
11179 int len;
11180
11181 CHECK_HTTP_MESSAGE_FIRST();
11182
11183 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020011184 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011185 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
11186 ptr = ctx.line + ctx.val;
11187 len = ctx.vlen;
11188 while (len--)
11189 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
11190 }
11191
11192 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020011193 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 +000011194 beg = http_get_path(txn);
11195 if (!beg)
11196 beg = end;
11197
11198 for (ptr = beg; ptr < end ; ptr++);
11199
11200 if (beg < ptr && *beg == '/') {
11201 while (beg < ptr)
11202 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
11203 }
11204 hash = full_hash(hash);
11205
11206 smp->type = SMP_T_UINT;
11207 smp->data.uint = hash;
11208 smp->flags = SMP_F_VOL_1ST;
11209 return 1;
11210}
11211
11212/* This concatenates the source address with the 32-bit hash of the Host and
11213 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
11214 * per-url counters. The result is a binary block from 8 to 20 bytes depending
11215 * on the source address length. The URL hash is stored before the address so
11216 * that in environments where IPv6 is insignificant, truncating the output to
11217 * 8 bytes would still work.
11218 */
11219static int
11220smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010011221 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011222{
11223 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011224 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011225
Willy Tarreaue155ec22013-11-18 18:33:22 +010011226 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011227 return 0;
11228
11229 temp = get_trash_chunk();
Dragan Dosen9410d752016-06-16 11:08:08 +020011230 *(unsigned int *)temp->str = htonl(smp->data.sint);
11231 temp->len += sizeof(unsigned int);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011232
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011233 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011234 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011235 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011236 temp->len += 4;
11237 break;
11238 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020011239 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011240 temp->len += 16;
11241 break;
11242 default:
11243 return 0;
11244 }
11245
11246 smp->data.str = *temp;
11247 smp->type = SMP_T_BIN;
11248 return 1;
11249}
11250
Willy Tarreau185b5c42012-04-26 15:11:51 +020011251/* This function is used to validate the arguments passed to any "hdr" fetch
11252 * keyword. These keywords support an optional positive or negative occurrence
11253 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
11254 * is assumed that the types are already the correct ones. Returns 0 on error,
11255 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
11256 * error message in case of error, that the caller is responsible for freeing.
11257 * The initial location must either be freeable or NULL.
11258 */
11259static int val_hdr(struct arg *arg, char **err_msg)
11260{
11261 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020011262 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020011263 return 0;
11264 }
11265 return 1;
11266}
11267
Willy Tarreau276fae92013-07-25 14:36:01 +020011268/* takes an UINT value on input supposed to represent the time since EPOCH,
11269 * adds an optional offset found in args[0] and emits a string representing
11270 * the date in RFC-1123/5322 format.
11271 */
11272static int sample_conv_http_date(const struct arg *args, struct sample *smp)
11273{
Cyril Bontéf980b362016-01-22 19:40:28 +010011274 const char day[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
Willy Tarreau276fae92013-07-25 14:36:01 +020011275 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
11276 struct chunk *temp;
11277 struct tm *tm;
11278 time_t curr_date = smp->data.uint;
11279
11280 /* add offset */
11281 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
11282 curr_date += args[0].data.sint;
11283
11284 tm = gmtime(&curr_date);
Thierry FOURNIER95558722015-07-08 00:15:20 +020011285 if (!tm)
11286 return 0;
Willy Tarreau276fae92013-07-25 14:36:01 +020011287
11288 temp = get_trash_chunk();
11289 temp->len = snprintf(temp->str, temp->size - temp->len,
11290 "%s, %02d %s %04d %02d:%02d:%02d GMT",
11291 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
11292 tm->tm_hour, tm->tm_min, tm->tm_sec);
11293
11294 smp->data.str = *temp;
11295 smp->type = SMP_T_STR;
11296 return 1;
11297}
11298
Thierry FOURNIERad903512014-04-11 17:51:01 +020011299/* Match language range with language tag. RFC2616 14.4:
11300 *
11301 * A language-range matches a language-tag if it exactly equals
11302 * the tag, or if it exactly equals a prefix of the tag such
11303 * that the first tag character following the prefix is "-".
11304 *
11305 * Return 1 if the strings match, else return 0.
11306 */
11307static inline int language_range_match(const char *range, int range_len,
11308 const char *tag, int tag_len)
11309{
11310 const char *end = range + range_len;
11311 const char *tend = tag + tag_len;
11312 while (range < end) {
11313 if (*range == '-' && tag == tend)
11314 return 1;
11315 if (*range != *tag || tag == tend)
11316 return 0;
11317 range++;
11318 tag++;
11319 }
11320 /* Return true only if the last char of the tag is matched. */
11321 return tag == tend;
11322}
11323
11324/* Arguments: The list of expected value, the number of parts returned and the separator */
11325static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
11326{
11327 const char *al = smp->data.str.str;
11328 const char *end = al + smp->data.str.len;
11329 const char *token;
11330 int toklen;
11331 int qvalue;
11332 const char *str;
11333 const char *w;
11334 int best_q = 0;
11335
11336 /* Set the constant to the sample, because the output of the
11337 * function will be peek in the constant configuration string.
11338 */
11339 smp->flags |= SMP_F_CONST;
11340 smp->data.str.size = 0;
11341 smp->data.str.str = "";
11342 smp->data.str.len = 0;
11343
11344 /* Parse the accept language */
11345 while (1) {
11346
11347 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011348 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011349 al++;
11350 if (al >= end)
11351 break;
11352
11353 /* Start of the fisrt word. */
11354 token = al;
11355
11356 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011357 while (al < end && *al != ';' && *al != ',' && !isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011358 al++;
11359 if (al == token)
11360 goto expect_comma;
11361
11362 /* Length of the token. */
11363 toklen = al - token;
11364 qvalue = 1000;
11365
11366 /* Check if the token exists in the list. If the token not exists,
11367 * jump to the next token.
11368 */
11369 str = args[0].data.str.str;
11370 w = str;
11371 while (1) {
11372 if (*str == ';' || *str == '\0') {
11373 if (language_range_match(token, toklen, w, str-w))
11374 goto look_for_q;
11375 if (*str == '\0')
11376 goto expect_comma;
11377 w = str + 1;
11378 }
11379 str++;
11380 }
11381 goto expect_comma;
11382
11383look_for_q:
11384
11385 /* Jump spaces, quit if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011386 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011387 al++;
11388 if (al >= end)
11389 goto process_value;
11390
11391 /* If ',' is found, process the result */
11392 if (*al == ',')
11393 goto process_value;
11394
11395 /* If the character is different from ';', look
11396 * for the end of the header part in best effort.
11397 */
11398 if (*al != ';')
11399 goto expect_comma;
11400
11401 /* Assumes that the char is ';', now expect "q=". */
11402 al++;
11403
11404 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011405 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011406 al++;
11407 if (al >= end)
11408 goto process_value;
11409
11410 /* Expect 'q'. If no 'q', continue in best effort */
11411 if (*al != 'q')
11412 goto process_value;
11413 al++;
11414
11415 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011416 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011417 al++;
11418 if (al >= end)
11419 goto process_value;
11420
11421 /* Expect '='. If no '=', continue in best effort */
11422 if (*al != '=')
11423 goto process_value;
11424 al++;
11425
11426 /* Jump spaces, process value if the end is detected. */
Willy Tarreau463ae6f2014-07-08 00:59:48 +020011427 while (al < end && isspace((unsigned char)*al))
Thierry FOURNIERad903512014-04-11 17:51:01 +020011428 al++;
11429 if (al >= end)
11430 goto process_value;
11431
11432 /* Parse the q value. */
11433 qvalue = parse_qvalue(al, &al);
11434
11435process_value:
11436
11437 /* If the new q value is the best q value, then store the associated
11438 * language in the response. If qvalue is the biggest value (1000),
11439 * break the process.
11440 */
11441 if (qvalue > best_q) {
11442 smp->data.str.str = (char *)w;
11443 smp->data.str.len = str - w;
11444 if (qvalue >= 1000)
11445 break;
11446 best_q = qvalue;
11447 }
11448
11449expect_comma:
11450
11451 /* Expect comma or end. If the end is detected, quit the loop. */
11452 while (al < end && *al != ',')
11453 al++;
11454 if (al >= end)
11455 break;
11456
11457 /* Comma is found, jump it and restart the analyzer. */
11458 al++;
11459 }
11460
11461 /* Set default value if required. */
11462 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11463 smp->data.str.str = args[1].data.str.str;
11464 smp->data.str.len = args[1].data.str.len;
11465 }
11466
11467 /* Return true only if a matching language was found. */
11468 return smp->data.str.len != 0;
11469}
11470
William Lallemand73025dd2014-04-24 14:38:37 +020011471/*
11472 * Return the struct http_req_action_kw associated to a keyword.
11473 */
11474struct http_req_action_kw *action_http_req_custom(const char *kw)
11475{
11476 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11477 struct http_req_action_kw_list *kw_list;
11478 int i;
11479
11480 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11481 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11482 if (!strcmp(kw, kw_list->kw[i].kw))
11483 return &kw_list->kw[i];
11484 }
11485 }
11486 }
11487 return NULL;
11488}
11489
11490/*
11491 * Return the struct http_res_action_kw associated to a keyword.
11492 */
11493struct http_res_action_kw *action_http_res_custom(const char *kw)
11494{
11495 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11496 struct http_res_action_kw_list *kw_list;
11497 int i;
11498
11499 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11500 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11501 if (!strcmp(kw, kw_list->kw[i].kw))
11502 return &kw_list->kw[i];
11503 }
11504 }
11505 }
11506 return NULL;
11507}
11508
Willy Tarreau4a568972010-05-12 08:08:50 +020011509/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011510/* All supported ACL keywords must be declared here. */
11511/************************************************************************/
11512
11513/* Note: must not be declared <const> as its list will be overwritten.
11514 * Please take care of keeping this list alphabetically sorted.
11515 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011516static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011517 { "base", "base", PAT_MATCH_STR },
11518 { "base_beg", "base", PAT_MATCH_BEG },
11519 { "base_dir", "base", PAT_MATCH_DIR },
11520 { "base_dom", "base", PAT_MATCH_DOM },
11521 { "base_end", "base", PAT_MATCH_END },
11522 { "base_len", "base", PAT_MATCH_LEN },
11523 { "base_reg", "base", PAT_MATCH_REG },
11524 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011525
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011526 { "cook", "req.cook", PAT_MATCH_STR },
11527 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11528 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11529 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11530 { "cook_end", "req.cook", PAT_MATCH_END },
11531 { "cook_len", "req.cook", PAT_MATCH_LEN },
11532 { "cook_reg", "req.cook", PAT_MATCH_REG },
11533 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011534
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011535 { "hdr", "req.hdr", PAT_MATCH_STR },
11536 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11537 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11538 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11539 { "hdr_end", "req.hdr", PAT_MATCH_END },
11540 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11541 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11542 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011543
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011544 /* these two declarations uses strings with list storage (in place
11545 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11546 * and delete functions are relative to the list management. The parse
11547 * and match method are related to the corresponding fetch methods. This
11548 * is very particular ACL declaration mode.
11549 */
11550 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11551 { "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 +020011552
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011553 { "path", "path", PAT_MATCH_STR },
11554 { "path_beg", "path", PAT_MATCH_BEG },
11555 { "path_dir", "path", PAT_MATCH_DIR },
11556 { "path_dom", "path", PAT_MATCH_DOM },
11557 { "path_end", "path", PAT_MATCH_END },
11558 { "path_len", "path", PAT_MATCH_LEN },
11559 { "path_reg", "path", PAT_MATCH_REG },
11560 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011561
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011562 { "req_ver", "req.ver", PAT_MATCH_STR },
11563 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011564
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011565 { "scook", "res.cook", PAT_MATCH_STR },
11566 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11567 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11568 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11569 { "scook_end", "res.cook", PAT_MATCH_END },
11570 { "scook_len", "res.cook", PAT_MATCH_LEN },
11571 { "scook_reg", "res.cook", PAT_MATCH_REG },
11572 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011573
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011574 { "shdr", "res.hdr", PAT_MATCH_STR },
11575 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11576 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11577 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11578 { "shdr_end", "res.hdr", PAT_MATCH_END },
11579 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11580 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11581 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011582
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011583 { "url", "url", PAT_MATCH_STR },
11584 { "url_beg", "url", PAT_MATCH_BEG },
11585 { "url_dir", "url", PAT_MATCH_DIR },
11586 { "url_dom", "url", PAT_MATCH_DOM },
11587 { "url_end", "url", PAT_MATCH_END },
11588 { "url_len", "url", PAT_MATCH_LEN },
11589 { "url_reg", "url", PAT_MATCH_REG },
11590 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011591
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011592 { "urlp", "urlp", PAT_MATCH_STR },
11593 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11594 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11595 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11596 { "urlp_end", "urlp", PAT_MATCH_END },
11597 { "urlp_len", "urlp", PAT_MATCH_LEN },
11598 { "urlp_reg", "urlp", PAT_MATCH_REG },
11599 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011600
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011601 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011602}};
11603
11604/************************************************************************/
11605/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011606/************************************************************************/
11607/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011608static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011609 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011610 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11611 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11612
William Lallemanda43ba4e2014-01-28 18:14:25 +010011613 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011614 { "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 +020011615
11616 /* retrieve these captures from the HTTP logs */
11617 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11618 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11619 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11620
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011621 { "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 +020011622 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011623
Willy Tarreau409bcde2013-01-08 00:31:00 +010011624 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11625 * are only here to match the ACL's name, are request-only and are used
11626 * for ACL compatibility only.
11627 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011628 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11629 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011630 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11631 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11632
11633 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11634 * only here to match the ACL's name, are request-only and are used for
11635 * ACL compatibility only.
11636 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011637 { "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 +010011638 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11639 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11640 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11641
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011642 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011643 { "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 +010011644 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011645 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011646 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011647
11648 /* HTTP protocol on the request path */
11649 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011650 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011651
11652 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011653 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11654 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011655
11656 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011657 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11658 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011659
Willy Tarreau18ed2562013-01-14 15:56:36 +010011660 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011661 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011662 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11663 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11664
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011665 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011666 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011667 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011668 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11669 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11670 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11671
11672 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011673 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011674 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11675 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11676
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011677 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011678 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011679 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011680 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11681 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11682 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11683
Willy Tarreau409bcde2013-01-08 00:31:00 +010011684 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011685 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011686 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11687 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011688 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011689
11690 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011691 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011692 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11693 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11694 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11695
11696 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011697 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011698 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11699 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011700 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11701 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011702 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11703 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011704 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11705 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011706}};
11707
Willy Tarreau8797c062007-05-07 00:55:35 +020011708
Willy Tarreau276fae92013-07-25 14:36:01 +020011709/* Note: must not be declared <const> as its list will be overwritten */
11710static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011711 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11712 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011713 { NULL, NULL, 0, 0, 0 },
11714}};
11715
Willy Tarreau8797c062007-05-07 00:55:35 +020011716__attribute__((constructor))
11717static void __http_protocol_init(void)
11718{
11719 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011720 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011721 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020011722}
11723
11724
Willy Tarreau58f10d72006-12-04 02:26:12 +010011725/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011726 * Local variables:
11727 * c-indent-level: 8
11728 * c-basic-offset: 8
11729 * End:
11730 */