blob: 84274c83c133d50d7cfc220df0db723935b33dc7 [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"
127 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
128
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 Tarreau80587432006-12-24 17:47:20 +0100255void init_proto_http()
256{
Willy Tarreau42250582007-04-01 01:30:43 +0200257 int i;
258 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100259 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200260
Willy Tarreau80587432006-12-24 17:47:20 +0100261 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
262 if (!http_err_msgs[msg]) {
263 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
264 abort();
265 }
266
267 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
268 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
269 }
Willy Tarreau42250582007-04-01 01:30:43 +0200270
271 /* initialize the log header encoding map : '{|}"#' should be encoded with
272 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
273 * URL encoding only requires '"', '#' to be encoded as well as non-
274 * printable characters above.
275 */
276 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
277 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100278 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200279 for (i = 0; i < 32; i++) {
280 FD_SET(i, hdr_encode_map);
281 FD_SET(i, url_encode_map);
282 }
283 for (i = 127; i < 256; i++) {
284 FD_SET(i, hdr_encode_map);
285 FD_SET(i, url_encode_map);
286 }
287
288 tmp = "\"#{|}";
289 while (*tmp) {
290 FD_SET(*tmp, hdr_encode_map);
291 tmp++;
292 }
293
294 tmp = "\"#";
295 while (*tmp) {
296 FD_SET(*tmp, url_encode_map);
297 tmp++;
298 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200299
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100300 /* initialize the http header encoding map. The draft httpbis define the
301 * header content as:
302 *
303 * HTTP-message = start-line
304 * *( header-field CRLF )
305 * CRLF
306 * [ message-body ]
307 * header-field = field-name ":" OWS field-value OWS
308 * field-value = *( field-content / obs-fold )
309 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
310 * obs-fold = CRLF 1*( SP / HTAB )
311 * field-vchar = VCHAR / obs-text
312 * VCHAR = %x21-7E
313 * obs-text = %x80-FF
314 *
315 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
316 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
317 * "obs-fold" is volontary forgotten because haproxy remove this.
318 */
319 memset(http_encode_map, 0, sizeof(http_encode_map));
320 for (i = 0x00; i <= 0x08; i++)
321 FD_SET(i, http_encode_map);
322 for (i = 0x0a; i <= 0x1f; i++)
323 FD_SET(i, http_encode_map);
324 FD_SET(0x7f, http_encode_map);
325
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200326 /* memory allocations */
327 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100328 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100329}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200330
Willy Tarreau53b6c742006-12-17 13:37:46 +0100331/*
332 * We have 26 list of methods (1 per first letter), each of which can have
333 * up to 3 entries (2 valid, 1 null).
334 */
335struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100336 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100337 int len;
338 const char text[8];
339};
340
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100341const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100342 ['C' - 'A'] = {
343 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
344 },
345 ['D' - 'A'] = {
346 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
347 },
348 ['G' - 'A'] = {
349 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
350 },
351 ['H' - 'A'] = {
352 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
353 },
354 ['P' - 'A'] = {
355 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
356 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
357 },
358 ['T' - 'A'] = {
359 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
360 },
361 /* rest is empty like this :
362 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
363 */
364};
365
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100366const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
367 [HTTP_METH_NONE] = { "", 0 },
368 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
369 [HTTP_METH_GET] = { "GET", 3 },
370 [HTTP_METH_HEAD] = { "HEAD", 4 },
371 [HTTP_METH_POST] = { "POST", 4 },
372 [HTTP_METH_PUT] = { "PUT", 3 },
373 [HTTP_METH_DELETE] = { "DELETE", 6 },
374 [HTTP_METH_TRACE] = { "TRACE", 5 },
375 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
376};
377
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100378/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200379 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100380 * RFC2616 for those chars.
381 */
382
383const char http_is_spht[256] = {
384 [' '] = 1, ['\t'] = 1,
385};
386
387const char http_is_crlf[256] = {
388 ['\r'] = 1, ['\n'] = 1,
389};
390
391const char http_is_lws[256] = {
392 [' '] = 1, ['\t'] = 1,
393 ['\r'] = 1, ['\n'] = 1,
394};
395
396const char http_is_sep[256] = {
397 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
398 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
399 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
400 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
401 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
402};
403
404const char http_is_ctl[256] = {
405 [0 ... 31] = 1,
406 [127] = 1,
407};
408
409/*
410 * A token is any ASCII char that is neither a separator nor a CTL char.
411 * Do not overwrite values in assignment since gcc-2.95 will not handle
412 * them correctly. Instead, define every non-CTL char's status.
413 */
414const char http_is_token[256] = {
415 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
416 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
417 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
418 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
419 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
420 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
421 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
422 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
423 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
424 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
425 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
426 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
427 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
428 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
429 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
430 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
431 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
432 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
433 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
434 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
435 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
436 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
437 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
438 ['|'] = 1, ['}'] = 0, ['~'] = 1,
439};
440
441
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100442/*
443 * An http ver_token is any ASCII which can be found in an HTTP version,
444 * which includes 'H', 'T', 'P', '/', '.' and any digit.
445 */
446const char http_is_ver_token[256] = {
447 ['.'] = 1, ['/'] = 1,
448 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
449 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
450 ['H'] = 1, ['P'] = 1, ['T'] = 1,
451};
452
453
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100454/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100455 * Adds a header and its CRLF at the tail of the message's buffer, just before
456 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100457 * The header is also automatically added to the index <hdr_idx>, and the end
458 * of headers is automatically adjusted. The number of bytes added is returned
459 * on success, otherwise <0 is returned indicating an error.
460 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100461int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100462{
463 int bytes, len;
464
465 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200466 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100467 if (!bytes)
468 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100469 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100470 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
471}
472
473/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100474 * Adds a header and its CRLF at the tail of the message's buffer, just before
475 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100476 * the buffer is only opened and the space reserved, but nothing is copied.
477 * The header is also automatically added to the index <hdr_idx>, and the end
478 * of headers is automatically adjusted. The number of bytes added is returned
479 * on success, otherwise <0 is returned indicating an error.
480 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100481int http_header_add_tail2(struct http_msg *msg,
482 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100483{
484 int bytes;
485
Willy Tarreau9b28e032012-10-12 23:49:43 +0200486 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100487 if (!bytes)
488 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100489 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100490 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
491}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200492
493/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100494 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
495 * If so, returns the position of the first non-space character relative to
496 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
497 * to return a pointer to the place after the first space. Returns 0 if the
498 * header name does not match. Checks are case-insensitive.
499 */
500int http_header_match2(const char *hdr, const char *end,
501 const char *name, int len)
502{
503 const char *val;
504
505 if (hdr + len >= end)
506 return 0;
507 if (hdr[len] != ':')
508 return 0;
509 if (strncasecmp(hdr, name, len) != 0)
510 return 0;
511 val = hdr + len + 1;
512 while (val < end && HTTP_IS_SPHT(*val))
513 val++;
514 if ((val >= end) && (len + 2 <= end - hdr))
515 return len + 2; /* we may replace starting from second space */
516 return val - hdr;
517}
518
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200519/* Find the first or next occurrence of header <name> in message buffer <sol>
520 * using headers index <idx>, and return it in the <ctx> structure. This
521 * structure holds everything necessary to use the header and find next
522 * occurrence. If its <idx> member is 0, the header is searched from the
523 * beginning. Otherwise, the next occurrence is returned. The function returns
524 * 1 when it finds a value, and 0 when there is no more. It is very similar to
525 * http_find_header2() except that it is designed to work with full-line headers
526 * whose comma is not a delimiter but is part of the syntax. As a special case,
527 * if ctx->val is NULL when searching for a new values of a header, the current
528 * header is rescanned. This allows rescanning after a header deletion.
529 */
530int http_find_full_header2(const char *name, int len,
531 char *sol, struct hdr_idx *idx,
532 struct hdr_ctx *ctx)
533{
534 char *eol, *sov;
535 int cur_idx, old_idx;
536
537 cur_idx = ctx->idx;
538 if (cur_idx) {
539 /* We have previously returned a header, let's search another one */
540 sol = ctx->line;
541 eol = sol + idx->v[cur_idx].len;
542 goto next_hdr;
543 }
544
545 /* first request for this header */
546 sol += hdr_idx_first_pos(idx);
547 old_idx = 0;
548 cur_idx = hdr_idx_first_idx(idx);
549 while (cur_idx) {
550 eol = sol + idx->v[cur_idx].len;
551
552 if (len == 0) {
553 /* No argument was passed, we want any header.
554 * To achieve this, we simply build a fake request. */
555 while (sol + len < eol && sol[len] != ':')
556 len++;
557 name = sol;
558 }
559
560 if ((len < eol - sol) &&
561 (sol[len] == ':') &&
562 (strncasecmp(sol, name, len) == 0)) {
563 ctx->del = len;
564 sov = sol + len + 1;
565 while (sov < eol && http_is_lws[(unsigned char)*sov])
566 sov++;
567
568 ctx->line = sol;
569 ctx->prev = old_idx;
570 ctx->idx = cur_idx;
571 ctx->val = sov - sol;
572 ctx->tws = 0;
573 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
574 eol--;
575 ctx->tws++;
576 }
577 ctx->vlen = eol - sov;
578 return 1;
579 }
580 next_hdr:
581 sol = eol + idx->v[cur_idx].cr + 1;
582 old_idx = cur_idx;
583 cur_idx = idx->v[cur_idx].next;
584 }
585 return 0;
586}
587
Willy Tarreau68085d82010-01-18 14:54:04 +0100588/* Find the end of the header value contained between <s> and <e>. See RFC2616,
589 * par 2.2 for more information. Note that it requires a valid header to return
590 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200591 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100592char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200593{
594 int quoted, qdpair;
595
596 quoted = qdpair = 0;
597 for (; s < e; s++) {
598 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200599 else if (quoted) {
600 if (*s == '\\') qdpair = 1;
601 else if (*s == '"') quoted = 0;
602 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200603 else if (*s == '"') quoted = 1;
604 else if (*s == ',') return s;
605 }
606 return s;
607}
608
609/* Find the first or next occurrence of header <name> in message buffer <sol>
610 * using headers index <idx>, and return it in the <ctx> structure. This
611 * structure holds everything necessary to use the header and find next
612 * occurrence. If its <idx> member is 0, the header is searched from the
613 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100614 * 1 when it finds a value, and 0 when there is no more. It is designed to work
615 * with headers defined as comma-separated lists. As a special case, if ctx->val
616 * is NULL when searching for a new values of a header, the current header is
617 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200618 */
619int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100620 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200621 struct hdr_ctx *ctx)
622{
Willy Tarreau68085d82010-01-18 14:54:04 +0100623 char *eol, *sov;
624 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200625
Willy Tarreau68085d82010-01-18 14:54:04 +0100626 cur_idx = ctx->idx;
627 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200628 /* We have previously returned a value, let's search
629 * another one on the same line.
630 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200631 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200632 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100633 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200634 eol = sol + idx->v[cur_idx].len;
635
636 if (sov >= eol)
637 /* no more values in this header */
638 goto next_hdr;
639
Willy Tarreau68085d82010-01-18 14:54:04 +0100640 /* values remaining for this header, skip the comma but save it
641 * for later use (eg: for header deletion).
642 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200643 sov++;
644 while (sov < eol && http_is_lws[(unsigned char)*sov])
645 sov++;
646
647 goto return_hdr;
648 }
649
650 /* first request for this header */
651 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100652 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200653 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200654 while (cur_idx) {
655 eol = sol + idx->v[cur_idx].len;
656
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200657 if (len == 0) {
658 /* No argument was passed, we want any header.
659 * To achieve this, we simply build a fake request. */
660 while (sol + len < eol && sol[len] != ':')
661 len++;
662 name = sol;
663 }
664
Willy Tarreau33a7e692007-06-10 19:45:56 +0200665 if ((len < eol - sol) &&
666 (sol[len] == ':') &&
667 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100668 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200669 sov = sol + len + 1;
670 while (sov < eol && http_is_lws[(unsigned char)*sov])
671 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100672
Willy Tarreau33a7e692007-06-10 19:45:56 +0200673 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100674 ctx->prev = old_idx;
675 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200676 ctx->idx = cur_idx;
677 ctx->val = sov - sol;
678
679 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200680 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200681 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200682 eol--;
683 ctx->tws++;
684 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200685 ctx->vlen = eol - sov;
686 return 1;
687 }
688 next_hdr:
689 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100690 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200691 cur_idx = idx->v[cur_idx].next;
692 }
693 return 0;
694}
695
696int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100697 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200698 struct hdr_ctx *ctx)
699{
700 return http_find_header2(name, strlen(name), sol, idx, ctx);
701}
702
Willy Tarreau68085d82010-01-18 14:54:04 +0100703/* Remove one value of a header. This only works on a <ctx> returned by one of
704 * the http_find_header functions. The value is removed, as well as surrounding
705 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100706 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100707 * message <msg>. The new index is returned. If it is zero, it means there is
708 * no more header, so any processing may stop. The ctx is always left in a form
709 * that can be handled by http_find_header2() to find next occurrence.
710 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100711int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100712{
713 int cur_idx = ctx->idx;
714 char *sol = ctx->line;
715 struct hdr_idx_elem *hdr;
716 int delta, skip_comma;
717
718 if (!cur_idx)
719 return 0;
720
721 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200722 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100723 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200724 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100725 http_msg_move_end(msg, delta);
726 idx->used--;
727 hdr->len = 0; /* unused entry */
728 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100729 if (idx->tail == ctx->idx)
730 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100731 ctx->idx = ctx->prev; /* walk back to the end of previous header */
732 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
733 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200734 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100735 return ctx->idx;
736 }
737
738 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200739 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
740 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100741 */
742
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200743 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200744 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200745 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100746 NULL, 0);
747 hdr->len += delta;
748 http_msg_move_end(msg, delta);
749 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200750 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100751 return ctx->idx;
752}
753
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100754/* This function handles a server error at the stream interface level. The
755 * stream interface is assumed to be already in a closed state. An optional
756 * message is copied into the input buffer, and an HTTP status code stored.
757 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100758 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200759 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200760static void http_server_error(struct session *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100761 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200762{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200763 channel_auto_read(si->ob);
764 channel_abort(si->ob);
765 channel_auto_close(si->ob);
766 channel_erase(si->ob);
767 channel_auto_close(si->ib);
768 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100769 if (status > 0 && msg) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200770 s->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200771 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200772 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200773 if (!(s->flags & SN_ERR_MASK))
774 s->flags |= err;
775 if (!(s->flags & SN_FINST_MASK))
776 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200777}
778
Willy Tarreau80587432006-12-24 17:47:20 +0100779/* This function returns the appropriate error location for the given session
780 * and message.
781 */
782
Willy Tarreau783f2582012-09-04 12:19:04 +0200783struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100784{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200785 if (s->be->errmsg[msgnum].str)
786 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100787 else if (s->fe->errmsg[msgnum].str)
788 return &s->fe->errmsg[msgnum];
789 else
790 return &http_err_chunks[msgnum];
791}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200792
Willy Tarreau53b6c742006-12-17 13:37:46 +0100793/*
794 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
795 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
796 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100797enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100798{
799 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100800 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100801
802 m = ((unsigned)*str - 'A');
803
804 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100805 for (h = http_methods[m]; h->len > 0; h++) {
806 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100807 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100808 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100809 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100810 };
811 return HTTP_METH_OTHER;
812 }
813 return HTTP_METH_NONE;
814
815}
816
Willy Tarreau21d2af32008-02-14 20:25:24 +0100817/* Parse the URI from the given transaction (which is assumed to be in request
818 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
819 * It is returned otherwise.
820 */
821static char *
822http_get_path(struct http_txn *txn)
823{
824 char *ptr, *end;
825
Willy Tarreau9b28e032012-10-12 23:49:43 +0200826 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100827 end = ptr + txn->req.sl.rq.u_l;
828
829 if (ptr >= end)
830 return NULL;
831
832 /* RFC2616, par. 5.1.2 :
833 * Request-URI = "*" | absuri | abspath | authority
834 */
835
836 if (*ptr == '*')
837 return NULL;
838
839 if (isalpha((unsigned char)*ptr)) {
840 /* this is a scheme as described by RFC3986, par. 3.1 */
841 ptr++;
842 while (ptr < end &&
843 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
844 ptr++;
845 /* skip '://' */
846 if (ptr == end || *ptr++ != ':')
847 return NULL;
848 if (ptr == end || *ptr++ != '/')
849 return NULL;
850 if (ptr == end || *ptr++ != '/')
851 return NULL;
852 }
853 /* skip [user[:passwd]@]host[:[port]] */
854
855 while (ptr < end && *ptr != '/')
856 ptr++;
857
858 if (ptr == end)
859 return NULL;
860
861 /* OK, we got the '/' ! */
862 return ptr;
863}
864
William Lallemand65ad6e12014-01-31 15:08:02 +0100865/* Parse the URI from the given string and look for the "/" beginning the PATH.
866 * If not found, return NULL. It is returned otherwise.
867 */
868static char *
869http_get_path_from_string(char *str)
870{
871 char *ptr = str;
872
873 /* RFC2616, par. 5.1.2 :
874 * Request-URI = "*" | absuri | abspath | authority
875 */
876
877 if (*ptr == '*')
878 return NULL;
879
880 if (isalpha((unsigned char)*ptr)) {
881 /* this is a scheme as described by RFC3986, par. 3.1 */
882 ptr++;
883 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
884 ptr++;
885 /* skip '://' */
886 if (*ptr == '\0' || *ptr++ != ':')
887 return NULL;
888 if (*ptr == '\0' || *ptr++ != '/')
889 return NULL;
890 if (*ptr == '\0' || *ptr++ != '/')
891 return NULL;
892 }
893 /* skip [user[:passwd]@]host[:[port]] */
894
895 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
896 ptr++;
897
898 if (*ptr == '\0' || *ptr == ' ')
899 return NULL;
900
901 /* OK, we got the '/' ! */
902 return ptr;
903}
904
Willy Tarreau71241ab2012-12-27 11:30:54 +0100905/* Returns a 302 for a redirectable request that reaches a server working in
906 * in redirect mode. This may only be called just after the stream interface
907 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
908 * follow normal proxy processing. NOTE: this function is designed to support
909 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100910 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100911void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100912{
913 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100914 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100915 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200916 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100917
918 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100919 trash.len = strlen(HTTP_302);
920 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100921
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100922 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100923
Willy Tarreauefb453c2008-10-26 20:49:47 +0100924 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100925 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100926 return;
927
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100928 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100929 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100930 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
931 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100932 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100933
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200934 /* 3: add the request URI. Since it was already forwarded, we need
935 * to temporarily rewind the buffer.
936 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100937 txn = &s->txn;
Willy Tarreau211cdec2014-04-17 20:18:08 +0200938 b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200939
Willy Tarreauefb453c2008-10-26 20:49:47 +0100940 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200941 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 +0200942
Willy Tarreau9b28e032012-10-12 23:49:43 +0200943 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200944
Willy Tarreauefb453c2008-10-26 20:49:47 +0100945 if (!path)
946 return;
947
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100948 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100949 return;
950
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100951 memcpy(trash.str + trash.len, path, len);
952 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100953
954 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100955 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
956 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100957 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100958 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
959 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100960 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100961
962 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200963 si_shutr(si);
964 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100965 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100966 si->state = SI_ST_CLO;
967
968 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200969 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100970
971 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100972 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500973 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100974}
975
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100976/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100977 * that the server side is closed. Note that err_type is actually a
978 * bitmask, where almost only aborts may be cumulated with other
979 * values. We consider that aborted operations are more important
980 * than timeouts or errors due to the fact that nobody else in the
981 * logs might explain incomplete retries. All others should avoid
982 * being cumulated. It should normally not be possible to have multiple
983 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100984 * Note that connection errors appearing on the second request of a keep-alive
985 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100986 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100987void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100988{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100989 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990
991 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100992 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200993 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100994 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100995 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100996 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
997 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100998 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100999 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001000 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001001 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001002 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +02001003 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001004 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001005 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001006 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1007 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001008 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001009 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +01001010 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001011 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001012 else if (err_type & SI_ET_CONN_RES)
1013 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001014 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1015 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001016 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001017 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001018 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001019}
1020
Willy Tarreau42250582007-04-01 01:30:43 +02001021extern const char sess_term_cond[8];
1022extern const char sess_fin_state[8];
1023extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001024struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001025struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001026struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001027
Willy Tarreau117f59e2007-03-04 18:17:17 +01001028/*
1029 * Capture headers from message starting at <som> according to header list
1030 * <cap_hdr>, and fill the <idx> structure appropriately.
1031 */
1032void capture_headers(char *som, struct hdr_idx *idx,
1033 char **cap, struct cap_hdr *cap_hdr)
1034{
1035 char *eol, *sol, *col, *sov;
1036 int cur_idx;
1037 struct cap_hdr *h;
1038 int len;
1039
1040 sol = som + hdr_idx_first_pos(idx);
1041 cur_idx = hdr_idx_first_idx(idx);
1042
1043 while (cur_idx) {
1044 eol = sol + idx->v[cur_idx].len;
1045
1046 col = sol;
1047 while (col < eol && *col != ':')
1048 col++;
1049
1050 sov = col + 1;
1051 while (sov < eol && http_is_lws[(unsigned char)*sov])
1052 sov++;
1053
1054 for (h = cap_hdr; h; h = h->next) {
1055 if ((h->namelen == col - sol) &&
1056 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1057 if (cap[h->index] == NULL)
1058 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001059 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001060
1061 if (cap[h->index] == NULL) {
1062 Alert("HTTP capture : out of memory.\n");
1063 continue;
1064 }
1065
1066 len = eol - sov;
1067 if (len > h->len)
1068 len = h->len;
1069
1070 memcpy(cap[h->index], sov, len);
1071 cap[h->index][len]=0;
1072 }
1073 }
1074 sol = eol + idx->v[cur_idx].cr + 1;
1075 cur_idx = idx->v[cur_idx].next;
1076 }
1077}
1078
1079
Willy Tarreau42250582007-04-01 01:30:43 +02001080/* either we find an LF at <ptr> or we jump to <bad>.
1081 */
1082#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1083
1084/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1085 * otherwise to <http_msg_ood> with <state> set to <st>.
1086 */
1087#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1088 ptr++; \
1089 if (likely(ptr < end)) \
1090 goto good; \
1091 else { \
1092 state = (st); \
1093 goto http_msg_ood; \
1094 } \
1095 } while (0)
1096
1097
Willy Tarreaubaaee002006-06-26 02:48:02 +02001098/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001099 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001100 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1101 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1102 * will give undefined results.
1103 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1104 * and that msg->sol points to the beginning of the response.
1105 * If a complete line is found (which implies that at least one CR or LF is
1106 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1107 * returned indicating an incomplete line (which does not mean that parts have
1108 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1109 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1110 * upon next call.
1111 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001112 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001113 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1114 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001115 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001116 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001117const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001118 enum ht_state state, const char *ptr, const char *end,
1119 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001120{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001121 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001122
Willy Tarreau8973c702007-01-21 23:58:29 +01001123 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001124 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001125 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001126 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001127 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1128
1129 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001130 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001131 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1132 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001133 state = HTTP_MSG_ERROR;
1134 break;
1135
Willy Tarreau8973c702007-01-21 23:58:29 +01001136 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001137 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001138 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001139 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001140 goto http_msg_rpcode;
1141 }
1142 if (likely(HTTP_IS_SPHT(*ptr)))
1143 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1144 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001145 state = HTTP_MSG_ERROR;
1146 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001147
Willy Tarreau8973c702007-01-21 23:58:29 +01001148 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001149 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001150 if (likely(!HTTP_IS_LWS(*ptr)))
1151 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1152
1153 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001154 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001155 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1156 }
1157
1158 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001159 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001160 http_msg_rsp_reason:
1161 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001162 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001163 msg->sl.st.r_l = 0;
1164 goto http_msg_rpline_eol;
1165
Willy Tarreau8973c702007-01-21 23:58:29 +01001166 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001167 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001169 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001170 goto http_msg_rpreason;
1171 }
1172 if (likely(HTTP_IS_SPHT(*ptr)))
1173 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1174 /* so it's a CR/LF, so there is no reason phrase */
1175 goto http_msg_rsp_reason;
1176
Willy Tarreau8973c702007-01-21 23:58:29 +01001177 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001178 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 if (likely(!HTTP_IS_CRLF(*ptr)))
1180 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001181 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001182 http_msg_rpline_eol:
1183 /* We have seen the end of line. Note that we do not
1184 * necessarily have the \n yet, but at least we know that we
1185 * have EITHER \r OR \n, otherwise the response would not be
1186 * complete. We can then record the response length and return
1187 * to the caller which will be able to register it.
1188 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001189 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001190 return ptr;
1191
Willy Tarreau8973c702007-01-21 23:58:29 +01001192 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001193#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001194 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1195 exit(1);
1196#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001197 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001198 }
1199
1200 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001201 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001202 if (ret_state)
1203 *ret_state = state;
1204 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001205 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001206 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001207}
1208
Willy Tarreau8973c702007-01-21 23:58:29 +01001209/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001210 * This function parses a request line between <ptr> and <end>, starting with
1211 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1212 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1213 * will give undefined results.
1214 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1215 * and that msg->sol points to the beginning of the request.
1216 * If a complete line is found (which implies that at least one CR or LF is
1217 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1218 * returned indicating an incomplete line (which does not mean that parts have
1219 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1220 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1221 * upon next call.
1222 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001223 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001224 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1225 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001226 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001227 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001228const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001229 enum ht_state state, const char *ptr, const char *end,
1230 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001231{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001232 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001233
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001234 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001235 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001236 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 if (likely(HTTP_IS_TOKEN(*ptr)))
1238 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001239
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001240 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001241 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001242 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1243 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001244
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001245 if (likely(HTTP_IS_CRLF(*ptr))) {
1246 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001247 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001248 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001249 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001250 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001251 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001252 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001253 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001254 msg->sl.rq.v_l = 0;
1255 goto http_msg_rqline_eol;
1256 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001257 state = HTTP_MSG_ERROR;
1258 break;
1259
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001260 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001261 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001262 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001263 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 goto http_msg_rquri;
1265 }
1266 if (likely(HTTP_IS_SPHT(*ptr)))
1267 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1268 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1269 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001270
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001271 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001272 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001273 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001274 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001275
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001276 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001277 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1279 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001280
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001281 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001282 /* non-ASCII chars are forbidden unless option
1283 * accept-invalid-http-request is enabled in the frontend.
1284 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001285 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001286 if (msg->err_pos < -1)
1287 goto invalid_char;
1288 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001289 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001290 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1291 }
1292
1293 if (likely(HTTP_IS_CRLF(*ptr))) {
1294 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1295 goto http_msg_req09_uri_e;
1296 }
1297
1298 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001299 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001300 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001301 state = HTTP_MSG_ERROR;
1302 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001303
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001304 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001305 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001306 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001307 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001308 goto http_msg_rqver;
1309 }
1310 if (likely(HTTP_IS_SPHT(*ptr)))
1311 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1312 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1313 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001314
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001315 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001316 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001317 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001318 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001319
1320 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001321 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001322 http_msg_rqline_eol:
1323 /* We have seen the end of line. Note that we do not
1324 * necessarily have the \n yet, but at least we know that we
1325 * have EITHER \r OR \n, otherwise the request would not be
1326 * complete. We can then record the request length and return
1327 * to the caller which will be able to register it.
1328 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001329 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001330 return ptr;
1331 }
1332
1333 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001334 state = HTTP_MSG_ERROR;
1335 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001336
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001337 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001338#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001339 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1340 exit(1);
1341#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001342 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001343 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001344
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001345 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001346 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001347 if (ret_state)
1348 *ret_state = state;
1349 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001350 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001351 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001352}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001353
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001354/*
1355 * Returns the data from Authorization header. Function may be called more
1356 * than once so data is stored in txn->auth_data. When no header is found
1357 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001358 * searching again for something we are unable to find anyway. However, if
1359 * the result if valid, the cache is not reused because we would risk to
1360 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001361 */
1362
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001363/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1364 * set according to global.tune.bufsize.
1365 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001366char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001367
1368int
1369get_http_auth(struct session *s)
1370{
1371
1372 struct http_txn *txn = &s->txn;
1373 struct chunk auth_method;
1374 struct hdr_ctx ctx;
1375 char *h, *p;
1376 int len;
1377
1378#ifdef DEBUG_AUTH
1379 printf("Auth for session %p: %d\n", s, txn->auth.method);
1380#endif
1381
1382 if (txn->auth.method == HTTP_AUTH_WRONG)
1383 return 0;
1384
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001385 txn->auth.method = HTTP_AUTH_WRONG;
1386
1387 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001388
1389 if (txn->flags & TX_USE_PX_CONN) {
1390 h = "Proxy-Authorization";
1391 len = strlen(h);
1392 } else {
1393 h = "Authorization";
1394 len = strlen(h);
1395 }
1396
Willy Tarreau9b28e032012-10-12 23:49:43 +02001397 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001398 return 0;
1399
1400 h = ctx.line + ctx.val;
1401
1402 p = memchr(h, ' ', ctx.vlen);
1403 if (!p || p == h)
1404 return 0;
1405
1406 chunk_initlen(&auth_method, h, 0, p-h);
1407 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1408
1409 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1410
1411 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001412 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001413
1414 if (len < 0)
1415 return 0;
1416
1417
1418 get_http_auth_buff[len] = '\0';
1419
1420 p = strchr(get_http_auth_buff, ':');
1421
1422 if (!p)
1423 return 0;
1424
1425 txn->auth.user = get_http_auth_buff;
1426 *p = '\0';
1427 txn->auth.pass = p+1;
1428
1429 txn->auth.method = HTTP_AUTH_BASIC;
1430 return 1;
1431 }
1432
1433 return 0;
1434}
1435
Willy Tarreau58f10d72006-12-04 02:26:12 +01001436
Willy Tarreau8973c702007-01-21 23:58:29 +01001437/*
1438 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001439 * depending on the initial msg->msg_state. The caller is responsible for
1440 * ensuring that the message does not wrap. The function can be preempted
1441 * everywhere when data are missing and recalled at the exact same location
1442 * with no information loss. The message may even be realigned between two
1443 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001444 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001445 * fields. Note that msg->sol will be initialized after completing the first
1446 * state, so that none of the msg pointers has to be initialized prior to the
1447 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001448 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001449void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450{
Willy Tarreau3770f232013-12-07 00:01:53 +01001451 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001453 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001454
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001455 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001456 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001457 ptr = buf->p + msg->next;
1458 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001459
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001460 if (unlikely(ptr >= end))
1461 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001462
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001464 /*
1465 * First, states that are specific to the response only.
1466 * We check them first so that request and headers are
1467 * closer to each other (accessed more often).
1468 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001469 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001470 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001471 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001472 /* we have a start of message, but we have to check
1473 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001474 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001475 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001476 if (unlikely(ptr != buf->p)) {
1477 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001478 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001479 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001480 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001481 }
Willy Tarreau26927362012-05-18 23:22:52 +02001482 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001483 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001484 hdr_idx_init(idx);
1485 state = HTTP_MSG_RPVER;
1486 goto http_msg_rpver;
1487 }
1488
1489 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1490 goto http_msg_invalid;
1491
1492 if (unlikely(*ptr == '\n'))
1493 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1494 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1495 /* stop here */
1496
Willy Tarreau8973c702007-01-21 23:58:29 +01001497 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001498 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001499 EXPECT_LF_HERE(ptr, http_msg_invalid);
1500 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1501 /* stop here */
1502
Willy Tarreau8973c702007-01-21 23:58:29 +01001503 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001504 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001505 case HTTP_MSG_RPVER_SP:
1506 case HTTP_MSG_RPCODE:
1507 case HTTP_MSG_RPCODE_SP:
1508 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001509 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001510 state, ptr, end,
1511 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001512 if (unlikely(!ptr))
1513 return;
1514
1515 /* we have a full response and we know that we have either a CR
1516 * or an LF at <ptr>.
1517 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001518 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1519
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001520 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001521 if (likely(*ptr == '\r'))
1522 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1523 goto http_msg_rpline_end;
1524
Willy Tarreau8973c702007-01-21 23:58:29 +01001525 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001526 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001527 /* msg->sol must point to the first of CR or LF. */
1528 EXPECT_LF_HERE(ptr, http_msg_invalid);
1529 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1530 /* stop here */
1531
1532 /*
1533 * Second, states that are specific to the request only
1534 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001536 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001538 /* we have a start of message, but we have to check
1539 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001540 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001541 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001542 if (likely(ptr != buf->p)) {
1543 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001544 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001545 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001546 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 }
Willy Tarreau26927362012-05-18 23:22:52 +02001548 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001549 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001550 state = HTTP_MSG_RQMETH;
1551 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001553
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1555 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001556
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 if (unlikely(*ptr == '\n'))
1558 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1559 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001560 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001561
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001563 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 EXPECT_LF_HERE(ptr, http_msg_invalid);
1565 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001566 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001567
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001569 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 case HTTP_MSG_RQMETH_SP:
1571 case HTTP_MSG_RQURI:
1572 case HTTP_MSG_RQURI_SP:
1573 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001574 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001575 state, ptr, end,
1576 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 if (unlikely(!ptr))
1578 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001579
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 /* we have a full request and we know that we have either a CR
1581 * or an LF at <ptr>.
1582 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001583 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001584
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001585 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 if (likely(*ptr == '\r'))
1587 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001588 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001589
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001590 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001591 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001592 /* check for HTTP/0.9 request : no version information available.
1593 * msg->sol must point to the first of CR or LF.
1594 */
1595 if (unlikely(msg->sl.rq.v_l == 0))
1596 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001597
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001598 EXPECT_LF_HERE(ptr, http_msg_invalid);
1599 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001600 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001601
Willy Tarreau8973c702007-01-21 23:58:29 +01001602 /*
1603 * Common states below
1604 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001606 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001607 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001608 if (likely(!HTTP_IS_CRLF(*ptr))) {
1609 goto http_msg_hdr_name;
1610 }
1611
1612 if (likely(*ptr == '\r'))
1613 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1614 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001615
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001616 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001617 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001618 /* assumes msg->sol points to the first char */
1619 if (likely(HTTP_IS_TOKEN(*ptr)))
1620 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001621
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001622 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001624
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001625 if (likely(msg->err_pos < -1) || *ptr == '\n')
1626 goto http_msg_invalid;
1627
1628 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001629 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001630
1631 /* and we still accept this non-token character */
1632 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001633
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001634 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001635 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001636 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001637 if (likely(HTTP_IS_SPHT(*ptr)))
1638 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001641 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001642
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001643 if (likely(!HTTP_IS_CRLF(*ptr))) {
1644 goto http_msg_hdr_val;
1645 }
1646
1647 if (likely(*ptr == '\r'))
1648 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1649 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001650
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001651 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001652 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001653 EXPECT_LF_HERE(ptr, http_msg_invalid);
1654 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001655
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001656 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001657 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001658 if (likely(HTTP_IS_SPHT(*ptr))) {
1659 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001660 for (; buf->p + msg->sov < ptr; msg->sov++)
1661 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001662 goto http_msg_hdr_l1_sp;
1663 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001664 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001665 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 goto http_msg_complete_header;
1667
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001668 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001669 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001670 /* assumes msg->sol points to the first char, and msg->sov
1671 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001672 */
1673 if (likely(!HTTP_IS_CRLF(*ptr)))
1674 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001675
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001676 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001677 /* Note: we could also copy eol into ->eoh so that we have the
1678 * real header end in case it ends with lots of LWS, but is this
1679 * really needed ?
1680 */
1681 if (likely(*ptr == '\r'))
1682 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1683 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001684
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001686 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001687 EXPECT_LF_HERE(ptr, http_msg_invalid);
1688 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001689
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001691 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1693 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001694 for (; buf->p + msg->eol < ptr; msg->eol++)
1695 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001696 goto http_msg_hdr_val;
1697 }
1698 http_msg_complete_header:
1699 /*
1700 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001701 * Assumes msg->sol points to the first char, msg->sov points
1702 * to the first character of the value and msg->eol to the
1703 * first CR or LF so we know how the line ends. We insert last
1704 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001705 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001706 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001707 idx, idx->tail) < 0))
1708 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001709
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001710 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001711 if (likely(!HTTP_IS_CRLF(*ptr))) {
1712 goto http_msg_hdr_name;
1713 }
1714
1715 if (likely(*ptr == '\r'))
1716 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1717 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001718
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001719 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001720 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001721 /* Assumes msg->sol points to the first of either CR or LF.
1722 * Sets ->sov and ->next to the total header length, ->eoh to
1723 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1724 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 EXPECT_LF_HERE(ptr, http_msg_invalid);
1726 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001727 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001728 msg->eoh = msg->sol;
1729 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001730 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001731 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001732 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001733
1734 case HTTP_MSG_ERROR:
1735 /* this may only happen if we call http_msg_analyser() twice with an error */
1736 break;
1737
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001739#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1741 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001742#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001743 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 }
1745 http_msg_ood:
1746 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001747 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001748 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001749 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 http_msg_invalid:
1752 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001753 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001754 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001755 return;
1756}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001757
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001758/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1759 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1760 * nothing is done and 1 is returned.
1761 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001762static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001763{
1764 int delta;
1765 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001766 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001767
1768 if (msg->sl.rq.v_l != 0)
1769 return 1;
1770
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001771 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1772 if (txn->meth != HTTP_METH_GET)
1773 return 0;
1774
Willy Tarreau9b28e032012-10-12 23:49:43 +02001775 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001776 delta = 0;
1777
1778 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001779 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1780 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001781 }
1782 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001783 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001784 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001785 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001786 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001787 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001788 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001789 NULL, NULL);
1790 if (unlikely(!cur_end))
1791 return 0;
1792
1793 /* we have a full HTTP/1.0 request now and we know that
1794 * we have either a CR or an LF at <ptr>.
1795 */
1796 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1797 return 1;
1798}
1799
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001800/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001801 * and "keep-alive" values. If we already know that some headers may safely
1802 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001803 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1804 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001805 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001806 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1807 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1808 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001809 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001810 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001811void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001812{
Willy Tarreau5b154472009-12-21 20:11:07 +01001813 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001814 const char *hdr_val = "Connection";
1815 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001816
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001817 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001818 return;
1819
Willy Tarreau88d349d2010-01-25 12:15:43 +01001820 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1821 hdr_val = "Proxy-Connection";
1822 hdr_len = 16;
1823 }
1824
Willy Tarreau5b154472009-12-21 20:11:07 +01001825 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001826 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001827 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001828 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1829 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001830 if (to_del & 2)
1831 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001832 else
1833 txn->flags |= TX_CON_KAL_SET;
1834 }
1835 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1836 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001837 if (to_del & 1)
1838 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001839 else
1840 txn->flags |= TX_CON_CLO_SET;
1841 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001842 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1843 txn->flags |= TX_HDR_CONN_UPG;
1844 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001845 }
1846
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001847 txn->flags |= TX_HDR_CONN_PRS;
1848 return;
1849}
Willy Tarreau5b154472009-12-21 20:11:07 +01001850
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001851/* Apply desired changes on the Connection: header. Values may be removed and/or
1852 * added depending on the <wanted> flags, which are exclusively composed of
1853 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1854 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1855 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001856void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001857{
1858 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001859 const char *hdr_val = "Connection";
1860 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001861
1862 ctx.idx = 0;
1863
Willy Tarreau88d349d2010-01-25 12:15:43 +01001864
1865 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1866 hdr_val = "Proxy-Connection";
1867 hdr_len = 16;
1868 }
1869
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001870 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001871 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001872 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1873 if (wanted & TX_CON_KAL_SET)
1874 txn->flags |= TX_CON_KAL_SET;
1875 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001876 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001877 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001878 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1879 if (wanted & TX_CON_CLO_SET)
1880 txn->flags |= TX_CON_CLO_SET;
1881 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001882 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001883 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001884 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001885
1886 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1887 return;
1888
1889 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1890 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001891 hdr_val = "Connection: close";
1892 hdr_len = 17;
1893 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1894 hdr_val = "Proxy-Connection: close";
1895 hdr_len = 23;
1896 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001897 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001898 }
1899
1900 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1901 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001902 hdr_val = "Connection: keep-alive";
1903 hdr_len = 22;
1904 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1905 hdr_val = "Proxy-Connection: keep-alive";
1906 hdr_len = 28;
1907 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001908 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001909 }
1910 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001911}
1912
Willy Tarreauc24715e2014-04-17 15:21:20 +02001913/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1914 * the first byte of data after the chunk size, so that we know we can forward
1915 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1916 * the chunk size. That way it is always possible to differentiate between the
1917 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001918 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001919 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001920 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001921static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001922{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001923 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001924 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001925 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001926 const char *end = buf->data + buf->size;
1927 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001928 unsigned int chunk = 0;
1929
1930 /* The chunk size is in the following form, though we are only
1931 * interested in the size and CRLF :
1932 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1933 */
1934 while (1) {
1935 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001936 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001937 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001938 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001939 if (c < 0) /* not a hex digit anymore */
1940 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001941 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001942 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001943 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001944 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001945 chunk = (chunk << 4) + c;
1946 }
1947
Willy Tarreaud98cf932009-12-27 22:54:55 +01001948 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001949 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001950 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951
1952 while (http_is_spht[(unsigned char)*ptr]) {
1953 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001954 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001955 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001956 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001957 }
1958
Willy Tarreaud98cf932009-12-27 22:54:55 +01001959 /* Up to there, we know that at least one byte is present at *ptr. Check
1960 * for the end of chunk size.
1961 */
1962 while (1) {
1963 if (likely(HTTP_IS_CRLF(*ptr))) {
1964 /* we now have a CR or an LF at ptr */
1965 if (likely(*ptr == '\r')) {
1966 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001967 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001968 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001969 return 0;
1970 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001971
Willy Tarreaud98cf932009-12-27 22:54:55 +01001972 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001973 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001974 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001975 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001976 /* done */
1977 break;
1978 }
1979 else if (*ptr == ';') {
1980 /* chunk extension, ends at next CRLF */
1981 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001982 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001983 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001984 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001985
1986 while (!HTTP_IS_CRLF(*ptr)) {
1987 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001988 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001989 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001990 return 0;
1991 }
1992 /* we have a CRLF now, loop above */
1993 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001994 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001995 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001996 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001997 }
1998
Willy Tarreaud98cf932009-12-27 22:54:55 +01001999 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02002000 * which may or may not be present. We save that into ->next,
2001 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01002002 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02002003 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02002004 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02002005 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002006 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01002007 msg->chunk_len = chunk;
2008 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002009 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002010 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002011 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002012 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002013 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002014}
2015
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002016/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002017 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002018 * the trailers is found, it is automatically scheduled to be forwarded,
2019 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2020 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002021 * except maybe msg->next if it could parse some lines, and returns zero.
2022 * If a parse error is encountered, the function returns < 0 and does not
2023 * change anything except maybe msg->next. Note that the message must
2024 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002025 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002026 * forwarding, and that msg->next exactly matches the length of trailers
2027 * already parsed and not forwarded. It is also important to note that this
2028 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002029 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002030static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002031{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002032 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002033
Willy Tarreaua458b672012-03-05 11:17:50 +01002034 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002035 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002036 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002037 const char *ptr = b_ptr(buf, msg->next);
2038 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002039 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002040
2041 /* scan current line and stop at LF or CRLF */
2042 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002043 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002044 return 0;
2045
2046 if (*ptr == '\n') {
2047 if (!p1)
2048 p1 = ptr;
2049 p2 = ptr;
2050 break;
2051 }
2052
2053 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002054 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002055 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002056 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002057 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002058 p1 = ptr;
2059 }
2060
2061 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002062 if (ptr >= buf->data + buf->size)
2063 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002064 }
2065
2066 /* after LF; point to beginning of next line */
2067 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002068 if (p2 >= buf->data + buf->size)
2069 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002070
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002071 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002072 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002073 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002074
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002075 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002076 /* LF/CRLF at beginning of line => end of trailers at p2.
2077 * Everything was scheduled for forwarding, there's nothing
2078 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002079 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002080 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002081 msg->msg_state = HTTP_MSG_DONE;
2082 return 1;
2083 }
2084 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002085 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002086 }
2087}
2088
Willy Tarreauc24715e2014-04-17 15:21:20 +02002089/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2090 * or a possible LF alone at the end of a chunk. It automatically adjusts
2091 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002092 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002093 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2094 * not enough data are available, the function does not change anything and
2095 * returns zero. If a parse error is encountered, the function returns < 0 and
2096 * does not change anything. Note: this function is designed to parse wrapped
2097 * CRLF at the end of the buffer.
2098 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002099static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002100{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002101 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002102 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002103 int bytes;
2104
2105 /* NB: we'll check data availabilty at the end. It's not a
2106 * problem because whatever we match first will be checked
2107 * against the correct length.
2108 */
2109 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002110 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002111 if (*ptr == '\r') {
2112 bytes++;
2113 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002114 if (ptr >= buf->data + buf->size)
2115 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002116 }
2117
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002118 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002119 return 0;
2120
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002121 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002122 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002123 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002124 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002125
2126 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002127 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002128 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002129 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002130 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002131 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2132 return 1;
2133}
Willy Tarreau5b154472009-12-21 20:11:07 +01002134
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002135/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2136 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2137 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2138 * Unparsable qvalues return 1000 as "q=1.000".
2139 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002140int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002141{
2142 int q = 1000;
2143
2144 if (!isdigit(*qvalue))
2145 goto out;
2146 q = (*qvalue++ - '0') * 1000;
2147
2148 if (*qvalue++ != '.')
2149 goto out;
2150
2151 if (!isdigit(*qvalue))
2152 goto out;
2153 q += (*qvalue++ - '0') * 100;
2154
2155 if (!isdigit(*qvalue))
2156 goto out;
2157 q += (*qvalue++ - '0') * 10;
2158
2159 if (!isdigit(*qvalue))
2160 goto out;
2161 q += (*qvalue++ - '0') * 1;
2162 out:
2163 if (q > 1000)
2164 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002165 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002166 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002167 return q;
2168}
William Lallemand82fe75c2012-10-23 10:25:10 +02002169
2170/*
2171 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002172 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002173int select_compression_request_header(struct session *s, struct buffer *req)
2174{
2175 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002176 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002177 struct hdr_ctx ctx;
2178 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002179 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002180
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002181 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2182 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002183 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2184 */
2185 ctx.idx = 0;
2186 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2187 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002188 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2189 (ctx.vlen < 31 ||
2190 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2191 ctx.line[ctx.val + 30] < '6' ||
2192 (ctx.line[ctx.val + 30] == '6' &&
2193 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2194 s->comp_algo = NULL;
2195 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002196 }
2197
William Lallemand82fe75c2012-10-23 10:25:10 +02002198 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002199 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 +01002200 int best_q = 0;
2201
William Lallemand82fe75c2012-10-23 10:25:10 +02002202 ctx.idx = 0;
2203 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002204 const char *qval;
2205 int q;
2206 int toklen;
2207
2208 /* try to isolate the token from the optional q-value */
2209 toklen = 0;
2210 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2211 toklen++;
2212
2213 qval = ctx.line + ctx.val + toklen;
2214 while (1) {
2215 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2216 qval++;
2217
2218 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2219 qval = NULL;
2220 break;
2221 }
2222 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002223
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002224 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2225 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002226
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002227 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2228 qval = NULL;
2229 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002230 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002231 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2232 break;
2233
2234 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2235 qval++;
2236 }
2237
2238 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002239 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002240
2241 if (q <= best_q)
2242 continue;
2243
2244 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2245 if (*(ctx.line + ctx.val) == '*' ||
2246 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2247 s->comp_algo = comp_algo;
2248 best_q = q;
2249 break;
2250 }
2251 }
2252 }
2253 }
2254
2255 /* remove all occurrences of the header when "compression offload" is set */
2256 if (s->comp_algo) {
2257 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2258 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2259 ctx.idx = 0;
2260 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2261 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002262 }
2263 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002264 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002265 }
2266
2267 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002268 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2269 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002270 if (comp_algo->add_data == identity_add_data) {
2271 s->comp_algo = comp_algo;
2272 return 1;
2273 }
2274 }
2275 }
2276
2277 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002278 return 0;
2279}
2280
2281/*
2282 * Selects a comression algorithm depending of the server response.
2283 */
2284int select_compression_response_header(struct session *s, struct buffer *res)
2285{
2286 struct http_txn *txn = &s->txn;
2287 struct http_msg *msg = &txn->rsp;
2288 struct hdr_ctx ctx;
2289 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002290
2291 /* no common compression algorithm was found in request header */
2292 if (s->comp_algo == NULL)
2293 goto fail;
2294
2295 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002296 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002297 goto fail;
2298
William Lallemandd3002612012-11-26 14:34:47 +01002299 /* 200 only */
2300 if (txn->status != 200)
2301 goto fail;
2302
William Lallemand82fe75c2012-10-23 10:25:10 +02002303 /* Content-Length is null */
2304 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2305 goto fail;
2306
2307 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002308 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002309 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2310 goto fail;
2311
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002312 /* no compression when Cache-Control: no-transform is present in the message */
2313 ctx.idx = 0;
2314 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2315 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2316 goto fail;
2317 }
2318
William Lallemand82fe75c2012-10-23 10:25:10 +02002319 comp_type = NULL;
2320
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002321 /* we don't want to compress multipart content-types, nor content-types that are
2322 * not listed in the "compression type" directive if any. If no content-type was
2323 * found but configuration requires one, we don't compress either. Backend has
2324 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002325 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002326 ctx.idx = 0;
2327 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2328 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2329 goto fail;
2330
2331 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2332 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002333 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002334 if (ctx.vlen >= comp_type->name_len &&
2335 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002336 /* this Content-Type should be compressed */
2337 break;
2338 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002339 /* this Content-Type should not be compressed */
2340 if (comp_type == NULL)
2341 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002342 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002343 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002344 else { /* no content-type header */
2345 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2346 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002347 }
2348
William Lallemandd85f9172012-11-09 17:05:39 +01002349 /* limit compression rate */
2350 if (global.comp_rate_lim > 0)
2351 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2352 goto fail;
2353
William Lallemand072a2bf2012-11-20 17:01:01 +01002354 /* limit cpu usage */
2355 if (idle_pct < compress_min_idle)
2356 goto fail;
2357
William Lallemand4c49fae2012-11-07 15:00:23 +01002358 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002359 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002360 goto fail;
2361
William Lallemandec3e3892012-11-12 17:02:18 +01002362 s->flags |= SN_COMP_READY;
2363
William Lallemand82fe75c2012-10-23 10:25:10 +02002364 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002365 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002366 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2367 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2368
2369 /* add Transfer-Encoding header */
2370 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2371 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2372
2373 /*
2374 * Add Content-Encoding header when it's not identity encoding.
2375 * RFC 2616 : Identity encoding: This content-coding is used only in the
2376 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2377 * header.
2378 */
2379 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002380 trash.len = 18;
2381 memcpy(trash.str, "Content-Encoding: ", trash.len);
2382 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2383 trash.len += s->comp_algo->name_len;
2384 trash.str[trash.len] = '\0';
2385 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002386 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002387 return 1;
2388
2389fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002390 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002391 return 0;
2392}
2393
2394
Willy Tarreaud787e662009-07-07 10:14:51 +02002395/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2396 * processing can continue on next analysers, or zero if it either needs more
2397 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2398 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2399 * when it has nothing left to do, and may remove any analyser when it wants to
2400 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002401 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002402int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002403{
Willy Tarreau59234e92008-11-30 23:51:27 +01002404 /*
2405 * We will parse the partial (or complete) lines.
2406 * We will check the request syntax, and also join multi-line
2407 * headers. An index of all the lines will be elaborated while
2408 * parsing.
2409 *
2410 * For the parsing, we use a 28 states FSM.
2411 *
2412 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002413 * req->buf->p = beginning of request
2414 * req->buf->p + msg->eoh = end of processed headers / start of current one
2415 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002416 * msg->eol = end of current header or line (LF or CRLF)
2417 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002418 *
2419 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002420 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002421 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2422 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002423 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002424
Willy Tarreau59234e92008-11-30 23:51:27 +01002425 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002426 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002427 struct http_txn *txn = &s->txn;
2428 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002429 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002430
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002431 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 +01002432 now_ms, __FUNCTION__,
2433 s,
2434 req,
2435 req->rex, req->wex,
2436 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002437 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002438 req->analysers);
2439
Willy Tarreau52a0c602009-08-16 22:45:38 +02002440 /* we're speaking HTTP here, so let's speak HTTP to the client */
2441 s->srv_error = http_return_srv_error;
2442
Willy Tarreau83e3af02009-12-28 17:39:57 +01002443 /* There's a protected area at the end of the buffer for rewriting
2444 * purposes. We don't want to start to parse the request if the
2445 * protected area is affected, because we may have to move processed
2446 * data later, which is much more complicated.
2447 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002448 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002449 if (txn->flags & TX_NOT_FIRST) {
2450 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002451 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002452 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002453 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002454 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002455 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002456 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002457 return 0;
2458 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002459 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2460 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2461 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002462 }
2463
Willy Tarreau065e8332010-01-08 00:30:20 +01002464 /* Note that we have the same problem with the response ; we
2465 * may want to send a redirect, error or anything which requires
2466 * some spare space. So we'll ensure that we have at least
2467 * maxrewrite bytes available in the response buffer before
2468 * processing that one. This will only affect pipelined
2469 * keep-alive requests.
2470 */
2471 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002472 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002473 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2474 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2475 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002476 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002477 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002478 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002479 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002480 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002481 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002482 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002483 return 0;
2484 }
2485 }
2486
Willy Tarreau9b28e032012-10-12 23:49:43 +02002487 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002488 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002489 }
2490
Willy Tarreau59234e92008-11-30 23:51:27 +01002491 /* 1: we might have to print this header in debug mode */
2492 if (unlikely((global.mode & MODE_DEBUG) &&
2493 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002494 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002495 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002496
Willy Tarreau9b28e032012-10-12 23:49:43 +02002497 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002498 /* this is a bit complex : in case of error on the request line,
2499 * we know that rq.l is still zero, so we display only the part
2500 * up to the end of the line (truncated by debug_hdr).
2501 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002502 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002503 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002504
Willy Tarreau59234e92008-11-30 23:51:27 +01002505 sol += hdr_idx_first_pos(&txn->hdr_idx);
2506 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002507
Willy Tarreau59234e92008-11-30 23:51:27 +01002508 while (cur_idx) {
2509 eol = sol + txn->hdr_idx.v[cur_idx].len;
2510 debug_hdr("clihdr", s, sol, eol);
2511 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2512 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002513 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002514 }
2515
Willy Tarreau58f10d72006-12-04 02:26:12 +01002516
Willy Tarreau59234e92008-11-30 23:51:27 +01002517 /*
2518 * Now we quickly check if we have found a full valid request.
2519 * If not so, we check the FD and buffer states before leaving.
2520 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002521 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002522 * requests are checked first. When waiting for a second request
2523 * on a keep-alive session, if we encounter and error, close, t/o,
2524 * we note the error in the session flags but don't set any state.
2525 * Since the error will be noted there, it will not be counted by
2526 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002527 * Last, we may increase some tracked counters' http request errors on
2528 * the cases that are deliberately the client's fault. For instance,
2529 * a timeout or connection reset is not counted as an error. However
2530 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002531 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002532
Willy Tarreau655dce92009-11-08 13:10:58 +01002533 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002534 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002535 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002536 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002537 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002538 session_inc_http_req_ctr(s);
2539 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002540 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002541 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002542 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002543
Willy Tarreau59234e92008-11-30 23:51:27 +01002544 /* 1: Since we are in header mode, if there's no space
2545 * left for headers, we won't be able to free more
2546 * later, so the session will never terminate. We
2547 * must terminate it now.
2548 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002549 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 /* FIXME: check if URI is set and return Status
2551 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002552 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002553 session_inc_http_req_ctr(s);
2554 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002555 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002556 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002557 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002558 goto return_bad_req;
2559 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002560
Willy Tarreau59234e92008-11-30 23:51:27 +01002561 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002562 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002563 if (!(s->flags & SN_ERR_MASK))
2564 s->flags |= SN_ERR_CLICL;
2565
Willy Tarreaufcffa692010-01-10 14:21:19 +01002566 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002567 goto failed_keep_alive;
2568
Willy Tarreau59234e92008-11-30 23:51:27 +01002569 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002570 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002571 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002572 session_inc_http_err_ctr(s);
2573 }
2574
Willy Tarreaudc979f22012-12-04 10:39:01 +01002575 txn->status = 400;
2576 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002577 msg->msg_state = HTTP_MSG_ERROR;
2578 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002579
Willy Tarreauda7ff642010-06-23 11:44:09 +02002580 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002581 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002582 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002583 if (s->listener->counters)
2584 s->listener->counters->failed_req++;
2585
Willy Tarreau59234e92008-11-30 23:51:27 +01002586 if (!(s->flags & SN_FINST_MASK))
2587 s->flags |= SN_FINST_R;
2588 return 0;
2589 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002590
Willy Tarreau59234e92008-11-30 23:51:27 +01002591 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002592 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002593 if (!(s->flags & SN_ERR_MASK))
2594 s->flags |= SN_ERR_CLITO;
2595
Willy Tarreaufcffa692010-01-10 14:21:19 +01002596 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002597 goto failed_keep_alive;
2598
Willy Tarreau59234e92008-11-30 23:51:27 +01002599 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002600 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002601 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002602 session_inc_http_err_ctr(s);
2603 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002605 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002606 msg->msg_state = HTTP_MSG_ERROR;
2607 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002608
Willy Tarreauda7ff642010-06-23 11:44:09 +02002609 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002610 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002611 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002612 if (s->listener->counters)
2613 s->listener->counters->failed_req++;
2614
Willy Tarreau59234e92008-11-30 23:51:27 +01002615 if (!(s->flags & SN_FINST_MASK))
2616 s->flags |= SN_FINST_R;
2617 return 0;
2618 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002619
Willy Tarreau59234e92008-11-30 23:51:27 +01002620 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002621 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002622 if (!(s->flags & SN_ERR_MASK))
2623 s->flags |= SN_ERR_CLICL;
2624
Willy Tarreaufcffa692010-01-10 14:21:19 +01002625 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002626 goto failed_keep_alive;
2627
Willy Tarreau4076a152009-04-02 15:18:36 +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 Tarreau59234e92008-11-30 23:51:27 +01002630 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002631 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002632 msg->msg_state = HTTP_MSG_ERROR;
2633 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002634
Willy Tarreauda7ff642010-06-23 11:44:09 +02002635 session_inc_http_err_ctr(s);
2636 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002637 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002638 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002639 if (s->listener->counters)
2640 s->listener->counters->failed_req++;
2641
Willy Tarreau59234e92008-11-30 23:51:27 +01002642 if (!(s->flags & SN_FINST_MASK))
2643 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002644 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002645 }
2646
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002647 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002648 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2649 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002650#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002651 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 +01002652 /* We need more data, we have to re-enable quick-ack in case we
2653 * previously disabled it, otherwise we might cause the client
2654 * to delay next data.
2655 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002656 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002657 }
2658#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002659
Willy Tarreaufcffa692010-01-10 14:21:19 +01002660 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2661 /* If the client starts to talk, let's fall back to
2662 * request timeout processing.
2663 */
2664 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002665 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002666 }
2667
Willy Tarreau59234e92008-11-30 23:51:27 +01002668 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002669 if (!tick_isset(req->analyse_exp)) {
2670 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2671 (txn->flags & TX_WAIT_NEXT_RQ) &&
2672 tick_isset(s->be->timeout.httpka))
2673 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2674 else
2675 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2676 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002677
Willy Tarreau59234e92008-11-30 23:51:27 +01002678 /* we're not ready yet */
2679 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002680
2681 failed_keep_alive:
2682 /* Here we process low-level errors for keep-alive requests. In
2683 * short, if the request is not the first one and it experiences
2684 * a timeout, read error or shutdown, we just silently close so
2685 * that the client can try again.
2686 */
2687 txn->status = 0;
2688 msg->msg_state = HTTP_MSG_RQBEFORE;
2689 req->analysers = 0;
2690 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002691 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002692 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002693 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002694 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002695 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002696
Willy Tarreaud787e662009-07-07 10:14:51 +02002697 /* OK now we have a complete HTTP request with indexed headers. Let's
2698 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002699 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002700 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002701 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002702 * byte after the last LF. msg->sov points to the first byte of data.
2703 * msg->eol cannot be trusted because it may have been left uninitialized
2704 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002705 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002706
Willy Tarreauda7ff642010-06-23 11:44:09 +02002707 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002708 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2709
Willy Tarreaub16a5742010-01-10 14:46:16 +01002710 if (txn->flags & TX_WAIT_NEXT_RQ) {
2711 /* kill the pending keep-alive timeout */
2712 txn->flags &= ~TX_WAIT_NEXT_RQ;
2713 req->analyse_exp = TICK_ETERNITY;
2714 }
2715
2716
Willy Tarreaud787e662009-07-07 10:14:51 +02002717 /* Maybe we found in invalid header name while we were configured not
2718 * to block on that, so we have to capture it now.
2719 */
2720 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002721 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002722
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 /*
2724 * 1: identify the method
2725 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002726 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002727
2728 /* we can make use of server redirect on GET and HEAD */
2729 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2730 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002731
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 /*
2733 * 2: check if the URI matches the monitor_uri.
2734 * We have to do this for every request which gets in, because
2735 * the monitor-uri is defined by the frontend.
2736 */
2737 if (unlikely((s->fe->monitor_uri_len != 0) &&
2738 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002739 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002740 s->fe->monitor_uri,
2741 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002742 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002743 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002744 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002745 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002746
Willy Tarreau59234e92008-11-30 23:51:27 +01002747 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002748 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002749
Willy Tarreau59234e92008-11-30 23:51:27 +01002750 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002751 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002752 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002753
Willy Tarreau59234e92008-11-30 23:51:27 +01002754 ret = acl_pass(ret);
2755 if (cond->pol == ACL_COND_UNLESS)
2756 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002757
Willy Tarreau59234e92008-11-30 23:51:27 +01002758 if (ret) {
2759 /* we fail this request, let's return 503 service unavail */
2760 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002761 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002762 if (!(s->flags & SN_ERR_MASK))
2763 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002764 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002765 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002766 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002767
Willy Tarreau59234e92008-11-30 23:51:27 +01002768 /* nothing to fail, let's reply normaly */
2769 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002770 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002771 if (!(s->flags & SN_ERR_MASK))
2772 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002773 goto return_prx_cond;
2774 }
2775
2776 /*
2777 * 3: Maybe we have to copy the original REQURI for the logs ?
2778 * Note: we cannot log anymore if the request has been
2779 * classified as invalid.
2780 */
2781 if (unlikely(s->logs.logwait & LW_REQ)) {
2782 /* we have a complete HTTP request that we must log */
2783 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2784 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002785
Willy Tarreau59234e92008-11-30 23:51:27 +01002786 if (urilen >= REQURI_LEN)
2787 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002788 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002789 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002790
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002791 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002792 s->do_log(s);
2793 } else {
2794 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002795 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002796 }
Willy Tarreau06619262006-12-17 08:37:22 +01002797
Willy Tarreau59234e92008-11-30 23:51:27 +01002798 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002799 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002800 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002801
Willy Tarreau5b154472009-12-21 20:11:07 +01002802 /* ... and check if the request is HTTP/1.1 or above */
2803 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002804 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2805 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2806 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002807 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002808
2809 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002810 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 +01002811
Willy Tarreau88d349d2010-01-25 12:15:43 +01002812 /* if the frontend has "option http-use-proxy-header", we'll check if
2813 * we have what looks like a proxied connection instead of a connection,
2814 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2815 * Note that this is *not* RFC-compliant, however browsers and proxies
2816 * happen to do that despite being non-standard :-(
2817 * We consider that a request not beginning with either '/' or '*' is
2818 * a proxied connection, which covers both "scheme://location" and
2819 * CONNECT ip:port.
2820 */
2821 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002822 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002823 txn->flags |= TX_USE_PX_CONN;
2824
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002825 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002826 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002827
Willy Tarreau59234e92008-11-30 23:51:27 +01002828 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002829 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002830 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002831 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002832
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002833 /* 6: determine the transfer-length.
2834 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2835 * the presence of a message-body in a REQUEST and its transfer length
2836 * must be determined that way (in order of precedence) :
2837 * 1. The presence of a message-body in a request is signaled by the
2838 * inclusion of a Content-Length or Transfer-Encoding header field
2839 * in the request's header fields. When a request message contains
2840 * both a message-body of non-zero length and a method that does
2841 * not define any semantics for that request message-body, then an
2842 * origin server SHOULD either ignore the message-body or respond
2843 * with an appropriate error message (e.g., 413). A proxy or
2844 * gateway, when presented the same request, SHOULD either forward
2845 * the request inbound with the message- body or ignore the
2846 * message-body when determining a response.
2847 *
2848 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2849 * and the "chunked" transfer-coding (Section 6.2) is used, the
2850 * transfer-length is defined by the use of this transfer-coding.
2851 * If a Transfer-Encoding header field is present and the "chunked"
2852 * transfer-coding is not present, the transfer-length is defined
2853 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002854 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002855 * 3. If a Content-Length header field is present, its decimal value in
2856 * OCTETs represents both the entity-length and the transfer-length.
2857 * If a message is received with both a Transfer-Encoding header
2858 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002859 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002860 * 4. By the server closing the connection. (Closing the connection
2861 * cannot be used to indicate the end of a request body, since that
2862 * would leave no possibility for the server to send back a response.)
2863 *
2864 * Whenever a transfer-coding is applied to a message-body, the set of
2865 * transfer-codings MUST include "chunked", unless the message indicates
2866 * it is terminated by closing the connection. When the "chunked"
2867 * transfer-coding is used, it MUST be the last transfer-coding applied
2868 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002869 */
2870
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002871 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002872 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002873 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002874 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002875 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002876 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002877 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2878 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002879 /* bad transfer-encoding (chunked followed by something else) */
2880 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002881 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002882 break;
2883 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002884 }
2885
Willy Tarreau32b47f42009-10-18 20:55:02 +02002886 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002887 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002888 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002889 signed long long cl;
2890
Willy Tarreauad14f752011-09-02 20:33:27 +02002891 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002892 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002893 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002894 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002895
Willy Tarreauad14f752011-09-02 20:33:27 +02002896 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002897 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002898 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002899 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002900
Willy Tarreauad14f752011-09-02 20:33:27 +02002901 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002902 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002903 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002904 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002905
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002906 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002907 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002908 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002909 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002910
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002911 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002912 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002913 }
2914
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002915 /* bodyless requests have a known length */
2916 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002917 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002918
Willy Tarreaud787e662009-07-07 10:14:51 +02002919 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002920 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002921 req->analyse_exp = TICK_ETERNITY;
2922 return 1;
2923
2924 return_bad_req:
2925 /* We centralize bad requests processing here */
2926 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2927 /* we detected a parsing error. We want to archive this request
2928 * in the dedicated proxy area for later troubleshooting.
2929 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002930 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002931 }
2932
2933 txn->req.msg_state = HTTP_MSG_ERROR;
2934 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002935 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002936
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002937 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002938 if (s->listener->counters)
2939 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002940
2941 return_prx_cond:
2942 if (!(s->flags & SN_ERR_MASK))
2943 s->flags |= SN_ERR_PRXCOND;
2944 if (!(s->flags & SN_FINST_MASK))
2945 s->flags |= SN_FINST_R;
2946
2947 req->analysers = 0;
2948 req->analyse_exp = TICK_ETERNITY;
2949 return 0;
2950}
2951
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002952
Willy Tarreau347a35d2013-11-22 17:51:09 +01002953/* This function prepares an applet to handle the stats. It can deal with the
2954 * "100-continue" expectation, check that admin rules are met for POST requests,
2955 * and program a response message if something was unexpected. It cannot fail
2956 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002957 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002958 * s->target which is supposed to already point to the stats applet. The caller
2959 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002960 */
2961int http_handle_stats(struct session *s, struct channel *req)
2962{
2963 struct stats_admin_rule *stats_admin_rule;
2964 struct stream_interface *si = s->rep->prod;
2965 struct http_txn *txn = &s->txn;
2966 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002967 struct uri_auth *uri_auth = s->be->uri_auth;
2968 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002969 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002970
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002971 appctx = si_appctx(si);
2972 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2973 appctx->st1 = appctx->st2 = 0;
2974 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2975 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002976 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
2977 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002978
2979 uri = msg->chn->buf->p + msg->sl.rq.u;
2980 lookup = uri + uri_auth->uri_len;
2981
2982 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2983 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002984 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002985 break;
2986 }
2987 }
2988
2989 if (uri_auth->refresh) {
2990 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2991 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002992 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002993 break;
2994 }
2995 }
2996 }
2997
2998 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2999 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003000 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003001 break;
3002 }
3003 }
3004
3005 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3006 if (memcmp(h, ";st=", 4) == 0) {
3007 int i;
3008 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003009 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003010 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3011 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003012 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003013 break;
3014 }
3015 }
3016 break;
3017 }
3018 }
3019
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003020 appctx->ctx.stats.scope_str = 0;
3021 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003022 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3023 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3024 int itx = 0;
3025 const char *h2;
3026 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3027 const char *err;
3028
3029 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3030 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003031 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003032 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3033 itx++;
3034 h++;
3035 }
3036
3037 if (itx > STAT_SCOPE_TXT_MAXLEN)
3038 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003039 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003040
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003041 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003042 memcpy(scope_txt, h2, itx);
3043 scope_txt[itx] = '\0';
3044 err = invalid_char(scope_txt);
3045 if (err) {
3046 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003047 appctx->ctx.stats.scope_str = 0;
3048 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003049 }
3050 break;
3051 }
3052 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003053
3054 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003055 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003056 int ret = 1;
3057
3058 if (stats_admin_rule->cond) {
3059 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3060 ret = acl_pass(ret);
3061 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3062 ret = !ret;
3063 }
3064
3065 if (ret) {
3066 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003067 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003068 break;
3069 }
3070 }
3071
3072 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003073 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003074 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003075 /* we'll need the request body, possibly after sending 100-continue */
3076 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003077 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003078 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003079 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003080 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3081 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003082 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003083 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003084 else {
3085 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003086 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003087 }
3088
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003089 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003090 return 1;
3091}
3092
Lukas Tribus67db8df2013-06-23 17:37:13 +02003093/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3094 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3095 */
3096static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3097{
3098#ifdef IP_TOS
3099 if (from.ss_family == AF_INET)
3100 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3101#endif
3102#ifdef IPV6_TCLASS
3103 if (from.ss_family == AF_INET6) {
3104 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3105 /* v4-mapped addresses need IP_TOS */
3106 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3107 else
3108 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3109 }
3110#endif
3111}
3112
Willy Tarreau20b0de52012-12-24 15:45:22 +01003113/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003114 * transaction <txn>. Returns the first rule that prevents further processing
3115 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3116 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3117 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003118 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003119static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003120http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003121{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003122 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003123 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003124 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003125
Willy Tarreauff011f22011-01-06 17:51:27 +01003126 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003127 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003128 continue;
3129
Willy Tarreau96257ec2012-12-27 10:46:37 +01003130 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003131 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003132 int ret;
3133
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003134 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003135 ret = acl_pass(ret);
3136
Willy Tarreauff011f22011-01-06 17:51:27 +01003137 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003138 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003139
3140 if (!ret) /* condition not matched */
3141 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003142 }
3143
Willy Tarreau20b0de52012-12-24 15:45:22 +01003144
Willy Tarreau96257ec2012-12-27 10:46:37 +01003145 switch (rule->action) {
3146 case HTTP_REQ_ACT_ALLOW:
3147 return NULL; /* "allow" rules are OK */
3148
3149 case HTTP_REQ_ACT_DENY:
3150 txn->flags |= TX_CLDENY;
3151 return rule;
3152
Willy Tarreauccbcc372012-12-27 12:37:57 +01003153 case HTTP_REQ_ACT_TARPIT:
3154 txn->flags |= TX_CLTARPIT;
3155 return rule;
3156
Willy Tarreau96257ec2012-12-27 10:46:37 +01003157 case HTTP_REQ_ACT_AUTH:
3158 return rule;
3159
Willy Tarreau81499eb2012-12-27 12:19:02 +01003160 case HTTP_REQ_ACT_REDIR:
3161 return rule;
3162
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003163 case HTTP_REQ_ACT_SET_NICE:
3164 s->task->nice = rule->arg.nice;
3165 break;
3166
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003167 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003168 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003169 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003170 break;
3171
Willy Tarreau51347ed2013-06-11 19:34:13 +02003172 case HTTP_REQ_ACT_SET_MARK:
3173#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003174 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003175 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003176#endif
3177 break;
3178
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003179 case HTTP_REQ_ACT_SET_LOGL:
3180 s->logs.level = rule->arg.loglevel;
3181 break;
3182
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003183 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003184 case HTTP_REQ_ACT_SET_HDR:
3185 ctx.idx = 0;
3186 /* remove all occurrences of the header */
3187 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3188 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3189 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003190 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003191 if (rule->action == HTTP_REQ_ACT_DEL_HDR)
3192 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003193 /* now fall through to header addition */
3194
3195 case HTTP_REQ_ACT_ADD_HDR:
3196 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3197 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3198 trash.len = rule->arg.hdr_add.name_len;
3199 trash.str[trash.len++] = ':';
3200 trash.str[trash.len++] = ' ';
3201 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3202 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3203 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003204
3205 case HTTP_REQ_ACT_DEL_ACL:
3206 case HTTP_REQ_ACT_DEL_MAP: {
3207 struct pat_ref *ref;
3208 char *key;
3209 int len;
3210
3211 /* collect reference */
3212 ref = pat_ref_lookup(rule->arg.map.ref);
3213 if (!ref)
3214 continue;
3215
3216 /* collect key */
3217 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3218 key = trash.str;
3219 key[len] = '\0';
3220
3221 /* perform update */
3222 /* returned code: 1=ok, 0=ko */
3223 pat_ref_delete(ref, key);
3224
3225 break;
3226 }
3227
3228 case HTTP_REQ_ACT_ADD_ACL: {
3229 struct pat_ref *ref;
3230 char *key;
3231 struct chunk *trash_key;
3232 int len;
3233
3234 trash_key = get_trash_chunk();
3235
3236 /* collect reference */
3237 ref = pat_ref_lookup(rule->arg.map.ref);
3238 if (!ref)
3239 continue;
3240
3241 /* collect key */
3242 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3243 key = trash_key->str;
3244 key[len] = '\0';
3245
3246 /* perform update */
3247 /* add entry only if it does not already exist */
3248 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003249 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003250
3251 break;
3252 }
3253
3254 case HTTP_REQ_ACT_SET_MAP: {
3255 struct pat_ref *ref;
3256 char *key, *value;
3257 struct chunk *trash_key, *trash_value;
3258 int len;
3259
3260 trash_key = get_trash_chunk();
3261 trash_value = get_trash_chunk();
3262
3263 /* collect reference */
3264 ref = pat_ref_lookup(rule->arg.map.ref);
3265 if (!ref)
3266 continue;
3267
3268 /* collect key */
3269 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3270 key = trash_key->str;
3271 key[len] = '\0';
3272
3273 /* collect value */
3274 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3275 value = trash_value->str;
3276 value[len] = '\0';
3277
3278 /* perform update */
3279 if (pat_ref_find_elt(ref, key) != NULL)
3280 /* update entry if it exists */
3281 pat_ref_set(ref, key, value, NULL);
3282 else
3283 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003284 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003285
3286 break;
3287 }
William Lallemand73025dd2014-04-24 14:38:37 +02003288
3289 case HTTP_REQ_ACT_CUSTOM_CONT:
3290 rule->action_ptr(rule, px, s, txn);
3291 break;
3292
3293 case HTTP_REQ_ACT_CUSTOM_STOP:
3294 rule->action_ptr(rule, px, s, txn);
3295 return rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003296 }
3297 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003298
3299 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003300 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003301}
3302
Willy Tarreau71241ab2012-12-27 11:30:54 +01003303
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003304/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3305 * transaction <txn>. Returns the first rule that prevents further processing
3306 * of the response (deny, ...) or NULL if it executed all rules or stopped
3307 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3308 * rule.
3309 */
3310static struct http_res_rule *
3311http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3312{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003313 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003314 struct http_res_rule *rule;
3315 struct hdr_ctx ctx;
3316
3317 list_for_each_entry(rule, rules, list) {
3318 if (rule->action >= HTTP_RES_ACT_MAX)
3319 continue;
3320
3321 /* check optional condition */
3322 if (rule->cond) {
3323 int ret;
3324
3325 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3326 ret = acl_pass(ret);
3327
3328 if (rule->cond->pol == ACL_COND_UNLESS)
3329 ret = !ret;
3330
3331 if (!ret) /* condition not matched */
3332 continue;
3333 }
3334
3335
3336 switch (rule->action) {
3337 case HTTP_RES_ACT_ALLOW:
3338 return NULL; /* "allow" rules are OK */
3339
3340 case HTTP_RES_ACT_DENY:
3341 txn->flags |= TX_SVDENY;
3342 return rule;
3343
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003344 case HTTP_RES_ACT_SET_NICE:
3345 s->task->nice = rule->arg.nice;
3346 break;
3347
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003348 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003349 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003350 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003351 break;
3352
Willy Tarreau51347ed2013-06-11 19:34:13 +02003353 case HTTP_RES_ACT_SET_MARK:
3354#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003355 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003356 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003357#endif
3358 break;
3359
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003360 case HTTP_RES_ACT_SET_LOGL:
3361 s->logs.level = rule->arg.loglevel;
3362 break;
3363
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003364 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003365 case HTTP_RES_ACT_SET_HDR:
3366 ctx.idx = 0;
3367 /* remove all occurrences of the header */
3368 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3369 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3370 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3371 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003372 if (rule->action == HTTP_RES_ACT_DEL_HDR)
3373 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003374 /* now fall through to header addition */
3375
3376 case HTTP_RES_ACT_ADD_HDR:
3377 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3378 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3379 trash.len = rule->arg.hdr_add.name_len;
3380 trash.str[trash.len++] = ':';
3381 trash.str[trash.len++] = ' ';
3382 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3383 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3384 break;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003385
3386 case HTTP_RES_ACT_DEL_ACL:
3387 case HTTP_RES_ACT_DEL_MAP: {
3388 struct pat_ref *ref;
3389 char *key;
3390 int len;
3391
3392 /* collect reference */
3393 ref = pat_ref_lookup(rule->arg.map.ref);
3394 if (!ref)
3395 continue;
3396
3397 /* collect key */
3398 len = build_logline(s, trash.str, trash.size, &rule->arg.map.key);
3399 key = trash.str;
3400 key[len] = '\0';
3401
3402 /* perform update */
3403 /* returned code: 1=ok, 0=ko */
3404 pat_ref_delete(ref, key);
3405
3406 break;
3407 }
3408
3409 case HTTP_RES_ACT_ADD_ACL: {
3410 struct pat_ref *ref;
3411 char *key;
3412 struct chunk *trash_key;
3413 int len;
3414
3415 trash_key = get_trash_chunk();
3416
3417 /* collect reference */
3418 ref = pat_ref_lookup(rule->arg.map.ref);
3419 if (!ref)
3420 continue;
3421
3422 /* collect key */
3423 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3424 key = trash_key->str;
3425 key[len] = '\0';
3426
3427 /* perform update */
3428 /* check if the entry already exists */
3429 if (pat_ref_find_elt(ref, key) == NULL)
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003430 pat_ref_add(ref, key, NULL, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003431
3432 break;
3433 }
3434
3435 case HTTP_RES_ACT_SET_MAP: {
3436 struct pat_ref *ref;
3437 char *key, *value;
3438 struct chunk *trash_key, *trash_value;
3439 int len;
3440
3441 trash_key = get_trash_chunk();
3442 trash_value = get_trash_chunk();
3443
3444 /* collect reference */
3445 ref = pat_ref_lookup(rule->arg.map.ref);
3446 if (!ref)
3447 continue;
3448
3449 /* collect key */
3450 len = build_logline(s, trash_key->str, trash_key->size, &rule->arg.map.key);
3451 key = trash_key->str;
3452 key[len] = '\0';
3453
3454 /* collect value */
3455 len = build_logline(s, trash_value->str, trash_value->size, &rule->arg.map.value);
3456 value = trash_value->str;
3457 value[len] = '\0';
3458
3459 /* perform update */
3460 if (pat_ref_find_elt(ref, key) != NULL)
3461 /* update entry if it exists */
3462 pat_ref_set(ref, key, value, NULL);
3463 else
3464 /* insert a new entry */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02003465 pat_ref_add(ref, key, value, NULL);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02003466
3467 break;
3468 }
William Lallemand73025dd2014-04-24 14:38:37 +02003469
3470 case HTTP_RES_ACT_CUSTOM_CONT:
3471 rule->action_ptr(rule, px, s, txn);
3472 break;
3473
3474 case HTTP_RES_ACT_CUSTOM_STOP:
3475 rule->action_ptr(rule, px, s, txn);
3476 return rule;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003477 }
3478 }
3479
3480 /* we reached the end of the rules, nothing to report */
3481 return NULL;
3482}
3483
3484
Willy Tarreau71241ab2012-12-27 11:30:54 +01003485/* Perform an HTTP redirect based on the information in <rule>. The function
3486 * returns non-zero on success, or zero in case of a, irrecoverable error such
3487 * as too large a request to build a valid response.
3488 */
3489static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3490{
3491 struct http_msg *msg = &txn->req;
3492 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003493 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003494
3495 /* build redirect message */
3496 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003497 case 308:
3498 msg_fmt = HTTP_308;
3499 break;
3500 case 307:
3501 msg_fmt = HTTP_307;
3502 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003503 case 303:
3504 msg_fmt = HTTP_303;
3505 break;
3506 case 301:
3507 msg_fmt = HTTP_301;
3508 break;
3509 case 302:
3510 default:
3511 msg_fmt = HTTP_302;
3512 break;
3513 }
3514
3515 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3516 return 0;
3517
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003518 location = trash.str + trash.len;
3519
Willy Tarreau71241ab2012-12-27 11:30:54 +01003520 switch(rule->type) {
3521 case REDIRECT_TYPE_SCHEME: {
3522 const char *path;
3523 const char *host;
3524 struct hdr_ctx ctx;
3525 int pathlen;
3526 int hostlen;
3527
3528 host = "";
3529 hostlen = 0;
3530 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003531 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003532 host = ctx.line + ctx.val;
3533 hostlen = ctx.vlen;
3534 }
3535
3536 path = http_get_path(txn);
3537 /* build message using path */
3538 if (path) {
3539 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3540 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3541 int qs = 0;
3542 while (qs < pathlen) {
3543 if (path[qs] == '?') {
3544 pathlen = qs;
3545 break;
3546 }
3547 qs++;
3548 }
3549 }
3550 } else {
3551 path = "/";
3552 pathlen = 1;
3553 }
3554
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003555 if (rule->rdr_str) { /* this is an old "redirect" rule */
3556 /* check if we can add scheme + "://" + host + path */
3557 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3558 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003559
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003560 /* add scheme */
3561 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3562 trash.len += rule->rdr_len;
3563 }
3564 else {
3565 /* add scheme with executing log format */
3566 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003567
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003568 /* check if we can add scheme + "://" + host + path */
3569 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3570 return 0;
3571 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003572 /* add "://" */
3573 memcpy(trash.str + trash.len, "://", 3);
3574 trash.len += 3;
3575
3576 /* add host */
3577 memcpy(trash.str + trash.len, host, hostlen);
3578 trash.len += hostlen;
3579
3580 /* add path */
3581 memcpy(trash.str + trash.len, path, pathlen);
3582 trash.len += pathlen;
3583
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003584 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003585 if (trash.len && trash.str[trash.len - 1] != '/' &&
3586 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3587 if (trash.len > trash.size - 5)
3588 return 0;
3589 trash.str[trash.len] = '/';
3590 trash.len++;
3591 }
3592
3593 break;
3594 }
3595 case REDIRECT_TYPE_PREFIX: {
3596 const char *path;
3597 int pathlen;
3598
3599 path = http_get_path(txn);
3600 /* build message using path */
3601 if (path) {
3602 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3603 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3604 int qs = 0;
3605 while (qs < pathlen) {
3606 if (path[qs] == '?') {
3607 pathlen = qs;
3608 break;
3609 }
3610 qs++;
3611 }
3612 }
3613 } else {
3614 path = "/";
3615 pathlen = 1;
3616 }
3617
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003618 if (rule->rdr_str) { /* this is an old "redirect" rule */
3619 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3620 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003621
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003622 /* add prefix. Note that if prefix == "/", we don't want to
3623 * add anything, otherwise it makes it hard for the user to
3624 * configure a self-redirection.
3625 */
3626 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3627 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3628 trash.len += rule->rdr_len;
3629 }
3630 }
3631 else {
3632 /* add prefix with executing log format */
3633 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3634
3635 /* Check length */
3636 if (trash.len + pathlen > trash.size - 4)
3637 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003638 }
3639
3640 /* add path */
3641 memcpy(trash.str + trash.len, path, pathlen);
3642 trash.len += pathlen;
3643
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003644 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003645 if (trash.len && trash.str[trash.len - 1] != '/' &&
3646 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3647 if (trash.len > trash.size - 5)
3648 return 0;
3649 trash.str[trash.len] = '/';
3650 trash.len++;
3651 }
3652
3653 break;
3654 }
3655 case REDIRECT_TYPE_LOCATION:
3656 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003657 if (rule->rdr_str) { /* this is an old "redirect" rule */
3658 if (trash.len + rule->rdr_len > trash.size - 4)
3659 return 0;
3660
3661 /* add location */
3662 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3663 trash.len += rule->rdr_len;
3664 }
3665 else {
3666 /* add location with executing log format */
3667 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003668
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003669 /* Check left length */
3670 if (trash.len > trash.size - 4)
3671 return 0;
3672 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003673 break;
3674 }
3675
3676 if (rule->cookie_len) {
3677 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3678 trash.len += 14;
3679 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3680 trash.len += rule->cookie_len;
3681 memcpy(trash.str + trash.len, "\r\n", 2);
3682 trash.len += 2;
3683 }
3684
3685 /* add end of headers and the keep-alive/close status.
3686 * We may choose to set keep-alive if the Location begins
3687 * with a slash, because the client will come back to the
3688 * same server.
3689 */
3690 txn->status = rule->code;
3691 /* let's log the request time */
3692 s->logs.tv_request = now;
3693
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003694 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003695 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3696 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3697 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3698 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3699 /* keep-alive possible */
3700 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3701 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3702 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3703 trash.len += 30;
3704 } else {
3705 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3706 trash.len += 24;
3707 }
3708 }
3709 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3710 trash.len += 4;
3711 bo_inject(txn->rsp.chn, trash.str, trash.len);
3712 /* "eat" the request */
3713 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02003714 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003715 msg->sov = 0;
3716 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3717 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3718 txn->req.msg_state = HTTP_MSG_CLOSED;
3719 txn->rsp.msg_state = HTTP_MSG_DONE;
3720 } else {
3721 /* keep-alive not possible */
3722 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3723 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3724 trash.len += 29;
3725 } else {
3726 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3727 trash.len += 23;
3728 }
3729 stream_int_retnclose(txn->req.chn->prod, &trash);
3730 txn->req.chn->analysers = 0;
3731 }
3732
3733 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003734 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003735 if (!(s->flags & SN_FINST_MASK))
3736 s->flags |= SN_FINST_R;
3737
3738 return 1;
3739}
3740
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003741/* This stream analyser runs all HTTP request processing which is common to
3742 * frontends and backends, which means blocking ACLs, filters, connection-close,
3743 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003744 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003745 * either needs more data or wants to immediately abort the request (eg: deny,
3746 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003747 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003748int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003749{
Willy Tarreaud787e662009-07-07 10:14:51 +02003750 struct http_txn *txn = &s->txn;
3751 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003752 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003753 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003754 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003755 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003756
Willy Tarreau655dce92009-11-08 13:10:58 +01003757 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003758 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003759 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003760 return 0;
3761 }
3762
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003763 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 +02003764 now_ms, __FUNCTION__,
3765 s,
3766 req,
3767 req->rex, req->wex,
3768 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003769 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003770 req->analysers);
3771
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003772 /* first check whether we have some ACLs set to block this request */
3773 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003774 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003775
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003776 ret = acl_pass(ret);
3777 if (cond->pol == ACL_COND_UNLESS)
3778 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003779
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003780 if (ret) {
3781 txn->status = 403;
3782 /* let's log the request time */
3783 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003784 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003785 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003786 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003787 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003788 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003789
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003790 /* just in case we have some per-backend tracking */
3791 session_inc_be_http_req_ctr(s);
3792
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003793 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003794 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003795
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003796 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
3797 if (txn->flags & TX_CLDENY)
3798 goto deny;
3799 else
3800 goto tarpit;
3801 }
3802
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003803 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003804 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003805 if (stats_check_uri(s->rep->prod, txn, px)) {
3806 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003807 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3808 txn->status = 500;
3809 s->logs.tv_request = now;
3810 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003811
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003812 if (!(s->flags & SN_ERR_MASK))
3813 s->flags |= SN_ERR_RESOURCE;
3814 goto return_prx_cond;
3815 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003816 /* parse the whole stats request and extract the relevant information */
3817 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003818 http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003819
3820 /* we can still get a deny on the stats page */
3821 if (txn->flags & TX_CLDENY)
3822 goto deny;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003823 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003824 }
3825
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003826 /* evaluate the req* rules except reqadd */
3827 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003828 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003829 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003830
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003831 if (txn->flags & TX_CLDENY)
3832 goto deny;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003833
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003834 if (txn->flags & TX_CLTARPIT)
3835 goto tarpit;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003836 }
Willy Tarreau06619262006-12-17 08:37:22 +01003837
Willy Tarreau70dffda2014-01-30 03:07:23 +01003838 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
Willy Tarreau5b154472009-12-21 20:11:07 +01003839 * only change if both the request and the config reference something else.
Willy Tarreau70dffda2014-01-30 03:07:23 +01003840 * Option httpclose by itself sets tunnel mode where headers are mangled.
3841 * However, if another mode is set, it will affect it (eg: server-close/
3842 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3843 * if FE and BE have the same settings (common). The method consists in
3844 * checking if options changed between the two calls (implying that either
3845 * one is non-null, or one of them is non-null and we are there for the first
3846 * time.
Willy Tarreau42736642009-10-18 21:04:35 +02003847 */
Willy Tarreau416ce612014-01-31 15:45:34 +01003848 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003849 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
Willy Tarreau70dffda2014-01-30 03:07:23 +01003850 int tmp = TX_CON_WANT_KAL;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003851
Willy Tarreau70dffda2014-01-30 03:07:23 +01003852 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
3853 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
3854 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
3855 tmp = TX_CON_WANT_TUN;
3856
3857 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3858 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3859 tmp = TX_CON_WANT_TUN;
3860 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003861
3862 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau70dffda2014-01-30 03:07:23 +01003863 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
3864 /* option httpclose + server_close => forceclose */
3865 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3866 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3867 tmp = TX_CON_WANT_CLO;
3868 else
3869 tmp = TX_CON_WANT_SCL;
3870 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003871
3872 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
3873 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
Willy Tarreau5b154472009-12-21 20:11:07 +01003874 tmp = TX_CON_WANT_CLO;
3875
Willy Tarreau5b154472009-12-21 20:11:07 +01003876 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3877 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003878
Willy Tarreau416ce612014-01-31 15:45:34 +01003879 if (!(txn->flags & TX_HDR_CONN_PRS) &&
3880 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003881 /* parse the Connection header and possibly clean it */
3882 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003883 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003884 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3885 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003886 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003887 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003888 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003889 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003890 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003891
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003892 /* check if client or config asks for explicit close in KAL/SCL */
3893 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3894 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3895 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003896 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003897 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003898 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003899 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3900 }
Willy Tarreau78599912009-10-17 20:12:21 +02003901
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003902 /* we can be blocked here because the request needs to be authenticated,
3903 * either to pass or to access stats.
3904 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003905 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003906 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003907
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003908 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003909 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003910
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003911 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Willy Tarreaue6d24162014-04-28 16:59:15 +02003912 txn->status = (txn->flags & TX_USE_PX_CONN) ? 407 : 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003913 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003914 /* on 401 we still count one error, because normal browsing
3915 * won't significantly increase the counter but brute force
3916 * attempts will.
3917 */
3918 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003919 goto return_prx_cond;
3920 }
3921
Willy Tarreau5e9edce2014-04-28 11:13:33 +02003922 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3923 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3924 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003925 goto done;
Willy Tarreau5e9edce2014-04-28 11:13:33 +02003926 }
3927
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003928 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_CUSTOM_STOP)
3929 goto done;
Willy Tarreau5e9edce2014-04-28 11:13:33 +02003930
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003931 /* add request headers from the rule sets in the same order */
3932 list_for_each_entry(wl, &px->req_add, list) {
3933 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003934 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003935 ret = acl_pass(ret);
3936 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3937 ret = !ret;
3938 if (!ret)
3939 continue;
3940 }
3941
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003942 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003943 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003944 }
3945
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003946 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003947 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003948 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3949 s->fe->fe_counters.intercepted_req++;
3950
3951 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3952 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3953 if (!(s->flags & SN_FINST_MASK))
3954 s->flags |= SN_FINST_R;
3955
Willy Tarreau70730dd2014-04-24 18:06:27 +02003956 /* we may want to compress the stats page */
3957 if (s->fe->comp || s->be->comp)
3958 select_compression_request_header(s, req->buf);
3959
3960 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
Willy Tarreaucfe7fdd2014-04-26 22:08:32 +02003961 req->analysers = (req->analysers & AN_REQ_HTTP_BODY) |
3962 AN_REQ_HTTP_XFER_BODY | AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_BE | AN_RES_HTTP_XFER_BODY;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003963 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003964 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003965
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003966 /* check whether we have some ACLs set to redirect this request */
3967 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003968 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003969 int ret;
3970
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003971 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003972 ret = acl_pass(ret);
3973 if (rule->cond->pol == ACL_COND_UNLESS)
3974 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003975 if (!ret)
3976 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003977 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003978 if (!http_apply_redirect_rule(rule, s, txn))
3979 goto return_bad_req;
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003980 goto done;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003981 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003982
Willy Tarreau2be39392010-01-03 17:24:51 +01003983 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3984 * If this happens, then the data will not come immediately, so we must
3985 * send all what we have without waiting. Note that due to the small gain
3986 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003987 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003988 * itself once used.
3989 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003990 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003991
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003992 done: /* done with this analyser, continue with next ones that the calling
3993 * points will have set, if any.
3994 */
3995 req->analysers &= ~an_bit;
3996 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003997 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003998
Willy Tarreaubbba2a82014-04-28 13:57:26 +02003999 tarpit:
4000 /* When a connection is tarpitted, we use the tarpit timeout,
4001 * which may be the same as the connect timeout if unspecified.
4002 * If unset, then set it to zero because we really want it to
4003 * eventually expire. We build the tarpit as an analyser.
4004 */
4005 channel_erase(s->req);
4006
4007 /* wipe the request out so that we can drop the connection early
4008 * if the client closes first.
4009 */
4010 channel_dont_connect(req);
4011 req->analysers = 0; /* remove switching rules etc... */
4012 req->analysers |= AN_REQ_HTTP_TARPIT;
4013 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
4014 if (!req->analyse_exp)
4015 req->analyse_exp = tick_add(now_ms, 0);
4016 session_inc_http_err_ctr(s);
4017 s->fe->fe_counters.denied_req++;
4018 if (s->fe != s->be)
4019 s->be->be_counters.denied_req++;
4020 if (s->listener->counters)
4021 s->listener->counters->denied_req++;
4022 goto done;
4023
4024 deny: /* this request was blocked (denied) */
4025 txn->status = 403;
4026 s->logs.tv_request = now;
4027 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
4028 session_inc_http_err_ctr(s);
4029 s->fe->fe_counters.denied_req++;
4030 if (s->fe != s->be)
4031 s->be->be_counters.denied_req++;
4032 if (s->listener->counters)
4033 s->listener->counters->denied_req++;
4034 goto return_prx_cond;
4035
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004036 return_bad_req:
4037 /* We centralize bad requests processing here */
4038 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
4039 /* we detected a parsing error. We want to archive this request
4040 * in the dedicated proxy area for later troubleshooting.
4041 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004042 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004043 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02004044
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004045 txn->req.msg_state = HTTP_MSG_ERROR;
4046 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004047 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004048
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004049 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004050 if (s->listener->counters)
4051 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02004052
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004053 return_prx_cond:
4054 if (!(s->flags & SN_ERR_MASK))
4055 s->flags |= SN_ERR_PRXCOND;
4056 if (!(s->flags & SN_FINST_MASK))
4057 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01004058
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004059 req->analysers = 0;
4060 req->analyse_exp = TICK_ETERNITY;
4061 return 0;
4062}
Willy Tarreau58f10d72006-12-04 02:26:12 +01004063
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004064/* This function performs all the processing enabled for the current request.
4065 * It returns 1 if the processing can continue on next analysers, or zero if it
4066 * needs more data, encounters an error, or wants to immediately abort the
4067 * request. It relies on buffers flags, and updates s->req->analysers.
4068 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004069int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004070{
4071 struct http_txn *txn = &s->txn;
4072 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004073 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01004074
Willy Tarreau655dce92009-11-08 13:10:58 +01004075 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02004076 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004077 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02004078 return 0;
4079 }
4080
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004081 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 +02004082 now_ms, __FUNCTION__,
4083 s,
4084 req,
4085 req->rex, req->wex,
4086 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004087 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02004088 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01004089
William Lallemand82fe75c2012-10-23 10:25:10 +02004090 if (s->fe->comp || s->be->comp)
4091 select_compression_request_header(s, req->buf);
4092
Willy Tarreau59234e92008-11-30 23:51:27 +01004093 /*
4094 * Right now, we know that we have processed the entire headers
4095 * and that unwanted requests have been filtered out. We can do
4096 * whatever we want with the remaining request. Also, now we
4097 * may have separate values for ->fe, ->be.
4098 */
Willy Tarreau06619262006-12-17 08:37:22 +01004099
Willy Tarreau59234e92008-11-30 23:51:27 +01004100 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004101 * If HTTP PROXY is set we simply get remote server address parsing
4102 * incoming request. Note that this requires that a connection is
4103 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01004104 */
4105 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004106 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004107 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004108
Willy Tarreau9471b8c2013-12-15 13:31:35 +01004109 /* Note that for now we don't reuse existing proxy connections */
4110 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02004111 txn->req.msg_state = HTTP_MSG_ERROR;
4112 txn->status = 500;
4113 req->analysers = 0;
4114 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
4115
4116 if (!(s->flags & SN_ERR_MASK))
4117 s->flags |= SN_ERR_RESOURCE;
4118 if (!(s->flags & SN_FINST_MASK))
4119 s->flags |= SN_FINST_R;
4120
4121 return 0;
4122 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004123
4124 path = http_get_path(txn);
4125 url2sa(req->buf->p + msg->sl.rq.u,
4126 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01004127 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01004128 /* if the path was found, we have to remove everything between
4129 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
4130 * found, we need to replace from req->buf->p + msg->sl.rq.u for
4131 * u_l characters by a single "/".
4132 */
4133 if (path) {
4134 char *cur_ptr = req->buf->p;
4135 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4136 int delta;
4137
4138 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
4139 http_msg_move_end(&txn->req, delta);
4140 cur_end += delta;
4141 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4142 goto return_bad_req;
4143 }
4144 else {
4145 char *cur_ptr = req->buf->p;
4146 char *cur_end = cur_ptr + txn->req.sl.rq.l;
4147 int delta;
4148
4149 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
4150 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
4151 http_msg_move_end(&txn->req, delta);
4152 cur_end += delta;
4153 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
4154 goto return_bad_req;
4155 }
Willy Tarreau59234e92008-11-30 23:51:27 +01004156 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01004157
Willy Tarreau59234e92008-11-30 23:51:27 +01004158 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004159 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01004160 * Note that doing so might move headers in the request, but
4161 * the fields will stay coherent and the URI will not move.
4162 * This should only be performed in the backend.
4163 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02004164 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01004165 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
4166 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02004167
Willy Tarreau59234e92008-11-30 23:51:27 +01004168 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01004169 * 8: the appsession cookie was looked up very early in 1.2,
4170 * so let's do the same now.
4171 */
4172
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004173 /* It needs to look into the URI unless persistence must be ignored */
4174 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02004175 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 +01004176 }
4177
William Lallemanda73203e2012-03-12 12:48:57 +01004178 /* add unique-id if "header-unique-id" is specified */
4179
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004180 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
4181 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
4182 goto return_bad_req;
4183 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01004184 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02004185 }
William Lallemanda73203e2012-03-12 12:48:57 +01004186
4187 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004188 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
4189 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01004190 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004191 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004192 goto return_bad_req;
4193 }
4194
Cyril Bontéb21570a2009-11-29 20:04:48 +01004195 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004196 * 9: add X-Forwarded-For if either the frontend or the backend
4197 * asks for it.
4198 */
4199 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004200 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004201 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004202 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4203 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004204 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004205 /* The header is set to be added only if none is present
4206 * and we found it, so don't do anything.
4207 */
4208 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004209 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004210 /* Add an X-Forwarded-For header unless the source IP is
4211 * in the 'except' network range.
4212 */
4213 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004214 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004215 != s->fe->except_net.s_addr) &&
4216 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004217 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004218 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004219 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004220 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004221 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004222
4223 /* Note: we rely on the backend to get the header name to be used for
4224 * x-forwarded-for, because the header is really meant for the backends.
4225 * However, if the backend did not specify any option, we have to rely
4226 * on the frontend's header name.
4227 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004228 if (s->be->fwdfor_hdr_len) {
4229 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004230 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004231 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004232 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004233 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004234 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004235 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 +01004236
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004237 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004238 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004239 }
4240 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004241 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004242 /* FIXME: for the sake of completeness, we should also support
4243 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004244 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004245 int len;
4246 char pn[INET6_ADDRSTRLEN];
4247 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004248 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004249 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004250
Willy Tarreau59234e92008-11-30 23:51:27 +01004251 /* Note: we rely on the backend to get the header name to be used for
4252 * x-forwarded-for, because the header is really meant for the backends.
4253 * However, if the backend did not specify any option, we have to rely
4254 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004255 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004256 if (s->be->fwdfor_hdr_len) {
4257 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004258 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004259 } else {
4260 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004261 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004262 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004263 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004264
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004265 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004266 goto return_bad_req;
4267 }
4268 }
4269
4270 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004271 * 10: add X-Original-To if either the frontend or the backend
4272 * asks for it.
4273 */
4274 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4275
4276 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004277 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004278 /* Add an X-Original-To header unless the destination IP is
4279 * in the 'except' network range.
4280 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004281 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004282
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004283 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004284 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004285 (((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 +02004286 != s->fe->except_to.s_addr) &&
4287 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004288 (((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 +02004289 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004290 int len;
4291 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004292 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004293
4294 /* Note: we rely on the backend to get the header name to be used for
4295 * x-original-to, because the header is really meant for the backends.
4296 * However, if the backend did not specify any option, we have to rely
4297 * on the frontend's header name.
4298 */
4299 if (s->be->orgto_hdr_len) {
4300 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004301 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004302 } else {
4303 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004304 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004305 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004306 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 +02004307
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004308 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004309 goto return_bad_req;
4310 }
4311 }
4312 }
4313
Willy Tarreau50fc7772012-11-11 22:19:57 +01004314 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4315 * If an "Upgrade" token is found, the header is left untouched in order not to have
4316 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4317 * "Upgrade" is present in the Connection header.
4318 */
4319 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4320 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004321 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4322 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004323 unsigned int want_flags = 0;
4324
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004325 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004326 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004327 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4328 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004329 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004330 want_flags |= TX_CON_CLO_SET;
4331 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004332 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004333 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4334 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004335 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004336 want_flags |= TX_CON_KAL_SET;
4337 }
4338
4339 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004340 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004341 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004342
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004343
Willy Tarreau522d6c02009-12-06 18:49:18 +01004344 /* If we have no server assigned yet and we're balancing on url_param
4345 * with a POST request, we may be interested in checking the body for
4346 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004347 */
4348 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4349 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004350 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004351 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004352 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004353 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004354
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004355 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004356 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004357#ifdef TCP_QUICKACK
4358 /* We expect some data from the client. Unless we know for sure
4359 * we already have a full request, we have to re-enable quick-ack
4360 * in case we previously disabled it, otherwise we might cause
4361 * the client to delay further data.
4362 */
4363 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004364 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004365 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004366 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004367 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004368#endif
4369 }
Willy Tarreau03945942009-12-22 16:50:27 +01004370
Willy Tarreau59234e92008-11-30 23:51:27 +01004371 /*************************************************************
4372 * OK, that's finished for the headers. We have done what we *
4373 * could. Let's switch to the DATA state. *
4374 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004375 req->analyse_exp = TICK_ETERNITY;
4376 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004377
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004378 /* if the server closes the connection, we want to immediately react
4379 * and close the socket to save packets and syscalls.
4380 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004381 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4382 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004383
Willy Tarreau59234e92008-11-30 23:51:27 +01004384 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004385 /* OK let's go on with the BODY now */
4386 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004387
Willy Tarreau59234e92008-11-30 23:51:27 +01004388 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004389 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004390 /* we detected a parsing error. We want to archive this request
4391 * in the dedicated proxy area for later troubleshooting.
4392 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004393 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004394 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004395
Willy Tarreau59234e92008-11-30 23:51:27 +01004396 txn->req.msg_state = HTTP_MSG_ERROR;
4397 txn->status = 400;
4398 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004399 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004400
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004401 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004402 if (s->listener->counters)
4403 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004404
Willy Tarreau59234e92008-11-30 23:51:27 +01004405 if (!(s->flags & SN_ERR_MASK))
4406 s->flags |= SN_ERR_PRXCOND;
4407 if (!(s->flags & SN_FINST_MASK))
4408 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004409 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004410}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004411
Willy Tarreau60b85b02008-11-30 23:28:40 +01004412/* This function is an analyser which processes the HTTP tarpit. It always
4413 * returns zero, at the beginning because it prevents any other processing
4414 * from occurring, and at the end because it terminates the request.
4415 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004416int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004417{
4418 struct http_txn *txn = &s->txn;
4419
4420 /* This connection is being tarpitted. The CLIENT side has
4421 * already set the connect expiration date to the right
4422 * timeout. We just have to check that the client is still
4423 * there and that the timeout has not expired.
4424 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004425 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004426 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004427 !tick_is_expired(req->analyse_exp, now_ms))
4428 return 0;
4429
4430 /* We will set the queue timer to the time spent, just for
4431 * logging purposes. We fake a 500 server error, so that the
4432 * attacker will not suspect his connection has been tarpitted.
4433 * It will not cause trouble to the logs because we can exclude
4434 * the tarpitted connections by filtering on the 'PT' status flags.
4435 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004436 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4437
4438 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004439 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004440 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004441
4442 req->analysers = 0;
4443 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004444
Willy Tarreau60b85b02008-11-30 23:28:40 +01004445 if (!(s->flags & SN_ERR_MASK))
4446 s->flags |= SN_ERR_PRXCOND;
4447 if (!(s->flags & SN_FINST_MASK))
4448 s->flags |= SN_FINST_T;
4449 return 0;
4450}
4451
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004452/* This function is an analyser which waits for the HTTP request body. It waits
4453 * for either the buffer to be full, or the full advertised contents to have
4454 * reached the buffer. It must only be called after the standard HTTP request
4455 * processing has occurred, because it expects the request to be parsed and will
4456 * look for the Expect header. It may send a 100-Continue interim response. It
4457 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4458 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4459 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004460 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004461int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004462{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004463 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004464 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004465
4466 /* We have to parse the HTTP request body to find any required data.
4467 * "balance url_param check_post" should have been the only way to get
4468 * into this. We were brought here after HTTP header analysis, so all
4469 * related structures are ready.
4470 */
4471
Willy Tarreau890988f2014-04-10 11:59:33 +02004472 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4473 /* This is the first call */
4474 if (msg->msg_state < HTTP_MSG_BODY)
4475 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004476
Willy Tarreau890988f2014-04-10 11:59:33 +02004477 if (msg->msg_state < HTTP_MSG_100_SENT) {
4478 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4479 * send an HTTP/1.1 100 Continue intermediate response.
4480 */
4481 if (msg->flags & HTTP_MSGF_VER_11) {
4482 struct hdr_ctx ctx;
4483 ctx.idx = 0;
4484 /* Expect is allowed in 1.1, look for it */
4485 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4486 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4487 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4488 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004489 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004490 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004491 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004492
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004493 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004494 * req->buf->p still points to the beginning of the message. We
4495 * must save the body in msg->next because it survives buffer
4496 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004497 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004498 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004499
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004500 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004501 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4502 else
4503 msg->msg_state = HTTP_MSG_DATA;
4504 }
4505
Willy Tarreau890988f2014-04-10 11:59:33 +02004506 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4507 /* We're in content-length mode, we just have to wait for enough data. */
4508 if (req->buf->i - msg->sov < msg->body_len)
4509 goto missing_data;
4510
4511 /* OK we have everything we need now */
4512 goto http_end;
4513 }
4514
4515 /* OK here we're parsing a chunked-encoded message */
4516
Willy Tarreau522d6c02009-12-06 18:49:18 +01004517 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004518 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004519 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004520 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004521 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004522 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004523
Willy Tarreau115acb92009-12-26 13:56:06 +01004524 if (!ret)
4525 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004526 else if (ret < 0) {
4527 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004528 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004529 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004530 }
4531
Willy Tarreaud98cf932009-12-27 22:54:55 +01004532 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004533 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004534 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004535 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004536 if (msg->msg_state == HTTP_MSG_TRAILERS)
4537 goto http_end;
4538
Willy Tarreau226071e2014-04-10 11:55:45 +02004539 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004540 goto http_end;
4541
4542 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004543 /* we get here if we need to wait for more data. If the buffer is full,
4544 * we have the maximum we can expect.
4545 */
4546 if (buffer_full(req->buf, global.tune.maxrewrite))
4547 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004548
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004549 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004550 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004551 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004552
4553 if (!(s->flags & SN_ERR_MASK))
4554 s->flags |= SN_ERR_CLITO;
4555 if (!(s->flags & SN_FINST_MASK))
4556 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004557 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004558 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004559
4560 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004561 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004562 /* Not enough data. We'll re-use the http-request
4563 * timeout here. Ideally, we should set the timeout
4564 * relative to the accept() date. We just set the
4565 * request timeout once at the beginning of the
4566 * request.
4567 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004568 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004569 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004570 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004571 return 0;
4572 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004573
4574 http_end:
4575 /* The situation will not evolve, so let's give up on the analysis. */
4576 s->logs.tv_request = now; /* update the request timer to reflect full request */
4577 req->analysers &= ~an_bit;
4578 req->analyse_exp = TICK_ETERNITY;
4579 return 1;
4580
4581 return_bad_req: /* let's centralize all bad requests */
4582 txn->req.msg_state = HTTP_MSG_ERROR;
4583 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004584 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004585
Willy Tarreau79ebac62010-06-07 13:47:49 +02004586 if (!(s->flags & SN_ERR_MASK))
4587 s->flags |= SN_ERR_PRXCOND;
4588 if (!(s->flags & SN_FINST_MASK))
4589 s->flags |= SN_FINST_R;
4590
Willy Tarreau522d6c02009-12-06 18:49:18 +01004591 return_err_msg:
4592 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004593 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004594 if (s->listener->counters)
4595 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004596 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004597}
4598
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004599/* send a server's name with an outgoing request over an established connection.
4600 * Note: this function is designed to be called once the request has been scheduled
4601 * for being forwarded. This is the reason why it rewinds the buffer before
4602 * proceeding.
4603 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004604int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004605
4606 struct hdr_ctx ctx;
4607
Mark Lamourinec2247f02012-01-04 13:02:01 -05004608 char *hdr_name = be->server_id_hdr_name;
4609 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004610 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004611 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004612 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004613
William Lallemandd9e90662012-01-30 17:27:17 +01004614 ctx.idx = 0;
4615
Willy Tarreau211cdec2014-04-17 20:18:08 +02004616 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004617 if (old_o) {
4618 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004619 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004620 txn->req.next += old_o;
4621 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004622 }
4623
Willy Tarreau9b28e032012-10-12 23:49:43 +02004624 old_i = chn->buf->i;
4625 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 -05004626 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004627 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004628 }
4629
4630 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004631 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004632 memcpy(hdr_val, hdr_name, hdr_name_len);
4633 hdr_val += hdr_name_len;
4634 *hdr_val++ = ':';
4635 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004636 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4637 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004638
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004639 if (old_o) {
4640 /* If this was a forwarded request, we must readjust the amount of
4641 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004642 * variations. Note that the current state is >= HTTP_MSG_BODY,
4643 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004644 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004645 old_o += chn->buf->i - old_i;
4646 b_adv(chn->buf, old_o);
4647 txn->req.next -= old_o;
4648 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004649 }
4650
Mark Lamourinec2247f02012-01-04 13:02:01 -05004651 return 0;
4652}
4653
Willy Tarreau610ecce2010-01-04 21:15:02 +01004654/* Terminate current transaction and prepare a new one. This is very tricky
4655 * right now but it works.
4656 */
4657void http_end_txn_clean_session(struct session *s)
4658{
Willy Tarreau068621e2013-12-23 15:11:25 +01004659 int prev_status = s->txn.status;
4660
Willy Tarreau610ecce2010-01-04 21:15:02 +01004661 /* FIXME: We need a more portable way of releasing a backend's and a
4662 * server's connections. We need a safer way to reinitialize buffer
4663 * flags. We also need a more accurate method for computing per-request
4664 * data.
4665 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004666
Willy Tarreau4213a112013-12-15 10:25:42 +01004667 /* unless we're doing keep-alive, we want to quickly close the connection
4668 * to the server.
4669 */
4670 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4671 !si_conn_ready(s->req->cons)) {
4672 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4673 si_shutr(s->req->cons);
4674 si_shutw(s->req->cons);
4675 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004676
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004677 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004678 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004679 if (unlikely(s->srv_conn))
4680 sess_change_server(s, NULL);
4681 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004682
4683 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4684 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004685 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004686
4687 if (s->txn.status) {
4688 int n;
4689
4690 n = s->txn.status / 100;
4691 if (n < 1 || n > 5)
4692 n = 0;
4693
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004694 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004695 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004696 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004697 s->fe->fe_counters.p.http.comp_rsp++;
4698 }
Willy Tarreau24657792010-02-26 10:30:28 +01004699 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004700 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004701 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004702 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004703 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004704 s->be->be_counters.p.http.comp_rsp++;
4705 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004706 }
4707
4708 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004709 s->logs.bytes_in -= s->req->buf->i;
4710 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004711
4712 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004713 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004714 !(s->flags & SN_MONITOR) &&
4715 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4716 s->do_log(s);
4717 }
4718
4719 s->logs.accept_date = date; /* user-visible date for logging */
4720 s->logs.tv_accept = now; /* corrected date for internal use */
4721 tv_zero(&s->logs.tv_request);
4722 s->logs.t_queue = -1;
4723 s->logs.t_connect = -1;
4724 s->logs.t_data = -1;
4725 s->logs.t_close = 0;
4726 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4727 s->logs.srv_queue_size = 0; /* we will get this number soon */
4728
Willy Tarreau9b28e032012-10-12 23:49:43 +02004729 s->logs.bytes_in = s->req->total = s->req->buf->i;
4730 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004731
4732 if (s->pend_pos)
4733 pendconn_free(s->pend_pos);
4734
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004735 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004736 if (s->flags & SN_CURR_SESS) {
4737 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004738 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004739 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004740 if (may_dequeue_tasks(objt_server(s->target), s->be))
4741 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004742 }
4743
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004744 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004745
Willy Tarreau4213a112013-12-15 10:25:42 +01004746 /* only release our endpoint if we don't intend to reuse the
4747 * connection.
4748 */
4749 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4750 !si_conn_ready(s->req->cons)) {
4751 si_release_endpoint(s->req->cons);
4752 }
4753
Willy Tarreau610ecce2010-01-04 21:15:02 +01004754 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004755 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004756 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004757 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004758 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004759 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4760 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);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004761 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 +01004762 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004763
Willy Tarreau610ecce2010-01-04 21:15:02 +01004764 s->txn.meth = 0;
4765 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004766 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004767
4768 if (prev_status == 401 || prev_status == 407) {
4769 /* In HTTP keep-alive mode, if we receive a 401, we still have
4770 * a chance of being able to send the visitor again to the same
4771 * server over the same connection. This is required by some
4772 * broken protocols such as NTLM, and anyway whenever there is
4773 * an opportunity for sending the challenge to the proper place,
4774 * it's better to do it (at least it helps with debugging).
4775 */
4776 s->txn.flags |= TX_PREFER_LAST;
4777 }
4778
Willy Tarreauee55dc02010-06-01 10:56:34 +02004779 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004780 s->req->cons->flags |= SI_FL_INDEP_STR;
4781
Willy Tarreau96e31212011-05-30 18:10:30 +02004782 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004783 s->req->flags |= CF_NEVER_WAIT;
4784 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004785 }
4786
Willy Tarreau610ecce2010-01-04 21:15:02 +01004787 /* if the request buffer is not empty, it means we're
4788 * about to process another request, so send pending
4789 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004790 * Just don't do this if the buffer is close to be full,
4791 * because the request will wait for it to flush a little
4792 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004793 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004794 if (s->req->buf->i) {
4795 if (s->rep->buf->o &&
4796 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4797 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004798 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004799 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004800
4801 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004802 channel_auto_read(s->req);
4803 channel_auto_close(s->req);
4804 channel_auto_read(s->rep);
4805 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004806
Willy Tarreau27375622013-12-17 00:00:28 +01004807 /* we're in keep-alive with an idle connection, monitor it */
4808 si_idle_conn(s->req->cons);
4809
Willy Tarreau342b11c2010-11-24 16:22:09 +01004810 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004811 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004812}
4813
4814
4815/* This function updates the request state machine according to the response
4816 * state machine and buffer flags. It returns 1 if it changes anything (flag
4817 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4818 * it is only used to find when a request/response couple is complete. Both
4819 * this function and its equivalent should loop until both return zero. It
4820 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4821 */
4822int http_sync_req_state(struct session *s)
4823{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004824 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004825 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004826 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004827 unsigned int old_state = txn->req.msg_state;
4828
Willy Tarreau610ecce2010-01-04 21:15:02 +01004829 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4830 return 0;
4831
4832 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004833 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004834 * We can shut the read side unless we want to abort_on_close,
4835 * or we have a POST request. The issue with POST requests is
4836 * that some browsers still send a CRLF after the request, and
4837 * this CRLF must be read so that it does not remain in the kernel
4838 * buffers, otherwise a close could cause an RST on some systems
4839 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004840 * Note that if we're using keep-alive on the client side, we'd
4841 * rather poll now and keep the polling enabled for the whole
4842 * session's life than enabling/disabling it between each
4843 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004844 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004845 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4846 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4847 !(s->be->options & PR_O_ABRT_CLOSE) &&
4848 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004849 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004850
Willy Tarreau40f151a2012-12-20 12:10:09 +01004851 /* if the server closes the connection, we want to immediately react
4852 * and close the socket to save packets and syscalls.
4853 */
4854 chn->cons->flags |= SI_FL_NOHALF;
4855
Willy Tarreau610ecce2010-01-04 21:15:02 +01004856 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4857 goto wait_other_side;
4858
4859 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4860 /* The server has not finished to respond, so we
4861 * don't want to move in order not to upset it.
4862 */
4863 goto wait_other_side;
4864 }
4865
4866 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4867 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004868 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004869 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004870 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004871 goto wait_other_side;
4872 }
4873
4874 /* When we get here, it means that both the request and the
4875 * response have finished receiving. Depending on the connection
4876 * mode, we'll have to wait for the last bytes to leave in either
4877 * direction, and sometimes for a close to be effective.
4878 */
4879
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004880 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4881 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004882 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4883 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004884 }
4885 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4886 /* Option forceclose is set, or either side wants to close,
4887 * let's enforce it now that we're not expecting any new
4888 * data to come. The caller knows the session is complete
4889 * once both states are CLOSED.
4890 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004891 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4892 channel_shutr_now(chn);
4893 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004894 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004895 }
4896 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004897 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4898 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004899 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004900 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4901 channel_auto_read(chn);
4902 txn->req.msg_state = HTTP_MSG_TUNNEL;
4903 chn->flags |= CF_NEVER_WAIT;
4904 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004905 }
4906
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004907 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004908 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004909 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004910
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004911 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004912 txn->req.msg_state = HTTP_MSG_CLOSING;
4913 goto http_msg_closing;
4914 }
4915 else {
4916 txn->req.msg_state = HTTP_MSG_CLOSED;
4917 goto http_msg_closed;
4918 }
4919 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004920 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004921 }
4922
4923 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4924 http_msg_closing:
4925 /* nothing else to forward, just waiting for the output buffer
4926 * to be empty and for the shutw_now to take effect.
4927 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004928 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004929 txn->req.msg_state = HTTP_MSG_CLOSED;
4930 goto http_msg_closed;
4931 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004932 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004933 txn->req.msg_state = HTTP_MSG_ERROR;
4934 goto wait_other_side;
4935 }
4936 }
4937
4938 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4939 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004940 /* see above in MSG_DONE why we only do this in these states */
4941 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4942 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4943 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004944 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004945 goto wait_other_side;
4946 }
4947
4948 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004949 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004950}
4951
4952
4953/* This function updates the response state machine according to the request
4954 * state machine and buffer flags. It returns 1 if it changes anything (flag
4955 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4956 * it is only used to find when a request/response couple is complete. Both
4957 * this function and its equivalent should loop until both return zero. It
4958 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4959 */
4960int http_sync_res_state(struct session *s)
4961{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004962 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004963 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004964 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004965 unsigned int old_state = txn->rsp.msg_state;
4966
Willy Tarreau610ecce2010-01-04 21:15:02 +01004967 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4968 return 0;
4969
4970 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4971 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004972 * still monitor the server connection for a possible close
4973 * while the request is being uploaded, so we don't disable
4974 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004975 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004976 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004977
4978 if (txn->req.msg_state == HTTP_MSG_ERROR)
4979 goto wait_other_side;
4980
4981 if (txn->req.msg_state < HTTP_MSG_DONE) {
4982 /* The client seems to still be sending data, probably
4983 * because we got an error response during an upload.
4984 * We have the choice of either breaking the connection
4985 * or letting it pass through. Let's do the later.
4986 */
4987 goto wait_other_side;
4988 }
4989
4990 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4991 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004992 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004993 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004994 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004995 goto wait_other_side;
4996 }
4997
4998 /* When we get here, it means that both the request and the
4999 * response have finished receiving. Depending on the connection
5000 * mode, we'll have to wait for the last bytes to leave in either
5001 * direction, and sometimes for a close to be effective.
5002 */
5003
5004 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5005 /* Server-close mode : shut read and wait for the request
5006 * side to close its output buffer. The caller will detect
5007 * when we're in DONE and the other is in CLOSED and will
5008 * catch that for the final cleanup.
5009 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005010 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
5011 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005012 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005013 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5014 /* Option forceclose is set, or either side wants to close,
5015 * let's enforce it now that we're not expecting any new
5016 * data to come. The caller knows the session is complete
5017 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005018 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005019 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
5020 channel_shutr_now(chn);
5021 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01005022 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005023 }
5024 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01005025 /* The last possible modes are keep-alive and tunnel. Tunnel will
5026 * need to forward remaining data. Keep-alive will need to monitor
5027 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005028 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005029 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02005030 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01005031 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
5032 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005033 }
5034
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005035 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005036 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005037 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005038 txn->rsp.msg_state = HTTP_MSG_CLOSING;
5039 goto http_msg_closing;
5040 }
5041 else {
5042 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5043 goto http_msg_closed;
5044 }
5045 }
5046 goto wait_other_side;
5047 }
5048
5049 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
5050 http_msg_closing:
5051 /* nothing else to forward, just waiting for the output buffer
5052 * to be empty and for the shutw_now to take effect.
5053 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005054 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005055 txn->rsp.msg_state = HTTP_MSG_CLOSED;
5056 goto http_msg_closed;
5057 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005058 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005059 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005060 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005061 if (objt_server(s->target))
5062 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005063 goto wait_other_side;
5064 }
5065 }
5066
5067 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
5068 http_msg_closed:
5069 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005070 bi_erase(chn);
5071 channel_auto_close(chn);
5072 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005073 goto wait_other_side;
5074 }
5075
5076 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02005077 /* We force the response to leave immediately if we're waiting for the
5078 * other side, since there is no pending shutdown to push it out.
5079 */
5080 if (!channel_is_empty(chn))
5081 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02005082 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005083}
5084
5085
5086/* Resync the request and response state machines. Return 1 if either state
5087 * changes.
5088 */
5089int http_resync_states(struct session *s)
5090{
5091 struct http_txn *txn = &s->txn;
5092 int old_req_state = txn->req.msg_state;
5093 int old_res_state = txn->rsp.msg_state;
5094
Willy Tarreau610ecce2010-01-04 21:15:02 +01005095 http_sync_req_state(s);
5096 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005097 if (!http_sync_res_state(s))
5098 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005099 if (!http_sync_req_state(s))
5100 break;
5101 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02005102
Willy Tarreau610ecce2010-01-04 21:15:02 +01005103 /* OK, both state machines agree on a compatible state.
5104 * There are a few cases we're interested in :
5105 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
5106 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
5107 * directions, so let's simply disable both analysers.
5108 * - HTTP_MSG_CLOSED on the response only means we must abort the
5109 * request.
5110 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
5111 * with server-close mode means we've completed one request and we
5112 * must re-initialize the server connection.
5113 */
5114
5115 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
5116 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
5117 (txn->req.msg_state == HTTP_MSG_CLOSED &&
5118 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
5119 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005120 channel_auto_close(s->req);
5121 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005122 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005123 channel_auto_close(s->rep);
5124 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005125 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01005126 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
5127 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005128 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01005129 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01005130 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005131 channel_auto_close(s->rep);
5132 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01005133 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005134 channel_abort(s->req);
5135 channel_auto_close(s->req);
5136 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005137 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005138 }
Willy Tarreau4213a112013-12-15 10:25:42 +01005139 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
5140 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01005141 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01005142 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
5143 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
5144 /* server-close/keep-alive: terminate this transaction,
5145 * possibly killing the server connection and reinitialize
5146 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01005147 */
5148 http_end_txn_clean_session(s);
5149 }
5150
Willy Tarreau610ecce2010-01-04 21:15:02 +01005151 return txn->req.msg_state != old_req_state ||
5152 txn->rsp.msg_state != old_res_state;
5153}
5154
Willy Tarreaud98cf932009-12-27 22:54:55 +01005155/* This function is an analyser which forwards request body (including chunk
5156 * sizes if any). It is called as soon as we must forward, even if we forward
5157 * zero byte. The only situation where it must not be called is when we're in
5158 * tunnel mode and we want to forward till the close. It's used both to forward
5159 * remaining data and to resync after end of body. It expects the msg_state to
5160 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5161 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005162 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02005163 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005164 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005165int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005166{
5167 struct http_txn *txn = &s->txn;
5168 struct http_msg *msg = &s->txn.req;
5169
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005170 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5171 return 0;
5172
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005173 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005174 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005175 /* Output closed while we were sending data. We must abort and
5176 * wake the other side up.
5177 */
5178 msg->msg_state = HTTP_MSG_ERROR;
5179 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005180 return 1;
5181 }
5182
Willy Tarreaud98cf932009-12-27 22:54:55 +01005183 /* Note that we don't have to send 100-continue back because we don't
5184 * need the data to complete our job, and it's up to the server to
5185 * decide whether to return 100, 417 or anything else in return of
5186 * an "Expect: 100-continue" header.
5187 */
5188
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005189 if (msg->sov) {
5190 /* we have msg->sov which points to the first byte of message
5191 * body, and req->buf.p still points to the beginning of the
5192 * message. We forward the headers now, as we don't need them
5193 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005194 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005195 b_adv(req->buf, msg->sov);
5196 msg->next -= msg->sov;
5197 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005198
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005199 /* The previous analysers guarantee that the state is somewhere
5200 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5201 * msg->next are always correct.
5202 */
5203 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5204 if (msg->flags & HTTP_MSGF_TE_CHNK)
5205 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5206 else
5207 msg->msg_state = HTTP_MSG_DATA;
5208 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005209 }
5210
Willy Tarreau7ba23542014-04-17 21:50:00 +02005211 /* Some post-connect processing might want us to refrain from starting to
5212 * forward data. Currently, the only reason for this is "balance url_param"
5213 * whichs need to parse/process the request after we've enabled forwarding.
5214 */
5215 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5216 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5217 channel_auto_connect(req);
5218 goto missing_data;
5219 }
5220 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5221 }
5222
Willy Tarreau80a92c02014-03-12 10:41:13 +01005223 /* in most states, we should abort in case of early close */
5224 channel_auto_close(req);
5225
Willy Tarreauefdf0942014-04-24 20:08:57 +02005226 if (req->to_forward) {
5227 /* We can't process the buffer's contents yet */
5228 req->flags |= CF_WAKE_WRITE;
5229 goto missing_data;
5230 }
5231
Willy Tarreaud98cf932009-12-27 22:54:55 +01005232 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005233 if (msg->msg_state == HTTP_MSG_DATA) {
5234 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005235 /* we may have some pending data starting at req->buf->p */
5236 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005237 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005238 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005239 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005240 msg->next += msg->chunk_len;
5241 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005242
5243 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005244 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005245 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005246 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005247 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005248 }
5249 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005250 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005251 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005252 * TRAILERS state.
5253 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005254 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005255
Willy Tarreau54d23df2012-10-25 19:04:45 +02005256 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005257 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005258 else if (ret < 0) {
5259 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005260 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005261 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005262 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005263 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005264 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005265 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005266 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005267 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005268 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005269
5270 if (ret == 0)
5271 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005272 else if (ret < 0) {
5273 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005274 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005275 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005276 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005277 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005278 /* we're in MSG_CHUNK_SIZE now */
5279 }
5280 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005281 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005282
5283 if (ret == 0)
5284 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005285 else if (ret < 0) {
5286 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005287 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005288 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005289 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005290 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005291 /* we're in HTTP_MSG_DONE now */
5292 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005293 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005294 int old_state = msg->msg_state;
5295
Willy Tarreau610ecce2010-01-04 21:15:02 +01005296 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005297
5298 /* we may have some pending data starting at req->buf->p
5299 * such as last chunk of data or trailers.
5300 */
5301 b_adv(req->buf, msg->next);
5302 msg->next = 0;
5303
Willy Tarreau4fe41902010-06-07 22:27:41 +02005304 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005305 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5306 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005307 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005308 if (http_resync_states(s)) {
5309 /* some state changes occurred, maybe the analyser
5310 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005311 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005312 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005313 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005314 /* request errors are most likely due to
5315 * the server aborting the transfer.
5316 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005317 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005318 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005319 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005320 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005321 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005322 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005323 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005324 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005325
5326 /* If "option abortonclose" is set on the backend, we
5327 * want to monitor the client's connection and forward
5328 * any shutdown notification to the server, which will
5329 * decide whether to close or to go on processing the
5330 * request.
5331 */
5332 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005333 channel_auto_read(req);
5334 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005335 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005336 else if (s->txn.meth == HTTP_METH_POST) {
5337 /* POST requests may require to read extra CRLF
5338 * sent by broken browsers and which could cause
5339 * an RST to be sent upon close on some systems
5340 * (eg: Linux).
5341 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005342 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005343 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005344
Willy Tarreau610ecce2010-01-04 21:15:02 +01005345 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005346 }
5347 }
5348
Willy Tarreaud98cf932009-12-27 22:54:55 +01005349 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005350 /* we may have some pending data starting at req->buf->p */
5351 b_adv(req->buf, msg->next);
5352 msg->next = 0;
5353 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5354
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005355 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005356 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005357 if (!(s->flags & SN_ERR_MASK))
5358 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005359 if (!(s->flags & SN_FINST_MASK)) {
5360 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5361 s->flags |= SN_FINST_H;
5362 else
5363 s->flags |= SN_FINST_D;
5364 }
5365
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005366 s->fe->fe_counters.cli_aborts++;
5367 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005368 if (objt_server(s->target))
5369 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005370
5371 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005372 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005373
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005374 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005375 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005376 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005377
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005378 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005379 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005380 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005381 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005382 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005383
Willy Tarreau5c620922011-05-11 19:56:11 +02005384 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005385 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005386 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005387 * modes are already handled by the stream sock layer. We must not do
5388 * this in content-length mode because it could present the MSG_MORE
5389 * flag with the last block of forwarded data, which would cause an
5390 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005391 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005392 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005393 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005394
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005395 return 0;
5396
Willy Tarreaud98cf932009-12-27 22:54:55 +01005397 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005398 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005399 if (s->listener->counters)
5400 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005401
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005402 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005403 /* we may have some pending data starting at req->buf->p */
5404 b_adv(req->buf, msg->next);
5405 msg->next = 0;
5406
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005407 txn->req.msg_state = HTTP_MSG_ERROR;
5408 if (txn->status) {
5409 /* Note: we don't send any error if some data were already sent */
5410 stream_int_retnclose(req->prod, NULL);
5411 } else {
5412 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005413 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005414 }
5415 req->analysers = 0;
5416 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005417
5418 if (!(s->flags & SN_ERR_MASK))
5419 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005420 if (!(s->flags & SN_FINST_MASK)) {
5421 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5422 s->flags |= SN_FINST_H;
5423 else
5424 s->flags |= SN_FINST_D;
5425 }
5426 return 0;
5427
5428 aborted_xfer:
5429 txn->req.msg_state = HTTP_MSG_ERROR;
5430 if (txn->status) {
5431 /* Note: we don't send any error if some data were already sent */
5432 stream_int_retnclose(req->prod, NULL);
5433 } else {
5434 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005435 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005436 }
5437 req->analysers = 0;
5438 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5439
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005440 s->fe->fe_counters.srv_aborts++;
5441 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005442 if (objt_server(s->target))
5443 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005444
5445 if (!(s->flags & SN_ERR_MASK))
5446 s->flags |= SN_ERR_SRVCL;
5447 if (!(s->flags & SN_FINST_MASK)) {
5448 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5449 s->flags |= SN_FINST_H;
5450 else
5451 s->flags |= SN_FINST_D;
5452 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005453 return 0;
5454}
5455
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005456/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5457 * processing can continue on next analysers, or zero if it either needs more
5458 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5459 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5460 * when it has nothing left to do, and may remove any analyser when it wants to
5461 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005462 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005463int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005464{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005465 struct http_txn *txn = &s->txn;
5466 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005467 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005468 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005469 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005470 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005471
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005472 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 +02005473 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005474 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005475 rep,
5476 rep->rex, rep->wex,
5477 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005478 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005479 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005480
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005481 /*
5482 * Now parse the partial (or complete) lines.
5483 * We will check the response syntax, and also join multi-line
5484 * headers. An index of all the lines will be elaborated while
5485 * parsing.
5486 *
5487 * For the parsing, we use a 28 states FSM.
5488 *
5489 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005490 * rep->buf->p = beginning of response
5491 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5492 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005493 * msg->eol = end of current header or line (LF or CRLF)
5494 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005495 */
5496
Willy Tarreau628c40c2014-04-24 19:11:26 +02005497 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005498 /* There's a protected area at the end of the buffer for rewriting
5499 * purposes. We don't want to start to parse the request if the
5500 * protected area is affected, because we may have to move processed
5501 * data later, which is much more complicated.
5502 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005503 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005504 if (unlikely(!channel_reserved(rep))) {
5505 /* some data has still not left the buffer, wake us once that's done */
5506 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5507 goto abort_response;
5508 channel_dont_close(rep);
5509 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005510 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005511 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005512 }
5513
Willy Tarreau379357a2013-06-08 12:55:46 +02005514 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5515 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5516 buffer_slow_realign(rep->buf);
5517
Willy Tarreau9b28e032012-10-12 23:49:43 +02005518 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005519 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005520 }
5521
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005522 /* 1: we might have to print this header in debug mode */
5523 if (unlikely((global.mode & MODE_DEBUG) &&
5524 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005525 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005526 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005527
Willy Tarreau9b28e032012-10-12 23:49:43 +02005528 sol = rep->buf->p;
5529 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005530 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005531
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005532 sol += hdr_idx_first_pos(&txn->hdr_idx);
5533 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005534
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005535 while (cur_idx) {
5536 eol = sol + txn->hdr_idx.v[cur_idx].len;
5537 debug_hdr("srvhdr", s, sol, eol);
5538 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5539 cur_idx = txn->hdr_idx.v[cur_idx].next;
5540 }
5541 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005542
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005543 /*
5544 * Now we quickly check if we have found a full valid response.
5545 * If not so, we check the FD and buffer states before leaving.
5546 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005547 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005548 * responses are checked first.
5549 *
5550 * Depending on whether the client is still there or not, we
5551 * may send an error response back or not. Note that normally
5552 * we should only check for HTTP status there, and check I/O
5553 * errors somewhere else.
5554 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005555
Willy Tarreau655dce92009-11-08 13:10:58 +01005556 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005557 /* Invalid response */
5558 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5559 /* we detected a parsing error. We want to archive this response
5560 * in the dedicated proxy area for later troubleshooting.
5561 */
5562 hdr_response_bad:
5563 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005564 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005565
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005566 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005567 if (objt_server(s->target)) {
5568 objt_server(s->target)->counters.failed_resp++;
5569 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005570 }
Willy Tarreau64648412010-03-05 10:41:54 +01005571 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005572 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005573 rep->analysers = 0;
5574 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005575 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005576 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005577 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005578
5579 if (!(s->flags & SN_ERR_MASK))
5580 s->flags |= SN_ERR_PRXCOND;
5581 if (!(s->flags & SN_FINST_MASK))
5582 s->flags |= SN_FINST_H;
5583
5584 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005585 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005586
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005587 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005588 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005589 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005590 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005591 goto hdr_response_bad;
5592 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005593
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005594 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005595 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005596 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005597 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005598 else if (txn->flags & TX_NOT_FIRST)
5599 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005600
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005601 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005602 if (objt_server(s->target)) {
5603 objt_server(s->target)->counters.failed_resp++;
5604 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005605 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005606
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005607 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005608 rep->analysers = 0;
5609 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005610 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005611 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005612 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005613
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005614 if (!(s->flags & SN_ERR_MASK))
5615 s->flags |= SN_ERR_SRVCL;
5616 if (!(s->flags & SN_FINST_MASK))
5617 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005618 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005619 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005620
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005621 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005622 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005623 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005624 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005625 else if (txn->flags & TX_NOT_FIRST)
5626 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005627
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005628 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005629 if (objt_server(s->target)) {
5630 objt_server(s->target)->counters.failed_resp++;
5631 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005632 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005633
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005634 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005635 rep->analysers = 0;
5636 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005637 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005638 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005639 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005640
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005641 if (!(s->flags & SN_ERR_MASK))
5642 s->flags |= SN_ERR_SRVTO;
5643 if (!(s->flags & SN_FINST_MASK))
5644 s->flags |= SN_FINST_H;
5645 return 0;
5646 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005647
Willy Tarreauf003d372012-11-26 13:35:37 +01005648 /* client abort with an abortonclose */
5649 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5650 s->fe->fe_counters.cli_aborts++;
5651 s->be->be_counters.cli_aborts++;
5652 if (objt_server(s->target))
5653 objt_server(s->target)->counters.cli_aborts++;
5654
5655 rep->analysers = 0;
5656 channel_auto_close(rep);
5657
5658 txn->status = 400;
5659 bi_erase(rep);
5660 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5661
5662 if (!(s->flags & SN_ERR_MASK))
5663 s->flags |= SN_ERR_CLICL;
5664 if (!(s->flags & SN_FINST_MASK))
5665 s->flags |= SN_FINST_H;
5666
5667 /* process_session() will take care of the error */
5668 return 0;
5669 }
5670
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005671 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005672 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005673 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005674 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005675 else if (txn->flags & TX_NOT_FIRST)
5676 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005677
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005678 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005679 if (objt_server(s->target)) {
5680 objt_server(s->target)->counters.failed_resp++;
5681 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005682 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005683
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005684 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005685 rep->analysers = 0;
5686 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005687 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005688 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005689 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005690
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005691 if (!(s->flags & SN_ERR_MASK))
5692 s->flags |= SN_ERR_SRVCL;
5693 if (!(s->flags & SN_FINST_MASK))
5694 s->flags |= SN_FINST_H;
5695 return 0;
5696 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005697
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005698 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005699 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005700 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005701 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005702 else if (txn->flags & TX_NOT_FIRST)
5703 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005704
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005705 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005706 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005707 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005708
5709 if (!(s->flags & SN_ERR_MASK))
5710 s->flags |= SN_ERR_CLICL;
5711 if (!(s->flags & SN_FINST_MASK))
5712 s->flags |= SN_FINST_H;
5713
5714 /* process_session() will take care of the error */
5715 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005716 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005717
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005718 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005719 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005720 return 0;
5721 }
5722
5723 /* More interesting part now : we know that we have a complete
5724 * response which at least looks like HTTP. We have an indicator
5725 * of each header's length, so we can parse them quickly.
5726 */
5727
5728 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005729 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005730
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005731 /*
5732 * 1: get the status code
5733 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005734 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005735 if (n < 1 || n > 5)
5736 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005737 /* when the client triggers a 4xx from the server, it's most often due
5738 * to a missing object or permission. These events should be tracked
5739 * because if they happen often, it may indicate a brute force or a
5740 * vulnerability scan.
5741 */
5742 if (n == 4)
5743 session_inc_http_err_ctr(s);
5744
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005745 if (objt_server(s->target))
5746 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005747
Willy Tarreau5b154472009-12-21 20:11:07 +01005748 /* check if the response is HTTP/1.1 or above */
5749 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005750 ((rep->buf->p[5] > '1') ||
5751 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005752 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005753
5754 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005755 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 +01005756
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005757 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005758 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005759
Willy Tarreau9b28e032012-10-12 23:49:43 +02005760 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005761
Willy Tarreau39650402010-03-15 19:44:39 +01005762 /* Adjust server's health based on status code. Note: status codes 501
5763 * and 505 are triggered on demand by client request, so we must not
5764 * count them as server failures.
5765 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005766 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005767 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005768 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005769 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005770 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005771 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005772
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005773 /*
5774 * 2: check for cacheability.
5775 */
5776
5777 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005778 case 100:
5779 /*
5780 * We may be facing a 100-continue response, in which case this
5781 * is not the right response, and we're waiting for the next one.
5782 * Let's allow this response to go to the client and wait for the
5783 * next one.
5784 */
5785 hdr_idx_init(&txn->hdr_idx);
5786 msg->next -= channel_forward(rep, msg->next);
5787 msg->msg_state = HTTP_MSG_RPBEFORE;
5788 txn->status = 0;
5789 s->logs.t_data = -1; /* was not a response yet */
5790 goto next_one;
5791
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005792 case 200:
5793 case 203:
5794 case 206:
5795 case 300:
5796 case 301:
5797 case 410:
5798 /* RFC2616 @13.4:
5799 * "A response received with a status code of
5800 * 200, 203, 206, 300, 301 or 410 MAY be stored
5801 * by a cache (...) unless a cache-control
5802 * directive prohibits caching."
5803 *
5804 * RFC2616 @9.5: POST method :
5805 * "Responses to this method are not cacheable,
5806 * unless the response includes appropriate
5807 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005808 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005809 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005810 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005811 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5812 break;
5813 default:
5814 break;
5815 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005816
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005817 /*
5818 * 3: we may need to capture headers
5819 */
5820 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005821 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005822 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005823 txn->rsp.cap, s->fe->rsp_cap);
5824
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005825 /* 4: determine the transfer-length.
5826 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5827 * the presence of a message-body in a RESPONSE and its transfer length
5828 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005829 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005830 * All responses to the HEAD request method MUST NOT include a
5831 * message-body, even though the presence of entity-header fields
5832 * might lead one to believe they do. All 1xx (informational), 204
5833 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5834 * message-body. All other responses do include a message-body,
5835 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005836 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005837 * 1. Any response which "MUST NOT" include a message-body (such as the
5838 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5839 * always terminated by the first empty line after the header fields,
5840 * regardless of the entity-header fields present in the message.
5841 *
5842 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5843 * the "chunked" transfer-coding (Section 6.2) is used, the
5844 * transfer-length is defined by the use of this transfer-coding.
5845 * If a Transfer-Encoding header field is present and the "chunked"
5846 * transfer-coding is not present, the transfer-length is defined by
5847 * the sender closing the connection.
5848 *
5849 * 3. If a Content-Length header field is present, its decimal value in
5850 * OCTETs represents both the entity-length and the transfer-length.
5851 * If a message is received with both a Transfer-Encoding header
5852 * field and a Content-Length header field, the latter MUST be ignored.
5853 *
5854 * 4. If the message uses the media type "multipart/byteranges", and
5855 * the transfer-length is not otherwise specified, then this self-
5856 * delimiting media type defines the transfer-length. This media
5857 * type MUST NOT be used unless the sender knows that the recipient
5858 * can parse it; the presence in a request of a Range header with
5859 * multiple byte-range specifiers from a 1.1 client implies that the
5860 * client can parse multipart/byteranges responses.
5861 *
5862 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005863 */
5864
5865 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005866 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005867 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005868 * FIXME: should we parse anyway and return an error on chunked encoding ?
5869 */
5870 if (txn->meth == HTTP_METH_HEAD ||
5871 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005872 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005873 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005874 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005875 goto skip_content_length;
5876 }
5877
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005878 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005879 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005880 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005881 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005882 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005883 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5884 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005885 /* bad transfer-encoding (chunked followed by something else) */
5886 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005887 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005888 break;
5889 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005890 }
5891
5892 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5893 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005894 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005895 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005896 signed long long cl;
5897
Willy Tarreauad14f752011-09-02 20:33:27 +02005898 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005899 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005900 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005901 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005902
Willy Tarreauad14f752011-09-02 20:33:27 +02005903 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005904 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005905 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005906 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005907
Willy Tarreauad14f752011-09-02 20:33:27 +02005908 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005909 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005910 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005911 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005912
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005913 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005914 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005915 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005916 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005917
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005918 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005919 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005920 }
5921
William Lallemand82fe75c2012-10-23 10:25:10 +02005922 if (s->fe->comp || s->be->comp)
5923 select_compression_response_header(s, rep->buf);
5924
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005925skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005926 /* Now we have to check if we need to modify the Connection header.
5927 * This is more difficult on the response than it is on the request,
5928 * because we can have two different HTTP versions and we don't know
5929 * how the client will interprete a response. For instance, let's say
5930 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5931 * HTTP/1.1 response without any header. Maybe it will bound itself to
5932 * HTTP/1.0 because it only knows about it, and will consider the lack
5933 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5934 * the lack of header as a keep-alive. Thus we will use two flags
5935 * indicating how a request MAY be understood by the client. In case
5936 * of multiple possibilities, we'll fix the header to be explicit. If
5937 * ambiguous cases such as both close and keepalive are seen, then we
5938 * will fall back to explicit close. Note that we won't take risks with
5939 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005940 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005941 */
5942
Willy Tarreaudc008c52010-02-01 16:20:08 +01005943 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5944 txn->status == 101)) {
5945 /* Either we've established an explicit tunnel, or we're
5946 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005947 * to understand the next protocols. We have to switch to tunnel
5948 * mode, so that we transfer the request and responses then let
5949 * this protocol pass unmodified. When we later implement specific
5950 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005951 * header which contains information about that protocol for
5952 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005953 */
5954 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5955 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005956 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5957 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005958 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5959 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005960 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005961
Willy Tarreau70dffda2014-01-30 03:07:23 +01005962 /* this situation happens when combining pretend-keepalive with httpclose. */
5963 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005964 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5965 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005966 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5967
Willy Tarreau60466522010-01-18 19:08:45 +01005968 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005969 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005970 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5971 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005972
Willy Tarreau60466522010-01-18 19:08:45 +01005973 /* now adjust header transformations depending on current state */
5974 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5975 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5976 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005977 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005978 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005979 }
Willy Tarreau60466522010-01-18 19:08:45 +01005980 else { /* SCL / KAL */
5981 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005982 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005983 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005984 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005985
Willy Tarreau60466522010-01-18 19:08:45 +01005986 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005987 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005988
Willy Tarreau60466522010-01-18 19:08:45 +01005989 /* Some keep-alive responses are converted to Server-close if
5990 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005991 */
Willy Tarreau60466522010-01-18 19:08:45 +01005992 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5993 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005994 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005995 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005996 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005997 }
5998
Willy Tarreau7959a552013-09-23 16:44:27 +02005999 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006000 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02006001
Willy Tarreauf118d9f2014-04-24 18:26:08 +02006002 /* end of job, return OK */
6003 rep->analysers &= ~an_bit;
6004 rep->analyse_exp = TICK_ETERNITY;
6005 channel_auto_close(rep);
6006 return 1;
6007
6008 abort_keep_alive:
6009 /* A keep-alive request to the server failed on a network error.
6010 * The client is required to retry. We need to close without returning
6011 * any other information so that the client retries.
6012 */
6013 txn->status = 0;
6014 rep->analysers = 0;
6015 s->req->analysers = 0;
6016 channel_auto_close(rep);
6017 s->logs.logwait = 0;
6018 s->logs.level = 0;
6019 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
6020 bi_erase(rep);
6021 stream_int_retnclose(rep->cons, NULL);
6022 return 0;
6023}
6024
6025/* This function performs all the processing enabled for the current response.
6026 * It normally returns 1 unless it wants to break. It relies on buffers flags,
6027 * and updates s->rep->analysers. It might make sense to explode it into several
6028 * other functions. It works like process_request (see indications above).
6029 */
6030int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
6031{
6032 struct http_txn *txn = &s->txn;
6033 struct http_msg *msg = &txn->rsp;
6034 struct proxy *cur_proxy;
6035 struct cond_wordlist *wl;
6036 struct http_res_rule *http_res_last_rule = NULL;
6037
6038 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
6039 now_ms, __FUNCTION__,
6040 s,
6041 rep,
6042 rep->rex, rep->wex,
6043 rep->flags,
6044 rep->buf->i,
6045 rep->analysers);
6046
6047 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
6048 return 0;
6049
6050 rep->analysers &= ~an_bit;
6051 rep->analyse_exp = TICK_ETERNITY;
6052
Willy Tarreau70730dd2014-04-24 18:06:27 +02006053 /* The stats applet needs to adjust the Connection header but we don't
6054 * apply any filter there.
6055 */
6056 if (unlikely(objt_applet(s->target) == &http_stats_applet))
6057 goto skip_filters;
6058
Willy Tarreau58975672014-04-24 21:13:57 +02006059 /*
6060 * We will have to evaluate the filters.
6061 * As opposed to version 1.2, now they will be evaluated in the
6062 * filters order and not in the header order. This means that
6063 * each filter has to be validated among all headers.
6064 *
6065 * Filters are tried with ->be first, then with ->fe if it is
6066 * different from ->be.
6067 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006068
Willy Tarreau58975672014-04-24 21:13:57 +02006069 cur_proxy = s->be;
6070 while (1) {
6071 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006072
Willy Tarreau58975672014-04-24 21:13:57 +02006073 /* evaluate http-response rules */
6074 if (!http_res_last_rule)
6075 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 +02006076
Willy Tarreau58975672014-04-24 21:13:57 +02006077 /* try headers filters */
6078 if (rule_set->rsp_exp != NULL) {
6079 if (apply_filters_to_response(s, rep, rule_set) < 0) {
6080 return_bad_resp:
6081 if (objt_server(s->target)) {
6082 objt_server(s->target)->counters.failed_resp++;
6083 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01006084 }
Willy Tarreau58975672014-04-24 21:13:57 +02006085 s->be->be_counters.failed_resp++;
6086 return_srv_prx_502:
6087 rep->analysers = 0;
6088 txn->status = 502;
6089 s->logs.t_data = -1; /* was not a valid response */
6090 rep->prod->flags |= SI_FL_NOLINGER;
6091 bi_erase(rep);
6092 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
6093 if (!(s->flags & SN_ERR_MASK))
6094 s->flags |= SN_ERR_PRXCOND;
6095 if (!(s->flags & SN_FINST_MASK))
6096 s->flags |= SN_FINST_H;
6097 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006098 }
Willy Tarreau58975672014-04-24 21:13:57 +02006099 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006100
Willy Tarreau58975672014-04-24 21:13:57 +02006101 /* has the response been denied ? */
6102 if (txn->flags & TX_SVDENY) {
6103 if (objt_server(s->target))
6104 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006105
Willy Tarreau58975672014-04-24 21:13:57 +02006106 s->be->be_counters.denied_resp++;
6107 s->fe->fe_counters.denied_resp++;
6108 if (s->listener->counters)
6109 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006110
Willy Tarreau58975672014-04-24 21:13:57 +02006111 goto return_srv_prx_502;
6112 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02006113
Willy Tarreau58975672014-04-24 21:13:57 +02006114 /* add response headers from the rule sets in the same order */
6115 list_for_each_entry(wl, &rule_set->rsp_add, list) {
6116 if (txn->status < 200)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006117 break;
Willy Tarreau58975672014-04-24 21:13:57 +02006118 if (wl->cond) {
6119 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
6120 ret = acl_pass(ret);
6121 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
6122 ret = !ret;
6123 if (!ret)
6124 continue;
6125 }
6126 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
6127 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006128 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006129
Willy Tarreau58975672014-04-24 21:13:57 +02006130 /* check whether we're already working on the frontend */
6131 if (cur_proxy == s->fe)
6132 break;
6133 cur_proxy = s->fe;
6134 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006135
Willy Tarreau58975672014-04-24 21:13:57 +02006136 /* OK that's all we can do for 1xx responses */
6137 if (unlikely(txn->status < 200))
6138 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006139
Willy Tarreau58975672014-04-24 21:13:57 +02006140 /*
6141 * Now check for a server cookie.
6142 */
6143 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
6144 (s->be->options & PR_O_CHK_CACHE))
6145 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006146
Willy Tarreau58975672014-04-24 21:13:57 +02006147 /*
6148 * Check for cache-control or pragma headers if required.
6149 */
6150 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
6151 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02006152
Willy Tarreau58975672014-04-24 21:13:57 +02006153 /*
6154 * Add server cookie in the response if needed
6155 */
6156 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
6157 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
6158 (!(s->flags & SN_DIRECT) ||
6159 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
6160 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
6161 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
6162 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
6163 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
6164 !(s->flags & SN_IGNORE_PRST)) {
6165 /* the server is known, it's not the one the client requested, or the
6166 * cookie's last seen date needs to be refreshed. We have to
6167 * insert a set-cookie here, except if we want to insert only on POST
6168 * requests and this one isn't. Note that servers which don't have cookies
6169 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006170 */
Willy Tarreau58975672014-04-24 21:13:57 +02006171 if (!objt_server(s->target)->cookie) {
6172 chunk_printf(&trash,
6173 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
6174 s->be->cookie_name);
6175 }
6176 else {
6177 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006178
Willy Tarreau58975672014-04-24 21:13:57 +02006179 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
6180 /* emit last_date, which is mandatory */
6181 trash.str[trash.len++] = COOKIE_DELIM_DATE;
6182 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
6183 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006184
Willy Tarreau58975672014-04-24 21:13:57 +02006185 if (s->be->cookie_maxlife) {
6186 /* emit first_date, which is either the original one or
6187 * the current date.
6188 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006189 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02006190 s30tob64(txn->cookie_first_date ?
6191 txn->cookie_first_date >> 2 :
6192 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006193 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006194 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006195 }
Willy Tarreau58975672014-04-24 21:13:57 +02006196 chunk_appendf(&trash, "; path=/");
6197 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006198
Willy Tarreau58975672014-04-24 21:13:57 +02006199 if (s->be->cookie_domain)
6200 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006201
Willy Tarreau58975672014-04-24 21:13:57 +02006202 if (s->be->ck_opts & PR_CK_HTTPONLY)
6203 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006204
Willy Tarreau58975672014-04-24 21:13:57 +02006205 if (s->be->ck_opts & PR_CK_SECURE)
6206 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006207
Willy Tarreau58975672014-04-24 21:13:57 +02006208 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6209 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006210
Willy Tarreau58975672014-04-24 21:13:57 +02006211 txn->flags &= ~TX_SCK_MASK;
6212 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6213 /* the server did not change, only the date was updated */
6214 txn->flags |= TX_SCK_UPDATED;
6215 else
6216 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006217
Willy Tarreau58975672014-04-24 21:13:57 +02006218 /* Here, we will tell an eventual cache on the client side that we don't
6219 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6220 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6221 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006222 */
Willy Tarreau58975672014-04-24 21:13:57 +02006223 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006224
Willy Tarreau58975672014-04-24 21:13:57 +02006225 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006226
Willy Tarreau58975672014-04-24 21:13:57 +02006227 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6228 "Cache-control: private", 22) < 0))
6229 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006230 }
Willy Tarreau58975672014-04-24 21:13:57 +02006231 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006232
Willy Tarreau58975672014-04-24 21:13:57 +02006233 /*
6234 * Check if result will be cacheable with a cookie.
6235 * We'll block the response if security checks have caught
6236 * nasty things such as a cacheable cookie.
6237 */
6238 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6239 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6240 (s->be->options & PR_O_CHK_CACHE)) {
6241 /* we're in presence of a cacheable response containing
6242 * a set-cookie header. We'll block it as requested by
6243 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006244 */
Willy Tarreau58975672014-04-24 21:13:57 +02006245 if (objt_server(s->target))
6246 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006247
Willy Tarreau58975672014-04-24 21:13:57 +02006248 s->be->be_counters.denied_resp++;
6249 s->fe->fe_counters.denied_resp++;
6250 if (s->listener->counters)
6251 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006252
Willy Tarreau58975672014-04-24 21:13:57 +02006253 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6254 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6255 send_log(s->be, LOG_ALERT,
6256 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6257 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6258 goto return_srv_prx_502;
6259 }
Willy Tarreau03945942009-12-22 16:50:27 +01006260
Willy Tarreau70730dd2014-04-24 18:06:27 +02006261 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006262 /*
6263 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6264 * If an "Upgrade" token is found, the header is left untouched in order
6265 * not to have to deal with some client bugs : some of them fail an upgrade
6266 * if anything but "Upgrade" is present in the Connection header.
6267 */
6268 if (!(txn->flags & TX_HDR_CONN_UPG) &&
6269 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6270 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6271 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6272 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006273
Willy Tarreau58975672014-04-24 21:13:57 +02006274 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6275 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6276 /* we want a keep-alive response here. Keep-alive header
6277 * required if either side is not 1.1.
6278 */
6279 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6280 want_flags |= TX_CON_KAL_SET;
6281 }
6282 else {
6283 /* we want a close response here. Close header required if
6284 * the server is 1.1, regardless of the client.
6285 */
6286 if (msg->flags & HTTP_MSGF_VER_11)
6287 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006288 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006289
Willy Tarreau58975672014-04-24 21:13:57 +02006290 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6291 http_change_connection_header(txn, msg, want_flags);
6292 }
6293
6294 skip_header_mangling:
6295 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6296 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6297 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006298
Willy Tarreau58975672014-04-24 21:13:57 +02006299 /* if the user wants to log as soon as possible, without counting
6300 * bytes from the server, then this is the right moment. We have
6301 * to temporarily assign bytes_out to log what we currently have.
6302 */
6303 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6304 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6305 s->logs.bytes_out = txn->rsp.eoh;
6306 s->do_log(s);
6307 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006308 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006309 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006310}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006311
Willy Tarreaud98cf932009-12-27 22:54:55 +01006312/* This function is an analyser which forwards response body (including chunk
6313 * sizes if any). It is called as soon as we must forward, even if we forward
6314 * zero byte. The only situation where it must not be called is when we're in
6315 * tunnel mode and we want to forward till the close. It's used both to forward
6316 * remaining data and to resync after end of body. It expects the msg_state to
6317 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6318 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006319 *
6320 * It is capable of compressing response data both in content-length mode and
6321 * in chunked mode. The state machines follows different flows depending on
6322 * whether content-length and chunked modes are used, since there are no
6323 * trailers in content-length :
6324 *
6325 * chk-mode cl-mode
6326 * ,----- BODY -----.
6327 * / \
6328 * V size > 0 V chk-mode
6329 * .--> SIZE -------------> DATA -------------> CRLF
6330 * | | size == 0 | last byte |
6331 * | v final crlf v inspected |
6332 * | TRAILERS -----------> DONE |
6333 * | |
6334 * `----------------------------------------------'
6335 *
6336 * Compression only happens in the DATA state, and must be flushed in final
6337 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6338 * is performed at once on final states for all bytes parsed, or when leaving
6339 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006340 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006341int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006342{
6343 struct http_txn *txn = &s->txn;
6344 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006345 static struct buffer *tmpbuf = NULL;
6346 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006347 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006348
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006349 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6350 return 0;
6351
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006352 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006353 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006354 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006355 /* Output closed while we were sending data. We must abort and
6356 * wake the other side up.
6357 */
6358 msg->msg_state = HTTP_MSG_ERROR;
6359 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006360 return 1;
6361 }
6362
Willy Tarreau4fe41902010-06-07 22:27:41 +02006363 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006364 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006365
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006366 if (msg->sov) {
6367 /* we have msg->sov which points to the first byte of message
6368 * body, and res->buf.p still points to the beginning of the
6369 * message. We forward the headers now, as we don't need them
6370 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006371 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006372 b_adv(res->buf, msg->sov);
6373 msg->next -= msg->sov;
6374 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006375
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006376 /* The previous analysers guarantee that the state is somewhere
6377 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6378 * msg->next are always correct.
6379 */
6380 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6381 if (msg->flags & HTTP_MSGF_TE_CHNK)
6382 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6383 else
6384 msg->msg_state = HTTP_MSG_DATA;
6385 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006386 }
6387
Willy Tarreauefdf0942014-04-24 20:08:57 +02006388 if (res->to_forward) {
6389 /* We can't process the buffer's contents yet */
6390 res->flags |= CF_WAKE_WRITE;
6391 goto missing_data;
6392 }
6393
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006394 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6395 /* We need a compression buffer in the DATA state to put the
6396 * output of compressed data, and in CRLF state to let the
6397 * TRAILERS state finish the job of removing the trailing CRLF.
6398 */
6399 if (unlikely(tmpbuf == NULL)) {
6400 /* this is the first time we need the compression buffer */
6401 tmpbuf = pool_alloc2(pool2_buffer);
6402 if (tmpbuf == NULL)
6403 goto aborted_xfer; /* no memory */
6404 }
6405
6406 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006407 if (ret < 0) {
6408 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006409 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006410 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006411 compressing = 1;
6412 }
6413
Willy Tarreaud98cf932009-12-27 22:54:55 +01006414 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006415 switch (msg->msg_state - HTTP_MSG_DATA) {
6416 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006417 /* we may have some pending data starting at res->buf->p */
6418 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006419 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006420 if (ret < 0)
6421 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006422
Willy Tarreaud5a67832014-04-21 10:54:27 +02006423 if (msg->chunk_len) {
6424 /* input empty or output full */
6425 if (res->buf->i > msg->next)
6426 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006427 goto missing_data;
6428 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006429 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006430 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006431 if (msg->chunk_len > res->buf->i - msg->next) {
6432 /* output full */
6433 res->flags |= CF_WAKE_WRITE;
6434 goto missing_data;
6435 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006436 msg->next += msg->chunk_len;
6437 msg->chunk_len = 0;
6438 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006439
6440 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006441 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006442 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006443 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006444 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006445 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006446 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006447 /* fall through for HTTP_MSG_CHUNK_CRLF */
6448
6449 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6450 /* we want the CRLF after the data */
6451
6452 ret = http_skip_chunk_crlf(msg);
6453 if (ret == 0)
6454 goto missing_data;
6455 else if (ret < 0) {
6456 if (msg->err_pos >= 0)
6457 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6458 goto return_bad_res;
6459 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006460 /* we're in MSG_CHUNK_SIZE now, fall through */
6461
6462 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006463 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006464 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006465 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006466 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006467
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006468 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006469 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006470 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006471 else if (ret < 0) {
6472 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006473 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006474 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006475 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006476 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006477 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006478
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006479 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006480 if (unlikely(compressing)) {
6481 /* we need to flush output contents before syncing FSMs */
6482 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6483 compressing = 0;
6484 }
6485
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006486 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006487 if (ret == 0)
6488 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006489 else if (ret < 0) {
6490 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006491 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006492 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006493 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006494 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006495
6496 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006497 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006498 if (unlikely(compressing)) {
6499 /* we need to flush output contents before syncing FSMs */
6500 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6501 compressing = 0;
6502 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006503
Willy Tarreauc623c172014-04-18 09:53:50 +02006504 /* we may have some pending data starting at res->buf->p
6505 * such as a last chunk of data or trailers.
6506 */
6507 b_adv(res->buf, msg->next);
6508 msg->next = 0;
6509
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006510 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006511 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006512 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6513 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006514 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006515
Willy Tarreau610ecce2010-01-04 21:15:02 +01006516 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006517 /* some state changes occurred, maybe the analyser
6518 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006519 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006520 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006521 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006522 /* response errors are most likely due to
6523 * the client aborting the transfer.
6524 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006525 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006526 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006527 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006528 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006529 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006530 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006531 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006532 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006533 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006534 }
6535 }
6536
Willy Tarreaud98cf932009-12-27 22:54:55 +01006537 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006538 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006539 if (unlikely(compressing)) {
6540 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006541 compressing = 0;
6542 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006543
Willy Tarreauc623c172014-04-18 09:53:50 +02006544 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6545 b_adv(res->buf, msg->next);
6546 msg->next = 0;
6547 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6548 }
6549
Willy Tarreauf003d372012-11-26 13:35:37 +01006550 if (res->flags & CF_SHUTW)
6551 goto aborted_xfer;
6552
6553 /* stop waiting for data if the input is closed before the end. If the
6554 * client side was already closed, it means that the client has aborted,
6555 * so we don't want to count this as a server abort. Otherwise it's a
6556 * server abort.
6557 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006558 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006559 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006560 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006561 if (!(s->flags & SN_ERR_MASK))
6562 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006563 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006564 if (objt_server(s->target))
6565 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006566 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006567 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006568
Willy Tarreau40dba092010-03-04 18:14:51 +01006569 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006570 if (!s->req->analysers)
6571 goto return_bad_res;
6572
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006573 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006574 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006575 * Similarly, with keep-alive on the client side, we don't want to forward a
6576 * close.
6577 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006578 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006579 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6580 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006581 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006582
Willy Tarreau5c620922011-05-11 19:56:11 +02006583 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006584 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006585 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006586 * modes are already handled by the stream sock layer. We must not do
6587 * this in content-length mode because it could present the MSG_MORE
6588 * flag with the last block of forwarded data, which would cause an
6589 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006590 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006591 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006592 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006593
Willy Tarreaud98cf932009-12-27 22:54:55 +01006594 /* the session handler will take care of timeouts and errors */
6595 return 0;
6596
Willy Tarreau40dba092010-03-04 18:14:51 +01006597 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006598 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006599 if (objt_server(s->target))
6600 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006601
6602 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006603 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006604 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006605 compressing = 0;
6606 }
6607
Willy Tarreauc623c172014-04-18 09:53:50 +02006608 /* we may have some pending data starting at res->buf->p */
6609 if (s->comp_algo == NULL) {
6610 b_adv(res->buf, msg->next);
6611 msg->next = 0;
6612 }
6613
Willy Tarreaud98cf932009-12-27 22:54:55 +01006614 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006615 /* don't send any error message as we're in the body */
6616 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006617 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006618 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006619 if (objt_server(s->target))
6620 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006621
6622 if (!(s->flags & SN_ERR_MASK))
6623 s->flags |= SN_ERR_PRXCOND;
6624 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006625 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006626 return 0;
6627
6628 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006629 if (unlikely(compressing)) {
6630 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6631 compressing = 0;
6632 }
6633
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006634 txn->rsp.msg_state = HTTP_MSG_ERROR;
6635 /* don't send any error message as we're in the body */
6636 stream_int_retnclose(res->cons, NULL);
6637 res->analysers = 0;
6638 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6639
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006640 s->fe->fe_counters.cli_aborts++;
6641 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006642 if (objt_server(s->target))
6643 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006644
6645 if (!(s->flags & SN_ERR_MASK))
6646 s->flags |= SN_ERR_CLICL;
6647 if (!(s->flags & SN_FINST_MASK))
6648 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006649 return 0;
6650}
6651
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006652/* Iterate the same filter through all request headers.
6653 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006654 * Since it can manage the switch to another backend, it updates the per-proxy
6655 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006656 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006657int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006658{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006659 char term;
6660 char *cur_ptr, *cur_end, *cur_next;
6661 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006662 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006663 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006664 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006665
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006666 last_hdr = 0;
6667
Willy Tarreau9b28e032012-10-12 23:49:43 +02006668 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006669 old_idx = 0;
6670
6671 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006672 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006673 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006674 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006675 (exp->action == ACT_ALLOW ||
6676 exp->action == ACT_DENY ||
6677 exp->action == ACT_TARPIT))
6678 return 0;
6679
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006680 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006681 if (!cur_idx)
6682 break;
6683
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006684 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006685 cur_ptr = cur_next;
6686 cur_end = cur_ptr + cur_hdr->len;
6687 cur_next = cur_end + cur_hdr->cr + 1;
6688
6689 /* Now we have one header between cur_ptr and cur_end,
6690 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006691 */
6692
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006693 /* The annoying part is that pattern matching needs
6694 * that we modify the contents to null-terminate all
6695 * strings before testing them.
6696 */
6697
6698 term = *cur_end;
6699 *cur_end = '\0';
6700
6701 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6702 switch (exp->action) {
6703 case ACT_SETBE:
6704 /* It is not possible to jump a second time.
6705 * FIXME: should we return an HTTP/500 here so that
6706 * the admin knows there's a problem ?
6707 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006708 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006709 break;
6710
6711 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006712 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006713 last_hdr = 1;
6714 break;
6715
6716 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006717 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006718 last_hdr = 1;
6719 break;
6720
6721 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006722 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006723 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006724 break;
6725
6726 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006727 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006728 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006729 break;
6730
6731 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006732 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6733 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006734 /* FIXME: if the user adds a newline in the replacement, the
6735 * index will not be recalculated for now, and the new line
6736 * will not be counted as a new header.
6737 */
6738
6739 cur_end += delta;
6740 cur_next += delta;
6741 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006742 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006743 break;
6744
6745 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006746 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006747 cur_next += delta;
6748
Willy Tarreaufa355d42009-11-29 18:12:29 +01006749 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006750 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6751 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006752 cur_hdr->len = 0;
6753 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006754 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006755 break;
6756
6757 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006758 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006759 if (cur_end)
6760 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006761
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006762 /* keep the link from this header to next one in case of later
6763 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006764 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006765 old_idx = cur_idx;
6766 }
6767 return 0;
6768}
6769
6770
6771/* Apply the filter to the request line.
6772 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6773 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006774 * Since it can manage the switch to another backend, it updates the per-proxy
6775 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006776 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006777int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006778{
6779 char term;
6780 char *cur_ptr, *cur_end;
6781 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006782 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006783 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006784
Willy Tarreau3d300592007-03-18 18:34:41 +01006785 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006786 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006787 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006788 (exp->action == ACT_ALLOW ||
6789 exp->action == ACT_DENY ||
6790 exp->action == ACT_TARPIT))
6791 return 0;
6792 else if (exp->action == ACT_REMOVE)
6793 return 0;
6794
6795 done = 0;
6796
Willy Tarreau9b28e032012-10-12 23:49:43 +02006797 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006798 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006799
6800 /* Now we have the request line between cur_ptr and cur_end */
6801
6802 /* The annoying part is that pattern matching needs
6803 * that we modify the contents to null-terminate all
6804 * strings before testing them.
6805 */
6806
6807 term = *cur_end;
6808 *cur_end = '\0';
6809
6810 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6811 switch (exp->action) {
6812 case ACT_SETBE:
6813 /* It is not possible to jump a second time.
6814 * FIXME: should we return an HTTP/500 here so that
6815 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006816 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006817 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006818 break;
6819
6820 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006821 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006822 done = 1;
6823 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006824
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006825 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006826 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006827 done = 1;
6828 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006829
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006830 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006831 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006832 done = 1;
6833 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006834
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006835 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006836 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006837 done = 1;
6838 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006839
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006840 case ACT_REPLACE:
6841 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006842 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6843 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006844 /* FIXME: if the user adds a newline in the replacement, the
6845 * index will not be recalculated for now, and the new line
6846 * will not be counted as a new header.
6847 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006848
Willy Tarreaufa355d42009-11-29 18:12:29 +01006849 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006850 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006851 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006852 HTTP_MSG_RQMETH,
6853 cur_ptr, cur_end + 1,
6854 NULL, NULL);
6855 if (unlikely(!cur_end))
6856 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006857
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006858 /* we have a full request and we know that we have either a CR
6859 * or an LF at <ptr>.
6860 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006861 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6862 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006863 /* there is no point trying this regex on headers */
6864 return 1;
6865 }
6866 }
6867 *cur_end = term; /* restore the string terminator */
6868 return done;
6869}
Willy Tarreau97de6242006-12-27 17:18:38 +01006870
Willy Tarreau58f10d72006-12-04 02:26:12 +01006871
Willy Tarreau58f10d72006-12-04 02:26:12 +01006872
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006873/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006874 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006875 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006876 * unparsable request. Since it can manage the switch to another backend, it
6877 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006878 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006879int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006880{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006881 struct http_txn *txn = &s->txn;
6882 struct hdr_exp *exp;
6883
6884 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006885 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006886
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006887 /*
6888 * The interleaving of transformations and verdicts
6889 * makes it difficult to decide to continue or stop
6890 * the evaluation.
6891 */
6892
Willy Tarreau6c123b12010-01-28 20:22:06 +01006893 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6894 break;
6895
Willy Tarreau3d300592007-03-18 18:34:41 +01006896 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006897 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006898 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006899 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006900
6901 /* if this filter had a condition, evaluate it now and skip to
6902 * next filter if the condition does not match.
6903 */
6904 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006905 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006906 ret = acl_pass(ret);
6907 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6908 ret = !ret;
6909
6910 if (!ret)
6911 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006912 }
6913
6914 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006915 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006916 if (unlikely(ret < 0))
6917 return -1;
6918
6919 if (likely(ret == 0)) {
6920 /* The filter did not match the request, it can be
6921 * iterated through all headers.
6922 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006923 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006924 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006925 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006926 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006927}
6928
6929
Willy Tarreaua15645d2007-03-18 16:22:39 +01006930
Willy Tarreau58f10d72006-12-04 02:26:12 +01006931/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006932 * Try to retrieve the server associated to the appsession.
6933 * If the server is found, it's assigned to the session.
6934 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006935void manage_client_side_appsession(struct session *s, const char *buf, int len) {
6936 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006937 appsess *asession = NULL;
6938 char *sessid_temp = NULL;
6939
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006940 if (len > s->be->appsession_len) {
6941 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006942 }
6943
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006944 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006945 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006946 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006947 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006948 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006949 }
6950
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006951 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006952 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006953 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006954 return;
6955 }
6956
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006957 memcpy(txn->sessid, buf, len);
6958 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006959 }
6960
6961 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6962 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006963 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006964 return;
6965 }
6966
Cyril Bontéb21570a2009-11-29 20:04:48 +01006967 memcpy(sessid_temp, buf, len);
6968 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006969
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006970 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006971 /* free previously allocated memory */
6972 pool_free2(apools.sessid, sessid_temp);
6973
6974 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006975 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
6976 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006977 asession->request_count++;
6978
6979 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006980 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006981
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006982 while (srv) {
6983 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006984 if ((srv->state & SRV_RUNNING) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006985 (s->be->options & PR_O_PERSIST) ||
6986 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006987 /* we found the server and it's usable */
6988 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006989 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006990 s->flags |= SN_DIRECT | SN_ASSIGNED;
6991 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006992
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006993 break;
6994 } else {
6995 txn->flags &= ~TX_CK_MASK;
6996 txn->flags |= TX_CK_DOWN;
6997 }
6998 }
6999 srv = srv->next;
7000 }
7001 }
7002 }
7003}
7004
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007005/* Find the end of a cookie value contained between <s> and <e>. It works the
7006 * same way as with headers above except that the semi-colon also ends a token.
7007 * See RFC2965 for more information. Note that it requires a valid header to
7008 * return a valid result.
7009 */
7010char *find_cookie_value_end(char *s, const char *e)
7011{
7012 int quoted, qdpair;
7013
7014 quoted = qdpair = 0;
7015 for (; s < e; s++) {
7016 if (qdpair) qdpair = 0;
7017 else if (quoted) {
7018 if (*s == '\\') qdpair = 1;
7019 else if (*s == '"') quoted = 0;
7020 }
7021 else if (*s == '"') quoted = 1;
7022 else if (*s == ',' || *s == ';') return s;
7023 }
7024 return s;
7025}
7026
7027/* Delete a value in a header between delimiters <from> and <next> in buffer
7028 * <buf>. The number of characters displaced is returned, and the pointer to
7029 * the first delimiter is updated if required. The function tries as much as
7030 * possible to respect the following principles :
7031 * - replace <from> delimiter by the <next> one unless <from> points to a
7032 * colon, in which case <next> is simply removed
7033 * - set exactly one space character after the new first delimiter, unless
7034 * there are not enough characters in the block being moved to do so.
7035 * - remove unneeded spaces before the previous delimiter and after the new
7036 * one.
7037 *
7038 * It is the caller's responsibility to ensure that :
7039 * - <from> points to a valid delimiter or the colon ;
7040 * - <next> points to a valid delimiter or the final CR/LF ;
7041 * - there are non-space chars before <from> ;
7042 * - there is a CR/LF at or after <next>.
7043 */
Willy Tarreauaf819352012-08-27 22:08:00 +02007044int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007045{
7046 char *prev = *from;
7047
7048 if (*prev == ':') {
7049 /* We're removing the first value, preserve the colon and add a
7050 * space if possible.
7051 */
7052 if (!http_is_crlf[(unsigned char)*next])
7053 next++;
7054 prev++;
7055 if (prev < next)
7056 *prev++ = ' ';
7057
7058 while (http_is_spht[(unsigned char)*next])
7059 next++;
7060 } else {
7061 /* Remove useless spaces before the old delimiter. */
7062 while (http_is_spht[(unsigned char)*(prev-1)])
7063 prev--;
7064 *from = prev;
7065
7066 /* copy the delimiter and if possible a space if we're
7067 * not at the end of the line.
7068 */
7069 if (!http_is_crlf[(unsigned char)*next]) {
7070 *prev++ = *next++;
7071 if (prev + 1 < next)
7072 *prev++ = ' ';
7073 while (http_is_spht[(unsigned char)*next])
7074 next++;
7075 }
7076 }
7077 return buffer_replace2(buf, prev, next, NULL, 0);
7078}
7079
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007080/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007081 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007082 * desirable to call it only when needed. This code is quite complex because
7083 * of the multiple very crappy and ambiguous syntaxes we have to support. it
7084 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01007085 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007086void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007087{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007088 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007089 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007090 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007091 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
7092 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007093
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007094 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01007095 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007096 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007097
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007098 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007099 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007100 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007101
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007102 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007103 hdr_beg = hdr_next;
7104 hdr_end = hdr_beg + cur_hdr->len;
7105 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007106
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007107 /* We have one full header between hdr_beg and hdr_end, and the
7108 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01007109 * "Cookie:" headers.
7110 */
7111
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007112 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007113 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007114 old_idx = cur_idx;
7115 continue;
7116 }
7117
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007118 del_from = NULL; /* nothing to be deleted */
7119 preserve_hdr = 0; /* assume we may kill the whole header */
7120
Willy Tarreau58f10d72006-12-04 02:26:12 +01007121 /* Now look for cookies. Conforming to RFC2109, we have to support
7122 * attributes whose name begin with a '$', and associate them with
7123 * the right cookie, if we want to delete this cookie.
7124 * So there are 3 cases for each cookie read :
7125 * 1) it's a special attribute, beginning with a '$' : ignore it.
7126 * 2) it's a server id cookie that we *MAY* want to delete : save
7127 * some pointers on it (last semi-colon, beginning of cookie...)
7128 * 3) it's an application cookie : we *MAY* have to delete a previous
7129 * "special" cookie.
7130 * At the end of loop, if a "special" cookie remains, we may have to
7131 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007132 * *MUST* delete it.
7133 *
7134 * Note: RFC2965 is unclear about the processing of spaces around
7135 * the equal sign in the ATTR=VALUE form. A careful inspection of
7136 * the RFC explicitly allows spaces before it, and not within the
7137 * tokens (attrs or values). An inspection of RFC2109 allows that
7138 * too but section 10.1.3 lets one think that spaces may be allowed
7139 * after the equal sign too, resulting in some (rare) buggy
7140 * implementations trying to do that. So let's do what servers do.
7141 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
7142 * allowed quoted strings in values, with any possible character
7143 * after a backslash, including control chars and delimitors, which
7144 * causes parsing to become ambiguous. Browsers also allow spaces
7145 * within values even without quotes.
7146 *
7147 * We have to keep multiple pointers in order to support cookie
7148 * removal at the beginning, middle or end of header without
7149 * corrupting the header. All of these headers are valid :
7150 *
7151 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
7152 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
7153 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
7154 * | | | | | | | | |
7155 * | | | | | | | | hdr_end <--+
7156 * | | | | | | | +--> next
7157 * | | | | | | +----> val_end
7158 * | | | | | +-----------> val_beg
7159 * | | | | +--------------> equal
7160 * | | | +----------------> att_end
7161 * | | +---------------------> att_beg
7162 * | +--------------------------> prev
7163 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01007164 */
7165
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007166 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
7167 /* Iterate through all cookies on this line */
7168
7169 /* find att_beg */
7170 att_beg = prev + 1;
7171 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7172 att_beg++;
7173
7174 /* find att_end : this is the first character after the last non
7175 * space before the equal. It may be equal to hdr_end.
7176 */
7177 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007178
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007179 while (equal < hdr_end) {
7180 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01007181 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007182 if (http_is_spht[(unsigned char)*equal++])
7183 continue;
7184 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007185 }
7186
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007187 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7188 * is between <att_beg> and <equal>, both may be identical.
7189 */
7190
7191 /* look for end of cookie if there is an equal sign */
7192 if (equal < hdr_end && *equal == '=') {
7193 /* look for the beginning of the value */
7194 val_beg = equal + 1;
7195 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7196 val_beg++;
7197
7198 /* find the end of the value, respecting quotes */
7199 next = find_cookie_value_end(val_beg, hdr_end);
7200
7201 /* make val_end point to the first white space or delimitor after the value */
7202 val_end = next;
7203 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7204 val_end--;
7205 } else {
7206 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007207 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007208
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007209 /* We have nothing to do with attributes beginning with '$'. However,
7210 * they will automatically be removed if a header before them is removed,
7211 * since they're supposed to be linked together.
7212 */
7213 if (*att_beg == '$')
7214 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007215
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007216 /* Ignore cookies with no equal sign */
7217 if (equal == next) {
7218 /* This is not our cookie, so we must preserve it. But if we already
7219 * scheduled another cookie for removal, we cannot remove the
7220 * complete header, but we can remove the previous block itself.
7221 */
7222 preserve_hdr = 1;
7223 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007224 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007225 val_end += delta;
7226 next += delta;
7227 hdr_end += delta;
7228 hdr_next += delta;
7229 cur_hdr->len += delta;
7230 http_msg_move_end(&txn->req, delta);
7231 prev = del_from;
7232 del_from = NULL;
7233 }
7234 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007235 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007236
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007237 /* if there are spaces around the equal sign, we need to
7238 * strip them otherwise we'll get trouble for cookie captures,
7239 * or even for rewrites. Since this happens extremely rarely,
7240 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007241 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007242 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7243 int stripped_before = 0;
7244 int stripped_after = 0;
7245
7246 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007247 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007248 equal += stripped_before;
7249 val_beg += stripped_before;
7250 }
7251
7252 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007253 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007254 val_beg += stripped_after;
7255 stripped_before += stripped_after;
7256 }
7257
7258 val_end += stripped_before;
7259 next += stripped_before;
7260 hdr_end += stripped_before;
7261 hdr_next += stripped_before;
7262 cur_hdr->len += stripped_before;
7263 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007264 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007265 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007266
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007267 /* First, let's see if we want to capture this cookie. We check
7268 * that we don't already have a client side cookie, because we
7269 * can only capture one. Also as an optimisation, we ignore
7270 * cookies shorter than the declared name.
7271 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007272 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7273 (val_end - att_beg >= s->fe->capture_namelen) &&
7274 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007275 int log_len = val_end - att_beg;
7276
7277 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7278 Alert("HTTP logging : out of memory.\n");
7279 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007280 if (log_len > s->fe->capture_len)
7281 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007282 memcpy(txn->cli_cookie, att_beg, log_len);
7283 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007284 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007285 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007286
Willy Tarreaubca99692010-10-06 19:25:55 +02007287 /* Persistence cookies in passive, rewrite or insert mode have the
7288 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007289 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007290 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007291 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007292 * For cookies in prefix mode, the form is :
7293 *
7294 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007295 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007296 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7297 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7298 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007299 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007300
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007301 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7302 * have the server ID between val_beg and delim, and the original cookie between
7303 * delim+1 and val_end. Otherwise, delim==val_end :
7304 *
7305 * Cookie: NAME=SRV; # in all but prefix modes
7306 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7307 * | || || | |+-> next
7308 * | || || | +--> val_end
7309 * | || || +---------> delim
7310 * | || |+------------> val_beg
7311 * | || +-------------> att_end = equal
7312 * | |+-----------------> att_beg
7313 * | +------------------> prev
7314 * +-------------------------> hdr_beg
7315 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007316
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007317 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007318 for (delim = val_beg; delim < val_end; delim++)
7319 if (*delim == COOKIE_DELIM)
7320 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007321 } else {
7322 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007323 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007324 /* Now check if the cookie contains a date field, which would
7325 * appear after a vertical bar ('|') just after the server name
7326 * and before the delimiter.
7327 */
7328 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7329 if (vbar1) {
7330 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007331 * right is the last seen date. It is a base64 encoded
7332 * 30-bit value representing the UNIX date since the
7333 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007334 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007335 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007336 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007337 if (val_end - vbar1 >= 5) {
7338 val = b64tos30(vbar1);
7339 if (val > 0)
7340 txn->cookie_last_date = val << 2;
7341 }
7342 /* look for a second vertical bar */
7343 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7344 if (vbar1 && (val_end - vbar1 > 5)) {
7345 val = b64tos30(vbar1 + 1);
7346 if (val > 0)
7347 txn->cookie_first_date = val << 2;
7348 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007349 }
7350 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007351
Willy Tarreauf64d1412010-10-07 20:06:11 +02007352 /* if the cookie has an expiration date and the proxy wants to check
7353 * it, then we do that now. We first check if the cookie is too old,
7354 * then only if it has expired. We detect strict overflow because the
7355 * time resolution here is not great (4 seconds). Cookies with dates
7356 * in the future are ignored if their offset is beyond one day. This
7357 * allows an admin to fix timezone issues without expiring everyone
7358 * and at the same time avoids keeping unwanted side effects for too
7359 * long.
7360 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007361 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7362 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007363 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007364 txn->flags &= ~TX_CK_MASK;
7365 txn->flags |= TX_CK_OLD;
7366 delim = val_beg; // let's pretend we have not found the cookie
7367 txn->cookie_first_date = 0;
7368 txn->cookie_last_date = 0;
7369 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007370 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7371 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007372 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007373 txn->flags &= ~TX_CK_MASK;
7374 txn->flags |= TX_CK_EXPIRED;
7375 delim = val_beg; // let's pretend we have not found the cookie
7376 txn->cookie_first_date = 0;
7377 txn->cookie_last_date = 0;
7378 }
7379
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007380 /* Here, we'll look for the first running server which supports the cookie.
7381 * This allows to share a same cookie between several servers, for example
7382 * to dedicate backup servers to specific servers only.
7383 * However, to prevent clients from sticking to cookie-less backup server
7384 * when they have incidentely learned an empty cookie, we simply ignore
7385 * empty cookies and mark them as invalid.
7386 * The same behaviour is applied when persistence must be ignored.
7387 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007388 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007389 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007390
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007391 while (srv) {
7392 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7393 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7394 if ((srv->state & SRV_RUNNING) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007395 (s->be->options & PR_O_PERSIST) ||
7396 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007397 /* we found the server and we can use it */
7398 txn->flags &= ~TX_CK_MASK;
7399 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007400 s->flags |= SN_DIRECT | SN_ASSIGNED;
7401 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007402 break;
7403 } else {
7404 /* we found a server, but it's down,
7405 * mark it as such and go on in case
7406 * another one is available.
7407 */
7408 txn->flags &= ~TX_CK_MASK;
7409 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007410 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007411 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007412 srv = srv->next;
7413 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007414
Willy Tarreauf64d1412010-10-07 20:06:11 +02007415 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007416 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007417 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007418 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007419 txn->flags |= TX_CK_UNUSED;
7420 else
7421 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007422 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007423
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007424 /* depending on the cookie mode, we may have to either :
7425 * - delete the complete cookie if we're in insert+indirect mode, so that
7426 * the server never sees it ;
7427 * - remove the server id from the cookie value, and tag the cookie as an
7428 * application cookie so that it does not get accidentely removed later,
7429 * if we're in cookie prefix mode
7430 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007431 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007432 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007433
Willy Tarreau9b28e032012-10-12 23:49:43 +02007434 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007435 val_end += delta;
7436 next += delta;
7437 hdr_end += delta;
7438 hdr_next += delta;
7439 cur_hdr->len += delta;
7440 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007441
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007442 del_from = NULL;
7443 preserve_hdr = 1; /* we want to keep this cookie */
7444 }
7445 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007446 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007447 del_from = prev;
7448 }
7449 } else {
7450 /* This is not our cookie, so we must preserve it. But if we already
7451 * scheduled another cookie for removal, we cannot remove the
7452 * complete header, but we can remove the previous block itself.
7453 */
7454 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007455
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007456 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007457 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007458 if (att_beg >= del_from)
7459 att_beg += delta;
7460 if (att_end >= del_from)
7461 att_end += delta;
7462 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007463 val_end += delta;
7464 next += delta;
7465 hdr_end += delta;
7466 hdr_next += delta;
7467 cur_hdr->len += delta;
7468 http_msg_move_end(&txn->req, delta);
7469 prev = del_from;
7470 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007471 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007472 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007473
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007474 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007475 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007476 int cmp_len, value_len;
7477 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007478
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007479 if (s->be->options2 & PR_O2_AS_PFX) {
7480 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7481 value_begin = att_beg + s->be->appsession_name_len;
7482 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007483 } else {
7484 cmp_len = att_end - att_beg;
7485 value_begin = val_beg;
7486 value_len = val_end - val_beg;
7487 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007488
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007489 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007490 if (cmp_len == s->be->appsession_name_len &&
7491 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7492 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007493 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007494 }
7495
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007496 /* continue with next cookie on this header line */
7497 att_beg = next;
7498 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007499
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007500 /* There are no more cookies on this line.
7501 * We may still have one (or several) marked for deletion at the
7502 * end of the line. We must do this now in two ways :
7503 * - if some cookies must be preserved, we only delete from the
7504 * mark to the end of line ;
7505 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007506 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007507 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007508 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007509 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007510 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007511 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007512 cur_hdr->len += delta;
7513 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007514 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007515
7516 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007517 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7518 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007519 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007520 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007521 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007522 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007523 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007524 }
7525
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007526 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007527 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007528 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007529}
7530
7531
Willy Tarreaua15645d2007-03-18 16:22:39 +01007532/* Iterate the same filter through all response headers contained in <rtr>.
7533 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7534 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007535int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007536{
7537 char term;
7538 char *cur_ptr, *cur_end, *cur_next;
7539 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007540 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007541 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007542 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007543
7544 last_hdr = 0;
7545
Willy Tarreau9b28e032012-10-12 23:49:43 +02007546 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007547 old_idx = 0;
7548
7549 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007550 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007551 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007552 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007553 (exp->action == ACT_ALLOW ||
7554 exp->action == ACT_DENY))
7555 return 0;
7556
7557 cur_idx = txn->hdr_idx.v[old_idx].next;
7558 if (!cur_idx)
7559 break;
7560
7561 cur_hdr = &txn->hdr_idx.v[cur_idx];
7562 cur_ptr = cur_next;
7563 cur_end = cur_ptr + cur_hdr->len;
7564 cur_next = cur_end + cur_hdr->cr + 1;
7565
7566 /* Now we have one header between cur_ptr and cur_end,
7567 * and the next header starts at cur_next.
7568 */
7569
7570 /* The annoying part is that pattern matching needs
7571 * that we modify the contents to null-terminate all
7572 * strings before testing them.
7573 */
7574
7575 term = *cur_end;
7576 *cur_end = '\0';
7577
7578 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7579 switch (exp->action) {
7580 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007581 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007582 last_hdr = 1;
7583 break;
7584
7585 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007586 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007587 last_hdr = 1;
7588 break;
7589
7590 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007591 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7592 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007593 /* FIXME: if the user adds a newline in the replacement, the
7594 * index will not be recalculated for now, and the new line
7595 * will not be counted as a new header.
7596 */
7597
7598 cur_end += delta;
7599 cur_next += delta;
7600 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007601 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007602 break;
7603
7604 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007605 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007606 cur_next += delta;
7607
Willy Tarreaufa355d42009-11-29 18:12:29 +01007608 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007609 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7610 txn->hdr_idx.used--;
7611 cur_hdr->len = 0;
7612 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007613 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007614 break;
7615
7616 }
7617 }
7618 if (cur_end)
7619 *cur_end = term; /* restore the string terminator */
7620
7621 /* keep the link from this header to next one in case of later
7622 * removal of next header.
7623 */
7624 old_idx = cur_idx;
7625 }
7626 return 0;
7627}
7628
7629
7630/* Apply the filter to the status line in the response buffer <rtr>.
7631 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7632 * or -1 if a replacement resulted in an invalid status line.
7633 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007634int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007635{
7636 char term;
7637 char *cur_ptr, *cur_end;
7638 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007639 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007640 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007641
7642
Willy Tarreau3d300592007-03-18 18:34:41 +01007643 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007644 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007645 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007646 (exp->action == ACT_ALLOW ||
7647 exp->action == ACT_DENY))
7648 return 0;
7649 else if (exp->action == ACT_REMOVE)
7650 return 0;
7651
7652 done = 0;
7653
Willy Tarreau9b28e032012-10-12 23:49:43 +02007654 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007655 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007656
7657 /* Now we have the status line between cur_ptr and cur_end */
7658
7659 /* The annoying part is that pattern matching needs
7660 * that we modify the contents to null-terminate all
7661 * strings before testing them.
7662 */
7663
7664 term = *cur_end;
7665 *cur_end = '\0';
7666
7667 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7668 switch (exp->action) {
7669 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007670 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007671 done = 1;
7672 break;
7673
7674 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007675 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007676 done = 1;
7677 break;
7678
7679 case ACT_REPLACE:
7680 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007681 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7682 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007683 /* FIXME: if the user adds a newline in the replacement, the
7684 * index will not be recalculated for now, and the new line
7685 * will not be counted as a new header.
7686 */
7687
Willy Tarreaufa355d42009-11-29 18:12:29 +01007688 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007689 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007690 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007691 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007692 cur_ptr, cur_end + 1,
7693 NULL, NULL);
7694 if (unlikely(!cur_end))
7695 return -1;
7696
7697 /* we have a full respnse and we know that we have either a CR
7698 * or an LF at <ptr>.
7699 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007700 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007701 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007702 /* there is no point trying this regex on headers */
7703 return 1;
7704 }
7705 }
7706 *cur_end = term; /* restore the string terminator */
7707 return done;
7708}
7709
7710
7711
7712/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007713 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007714 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7715 * unparsable response.
7716 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007717int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007718{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007719 struct http_txn *txn = &s->txn;
7720 struct hdr_exp *exp;
7721
7722 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007723 int ret;
7724
7725 /*
7726 * The interleaving of transformations and verdicts
7727 * makes it difficult to decide to continue or stop
7728 * the evaluation.
7729 */
7730
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007731 if (txn->flags & TX_SVDENY)
7732 break;
7733
Willy Tarreau3d300592007-03-18 18:34:41 +01007734 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007735 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7736 exp->action == ACT_PASS)) {
7737 exp = exp->next;
7738 continue;
7739 }
7740
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007741 /* if this filter had a condition, evaluate it now and skip to
7742 * next filter if the condition does not match.
7743 */
7744 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007745 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007746 ret = acl_pass(ret);
7747 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7748 ret = !ret;
7749 if (!ret)
7750 continue;
7751 }
7752
Willy Tarreaua15645d2007-03-18 16:22:39 +01007753 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007754 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007755 if (unlikely(ret < 0))
7756 return -1;
7757
7758 if (likely(ret == 0)) {
7759 /* The filter did not match the response, it can be
7760 * iterated through all headers.
7761 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007762 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007763 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007764 }
7765 return 0;
7766}
7767
7768
Willy Tarreaua15645d2007-03-18 16:22:39 +01007769/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007770 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007771 * desirable to call it only when needed. This function is also used when we
7772 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007773 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007774void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007775{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007776 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007777 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007778 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007779 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007780 char *hdr_beg, *hdr_end, *hdr_next;
7781 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007782
Willy Tarreaua15645d2007-03-18 16:22:39 +01007783 /* Iterate through the headers.
7784 * we start with the start line.
7785 */
7786 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007787 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007788
7789 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7790 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007791 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007792
7793 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007794 hdr_beg = hdr_next;
7795 hdr_end = hdr_beg + cur_hdr->len;
7796 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007797
Willy Tarreau24581ba2010-08-31 22:39:35 +02007798 /* We have one full header between hdr_beg and hdr_end, and the
7799 * next header starts at hdr_next. We're only interested in
7800 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007801 */
7802
Willy Tarreau24581ba2010-08-31 22:39:35 +02007803 is_cookie2 = 0;
7804 prev = hdr_beg + 10;
7805 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007806 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007807 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7808 if (!val) {
7809 old_idx = cur_idx;
7810 continue;
7811 }
7812 is_cookie2 = 1;
7813 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007814 }
7815
Willy Tarreau24581ba2010-08-31 22:39:35 +02007816 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7817 * <prev> points to the colon.
7818 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007819 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007820
Willy Tarreau24581ba2010-08-31 22:39:35 +02007821 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7822 * check-cache is enabled) and we are not interested in checking
7823 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007824 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007825 if (s->be->cookie_name == NULL &&
7826 s->be->appsession_name == NULL &&
7827 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007828 return;
7829
Willy Tarreau24581ba2010-08-31 22:39:35 +02007830 /* OK so now we know we have to process this response cookie.
7831 * The format of the Set-Cookie header is slightly different
7832 * from the format of the Cookie header in that it does not
7833 * support the comma as a cookie delimiter (thus the header
7834 * cannot be folded) because the Expires attribute described in
7835 * the original Netscape's spec may contain an unquoted date
7836 * with a comma inside. We have to live with this because
7837 * many browsers don't support Max-Age and some browsers don't
7838 * support quoted strings. However the Set-Cookie2 header is
7839 * clean.
7840 *
7841 * We have to keep multiple pointers in order to support cookie
7842 * removal at the beginning, middle or end of header without
7843 * corrupting the header (in case of set-cookie2). A special
7844 * pointer, <scav> points to the beginning of the set-cookie-av
7845 * fields after the first semi-colon. The <next> pointer points
7846 * either to the end of line (set-cookie) or next unquoted comma
7847 * (set-cookie2). All of these headers are valid :
7848 *
7849 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7850 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7851 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7852 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7853 * | | | | | | | | | |
7854 * | | | | | | | | +-> next hdr_end <--+
7855 * | | | | | | | +------------> scav
7856 * | | | | | | +--------------> val_end
7857 * | | | | | +--------------------> val_beg
7858 * | | | | +----------------------> equal
7859 * | | | +------------------------> att_end
7860 * | | +----------------------------> att_beg
7861 * | +------------------------------> prev
7862 * +-----------------------------------------> hdr_beg
7863 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007864
Willy Tarreau24581ba2010-08-31 22:39:35 +02007865 for (; prev < hdr_end; prev = next) {
7866 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007867
Willy Tarreau24581ba2010-08-31 22:39:35 +02007868 /* find att_beg */
7869 att_beg = prev + 1;
7870 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7871 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007872
Willy Tarreau24581ba2010-08-31 22:39:35 +02007873 /* find att_end : this is the first character after the last non
7874 * space before the equal. It may be equal to hdr_end.
7875 */
7876 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007877
Willy Tarreau24581ba2010-08-31 22:39:35 +02007878 while (equal < hdr_end) {
7879 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7880 break;
7881 if (http_is_spht[(unsigned char)*equal++])
7882 continue;
7883 att_end = equal;
7884 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007885
Willy Tarreau24581ba2010-08-31 22:39:35 +02007886 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7887 * is between <att_beg> and <equal>, both may be identical.
7888 */
7889
7890 /* look for end of cookie if there is an equal sign */
7891 if (equal < hdr_end && *equal == '=') {
7892 /* look for the beginning of the value */
7893 val_beg = equal + 1;
7894 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7895 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007896
Willy Tarreau24581ba2010-08-31 22:39:35 +02007897 /* find the end of the value, respecting quotes */
7898 next = find_cookie_value_end(val_beg, hdr_end);
7899
7900 /* make val_end point to the first white space or delimitor after the value */
7901 val_end = next;
7902 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7903 val_end--;
7904 } else {
7905 /* <equal> points to next comma, semi-colon or EOL */
7906 val_beg = val_end = next = equal;
7907 }
7908
7909 if (next < hdr_end) {
7910 /* Set-Cookie2 supports multiple cookies, and <next> points to
7911 * a colon or semi-colon before the end. So skip all attr-value
7912 * pairs and look for the next comma. For Set-Cookie, since
7913 * commas are permitted in values, skip to the end.
7914 */
7915 if (is_cookie2)
7916 next = find_hdr_value_end(next, hdr_end);
7917 else
7918 next = hdr_end;
7919 }
7920
7921 /* Now everything is as on the diagram above */
7922
7923 /* Ignore cookies with no equal sign */
7924 if (equal == val_end)
7925 continue;
7926
7927 /* If there are spaces around the equal sign, we need to
7928 * strip them otherwise we'll get trouble for cookie captures,
7929 * or even for rewrites. Since this happens extremely rarely,
7930 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007931 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007932 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7933 int stripped_before = 0;
7934 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007935
Willy Tarreau24581ba2010-08-31 22:39:35 +02007936 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007937 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007938 equal += stripped_before;
7939 val_beg += stripped_before;
7940 }
7941
7942 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007943 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007944 val_beg += stripped_after;
7945 stripped_before += stripped_after;
7946 }
7947
7948 val_end += stripped_before;
7949 next += stripped_before;
7950 hdr_end += stripped_before;
7951 hdr_next += stripped_before;
7952 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007953 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007954 }
7955
7956 /* First, let's see if we want to capture this cookie. We check
7957 * that we don't already have a server side cookie, because we
7958 * can only capture one. Also as an optimisation, we ignore
7959 * cookies shorter than the declared name.
7960 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007961 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007962 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007963 (val_end - att_beg >= s->fe->capture_namelen) &&
7964 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007965 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007966 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007967 Alert("HTTP logging : out of memory.\n");
7968 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007969 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007970 if (log_len > s->fe->capture_len)
7971 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007972 memcpy(txn->srv_cookie, att_beg, log_len);
7973 txn->srv_cookie[log_len] = 0;
7974 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007975 }
7976
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007977 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007978 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007979 if (!(s->flags & SN_IGNORE_PRST) &&
7980 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7981 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007982 /* assume passive cookie by default */
7983 txn->flags &= ~TX_SCK_MASK;
7984 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007985
7986 /* If the cookie is in insert mode on a known server, we'll delete
7987 * this occurrence because we'll insert another one later.
7988 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007989 * a direct access.
7990 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007991 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007992 /* The "preserve" flag was set, we don't want to touch the
7993 * server's cookie.
7994 */
7995 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007996 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
7997 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007998 /* this cookie must be deleted */
7999 if (*prev == ':' && next == hdr_end) {
8000 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008001 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008002 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
8003 txn->hdr_idx.used--;
8004 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01008005 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008006 hdr_next += delta;
8007 http_msg_move_end(&txn->rsp, delta);
8008 /* note: while both invalid now, <next> and <hdr_end>
8009 * are still equal, so the for() will stop as expected.
8010 */
8011 } else {
8012 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008013 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008014 next = prev;
8015 hdr_end += delta;
8016 hdr_next += delta;
8017 cur_hdr->len += delta;
8018 http_msg_move_end(&txn->rsp, delta);
8019 }
Willy Tarreauf1348312010-10-07 15:54:11 +02008020 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01008021 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008022 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008023 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008024 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008025 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01008026 * with this server since we know it.
8027 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008028 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008029 next += delta;
8030 hdr_end += delta;
8031 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008032 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008033 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008034
Willy Tarreauf1348312010-10-07 15:54:11 +02008035 txn->flags &= ~TX_SCK_MASK;
8036 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008037 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008038 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01008039 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02008040 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01008041 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008042 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02008043 next += delta;
8044 hdr_end += delta;
8045 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008046 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01008047 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008048
Willy Tarreau827aee92011-03-10 16:55:02 +01008049 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02008050 txn->flags &= ~TX_SCK_MASK;
8051 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008052 }
8053 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02008054 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008055 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008056 int cmp_len, value_len;
8057 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008058
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008059 if (s->be->options2 & PR_O2_AS_PFX) {
8060 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
8061 value_begin = att_beg + s->be->appsession_name_len;
8062 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008063 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008064 cmp_len = att_end - att_beg;
8065 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008066 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008067 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01008068
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008069 if ((cmp_len == s->be->appsession_name_len) &&
8070 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02008071 /* free a possibly previously allocated memory */
8072 pool_free2(apools.sessid, txn->sessid);
8073
Cyril Bontéb21570a2009-11-29 20:04:48 +01008074 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008075 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008076 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008077 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01008078 return;
8079 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008080 memcpy(txn->sessid, value_begin, value_len);
8081 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008082 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02008083 }
8084 /* that's done for this cookie, check the next one on the same
8085 * line when next != hdr_end (only if is_cookie2).
8086 */
8087 }
8088 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008089 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02008090 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008091
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008092 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008093 appsess *asession = NULL;
8094 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008095 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008096 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01008097 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008098 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
8099 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008100 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008101 return;
8102 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008103 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
8104
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008105 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
8106 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008107 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8108 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008109 return;
8110 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008111 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
8112 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008113
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008114 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008115 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01008116 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008117 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
8118 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008119 return;
8120 }
8121 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008122 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008123
8124 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008125 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008126 }
8127
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008128 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02008129 asession->request_count++;
8130 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008131}
8132
8133
Willy Tarreaua15645d2007-03-18 16:22:39 +01008134/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008135 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01008136 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008137void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008138{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008139 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008140 char *p1, *p2;
8141
8142 char *cur_ptr, *cur_end, *cur_next;
8143 int cur_idx;
8144
Willy Tarreau5df51872007-11-25 16:20:08 +01008145 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008146 return;
8147
8148 /* Iterate through the headers.
8149 * we start with the start line.
8150 */
8151 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008152 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01008153
8154 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
8155 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008156 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008157
8158 cur_hdr = &txn->hdr_idx.v[cur_idx];
8159 cur_ptr = cur_next;
8160 cur_end = cur_ptr + cur_hdr->len;
8161 cur_next = cur_end + cur_hdr->cr + 1;
8162
8163 /* We have one full header between cur_ptr and cur_end, and the
8164 * next header starts at cur_next. We're only interested in
8165 * "Cookie:" headers.
8166 */
8167
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008168 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
8169 if (val) {
8170 if ((cur_end - (cur_ptr + val) >= 8) &&
8171 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
8172 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
8173 return;
8174 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01008175 }
8176
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008177 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
8178 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01008179 continue;
8180
8181 /* OK, right now we know we have a cache-control header at cur_ptr */
8182
Willy Tarreauaa9dce32007-03-18 23:50:16 +01008183 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01008184
8185 if (p1 >= cur_end) /* no more info */
8186 continue;
8187
8188 /* p1 is at the beginning of the value */
8189 p2 = p1;
8190
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008191 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008192 p2++;
8193
8194 /* we have a complete value between p1 and p2 */
8195 if (p2 < cur_end && *p2 == '=') {
8196 /* we have something of the form no-cache="set-cookie" */
8197 if ((cur_end - p1 >= 21) &&
8198 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8199 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008200 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008201 continue;
8202 }
8203
8204 /* OK, so we know that either p2 points to the end of string or to a comma */
8205 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008206 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008207 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8208 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8209 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008210 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008211 return;
8212 }
8213
8214 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008215 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008216 continue;
8217 }
8218 }
8219}
8220
8221
Willy Tarreau58f10d72006-12-04 02:26:12 +01008222/*
8223 * Try to retrieve a known appsession in the URI, then the associated server.
8224 * If the server is found, it's assigned to the session.
8225 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008226void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008227{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008228 char *end_params, *first_param, *cur_param, *next_param;
8229 char separator;
8230 int value_len;
8231
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008232 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008233
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008234 if (s->be->appsession_name == NULL ||
8235 (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 +01008236 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008237 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008238
Cyril Bontéb21570a2009-11-29 20:04:48 +01008239 first_param = NULL;
8240 switch (mode) {
8241 case PR_O2_AS_M_PP:
8242 first_param = memchr(begin, ';', len);
8243 break;
8244 case PR_O2_AS_M_QS:
8245 first_param = memchr(begin, '?', len);
8246 break;
8247 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008248
Cyril Bontéb21570a2009-11-29 20:04:48 +01008249 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008250 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008251 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008252
Cyril Bontéb21570a2009-11-29 20:04:48 +01008253 switch (mode) {
8254 case PR_O2_AS_M_PP:
8255 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8256 end_params = (char *) begin + len;
8257 }
8258 separator = ';';
8259 break;
8260 case PR_O2_AS_M_QS:
8261 end_params = (char *) begin + len;
8262 separator = '&';
8263 break;
8264 default:
8265 /* unknown mode, shouldn't happen */
8266 return;
8267 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008268
Cyril Bontéb21570a2009-11-29 20:04:48 +01008269 cur_param = next_param = end_params;
8270 while (cur_param > first_param) {
8271 cur_param--;
8272 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8273 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008274 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8275 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8276 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008277 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008278 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8279 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008280 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008281 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008282 }
8283 break;
8284 }
8285 next_param = cur_param;
8286 }
8287 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008288#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008289 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008290 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008291#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008292}
8293
Willy Tarreaub2513902006-12-17 14:52:38 +01008294/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008295 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008296 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008297 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008298 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008299 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008300 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008301 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008302 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008303int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008304{
8305 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008306 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008307 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008308
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008309 if (!uri_auth)
8310 return 0;
8311
Cyril Bonté70be45d2010-10-12 00:14:35 +02008312 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008313 return 0;
8314
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008315 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008316 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008317 return 0;
8318
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008319 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008320 return 0;
8321
Willy Tarreaub2513902006-12-17 14:52:38 +01008322 return 1;
8323}
8324
Willy Tarreau4076a152009-04-02 15:18:36 +02008325/*
8326 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008327 * By default it tries to report the error position as msg->err_pos. However if
8328 * this one is not set, it will then report msg->next, which is the last known
8329 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008330 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008331 */
8332void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008333 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008334 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008335{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008336 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008337 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008338
Willy Tarreau9b28e032012-10-12 23:49:43 +02008339 es->len = MIN(chn->buf->i, sizeof(es->buf));
8340 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008341 len1 = MIN(len1, es->len);
8342 len2 = es->len - len1; /* remaining data if buffer wraps */
8343
Willy Tarreau9b28e032012-10-12 23:49:43 +02008344 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008345 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008346 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008347
Willy Tarreau4076a152009-04-02 15:18:36 +02008348 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008349 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008350 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008351 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008352
Willy Tarreau4076a152009-04-02 15:18:36 +02008353 es->when = date; // user-visible date
8354 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008355 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008356 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008357 if (objt_conn(s->req->prod->end))
8358 es->src = __objt_conn(s->req->prod->end)->addr.from;
8359 else
8360 memset(&es->src, 0, sizeof(es->src));
8361
Willy Tarreau078272e2010-12-12 12:46:33 +01008362 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008363 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008364 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008365 es->s_flags = s->flags;
8366 es->t_flags = s->txn.flags;
8367 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008368 es->b_out = chn->buf->o;
8369 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008370 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008371 es->m_clen = msg->chunk_len;
8372 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008373}
Willy Tarreaub2513902006-12-17 14:52:38 +01008374
Willy Tarreau294c4732011-12-16 21:35:50 +01008375/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8376 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8377 * performed over the whole headers. Otherwise it must contain a valid header
8378 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8379 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8380 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8381 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008382 * -1. The value fetch stops at commas, so this function is suited for use with
8383 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008384 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008385 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008386unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008387 struct hdr_idx *idx, int occ,
8388 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008389{
Willy Tarreau294c4732011-12-16 21:35:50 +01008390 struct hdr_ctx local_ctx;
8391 char *ptr_hist[MAX_HDR_HISTORY];
8392 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008393 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008394 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008395
Willy Tarreau294c4732011-12-16 21:35:50 +01008396 if (!ctx) {
8397 local_ctx.idx = 0;
8398 ctx = &local_ctx;
8399 }
8400
Willy Tarreaubce70882009-09-07 11:51:47 +02008401 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008402 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008403 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008404 occ--;
8405 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008406 *vptr = ctx->line + ctx->val;
8407 *vlen = ctx->vlen;
8408 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008409 }
8410 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008411 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008412 }
8413
8414 /* negative occurrence, we scan all the list then walk back */
8415 if (-occ > MAX_HDR_HISTORY)
8416 return 0;
8417
Willy Tarreau294c4732011-12-16 21:35:50 +01008418 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008419 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008420 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8421 len_hist[hist_ptr] = ctx->vlen;
8422 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008423 hist_ptr = 0;
8424 found++;
8425 }
8426 if (-occ > found)
8427 return 0;
8428 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008429 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8430 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8431 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008432 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008433 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008434 if (hist_ptr >= MAX_HDR_HISTORY)
8435 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008436 *vptr = ptr_hist[hist_ptr];
8437 *vlen = len_hist[hist_ptr];
8438 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008439}
8440
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008441/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8442 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8443 * performed over the whole headers. Otherwise it must contain a valid header
8444 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8445 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8446 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8447 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8448 * -1. This function differs from http_get_hdr() in that it only returns full
8449 * line header values and does not stop at commas.
8450 * The return value is 0 if nothing was found, or non-zero otherwise.
8451 */
8452unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8453 struct hdr_idx *idx, int occ,
8454 struct hdr_ctx *ctx, char **vptr, int *vlen)
8455{
8456 struct hdr_ctx local_ctx;
8457 char *ptr_hist[MAX_HDR_HISTORY];
8458 int len_hist[MAX_HDR_HISTORY];
8459 unsigned int hist_ptr;
8460 int found;
8461
8462 if (!ctx) {
8463 local_ctx.idx = 0;
8464 ctx = &local_ctx;
8465 }
8466
8467 if (occ >= 0) {
8468 /* search from the beginning */
8469 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8470 occ--;
8471 if (occ <= 0) {
8472 *vptr = ctx->line + ctx->val;
8473 *vlen = ctx->vlen;
8474 return 1;
8475 }
8476 }
8477 return 0;
8478 }
8479
8480 /* negative occurrence, we scan all the list then walk back */
8481 if (-occ > MAX_HDR_HISTORY)
8482 return 0;
8483
8484 found = hist_ptr = 0;
8485 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8486 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8487 len_hist[hist_ptr] = ctx->vlen;
8488 if (++hist_ptr >= MAX_HDR_HISTORY)
8489 hist_ptr = 0;
8490 found++;
8491 }
8492 if (-occ > found)
8493 return 0;
8494 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8495 * find occurrence -occ, so we have to check [hist_ptr+occ].
8496 */
8497 hist_ptr += occ;
8498 if (hist_ptr >= MAX_HDR_HISTORY)
8499 hist_ptr -= MAX_HDR_HISTORY;
8500 *vptr = ptr_hist[hist_ptr];
8501 *vlen = len_hist[hist_ptr];
8502 return 1;
8503}
8504
Willy Tarreaubaaee002006-06-26 02:48:02 +02008505/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008506 * Print a debug line with a header. Always stop at the first CR or LF char,
8507 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8508 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008509 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008510void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008511{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008512 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008513 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008514 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008515 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8516 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 +02008517
8518 for (max = 0; start + max < end; max++)
8519 if (start[max] == '\r' || start[max] == '\n')
8520 break;
8521
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008522 UBOUND(max, trash.size - trash.len - 3);
8523 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8524 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008525 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008526}
8527
Willy Tarreau0937bc42009-12-22 15:03:09 +01008528/*
8529 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8530 * the required fields are properly allocated and that we only need to (re)init
8531 * them. This should be used before processing any new request.
8532 */
8533void http_init_txn(struct session *s)
8534{
8535 struct http_txn *txn = &s->txn;
8536 struct proxy *fe = s->fe;
8537
8538 txn->flags = 0;
8539 txn->status = -1;
8540
Willy Tarreauf64d1412010-10-07 20:06:11 +02008541 txn->cookie_first_date = 0;
8542 txn->cookie_last_date = 0;
8543
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008544 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008545 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008546 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008547 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008548 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008549 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008550 txn->req.chunk_len = 0LL;
8551 txn->req.body_len = 0LL;
8552 txn->rsp.chunk_len = 0LL;
8553 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008554 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8555 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008556 txn->req.chn = s->req;
8557 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008558
8559 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008560
8561 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8562 if (fe->options2 & PR_O2_REQBUG_OK)
8563 txn->req.err_pos = -1; /* let buggy requests pass */
8564
Willy Tarreau46023632010-01-07 22:51:47 +01008565 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008566 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8567
Willy Tarreau46023632010-01-07 22:51:47 +01008568 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008569 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8570
8571 if (txn->hdr_idx.v)
8572 hdr_idx_init(&txn->hdr_idx);
8573}
8574
8575/* to be used at the end of a transaction */
8576void http_end_txn(struct session *s)
8577{
8578 struct http_txn *txn = &s->txn;
8579
Willy Tarreau75195602014-03-11 15:48:55 +01008580 /* release any possible compression context */
8581 if (s->flags & SN_COMP_READY)
8582 s->comp_algo->end(&s->comp_ctx);
8583 s->comp_algo = NULL;
8584 s->flags &= ~SN_COMP_READY;
8585
Willy Tarreau0937bc42009-12-22 15:03:09 +01008586 /* these ones will have been dynamically allocated */
8587 pool_free2(pool2_requri, txn->uri);
8588 pool_free2(pool2_capture, txn->cli_cookie);
8589 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008590 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008591 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008592
William Lallemanda73203e2012-03-12 12:48:57 +01008593 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008594 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008595 txn->uri = NULL;
8596 txn->srv_cookie = NULL;
8597 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008598
8599 if (txn->req.cap) {
8600 struct cap_hdr *h;
8601 for (h = s->fe->req_cap; h; h = h->next)
8602 pool_free2(h->pool, txn->req.cap[h->index]);
8603 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8604 }
8605
8606 if (txn->rsp.cap) {
8607 struct cap_hdr *h;
8608 for (h = s->fe->rsp_cap; h; h = h->next)
8609 pool_free2(h->pool, txn->rsp.cap[h->index]);
8610 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8611 }
8612
Willy Tarreau0937bc42009-12-22 15:03:09 +01008613}
8614
8615/* to be used at the end of a transaction to prepare a new one */
8616void http_reset_txn(struct session *s)
8617{
8618 http_end_txn(s);
8619 http_init_txn(s);
8620
8621 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008622 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008623 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008624 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008625 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008626 /* re-init store persistence */
8627 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008628 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008629
Willy Tarreau0937bc42009-12-22 15:03:09 +01008630 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008631
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008632 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008633
Willy Tarreau739cfba2010-01-25 23:11:14 +01008634 /* We must trim any excess data from the response buffer, because we
8635 * may have blocked an invalid response from a server that we don't
8636 * want to accidentely forward once we disable the analysers, nor do
8637 * we want those data to come along with next response. A typical
8638 * example of such data would be from a buggy server responding to
8639 * a HEAD with some data, or sending more than the advertised
8640 * content-length.
8641 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008642 if (unlikely(s->rep->buf->i))
8643 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008644
Willy Tarreau0937bc42009-12-22 15:03:09 +01008645 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008646 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008647
Willy Tarreaud04e8582010-05-31 12:31:35 +02008648 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008649 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008650
8651 s->req->rex = TICK_ETERNITY;
8652 s->req->wex = TICK_ETERNITY;
8653 s->req->analyse_exp = TICK_ETERNITY;
8654 s->rep->rex = TICK_ETERNITY;
8655 s->rep->wex = TICK_ETERNITY;
8656 s->rep->analyse_exp = TICK_ETERNITY;
8657}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008658
Willy Tarreauff011f22011-01-06 17:51:27 +01008659void free_http_req_rules(struct list *r) {
8660 struct http_req_rule *tr, *pr;
8661
8662 list_for_each_entry_safe(pr, tr, r, list) {
8663 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008664 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008665 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008666
8667 free(pr);
8668 }
8669}
8670
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008671/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008672struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8673{
8674 struct http_req_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02008675 struct http_req_action_kw *custom = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008676 int cur_arg;
8677
8678 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8679 if (!rule) {
8680 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008681 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008682 }
8683
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008684 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008685 rule->action = HTTP_REQ_ACT_ALLOW;
8686 cur_arg = 1;
8687 } else if (!strcmp(args[0], "deny")) {
8688 rule->action = HTTP_REQ_ACT_DENY;
8689 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008690 } else if (!strcmp(args[0], "tarpit")) {
8691 rule->action = HTTP_REQ_ACT_TARPIT;
8692 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008693 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008694 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008695 cur_arg = 1;
8696
8697 while(*args[cur_arg]) {
8698 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008699 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008700 cur_arg+=2;
8701 continue;
8702 } else
8703 break;
8704 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008705 } else if (!strcmp(args[0], "set-nice")) {
8706 rule->action = HTTP_REQ_ACT_SET_NICE;
8707 cur_arg = 1;
8708
8709 if (!*args[cur_arg] ||
8710 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8711 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8712 file, linenum, args[0]);
8713 goto out_err;
8714 }
8715 rule->arg.nice = atoi(args[cur_arg]);
8716 if (rule->arg.nice < -1024)
8717 rule->arg.nice = -1024;
8718 else if (rule->arg.nice > 1024)
8719 rule->arg.nice = 1024;
8720 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008721 } else if (!strcmp(args[0], "set-tos")) {
8722#ifdef IP_TOS
8723 char *err;
8724 rule->action = HTTP_REQ_ACT_SET_TOS;
8725 cur_arg = 1;
8726
8727 if (!*args[cur_arg] ||
8728 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8729 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8730 file, linenum, args[0]);
8731 goto out_err;
8732 }
8733
8734 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8735 if (err && *err != '\0') {
8736 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8737 file, linenum, err, args[0]);
8738 goto out_err;
8739 }
8740 cur_arg++;
8741#else
8742 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8743 goto out_err;
8744#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008745 } else if (!strcmp(args[0], "set-mark")) {
8746#ifdef SO_MARK
8747 char *err;
8748 rule->action = HTTP_REQ_ACT_SET_MARK;
8749 cur_arg = 1;
8750
8751 if (!*args[cur_arg] ||
8752 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8753 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8754 file, linenum, args[0]);
8755 goto out_err;
8756 }
8757
8758 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8759 if (err && *err != '\0') {
8760 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8761 file, linenum, err, args[0]);
8762 goto out_err;
8763 }
8764 cur_arg++;
8765 global.last_checks |= LSTCHK_NETADM;
8766#else
8767 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8768 goto out_err;
8769#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008770 } else if (!strcmp(args[0], "set-log-level")) {
8771 rule->action = HTTP_REQ_ACT_SET_LOGL;
8772 cur_arg = 1;
8773
8774 if (!*args[cur_arg] ||
8775 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8776 bad_log_level:
8777 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8778 file, linenum, args[0]);
8779 goto out_err;
8780 }
8781 if (strcmp(args[cur_arg], "silent") == 0)
8782 rule->arg.loglevel = -1;
8783 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8784 goto bad_log_level;
8785 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008786 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8787 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8788 cur_arg = 1;
8789
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008790 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8791 (*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 +01008792 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8793 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008794 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008795 }
8796
8797 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8798 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8799 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008800
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008801 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008802 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008803 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8804 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008805 free(proxy->conf.lfs_file);
8806 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8807 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008808 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008809 } else if (strcmp(args[0], "del-header") == 0) {
8810 rule->action = HTTP_REQ_ACT_DEL_HDR;
8811 cur_arg = 1;
8812
8813 if (!*args[cur_arg] ||
8814 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8815 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8816 file, linenum, args[0]);
8817 goto out_err;
8818 }
8819
8820 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8821 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8822
8823 proxy->conf.args.ctx = ARGC_HRQ;
8824 free(proxy->conf.lfs_file);
8825 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8826 proxy->conf.lfs_line = proxy->conf.args.line;
8827 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008828 } else if (strcmp(args[0], "redirect") == 0) {
8829 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008830 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008831
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008832 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008833 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8834 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8835 goto out_err;
8836 }
8837
8838 /* this redirect rule might already contain a parsed condition which
8839 * we'll pass to the http-request rule.
8840 */
8841 rule->action = HTTP_REQ_ACT_REDIR;
8842 rule->arg.redir = redir;
8843 rule->cond = redir->cond;
8844 redir->cond = NULL;
8845 cur_arg = 2;
8846 return rule;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008847 } else if (strncmp(args[0], "add-acl", 7) == 0) {
8848 /* http-request add-acl(<reference (acl name)>) <key pattern> */
8849 rule->action = HTTP_REQ_ACT_ADD_ACL;
8850 /*
8851 * '+ 8' for 'add-acl('
8852 * '- 9' for 'add-acl(' + trailing ')'
8853 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008854 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008855
8856 cur_arg = 1;
8857
8858 if (!*args[cur_arg] ||
8859 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8860 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8861 file, linenum, args[0]);
8862 goto out_err;
8863 }
8864
8865 LIST_INIT(&rule->arg.map.key);
8866 proxy->conf.args.ctx = ARGC_HRQ;
8867 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8868 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8869 file, linenum);
8870 free(proxy->conf.lfs_file);
8871 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8872 proxy->conf.lfs_line = proxy->conf.args.line;
8873 cur_arg += 1;
8874 } else if (strncmp(args[0], "del-acl", 7) == 0) {
8875 /* http-request del-acl(<reference (acl name)>) <key pattern> */
8876 rule->action = HTTP_REQ_ACT_DEL_ACL;
8877 /*
8878 * '+ 8' for 'del-acl('
8879 * '- 9' for 'del-acl(' + trailing ')'
8880 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008881 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008882
8883 cur_arg = 1;
8884
8885 if (!*args[cur_arg] ||
8886 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8887 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8888 file, linenum, args[0]);
8889 goto out_err;
8890 }
8891
8892 LIST_INIT(&rule->arg.map.key);
8893 proxy->conf.args.ctx = ARGC_HRQ;
8894 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8895 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8896 file, linenum);
8897 free(proxy->conf.lfs_file);
8898 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8899 proxy->conf.lfs_line = proxy->conf.args.line;
8900 cur_arg += 1;
8901 } else if (strncmp(args[0], "del-map", 7) == 0) {
8902 /* http-request del-map(<reference (map name)>) <key pattern> */
8903 rule->action = HTTP_REQ_ACT_DEL_MAP;
8904 /*
8905 * '+ 8' for 'del-map('
8906 * '- 9' for 'del-map(' + trailing ')'
8907 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008908 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008909
8910 cur_arg = 1;
8911
8912 if (!*args[cur_arg] ||
8913 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8914 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8915 file, linenum, args[0]);
8916 goto out_err;
8917 }
8918
8919 LIST_INIT(&rule->arg.map.key);
8920 proxy->conf.args.ctx = ARGC_HRQ;
8921 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8922 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8923 file, linenum);
8924 free(proxy->conf.lfs_file);
8925 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8926 proxy->conf.lfs_line = proxy->conf.args.line;
8927 cur_arg += 1;
8928 } else if (strncmp(args[0], "set-map", 7) == 0) {
8929 /* http-request set-map(<reference (map name)>) <key pattern> <value pattern> */
8930 rule->action = HTTP_REQ_ACT_SET_MAP;
8931 /*
8932 * '+ 8' for 'set-map('
8933 * '- 9' for 'set-map(' + trailing ')'
8934 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02008935 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008936
8937 cur_arg = 1;
8938
8939 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8940 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8941 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8942 file, linenum, args[0]);
8943 goto out_err;
8944 }
8945
8946 LIST_INIT(&rule->arg.map.key);
8947 LIST_INIT(&rule->arg.map.value);
8948 proxy->conf.args.ctx = ARGC_HRQ;
8949
8950 /* key pattern */
8951 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
8952 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8953 file, linenum);
8954
8955 /* value pattern */
8956 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
8957 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8958 file, linenum);
8959 free(proxy->conf.lfs_file);
8960 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8961 proxy->conf.lfs_line = proxy->conf.args.line;
8962
8963 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02008964 } else if (((custom = action_http_req_custom(args[0])) != NULL)) {
8965 char *errmsg = NULL;
8966 cur_arg = 1;
8967 /* try in the module list */
8968 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
8969 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8970 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8971 free(errmsg);
8972 goto out_err;
8973 }
Willy Tarreauff011f22011-01-06 17:51:27 +01008974 } else {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02008975 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', '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 +01008976 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008977 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008978 }
8979
8980 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8981 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008982 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008983
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008984 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8985 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8986 file, linenum, args[0], errmsg);
8987 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008988 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008989 }
8990 rule->cond = cond;
8991 }
8992 else if (*args[cur_arg]) {
8993 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8994 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8995 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008996 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008997 }
8998
8999 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01009000 out_err:
9001 free(rule);
9002 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01009003}
9004
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009005/* parse an "http-respose" rule */
9006struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
9007{
9008 struct http_res_rule *rule;
William Lallemand73025dd2014-04-24 14:38:37 +02009009 struct http_res_action_kw *custom = NULL;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009010 int cur_arg;
9011
9012 rule = calloc(1, sizeof(*rule));
9013 if (!rule) {
9014 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
9015 goto out_err;
9016 }
9017
9018 if (!strcmp(args[0], "allow")) {
9019 rule->action = HTTP_RES_ACT_ALLOW;
9020 cur_arg = 1;
9021 } else if (!strcmp(args[0], "deny")) {
9022 rule->action = HTTP_RES_ACT_DENY;
9023 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02009024 } else if (!strcmp(args[0], "set-nice")) {
9025 rule->action = HTTP_RES_ACT_SET_NICE;
9026 cur_arg = 1;
9027
9028 if (!*args[cur_arg] ||
9029 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9030 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
9031 file, linenum, args[0]);
9032 goto out_err;
9033 }
9034 rule->arg.nice = atoi(args[cur_arg]);
9035 if (rule->arg.nice < -1024)
9036 rule->arg.nice = -1024;
9037 else if (rule->arg.nice > 1024)
9038 rule->arg.nice = 1024;
9039 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02009040 } else if (!strcmp(args[0], "set-tos")) {
9041#ifdef IP_TOS
9042 char *err;
9043 rule->action = HTTP_RES_ACT_SET_TOS;
9044 cur_arg = 1;
9045
9046 if (!*args[cur_arg] ||
9047 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9048 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9049 file, linenum, args[0]);
9050 goto out_err;
9051 }
9052
9053 rule->arg.tos = strtol(args[cur_arg], &err, 0);
9054 if (err && *err != '\0') {
9055 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9056 file, linenum, err, args[0]);
9057 goto out_err;
9058 }
9059 cur_arg++;
9060#else
9061 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
9062 goto out_err;
9063#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02009064 } else if (!strcmp(args[0], "set-mark")) {
9065#ifdef SO_MARK
9066 char *err;
9067 rule->action = HTTP_RES_ACT_SET_MARK;
9068 cur_arg = 1;
9069
9070 if (!*args[cur_arg] ||
9071 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9072 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
9073 file, linenum, args[0]);
9074 goto out_err;
9075 }
9076
9077 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
9078 if (err && *err != '\0') {
9079 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
9080 file, linenum, err, args[0]);
9081 goto out_err;
9082 }
9083 cur_arg++;
9084 global.last_checks |= LSTCHK_NETADM;
9085#else
9086 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
9087 goto out_err;
9088#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02009089 } else if (!strcmp(args[0], "set-log-level")) {
9090 rule->action = HTTP_RES_ACT_SET_LOGL;
9091 cur_arg = 1;
9092
9093 if (!*args[cur_arg] ||
9094 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
9095 bad_log_level:
9096 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
9097 file, linenum, args[0]);
9098 goto out_err;
9099 }
9100 if (strcmp(args[cur_arg], "silent") == 0)
9101 rule->arg.loglevel = -1;
9102 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
9103 goto bad_log_level;
9104 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009105 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
9106 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
9107 cur_arg = 1;
9108
9109 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9110 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9111 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9112 file, linenum, args[0]);
9113 goto out_err;
9114 }
9115
9116 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9117 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9118 LIST_INIT(&rule->arg.hdr_add.fmt);
9119
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01009120 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009121 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009122 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9123 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009124 free(proxy->conf.lfs_file);
9125 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9126 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009127 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009128 } else if (strcmp(args[0], "del-header") == 0) {
9129 rule->action = HTTP_RES_ACT_DEL_HDR;
9130 cur_arg = 1;
9131
9132 if (!*args[cur_arg] ||
9133 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9134 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9135 file, linenum, args[0]);
9136 goto out_err;
9137 }
9138
9139 rule->arg.hdr_add.name = strdup(args[cur_arg]);
9140 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
9141
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009142 proxy->conf.args.ctx = ARGC_HRS;
9143 free(proxy->conf.lfs_file);
9144 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9145 proxy->conf.lfs_line = proxy->conf.args.line;
9146 cur_arg += 1;
9147 } else if (strncmp(args[0], "add-acl", 7) == 0) {
9148 /* http-request add-acl(<reference (acl name)>) <key pattern> */
9149 rule->action = HTTP_RES_ACT_ADD_ACL;
9150 /*
9151 * '+ 8' for 'add-acl('
9152 * '- 9' for 'add-acl(' + trailing ')'
9153 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009154 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009155
9156 cur_arg = 1;
9157
9158 if (!*args[cur_arg] ||
9159 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9160 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9161 file, linenum, args[0]);
9162 goto out_err;
9163 }
9164
9165 LIST_INIT(&rule->arg.map.key);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009166 proxy->conf.args.ctx = ARGC_HRS;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009167 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9168 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9169 file, linenum);
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009170 free(proxy->conf.lfs_file);
9171 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9172 proxy->conf.lfs_line = proxy->conf.args.line;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009173
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02009174 cur_arg += 1;
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009175 } else if (strncmp(args[0], "del-acl", 7) == 0) {
9176 /* http-response del-acl(<reference (acl name)>) <key pattern> */
9177 rule->action = HTTP_RES_ACT_DEL_ACL;
9178 /*
9179 * '+ 8' for 'del-acl('
9180 * '- 9' for 'del-acl(' + trailing ')'
9181 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009182 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009183
9184 cur_arg = 1;
9185
9186 if (!*args[cur_arg] ||
9187 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9188 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9189 file, linenum, args[0]);
9190 goto out_err;
9191 }
9192
9193 LIST_INIT(&rule->arg.map.key);
9194 proxy->conf.args.ctx = ARGC_HRS;
9195 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9196 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9197 file, linenum);
9198 free(proxy->conf.lfs_file);
9199 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9200 proxy->conf.lfs_line = proxy->conf.args.line;
9201 cur_arg += 1;
9202 } else if (strncmp(args[0], "del-map", 7) == 0) {
9203 /* http-response del-map(<reference (map name)>) <key pattern> */
9204 rule->action = HTTP_RES_ACT_DEL_MAP;
9205 /*
9206 * '+ 8' for 'del-map('
9207 * '- 9' for 'del-map(' + trailing ')'
9208 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009209 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009210
9211 cur_arg = 1;
9212
9213 if (!*args[cur_arg] ||
9214 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
9215 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
9216 file, linenum, args[0]);
9217 goto out_err;
9218 }
9219
9220 LIST_INIT(&rule->arg.map.key);
9221 proxy->conf.args.ctx = ARGC_HRS;
9222 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9223 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9224 file, linenum);
9225 free(proxy->conf.lfs_file);
9226 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9227 proxy->conf.lfs_line = proxy->conf.args.line;
9228 cur_arg += 1;
9229 } else if (strncmp(args[0], "set-map", 7) == 0) {
9230 /* http-response set-map(<reference (map name)>) <key pattern> <value pattern> */
9231 rule->action = HTTP_RES_ACT_SET_MAP;
9232 /*
9233 * '+ 8' for 'set-map('
9234 * '- 9' for 'set-map(' + trailing ')'
9235 */
Willy Tarreau6c09c2c2014-04-25 21:38:08 +02009236 rule->arg.map.ref = my_strndup(args[0] + 8, strlen(args[0]) - 9);
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009237
9238 cur_arg = 1;
9239
9240 if (!*args[cur_arg] || !*args[cur_arg+1] ||
9241 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
9242 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
9243 file, linenum, args[0]);
9244 goto out_err;
9245 }
9246
9247 LIST_INIT(&rule->arg.map.key);
9248 LIST_INIT(&rule->arg.map.value);
9249
9250 proxy->conf.args.ctx = ARGC_HRS;
9251
9252 /* key pattern */
9253 parse_logformat_string(args[cur_arg], proxy, &rule->arg.map.key, LOG_OPT_HTTP,
9254 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9255 file, linenum);
9256
9257 /* value pattern */
9258 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.map.value, LOG_OPT_HTTP,
9259 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
9260 file, linenum);
9261
9262 free(proxy->conf.lfs_file);
9263 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
9264 proxy->conf.lfs_line = proxy->conf.args.line;
9265
9266 cur_arg += 2;
William Lallemand73025dd2014-04-24 14:38:37 +02009267 } else if (((custom = action_http_res_custom(args[0])) != NULL)) {
9268 char *errmsg = NULL;
9269 cur_arg = 1;
9270 /* try in the module list */
9271 if (custom->parse(args, &cur_arg, proxy, rule, &errmsg) < 0) {
9272 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-response %s' rule : %s.\n",
9273 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
9274 free(errmsg);
9275 goto out_err;
9276 }
Willy Tarreaue365c0b2013-06-11 16:06:12 +02009277 } else {
Baptiste Assmannfabcbe02014-04-24 22:16:59 +02009278 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'del-header', 'set-header', '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 +02009279 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
9280 goto out_err;
9281 }
9282
9283 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
9284 struct acl_cond *cond;
9285 char *errmsg = NULL;
9286
9287 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
9288 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
9289 file, linenum, args[0], errmsg);
9290 free(errmsg);
9291 goto out_err;
9292 }
9293 rule->cond = cond;
9294 }
9295 else if (*args[cur_arg]) {
9296 Alert("parsing [%s:%d]: 'http-response %s' expects"
9297 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
9298 file, linenum, args[0], args[cur_arg]);
9299 goto out_err;
9300 }
9301
9302 return rule;
9303 out_err:
9304 free(rule);
9305 return NULL;
9306}
9307
Willy Tarreau4baae242012-12-27 12:00:31 +01009308/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009309 * with <err> filled with the error message. If <use_fmt> is not null, builds a
9310 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01009311 */
9312struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009313 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01009314{
9315 struct redirect_rule *rule;
9316 int cur_arg;
9317 int type = REDIRECT_TYPE_NONE;
9318 int code = 302;
9319 const char *destination = NULL;
9320 const char *cookie = NULL;
9321 int cookie_set = 0;
9322 unsigned int flags = REDIRECT_FLAG_NONE;
9323 struct acl_cond *cond = NULL;
9324
9325 cur_arg = 0;
9326 while (*(args[cur_arg])) {
9327 if (strcmp(args[cur_arg], "location") == 0) {
9328 if (!*args[cur_arg + 1])
9329 goto missing_arg;
9330
9331 type = REDIRECT_TYPE_LOCATION;
9332 cur_arg++;
9333 destination = args[cur_arg];
9334 }
9335 else if (strcmp(args[cur_arg], "prefix") == 0) {
9336 if (!*args[cur_arg + 1])
9337 goto missing_arg;
9338
9339 type = REDIRECT_TYPE_PREFIX;
9340 cur_arg++;
9341 destination = args[cur_arg];
9342 }
9343 else if (strcmp(args[cur_arg], "scheme") == 0) {
9344 if (!*args[cur_arg + 1])
9345 goto missing_arg;
9346
9347 type = REDIRECT_TYPE_SCHEME;
9348 cur_arg++;
9349 destination = args[cur_arg];
9350 }
9351 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
9352 if (!*args[cur_arg + 1])
9353 goto missing_arg;
9354
9355 cur_arg++;
9356 cookie = args[cur_arg];
9357 cookie_set = 1;
9358 }
9359 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
9360 if (!*args[cur_arg + 1])
9361 goto missing_arg;
9362
9363 cur_arg++;
9364 cookie = args[cur_arg];
9365 cookie_set = 0;
9366 }
9367 else if (strcmp(args[cur_arg], "code") == 0) {
9368 if (!*args[cur_arg + 1])
9369 goto missing_arg;
9370
9371 cur_arg++;
9372 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009373 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01009374 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04009375 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01009376 args[cur_arg - 1], args[cur_arg]);
9377 return NULL;
9378 }
9379 }
9380 else if (!strcmp(args[cur_arg],"drop-query")) {
9381 flags |= REDIRECT_FLAG_DROP_QS;
9382 }
9383 else if (!strcmp(args[cur_arg],"append-slash")) {
9384 flags |= REDIRECT_FLAG_APPEND_SLASH;
9385 }
9386 else if (strcmp(args[cur_arg], "if") == 0 ||
9387 strcmp(args[cur_arg], "unless") == 0) {
9388 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
9389 if (!cond) {
9390 memprintf(errmsg, "error in condition: %s", *errmsg);
9391 return NULL;
9392 }
9393 break;
9394 }
9395 else {
9396 memprintf(errmsg,
9397 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
9398 args[cur_arg]);
9399 return NULL;
9400 }
9401 cur_arg++;
9402 }
9403
9404 if (type == REDIRECT_TYPE_NONE) {
9405 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
9406 return NULL;
9407 }
9408
9409 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
9410 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01009411 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009412
9413 if (!use_fmt) {
9414 /* old-style static redirect rule */
9415 rule->rdr_str = strdup(destination);
9416 rule->rdr_len = strlen(destination);
9417 }
9418 else {
9419 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009420
9421 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
9422 * if prefix == "/", we don't want to add anything, otherwise it
9423 * makes it hard for the user to configure a self-redirection.
9424 */
9425 proxy->conf.args.ctx = ARGC_RDR;
9426 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01009427 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01009428 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
9429 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01009430 free(curproxy->conf.lfs_file);
9431 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
9432 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01009433 }
9434 }
9435
Willy Tarreau4baae242012-12-27 12:00:31 +01009436 if (cookie) {
9437 /* depending on cookie_set, either we want to set the cookie, or to clear it.
9438 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
9439 */
9440 rule->cookie_len = strlen(cookie);
9441 if (cookie_set) {
9442 rule->cookie_str = malloc(rule->cookie_len + 10);
9443 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9444 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
9445 rule->cookie_len += 9;
9446 } else {
9447 rule->cookie_str = malloc(rule->cookie_len + 21);
9448 memcpy(rule->cookie_str, cookie, rule->cookie_len);
9449 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9450 rule->cookie_len += 20;
9451 }
9452 }
9453 rule->type = type;
9454 rule->code = code;
9455 rule->flags = flags;
9456 LIST_INIT(&rule->list);
9457 return rule;
9458
9459 missing_arg:
9460 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9461 return NULL;
9462}
9463
Willy Tarreau8797c062007-05-07 00:55:35 +02009464/************************************************************************/
9465/* The code below is dedicated to ACL parsing and matching */
9466/************************************************************************/
9467
9468
Willy Tarreau14174bc2012-04-16 14:34:04 +02009469/* This function ensures that the prerequisites for an L7 fetch are ready,
9470 * which means that a request or response is ready. If some data is missing,
9471 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009472 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9473 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009474 *
9475 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009476 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9477 * decide whether or not an HTTP message is present ;
9478 * 0 if the requested data cannot be fetched or if it is certain that
9479 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009480 * 1 if an HTTP message is ready
9481 */
9482static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009483smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009484 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009485{
9486 struct http_txn *txn = l7;
9487 struct http_msg *msg = &txn->req;
9488
9489 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9490 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9491 */
9492
9493 if (unlikely(!s || !txn))
9494 return 0;
9495
9496 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009497 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009498
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009499 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009500 if (unlikely(!s->req))
9501 return 0;
9502
Willy Tarreauaae75e32013-03-29 12:31:49 +01009503 /* If the buffer does not leave enough free space at the end,
9504 * we must first realign it.
9505 */
9506 if (s->req->buf->p > s->req->buf->data &&
9507 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9508 buffer_slow_realign(s->req->buf);
9509
Willy Tarreau14174bc2012-04-16 14:34:04 +02009510 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009511 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009512 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009513
9514 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009515 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009516 http_msg_analyzer(msg, &txn->hdr_idx);
9517
9518 /* Still no valid request ? */
9519 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009520 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009521 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009522 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009523 }
9524 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009525 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009526 return 0;
9527 }
9528
9529 /* OK we just got a valid HTTP request. We have some minor
9530 * preparation to perform so that further checks can rely
9531 * on HTTP tests.
9532 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009533
9534 /* If the request was parsed but was too large, we must absolutely
9535 * return an error so that it is not processed. At the moment this
9536 * cannot happen, but if the parsers are to change in the future,
9537 * we want this check to be maintained.
9538 */
9539 if (unlikely(s->req->buf->i + s->req->buf->p >
9540 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9541 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009542 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009543 return 1;
9544 }
9545
Willy Tarreau9b28e032012-10-12 23:49:43 +02009546 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009547 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9548 s->flags |= SN_REDIRECTABLE;
9549
Willy Tarreau506d0502013-07-06 13:29:24 +02009550 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9551 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009552 }
9553
Willy Tarreau506d0502013-07-06 13:29:24 +02009554 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009555 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009556 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009557
9558 /* otherwise everything's ready for the request */
9559 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009560 else {
9561 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009562 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9563 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009564 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009565 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009566 }
9567
9568 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009569 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009570 return 1;
9571}
Willy Tarreau8797c062007-05-07 00:55:35 +02009572
Willy Tarreauc0239e02012-04-16 14:42:55 +02009573#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009574 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 +02009575
Willy Tarreau24e32d82012-04-23 23:55:44 +02009576#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009577 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 +02009578
Willy Tarreau8797c062007-05-07 00:55:35 +02009579
9580/* 1. Check on METHOD
9581 * We use the pre-parsed method if it is known, and store its number as an
9582 * integer. If it is unknown, we use the pointer and the length.
9583 */
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009584static int pat_parse_meth(const char *text, struct pattern *pattern, int mflags, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009585{
9586 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009587 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02009588
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009589 len = strlen(text);
9590 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009591
9592 pattern->val.i = meth;
9593 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009594 trash = get_trash_chunk();
9595 if (trash->size < len) {
9596 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
9597 len, trash->size);
9598 return 0;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009599 }
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009600 pattern->ptr.str = trash->str;
Willy Tarreau8797c062007-05-07 00:55:35 +02009601 pattern->len = len;
9602 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009603 else {
9604 pattern->ptr.str = NULL;
9605 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009606 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009607 return 1;
9608}
9609
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009610/* This function fetches the method of current HTTP request and stores
9611 * it in the global pattern struct as a chunk. There are two possibilities :
9612 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9613 * in <len> and <ptr> is NULL ;
9614 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9615 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009616 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009617 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009618static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009619smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009620 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009621{
9622 int meth;
9623 struct http_txn *txn = l7;
9624
Willy Tarreau24e32d82012-04-23 23:55:44 +02009625 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009626
Willy Tarreau8797c062007-05-07 00:55:35 +02009627 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009628 smp->type = SMP_T_METH;
9629 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009630 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009631 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9632 /* ensure the indexes are not affected */
9633 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009634 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009635 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9636 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009637 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009638 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009639 return 1;
9640}
9641
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009642/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009643static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009644{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009645 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009646 struct pattern_list *lst;
9647 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009648
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009649 list_for_each_entry(lst, &expr->patterns, list) {
9650 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009651
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009652 /* well-known method */
9653 if (pattern->val.i != HTTP_METH_OTHER) {
9654 if (smp->data.meth.meth == pattern->val.i)
9655 return pattern;
9656 else
9657 continue;
9658 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009659
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009660 /* Other method, we must compare the strings */
9661 if (pattern->len != smp->data.meth.str.len)
9662 continue;
9663
Thierry FOURNIERe47e4e22014-04-28 11:18:57 +02009664 icase = expr->mflags & PAT_MF_IGNORE_CASE;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009665 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0) ||
9666 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0))
9667 return pattern;
9668 }
9669 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009670}
9671
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009672static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009673smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009674 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009675{
9676 struct http_txn *txn = l7;
9677 char *ptr;
9678 int len;
9679
Willy Tarreauc0239e02012-04-16 14:42:55 +02009680 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009681
Willy Tarreau8797c062007-05-07 00:55:35 +02009682 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009683 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009684
9685 while ((len-- > 0) && (*ptr++ != '/'));
9686 if (len <= 0)
9687 return 0;
9688
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009689 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009690 smp->data.str.str = ptr;
9691 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009692
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009693 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009694 return 1;
9695}
9696
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009697static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009698smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009699 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009700{
9701 struct http_txn *txn = l7;
9702 char *ptr;
9703 int len;
9704
Willy Tarreauc0239e02012-04-16 14:42:55 +02009705 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009706
Willy Tarreauf26b2522012-12-14 08:33:14 +01009707 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9708 return 0;
9709
Willy Tarreau8797c062007-05-07 00:55:35 +02009710 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009711 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009712
9713 while ((len-- > 0) && (*ptr++ != '/'));
9714 if (len <= 0)
9715 return 0;
9716
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009717 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009718 smp->data.str.str = ptr;
9719 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009720
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009721 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009722 return 1;
9723}
9724
9725/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009726static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009727smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009728 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009729{
9730 struct http_txn *txn = l7;
9731 char *ptr;
9732 int len;
9733
Willy Tarreauc0239e02012-04-16 14:42:55 +02009734 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009735
Willy Tarreauf26b2522012-12-14 08:33:14 +01009736 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9737 return 0;
9738
Willy Tarreau8797c062007-05-07 00:55:35 +02009739 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009740 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009741
Willy Tarreauf853c462012-04-23 18:53:56 +02009742 smp->type = SMP_T_UINT;
9743 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009744 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009745 return 1;
9746}
9747
9748/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009749static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009750smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009751 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009752{
9753 struct http_txn *txn = l7;
9754
Willy Tarreauc0239e02012-04-16 14:42:55 +02009755 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009756
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009757 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009758 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009759 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009760 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009761 return 1;
9762}
9763
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009764static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009765smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009766 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009767{
9768 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009769 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009770
Willy Tarreauc0239e02012-04-16 14:42:55 +02009771 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009772
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009773 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 +02009774 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009775 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009776
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009777 smp->type = SMP_T_IPV4;
9778 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009779 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009780 return 1;
9781}
9782
9783static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009784smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009785 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009786{
9787 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009788 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009789
Willy Tarreauc0239e02012-04-16 14:42:55 +02009790 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009791
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009792 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 +02009793 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9794 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009795
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009796 smp->type = SMP_T_UINT;
9797 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009798 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009799 return 1;
9800}
9801
Willy Tarreau185b5c42012-04-26 15:11:51 +02009802/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9803 * Accepts an optional argument of type string containing the header field name,
9804 * and an optional argument of type signed or unsigned integer to request an
9805 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009806 * headers are considered from the first one. It does not stop on commas and
9807 * returns full lines instead (useful for User-Agent or Date for example).
9808 */
9809static int
9810smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009811 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009812{
9813 struct http_txn *txn = l7;
9814 struct hdr_idx *idx = &txn->hdr_idx;
9815 struct hdr_ctx *ctx = smp->ctx.a[0];
9816 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9817 int occ = 0;
9818 const char *name_str = NULL;
9819 int name_len = 0;
9820
9821 if (!ctx) {
9822 /* first call */
9823 ctx = &static_hdr_ctx;
9824 ctx->idx = 0;
9825 smp->ctx.a[0] = ctx;
9826 }
9827
9828 if (args) {
9829 if (args[0].type != ARGT_STR)
9830 return 0;
9831 name_str = args[0].data.str.str;
9832 name_len = args[0].data.str.len;
9833
9834 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9835 occ = args[1].data.uint;
9836 }
9837
9838 CHECK_HTTP_MESSAGE_FIRST();
9839
9840 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9841 /* search for header from the beginning */
9842 ctx->idx = 0;
9843
9844 if (!occ && !(opt & SMP_OPT_ITERATE))
9845 /* no explicit occurrence and single fetch => last header by default */
9846 occ = -1;
9847
9848 if (!occ)
9849 /* prepare to report multiple occurrences for ACL fetches */
9850 smp->flags |= SMP_F_NOT_LAST;
9851
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009852 smp->type = SMP_T_STR;
9853 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009854 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9855 return 1;
9856
9857 smp->flags &= ~SMP_F_NOT_LAST;
9858 return 0;
9859}
9860
9861/* 6. Check on HTTP header count. The number of occurrences is returned.
9862 * Accepts exactly 1 argument of type string. It does not stop on commas and
9863 * returns full lines instead (useful for User-Agent or Date for example).
9864 */
9865static int
9866smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009867 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009868{
9869 struct http_txn *txn = l7;
9870 struct hdr_idx *idx = &txn->hdr_idx;
9871 struct hdr_ctx ctx;
9872 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9873 int cnt;
9874
9875 if (!args || args->type != ARGT_STR)
9876 return 0;
9877
9878 CHECK_HTTP_MESSAGE_FIRST();
9879
9880 ctx.idx = 0;
9881 cnt = 0;
9882 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9883 cnt++;
9884
9885 smp->type = SMP_T_UINT;
9886 smp->data.uint = cnt;
9887 smp->flags = SMP_F_VOL_HDR;
9888 return 1;
9889}
9890
9891/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9892 * Accepts an optional argument of type string containing the header field name,
9893 * and an optional argument of type signed or unsigned integer to request an
9894 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009895 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009896 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009897static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009898smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009899 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009900{
9901 struct http_txn *txn = l7;
9902 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009903 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009904 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009905 int occ = 0;
9906 const char *name_str = NULL;
9907 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009908
Willy Tarreaua890d072013-04-02 12:01:06 +02009909 if (!ctx) {
9910 /* first call */
9911 ctx = &static_hdr_ctx;
9912 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009913 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009914 }
9915
Willy Tarreau185b5c42012-04-26 15:11:51 +02009916 if (args) {
9917 if (args[0].type != ARGT_STR)
9918 return 0;
9919 name_str = args[0].data.str.str;
9920 name_len = args[0].data.str.len;
9921
9922 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9923 occ = args[1].data.uint;
9924 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009925
Willy Tarreaue333ec92012-04-16 16:26:40 +02009926 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009927
Willy Tarreau185b5c42012-04-26 15:11:51 +02009928 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009929 /* search for header from the beginning */
9930 ctx->idx = 0;
9931
Willy Tarreau185b5c42012-04-26 15:11:51 +02009932 if (!occ && !(opt & SMP_OPT_ITERATE))
9933 /* no explicit occurrence and single fetch => last header by default */
9934 occ = -1;
9935
9936 if (!occ)
9937 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009938 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009939
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009940 smp->type = SMP_T_STR;
9941 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009942 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 +02009943 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009944
Willy Tarreau37406352012-04-23 16:16:37 +02009945 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009946 return 0;
9947}
9948
Willy Tarreauc11416f2007-06-17 16:58:38 +02009949/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009950 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009951 */
9952static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009953smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009954 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009955{
9956 struct http_txn *txn = l7;
9957 struct hdr_idx *idx = &txn->hdr_idx;
9958 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009959 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009960 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009961
Willy Tarreau24e32d82012-04-23 23:55:44 +02009962 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009963 return 0;
9964
Willy Tarreaue333ec92012-04-16 16:26:40 +02009965 CHECK_HTTP_MESSAGE_FIRST();
9966
Willy Tarreau33a7e692007-06-10 19:45:56 +02009967 ctx.idx = 0;
9968 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009969 while (http_find_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009970 cnt++;
9971
Willy Tarreauf853c462012-04-23 18:53:56 +02009972 smp->type = SMP_T_UINT;
9973 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009974 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009975 return 1;
9976}
9977
Willy Tarreau185b5c42012-04-26 15:11:51 +02009978/* Fetch an HTTP header's integer value. The integer value is returned. It
9979 * takes a mandatory argument of type string and an optional one of type int
9980 * to designate a specific occurrence. It returns an unsigned integer, which
9981 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009982 */
9983static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009984smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009985 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009986{
Willy Tarreauef38c392013-07-22 16:29:32 +02009987 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009988
Willy Tarreauf853c462012-04-23 18:53:56 +02009989 if (ret > 0) {
9990 smp->type = SMP_T_UINT;
9991 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9992 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009993
Willy Tarreaud53e2422012-04-16 17:21:11 +02009994 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009995}
9996
Cyril Bonté69fa9922012-10-25 00:01:06 +02009997/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9998 * and an optional one of type int to designate a specific occurrence.
9999 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +020010000 */
10001static int
Willy Tarreau185b5c42012-04-26 15:11:51 +020010002smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010003 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +020010004{
Willy Tarreaud53e2422012-04-16 17:21:11 +020010005 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010006
Willy Tarreauef38c392013-07-22 16:29:32 +020010007 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +020010008 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
10009 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +020010010 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +020010011 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +010010012 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +020010013 if (smp->data.str.len < temp->size - 1) {
10014 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
10015 temp->str[smp->data.str.len] = '\0';
10016 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
10017 smp->type = SMP_T_IPV6;
10018 break;
10019 }
10020 }
10021 }
10022
Willy Tarreaud53e2422012-04-16 17:21:11 +020010023 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +020010024 if (!(smp->flags & SMP_F_NOT_LAST))
10025 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +020010026 }
Willy Tarreaud53e2422012-04-16 17:21:11 +020010027 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +020010028}
10029
Willy Tarreau737b0c12007-06-10 21:28:46 +020010030/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
10031 * the first '/' after the possible hostname, and ends before the possible '?'.
10032 */
10033static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +020010034smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010035 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010036{
10037 struct http_txn *txn = l7;
10038 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +020010039
Willy Tarreauc0239e02012-04-16 14:42:55 +020010040 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +020010041
Willy Tarreau9b28e032012-10-12 23:49:43 +020010042 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +010010043 ptr = http_get_path(txn);
10044 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +020010045 return 0;
10046
10047 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010048 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +020010049 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010050
10051 while (ptr < end && *ptr != '?')
10052 ptr++;
10053
Willy Tarreauf853c462012-04-23 18:53:56 +020010054 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010055 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +020010056 return 1;
10057}
10058
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010059/* This produces a concatenation of the first occurrence of the Host header
10060 * followed by the path component if it begins with a slash ('/'). This means
10061 * that '*' will not be added, resulting in exactly the first Host entry.
10062 * If no Host header is found, then the path is returned as-is. The returned
10063 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010064 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010065 */
10066static int
10067smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010068 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010069{
10070 struct http_txn *txn = l7;
10071 char *ptr, *end, *beg;
10072 struct hdr_ctx ctx;
10073
10074 CHECK_HTTP_MESSAGE_FIRST();
10075
10076 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010077 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +020010078 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010079
10080 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +010010081 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010082 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +010010083 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010084 smp->data.str.len = ctx.vlen;
10085
10086 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010087 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010088 beg = http_get_path(txn);
10089 if (!beg)
10090 beg = end;
10091
10092 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10093
10094 if (beg < ptr && *beg == '/') {
10095 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
10096 smp->data.str.len += ptr - beg;
10097 }
10098
10099 smp->flags = SMP_F_VOL_1ST;
10100 return 1;
10101}
10102
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010103/* This produces a 32-bit hash of the concatenation of the first occurrence of
10104 * the Host header followed by the path component if it begins with a slash ('/').
10105 * This means that '*' will not be added, resulting in exactly the first Host
10106 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010107 * is hashed using the path hash followed by a full avalanche hash and provides a
10108 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010109 * high-traffic sites without having to store whole paths.
10110 */
10111static int
10112smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010113 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010114{
10115 struct http_txn *txn = l7;
10116 struct hdr_ctx ctx;
10117 unsigned int hash = 0;
10118 char *ptr, *beg, *end;
10119 int len;
10120
10121 CHECK_HTTP_MESSAGE_FIRST();
10122
10123 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010124 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010125 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10126 ptr = ctx.line + ctx.val;
10127 len = ctx.vlen;
10128 while (len--)
10129 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10130 }
10131
10132 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010133 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +010010134 beg = http_get_path(txn);
10135 if (!beg)
10136 beg = end;
10137
10138 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
10139
10140 if (beg < ptr && *beg == '/') {
10141 while (beg < ptr)
10142 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10143 }
10144 hash = full_hash(hash);
10145
10146 smp->type = SMP_T_UINT;
10147 smp->data.uint = hash;
10148 smp->flags = SMP_F_VOL_1ST;
10149 return 1;
10150}
10151
Willy Tarreau4a550602012-12-09 14:53:32 +010010152/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010153 * path as returned by smp_fetch_base32(). The idea is to have per-source and
10154 * per-path counters. The result is a binary block from 8 to 20 bytes depending
10155 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +010010156 * that in environments where IPv6 is insignificant, truncating the output to
10157 * 8 bytes would still work.
10158 */
10159static int
10160smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010161 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +010010162{
Willy Tarreau47ca5452012-12-23 20:22:19 +010010163 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010164 struct connection *cli_conn = objt_conn(l4->si[0].end);
10165
10166 if (!cli_conn)
10167 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +010010168
Willy Tarreauef38c392013-07-22 16:29:32 +020010169 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +010010170 return 0;
10171
Willy Tarreau47ca5452012-12-23 20:22:19 +010010172 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +010010173 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10174 temp->len += sizeof(smp->data.uint);
10175
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010176 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +010010177 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010178 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +010010179 temp->len += 4;
10180 break;
10181 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010182 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +010010183 temp->len += 16;
10184 break;
10185 default:
10186 return 0;
10187 }
10188
10189 smp->data.str = *temp;
10190 smp->type = SMP_T_BIN;
10191 return 1;
10192}
10193
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010194static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010195smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010196 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010197{
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010198 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
10199 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
10200 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010201
Willy Tarreau24e32d82012-04-23 23:55:44 +020010202 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010203
Willy Tarreauf853c462012-04-23 18:53:56 +020010204 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010205 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +020010206 return 1;
10207}
10208
Willy Tarreau7f18e522010-10-22 20:04:13 +020010209/* return a valid test if the current request is the first one on the connection */
10210static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010211smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010212 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +020010213{
10214 if (!s)
10215 return 0;
10216
Willy Tarreauf853c462012-04-23 18:53:56 +020010217 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +020010218 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +020010219 return 1;
10220}
10221
Willy Tarreau34db1082012-04-19 17:16:54 +020010222/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010223static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010224smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010225 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010226{
10227
Willy Tarreau24e32d82012-04-23 23:55:44 +020010228 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010229 return 0;
10230
Willy Tarreauc0239e02012-04-16 14:42:55 +020010231 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010232
Willy Tarreauc0239e02012-04-16 14:42:55 +020010233 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010234 return 0;
10235
Willy Tarreauf853c462012-04-23 18:53:56 +020010236 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010237 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010010238 return 1;
10239}
Willy Tarreau8797c062007-05-07 00:55:35 +020010240
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010241/* Accepts exactly 1 argument of type userlist */
10242static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010243smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010244 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010245{
10246
10247 if (!args || args->type != ARGT_USR)
10248 return 0;
10249
10250 CHECK_HTTP_MESSAGE_FIRST();
10251
10252 if (!get_http_auth(l4))
10253 return 0;
10254
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010255 /* if the user does not belong to the userlist or has a wrong password,
10256 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010257 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010258 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010259 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
10260 return 0;
10261
10262 /* pat_match_auth() will need the user list */
10263 smp->ctx.a[0] = args->data.usr;
10264
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010265 smp->type = SMP_T_STR;
10266 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +010010267 smp->data.str.str = l4->txn.auth.user;
10268 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +020010269
10270 return 1;
10271}
10272
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010273/* Try to find the next occurrence of a cookie name in a cookie header value.
10274 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
10275 * the cookie value is returned into *value and *value_l, and the function
10276 * returns a pointer to the next pointer to search from if the value was found.
10277 * Otherwise if the cookie was not found, NULL is returned and neither value
10278 * nor value_l are touched. The input <hdr> string should first point to the
10279 * header's value, and the <hdr_end> pointer must point to the first character
10280 * not part of the value. <list> must be non-zero if value may represent a list
10281 * of values (cookie headers). This makes it faster to abort parsing when no
10282 * list is expected.
10283 */
10284static char *
10285extract_cookie_value(char *hdr, const char *hdr_end,
10286 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +020010287 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010288{
10289 char *equal, *att_end, *att_beg, *val_beg, *val_end;
10290 char *next;
10291
10292 /* we search at least a cookie name followed by an equal, and more
10293 * generally something like this :
10294 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
10295 */
10296 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
10297 /* Iterate through all cookies on this line */
10298
10299 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
10300 att_beg++;
10301
10302 /* find att_end : this is the first character after the last non
10303 * space before the equal. It may be equal to hdr_end.
10304 */
10305 equal = att_end = att_beg;
10306
10307 while (equal < hdr_end) {
10308 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
10309 break;
10310 if (http_is_spht[(unsigned char)*equal++])
10311 continue;
10312 att_end = equal;
10313 }
10314
10315 /* here, <equal> points to '=', a delimitor or the end. <att_end>
10316 * is between <att_beg> and <equal>, both may be identical.
10317 */
10318
10319 /* look for end of cookie if there is an equal sign */
10320 if (equal < hdr_end && *equal == '=') {
10321 /* look for the beginning of the value */
10322 val_beg = equal + 1;
10323 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
10324 val_beg++;
10325
10326 /* find the end of the value, respecting quotes */
10327 next = find_cookie_value_end(val_beg, hdr_end);
10328
10329 /* make val_end point to the first white space or delimitor after the value */
10330 val_end = next;
10331 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
10332 val_end--;
10333 } else {
10334 val_beg = val_end = next = equal;
10335 }
10336
10337 /* We have nothing to do with attributes beginning with '$'. However,
10338 * they will automatically be removed if a header before them is removed,
10339 * since they're supposed to be linked together.
10340 */
10341 if (*att_beg == '$')
10342 continue;
10343
10344 /* Ignore cookies with no equal sign */
10345 if (equal == next)
10346 continue;
10347
10348 /* Now we have the cookie name between att_beg and att_end, and
10349 * its value between val_beg and val_end.
10350 */
10351
10352 if (att_end - att_beg == cookie_name_l &&
10353 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
10354 /* let's return this value and indicate where to go on from */
10355 *value = val_beg;
10356 *value_l = val_end - val_beg;
10357 return next + 1;
10358 }
10359
10360 /* Set-Cookie headers only have the name in the first attr=value part */
10361 if (!list)
10362 break;
10363 }
10364
10365 return NULL;
10366}
10367
William Lallemanda43ba4e2014-01-28 18:14:25 +010010368/* Fetch a captured HTTP request header. The index is the position of
10369 * the "capture" option in the configuration file
10370 */
10371static int
10372smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10373 const struct arg *args, struct sample *smp, const char *kw)
10374{
10375 struct proxy *fe = l4->fe;
10376 struct http_txn *txn = l7;
10377 int idx;
10378
10379 if (!args || args->type != ARGT_UINT)
10380 return 0;
10381
10382 idx = args->data.uint;
10383
10384 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
10385 return 0;
10386
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010387 smp->type = SMP_T_STR;
10388 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010389 smp->data.str.str = txn->req.cap[idx];
10390 smp->data.str.len = strlen(txn->req.cap[idx]);
10391
10392 return 1;
10393}
10394
10395/* Fetch a captured HTTP response header. The index is the position of
10396 * the "capture" option in the configuration file
10397 */
10398static int
10399smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10400 const struct arg *args, struct sample *smp, const char *kw)
10401{
10402 struct proxy *fe = l4->fe;
10403 struct http_txn *txn = l7;
10404 int idx;
10405
10406 if (!args || args->type != ARGT_UINT)
10407 return 0;
10408
10409 idx = args->data.uint;
10410
10411 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
10412 return 0;
10413
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010414 smp->type = SMP_T_STR;
10415 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +010010416 smp->data.str.str = txn->rsp.cap[idx];
10417 smp->data.str.len = strlen(txn->rsp.cap[idx]);
10418
10419 return 1;
10420}
10421
William Lallemand65ad6e12014-01-31 15:08:02 +010010422/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
10423static int
10424smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10425 const struct arg *args, struct sample *smp, const char *kw)
10426{
10427 struct chunk *temp;
10428 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +010010429 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010430
10431 if (!txn->uri)
10432 return 0;
10433
William Lallemand96a77852014-02-05 00:30:02 +010010434 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010435
William Lallemand96a77852014-02-05 00:30:02 +010010436 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10437 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010438
William Lallemand96a77852014-02-05 00:30:02 +010010439 temp = get_trash_chunk();
10440 temp->str = txn->uri;
10441 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +010010442 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010443 smp->type = SMP_T_STR;
10444 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010445
10446 return 1;
10447
10448}
10449
10450/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10451static int
10452smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10453 const struct arg *args, struct sample *smp, const char *kw)
10454{
10455 struct chunk *temp;
10456 struct http_txn *txn = l7;
10457 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010458
10459 if (!txn->uri)
10460 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010461
William Lallemand65ad6e12014-01-31 15:08:02 +010010462 ptr = txn->uri;
10463
10464 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10465 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010466
William Lallemand65ad6e12014-01-31 15:08:02 +010010467 if (!*ptr)
10468 return 0;
10469
10470 ptr++; /* skip the space */
10471
10472 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010473 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010474 if (!ptr)
10475 return 0;
10476 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10477 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010478
10479 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010480 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010481 smp->type = SMP_T_STR;
10482 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010483
10484 return 1;
10485}
10486
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010487/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10488 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10489 */
10490static int
10491smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10492 const struct arg *args, struct sample *smp, const char *kw)
10493{
10494 struct http_txn *txn = l7;
10495
10496 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10497 return 0;
10498
10499 if (txn->req.flags & HTTP_MSGF_VER_11)
10500 smp->data.str.str = "HTTP/1.1";
10501 else
10502 smp->data.str.str = "HTTP/1.0";
10503
10504 smp->data.str.len = 8;
10505 smp->type = SMP_T_STR;
10506 smp->flags = SMP_F_CONST;
10507 return 1;
10508
10509}
10510
10511/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10512 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10513 */
10514static int
10515smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10516 const struct arg *args, struct sample *smp, const char *kw)
10517{
10518 struct http_txn *txn = l7;
10519
10520 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10521 return 0;
10522
10523 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10524 smp->data.str.str = "HTTP/1.1";
10525 else
10526 smp->data.str.str = "HTTP/1.0";
10527
10528 smp->data.str.len = 8;
10529 smp->type = SMP_T_STR;
10530 smp->flags = SMP_F_CONST;
10531 return 1;
10532
10533}
10534
William Lallemand65ad6e12014-01-31 15:08:02 +010010535
Willy Tarreaue333ec92012-04-16 16:26:40 +020010536/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010537 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010538 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010539 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010540 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010541 * Accepts exactly 1 argument of type string. If the input options indicate
10542 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010543 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010544 */
10545static int
Willy Tarreau51539362012-05-08 12:46:28 +020010546smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010547 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010548{
10549 struct http_txn *txn = l7;
10550 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010551 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010552 const struct http_msg *msg;
10553 const char *hdr_name;
10554 int hdr_name_len;
10555 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010556 int occ = 0;
10557 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010558
Willy Tarreau24e32d82012-04-23 23:55:44 +020010559 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010560 return 0;
10561
Willy Tarreaua890d072013-04-02 12:01:06 +020010562 if (!ctx) {
10563 /* first call */
10564 ctx = &static_hdr_ctx;
10565 ctx->idx = 0;
10566 smp->ctx.a[2] = ctx;
10567 }
10568
Willy Tarreaue333ec92012-04-16 16:26:40 +020010569 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010570
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010571 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010572 msg = &txn->req;
10573 hdr_name = "Cookie";
10574 hdr_name_len = 6;
10575 } else {
10576 msg = &txn->rsp;
10577 hdr_name = "Set-Cookie";
10578 hdr_name_len = 10;
10579 }
10580
Willy Tarreau28376d62012-04-26 21:26:10 +020010581 if (!occ && !(opt & SMP_OPT_ITERATE))
10582 /* no explicit occurrence and single fetch => last cookie by default */
10583 occ = -1;
10584
10585 /* OK so basically here, either we want only one value and it's the
10586 * last one, or we want to iterate over all of them and we fetch the
10587 * next one.
10588 */
10589
Willy Tarreau9b28e032012-10-12 23:49:43 +020010590 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010591 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010592 /* search for the header from the beginning, we must first initialize
10593 * the search parameters.
10594 */
Willy Tarreau37406352012-04-23 16:16:37 +020010595 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010596 ctx->idx = 0;
10597 }
10598
Willy Tarreau28376d62012-04-26 21:26:10 +020010599 smp->flags |= SMP_F_VOL_HDR;
10600
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010601 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010602 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10603 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010604 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10605 goto out;
10606
Willy Tarreau24e32d82012-04-23 23:55:44 +020010607 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010608 continue;
10609
Willy Tarreau37406352012-04-23 16:16:37 +020010610 smp->ctx.a[0] = ctx->line + ctx->val;
10611 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010612 }
10613
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010614 smp->type = SMP_T_STR;
10615 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010616 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010617 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010618 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010619 &smp->data.str.str,
10620 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010621 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010622 found = 1;
10623 if (occ >= 0) {
10624 /* one value was returned into smp->data.str.{str,len} */
10625 smp->flags |= SMP_F_NOT_LAST;
10626 return 1;
10627 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010628 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010629 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010630 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010631 /* all cookie headers and values were scanned. If we're looking for the
10632 * last occurrence, we may return it now.
10633 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010634 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010635 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010636 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010637}
10638
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010639/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010640 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010641 * multiple cookies may be parsed on the same line. The returned sample is of
10642 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010643 */
10644static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010645smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010646 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010647{
10648 struct http_txn *txn = l7;
10649 struct hdr_idx *idx = &txn->hdr_idx;
10650 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010651 const struct http_msg *msg;
10652 const char *hdr_name;
10653 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010654 int cnt;
10655 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010656 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010657
Willy Tarreau24e32d82012-04-23 23:55:44 +020010658 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010659 return 0;
10660
Willy Tarreaue333ec92012-04-16 16:26:40 +020010661 CHECK_HTTP_MESSAGE_FIRST();
10662
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010663 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010664 msg = &txn->req;
10665 hdr_name = "Cookie";
10666 hdr_name_len = 6;
10667 } else {
10668 msg = &txn->rsp;
10669 hdr_name = "Set-Cookie";
10670 hdr_name_len = 10;
10671 }
10672
Willy Tarreau9b28e032012-10-12 23:49:43 +020010673 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010674 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010675 ctx.idx = 0;
10676 cnt = 0;
10677
10678 while (1) {
10679 /* Note: val_beg == NULL every time we need to fetch a new header */
10680 if (!val_beg) {
10681 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10682 break;
10683
Willy Tarreau24e32d82012-04-23 23:55:44 +020010684 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010685 continue;
10686
10687 val_beg = ctx.line + ctx.val;
10688 val_end = val_beg + ctx.vlen;
10689 }
10690
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010691 smp->type = SMP_T_STR;
10692 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010693 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010694 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010695 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010696 &smp->data.str.str,
10697 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010698 cnt++;
10699 }
10700 }
10701
Willy Tarreaub169eba2013-12-16 15:14:43 +010010702 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010703 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010704 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010705 return 1;
10706}
10707
Willy Tarreau51539362012-05-08 12:46:28 +020010708/* Fetch an cookie's integer value. The integer value is returned. It
10709 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10710 */
10711static int
10712smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010713 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010714{
Willy Tarreauef38c392013-07-22 16:29:32 +020010715 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010716
10717 if (ret > 0) {
10718 smp->type = SMP_T_UINT;
10719 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10720 }
10721
10722 return ret;
10723}
10724
Willy Tarreau8797c062007-05-07 00:55:35 +020010725/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010726/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010727/************************************************************************/
10728
David Cournapeau16023ee2010-12-23 20:55:41 +090010729/*
10730 * Given a path string and its length, find the position of beginning of the
10731 * query string. Returns NULL if no query string is found in the path.
10732 *
10733 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10734 *
10735 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10736 */
bedis4c75cca2012-10-05 08:38:24 +020010737static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010738{
10739 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010740
bedis4c75cca2012-10-05 08:38:24 +020010741 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010742 return p ? p + 1 : NULL;
10743}
10744
bedis4c75cca2012-10-05 08:38:24 +020010745static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010746{
bedis4c75cca2012-10-05 08:38:24 +020010747 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010748}
10749
10750/*
10751 * Given a url parameter, find the starting position of the first occurence,
10752 * or NULL if the parameter is not found.
10753 *
10754 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10755 * the function will return query_string+8.
10756 */
10757static char*
10758find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010759 char* url_param_name, size_t url_param_name_l,
10760 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010761{
10762 char *pos, *last;
10763
10764 pos = query_string;
10765 last = query_string + query_string_l - url_param_name_l - 1;
10766
10767 while (pos <= last) {
10768 if (pos[url_param_name_l] == '=') {
10769 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10770 return pos;
10771 pos += url_param_name_l + 1;
10772 }
bedis4c75cca2012-10-05 08:38:24 +020010773 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010774 pos++;
10775 pos++;
10776 }
10777 return NULL;
10778}
10779
10780/*
10781 * Given a url parameter name, returns its value and size into *value and
10782 * *value_l respectively, and returns non-zero. If the parameter is not found,
10783 * zero is returned and value/value_l are not touched.
10784 */
10785static int
10786find_url_param_value(char* path, size_t path_l,
10787 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010788 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010789{
10790 char *query_string, *qs_end;
10791 char *arg_start;
10792 char *value_start, *value_end;
10793
bedis4c75cca2012-10-05 08:38:24 +020010794 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010795 if (!query_string)
10796 return 0;
10797
10798 qs_end = path + path_l;
10799 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010800 url_param_name, url_param_name_l,
10801 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010802 if (!arg_start)
10803 return 0;
10804
10805 value_start = arg_start + url_param_name_l + 1;
10806 value_end = value_start;
10807
bedis4c75cca2012-10-05 08:38:24 +020010808 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010809 value_end++;
10810
10811 *value = value_start;
10812 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010813 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010814}
10815
10816static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010817smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010818 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010819{
bedis4c75cca2012-10-05 08:38:24 +020010820 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010821 struct http_txn *txn = l7;
10822 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010823
bedis4c75cca2012-10-05 08:38:24 +020010824 if (!args || args[0].type != ARGT_STR ||
10825 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010826 return 0;
10827
10828 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010829
bedis4c75cca2012-10-05 08:38:24 +020010830 if (args[1].type)
10831 delim = *args[1].data.str.str;
10832
Willy Tarreau9b28e032012-10-12 23:49:43 +020010833 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010834 args->data.str.str, args->data.str.len,
10835 &smp->data.str.str, &smp->data.str.len,
10836 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010837 return 0;
10838
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010839 smp->type = SMP_T_STR;
10840 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010841 return 1;
10842}
10843
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010844/* Return the signed integer value for the specified url parameter (see url_param
10845 * above).
10846 */
10847static int
10848smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010849 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010850{
Willy Tarreauef38c392013-07-22 16:29:32 +020010851 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010852
10853 if (ret > 0) {
10854 smp->type = SMP_T_UINT;
10855 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10856 }
10857
10858 return ret;
10859}
10860
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010861/* This produces a 32-bit hash of the concatenation of the first occurrence of
10862 * the Host header followed by the path component if it begins with a slash ('/').
10863 * This means that '*' will not be added, resulting in exactly the first Host
10864 * entry. If no Host header is found, then the path is used. The resulting value
10865 * is hashed using the url hash followed by a full avalanche hash and provides a
10866 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10867 * high-traffic sites without having to store whole paths.
10868 * this differs from the base32 functions in that it includes the url parameters
10869 * as well as the path
10870 */
10871static int
10872smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010873 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010874{
10875 struct http_txn *txn = l7;
10876 struct hdr_ctx ctx;
10877 unsigned int hash = 0;
10878 char *ptr, *beg, *end;
10879 int len;
10880
10881 CHECK_HTTP_MESSAGE_FIRST();
10882
10883 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010884 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010885 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10886 ptr = ctx.line + ctx.val;
10887 len = ctx.vlen;
10888 while (len--)
10889 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10890 }
10891
10892 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010893 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 +000010894 beg = http_get_path(txn);
10895 if (!beg)
10896 beg = end;
10897
10898 for (ptr = beg; ptr < end ; ptr++);
10899
10900 if (beg < ptr && *beg == '/') {
10901 while (beg < ptr)
10902 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10903 }
10904 hash = full_hash(hash);
10905
10906 smp->type = SMP_T_UINT;
10907 smp->data.uint = hash;
10908 smp->flags = SMP_F_VOL_1ST;
10909 return 1;
10910}
10911
10912/* This concatenates the source address with the 32-bit hash of the Host and
10913 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10914 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10915 * on the source address length. The URL hash is stored before the address so
10916 * that in environments where IPv6 is insignificant, truncating the output to
10917 * 8 bytes would still work.
10918 */
10919static int
10920smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010921 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010922{
10923 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010924 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010925
Willy Tarreaue155ec22013-11-18 18:33:22 +010010926 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010927 return 0;
10928
10929 temp = get_trash_chunk();
10930 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10931 temp->len += sizeof(smp->data.uint);
10932
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010933 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010934 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010935 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010936 temp->len += 4;
10937 break;
10938 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010939 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010940 temp->len += 16;
10941 break;
10942 default:
10943 return 0;
10944 }
10945
10946 smp->data.str = *temp;
10947 smp->type = SMP_T_BIN;
10948 return 1;
10949}
10950
Willy Tarreau185b5c42012-04-26 15:11:51 +020010951/* This function is used to validate the arguments passed to any "hdr" fetch
10952 * keyword. These keywords support an optional positive or negative occurrence
10953 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10954 * is assumed that the types are already the correct ones. Returns 0 on error,
10955 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10956 * error message in case of error, that the caller is responsible for freeing.
10957 * The initial location must either be freeable or NULL.
10958 */
10959static int val_hdr(struct arg *arg, char **err_msg)
10960{
10961 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010962 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010963 return 0;
10964 }
10965 return 1;
10966}
10967
Willy Tarreau276fae92013-07-25 14:36:01 +020010968/* takes an UINT value on input supposed to represent the time since EPOCH,
10969 * adds an optional offset found in args[0] and emits a string representing
10970 * the date in RFC-1123/5322 format.
10971 */
10972static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10973{
10974 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10975 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10976 struct chunk *temp;
10977 struct tm *tm;
10978 time_t curr_date = smp->data.uint;
10979
10980 /* add offset */
10981 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10982 curr_date += args[0].data.sint;
10983
10984 tm = gmtime(&curr_date);
10985
10986 temp = get_trash_chunk();
10987 temp->len = snprintf(temp->str, temp->size - temp->len,
10988 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10989 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10990 tm->tm_hour, tm->tm_min, tm->tm_sec);
10991
10992 smp->data.str = *temp;
10993 smp->type = SMP_T_STR;
10994 return 1;
10995}
10996
Thierry FOURNIERad903512014-04-11 17:51:01 +020010997/* Match language range with language tag. RFC2616 14.4:
10998 *
10999 * A language-range matches a language-tag if it exactly equals
11000 * the tag, or if it exactly equals a prefix of the tag such
11001 * that the first tag character following the prefix is "-".
11002 *
11003 * Return 1 if the strings match, else return 0.
11004 */
11005static inline int language_range_match(const char *range, int range_len,
11006 const char *tag, int tag_len)
11007{
11008 const char *end = range + range_len;
11009 const char *tend = tag + tag_len;
11010 while (range < end) {
11011 if (*range == '-' && tag == tend)
11012 return 1;
11013 if (*range != *tag || tag == tend)
11014 return 0;
11015 range++;
11016 tag++;
11017 }
11018 /* Return true only if the last char of the tag is matched. */
11019 return tag == tend;
11020}
11021
11022/* Arguments: The list of expected value, the number of parts returned and the separator */
11023static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
11024{
11025 const char *al = smp->data.str.str;
11026 const char *end = al + smp->data.str.len;
11027 const char *token;
11028 int toklen;
11029 int qvalue;
11030 const char *str;
11031 const char *w;
11032 int best_q = 0;
11033
11034 /* Set the constant to the sample, because the output of the
11035 * function will be peek in the constant configuration string.
11036 */
11037 smp->flags |= SMP_F_CONST;
11038 smp->data.str.size = 0;
11039 smp->data.str.str = "";
11040 smp->data.str.len = 0;
11041
11042 /* Parse the accept language */
11043 while (1) {
11044
11045 /* Jump spaces, quit if the end is detected. */
11046 while (al < end && isspace(*al))
11047 al++;
11048 if (al >= end)
11049 break;
11050
11051 /* Start of the fisrt word. */
11052 token = al;
11053
11054 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
11055 while (al < end && *al != ';' && *al != ',' && !isspace(*al))
11056 al++;
11057 if (al == token)
11058 goto expect_comma;
11059
11060 /* Length of the token. */
11061 toklen = al - token;
11062 qvalue = 1000;
11063
11064 /* Check if the token exists in the list. If the token not exists,
11065 * jump to the next token.
11066 */
11067 str = args[0].data.str.str;
11068 w = str;
11069 while (1) {
11070 if (*str == ';' || *str == '\0') {
11071 if (language_range_match(token, toklen, w, str-w))
11072 goto look_for_q;
11073 if (*str == '\0')
11074 goto expect_comma;
11075 w = str + 1;
11076 }
11077 str++;
11078 }
11079 goto expect_comma;
11080
11081look_for_q:
11082
11083 /* Jump spaces, quit if the end is detected. */
11084 while (al < end && isspace(*al))
11085 al++;
11086 if (al >= end)
11087 goto process_value;
11088
11089 /* If ',' is found, process the result */
11090 if (*al == ',')
11091 goto process_value;
11092
11093 /* If the character is different from ';', look
11094 * for the end of the header part in best effort.
11095 */
11096 if (*al != ';')
11097 goto expect_comma;
11098
11099 /* Assumes that the char is ';', now expect "q=". */
11100 al++;
11101
11102 /* Jump spaces, process value if the end is detected. */
11103 while (al < end && isspace(*al))
11104 al++;
11105 if (al >= end)
11106 goto process_value;
11107
11108 /* Expect 'q'. If no 'q', continue in best effort */
11109 if (*al != 'q')
11110 goto process_value;
11111 al++;
11112
11113 /* Jump spaces, process value if the end is detected. */
11114 while (al < end && isspace(*al))
11115 al++;
11116 if (al >= end)
11117 goto process_value;
11118
11119 /* Expect '='. If no '=', continue in best effort */
11120 if (*al != '=')
11121 goto process_value;
11122 al++;
11123
11124 /* Jump spaces, process value if the end is detected. */
11125 while (al < end && isspace(*al))
11126 al++;
11127 if (al >= end)
11128 goto process_value;
11129
11130 /* Parse the q value. */
11131 qvalue = parse_qvalue(al, &al);
11132
11133process_value:
11134
11135 /* If the new q value is the best q value, then store the associated
11136 * language in the response. If qvalue is the biggest value (1000),
11137 * break the process.
11138 */
11139 if (qvalue > best_q) {
11140 smp->data.str.str = (char *)w;
11141 smp->data.str.len = str - w;
11142 if (qvalue >= 1000)
11143 break;
11144 best_q = qvalue;
11145 }
11146
11147expect_comma:
11148
11149 /* Expect comma or end. If the end is detected, quit the loop. */
11150 while (al < end && *al != ',')
11151 al++;
11152 if (al >= end)
11153 break;
11154
11155 /* Comma is found, jump it and restart the analyzer. */
11156 al++;
11157 }
11158
11159 /* Set default value if required. */
11160 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
11161 smp->data.str.str = args[1].data.str.str;
11162 smp->data.str.len = args[1].data.str.len;
11163 }
11164
11165 /* Return true only if a matching language was found. */
11166 return smp->data.str.len != 0;
11167}
11168
William Lallemand73025dd2014-04-24 14:38:37 +020011169/*
11170 * Return the struct http_req_action_kw associated to a keyword.
11171 */
11172struct http_req_action_kw *action_http_req_custom(const char *kw)
11173{
11174 if (!LIST_ISEMPTY(&http_req_keywords.list)) {
11175 struct http_req_action_kw_list *kw_list;
11176 int i;
11177
11178 list_for_each_entry(kw_list, &http_req_keywords.list, list) {
11179 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11180 if (!strcmp(kw, kw_list->kw[i].kw))
11181 return &kw_list->kw[i];
11182 }
11183 }
11184 }
11185 return NULL;
11186}
11187
11188/*
11189 * Return the struct http_res_action_kw associated to a keyword.
11190 */
11191struct http_res_action_kw *action_http_res_custom(const char *kw)
11192{
11193 if (!LIST_ISEMPTY(&http_res_keywords.list)) {
11194 struct http_res_action_kw_list *kw_list;
11195 int i;
11196
11197 list_for_each_entry(kw_list, &http_res_keywords.list, list) {
11198 for (i = 0; kw_list->kw[i].kw != NULL; i++) {
11199 if (!strcmp(kw, kw_list->kw[i].kw))
11200 return &kw_list->kw[i];
11201 }
11202 }
11203 }
11204 return NULL;
11205}
11206
Willy Tarreau4a568972010-05-12 08:08:50 +020011207/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011208/* All supported ACL keywords must be declared here. */
11209/************************************************************************/
11210
11211/* Note: must not be declared <const> as its list will be overwritten.
11212 * Please take care of keeping this list alphabetically sorted.
11213 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011214static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011215 { "base", "base", PAT_MATCH_STR },
11216 { "base_beg", "base", PAT_MATCH_BEG },
11217 { "base_dir", "base", PAT_MATCH_DIR },
11218 { "base_dom", "base", PAT_MATCH_DOM },
11219 { "base_end", "base", PAT_MATCH_END },
11220 { "base_len", "base", PAT_MATCH_LEN },
11221 { "base_reg", "base", PAT_MATCH_REG },
11222 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020011223
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011224 { "cook", "req.cook", PAT_MATCH_STR },
11225 { "cook_beg", "req.cook", PAT_MATCH_BEG },
11226 { "cook_dir", "req.cook", PAT_MATCH_DIR },
11227 { "cook_dom", "req.cook", PAT_MATCH_DOM },
11228 { "cook_end", "req.cook", PAT_MATCH_END },
11229 { "cook_len", "req.cook", PAT_MATCH_LEN },
11230 { "cook_reg", "req.cook", PAT_MATCH_REG },
11231 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011232
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011233 { "hdr", "req.hdr", PAT_MATCH_STR },
11234 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
11235 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
11236 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
11237 { "hdr_end", "req.hdr", PAT_MATCH_END },
11238 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
11239 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
11240 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011241
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011242 /* these two declarations uses strings with list storage (in place
11243 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
11244 * and delete functions are relative to the list management. The parse
11245 * and match method are related to the corresponding fetch methods. This
11246 * is very particular ACL declaration mode.
11247 */
11248 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
11249 { "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 +020011250
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011251 { "path", "path", PAT_MATCH_STR },
11252 { "path_beg", "path", PAT_MATCH_BEG },
11253 { "path_dir", "path", PAT_MATCH_DIR },
11254 { "path_dom", "path", PAT_MATCH_DOM },
11255 { "path_end", "path", PAT_MATCH_END },
11256 { "path_len", "path", PAT_MATCH_LEN },
11257 { "path_reg", "path", PAT_MATCH_REG },
11258 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011259
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011260 { "req_ver", "req.ver", PAT_MATCH_STR },
11261 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011262
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011263 { "scook", "res.cook", PAT_MATCH_STR },
11264 { "scook_beg", "res.cook", PAT_MATCH_BEG },
11265 { "scook_dir", "res.cook", PAT_MATCH_DIR },
11266 { "scook_dom", "res.cook", PAT_MATCH_DOM },
11267 { "scook_end", "res.cook", PAT_MATCH_END },
11268 { "scook_len", "res.cook", PAT_MATCH_LEN },
11269 { "scook_reg", "res.cook", PAT_MATCH_REG },
11270 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011271
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011272 { "shdr", "res.hdr", PAT_MATCH_STR },
11273 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
11274 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
11275 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
11276 { "shdr_end", "res.hdr", PAT_MATCH_END },
11277 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
11278 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
11279 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011280
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011281 { "url", "url", PAT_MATCH_STR },
11282 { "url_beg", "url", PAT_MATCH_BEG },
11283 { "url_dir", "url", PAT_MATCH_DIR },
11284 { "url_dom", "url", PAT_MATCH_DOM },
11285 { "url_end", "url", PAT_MATCH_END },
11286 { "url_len", "url", PAT_MATCH_LEN },
11287 { "url_reg", "url", PAT_MATCH_REG },
11288 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011289
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010011290 { "urlp", "urlp", PAT_MATCH_STR },
11291 { "urlp_beg", "urlp", PAT_MATCH_BEG },
11292 { "urlp_dir", "urlp", PAT_MATCH_DIR },
11293 { "urlp_dom", "urlp", PAT_MATCH_DOM },
11294 { "urlp_end", "urlp", PAT_MATCH_END },
11295 { "urlp_len", "urlp", PAT_MATCH_LEN },
11296 { "urlp_reg", "urlp", PAT_MATCH_REG },
11297 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011298
Willy Tarreau8ed669b2013-01-11 15:49:37 +010011299 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020011300}};
11301
11302/************************************************************************/
11303/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020011304/************************************************************************/
11305/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020011306static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011307 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011308 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11309 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
11310
William Lallemanda43ba4e2014-01-28 18:14:25 +010011311 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011312 { "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 +020011313
11314 /* retrieve these captures from the HTTP logs */
11315 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11316 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11317 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
11318
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011319 { "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 +020011320 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010011321
Willy Tarreau409bcde2013-01-08 00:31:00 +010011322 /* cookie is valid in both directions (eg: for "stick ...") but cook*
11323 * are only here to match the ACL's name, are request-only and are used
11324 * for ACL compatibility only.
11325 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011326 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11327 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011328 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11329 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11330
11331 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
11332 * only here to match the ACL's name, are request-only and are used for
11333 * ACL compatibility only.
11334 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011335 { "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 +010011336 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11337 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11338 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11339
Willy Tarreau0a0daec2013-04-02 22:44:58 +020011340 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011341 { "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 +010011342 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010011343 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011344 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011345
11346 /* HTTP protocol on the request path */
11347 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011348 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011349
11350 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011351 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
11352 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011353
11354 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011355 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
11356 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011357
Willy Tarreau18ed2562013-01-14 15:56:36 +010011358 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011359 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011360 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11361 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11362
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011363 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011364 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011365 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011366 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11367 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
11368 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
11369
11370 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011371 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011372 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11373 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11374
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011375 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020011376 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011377 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010011378 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11379 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11380 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11381
Willy Tarreau409bcde2013-01-08 00:31:00 +010011382 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011383 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011384 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11385 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011386 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010011387
11388 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011389 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011390 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
11391 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
11392 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
11393
11394 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011395 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000011396 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
11397 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011398 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
11399 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010011400 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
11401 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010011402 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
11403 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020011404}};
11405
Willy Tarreau8797c062007-05-07 00:55:35 +020011406
Willy Tarreau276fae92013-07-25 14:36:01 +020011407/* Note: must not be declared <const> as its list will be overwritten */
11408static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020011409 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
11410 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020011411 { NULL, NULL, 0, 0, 0 },
11412}};
11413
Willy Tarreau8797c062007-05-07 00:55:35 +020011414__attribute__((constructor))
11415static void __http_protocol_init(void)
11416{
11417 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020011418 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020011419 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020011420}
11421
11422
Willy Tarreau58f10d72006-12-04 02:26:12 +010011423/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020011424 * Local variables:
11425 * c-indent-level: 8
11426 * c-basic-offset: 8
11427 * End:
11428 */