blob: 3675b85aa1fe17bb191e5c3c7ab8539e0265f5d0 [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>
67
Willy Tarreau522d6c02009-12-06 18:49:18 +010068const char HTTP_100[] =
69 "HTTP/1.1 100 Continue\r\n\r\n";
70
71const struct chunk http_100_chunk = {
72 .str = (char *)&HTTP_100,
73 .len = sizeof(HTTP_100)-1
74};
75
Willy Tarreaua9679ac2010-01-03 17:32:57 +010076/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020077const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010078 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020080 "Location: "; /* not terminated since it will be concatenated with the URL */
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Location: "; /* not terminated since it will be concatenated with the URL */
87
88/* same as 302 except that the browser MUST retry with the GET method */
89const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040095
96/* same as 302 except that the browser MUST retry with the same method */
97const char *HTTP_307 =
98 "HTTP/1.1 307 Temporary Redirect\r\n"
99 "Cache-Control: no-cache\r\n"
100 "Content-length: 0\r\n"
101 "Location: "; /* not terminated since it will be concatenated with the URL */
102
103/* same as 301 except that the browser MUST retry with the same method */
104const char *HTTP_308 =
105 "HTTP/1.1 308 Permanent Redirect\r\n"
106 "Content-length: 0\r\n"
107 "Location: "; /* not terminated since it will be concatenated with the URL */
108
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
110const char *HTTP_401_fmt =
111 "HTTP/1.0 401 Unauthorized\r\n"
112 "Cache-Control: no-cache\r\n"
113 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200114 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
116 "\r\n"
117 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
118
Willy Tarreau844a7e72010-01-31 21:46:18 +0100119const char *HTTP_407_fmt =
120 "HTTP/1.0 407 Unauthorized\r\n"
121 "Cache-Control: no-cache\r\n"
122 "Connection: close\r\n"
123 "Content-Type: text/html\r\n"
124 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
125 "\r\n"
126 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
127
Willy Tarreau0f772532006-12-23 20:51:41 +0100128
129const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200130 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100131 [HTTP_ERR_400] = 400,
132 [HTTP_ERR_403] = 403,
133 [HTTP_ERR_408] = 408,
134 [HTTP_ERR_500] = 500,
135 [HTTP_ERR_502] = 502,
136 [HTTP_ERR_503] = 503,
137 [HTTP_ERR_504] = 504,
138};
139
Willy Tarreau80587432006-12-24 17:47:20 +0100140static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200141 [HTTP_ERR_200] =
142 "HTTP/1.0 200 OK\r\n"
143 "Cache-Control: no-cache\r\n"
144 "Connection: close\r\n"
145 "Content-Type: text/html\r\n"
146 "\r\n"
147 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
148
Willy Tarreau0f772532006-12-23 20:51:41 +0100149 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100150 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100151 "Cache-Control: no-cache\r\n"
152 "Connection: close\r\n"
153 "Content-Type: text/html\r\n"
154 "\r\n"
155 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
156
157 [HTTP_ERR_403] =
158 "HTTP/1.0 403 Forbidden\r\n"
159 "Cache-Control: no-cache\r\n"
160 "Connection: close\r\n"
161 "Content-Type: text/html\r\n"
162 "\r\n"
163 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
164
165 [HTTP_ERR_408] =
166 "HTTP/1.0 408 Request Time-out\r\n"
167 "Cache-Control: no-cache\r\n"
168 "Connection: close\r\n"
169 "Content-Type: text/html\r\n"
170 "\r\n"
171 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
172
173 [HTTP_ERR_500] =
174 "HTTP/1.0 500 Server Error\r\n"
175 "Cache-Control: no-cache\r\n"
176 "Connection: close\r\n"
177 "Content-Type: text/html\r\n"
178 "\r\n"
179 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
180
181 [HTTP_ERR_502] =
182 "HTTP/1.0 502 Bad Gateway\r\n"
183 "Cache-Control: no-cache\r\n"
184 "Connection: close\r\n"
185 "Content-Type: text/html\r\n"
186 "\r\n"
187 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
188
189 [HTTP_ERR_503] =
190 "HTTP/1.0 503 Service Unavailable\r\n"
191 "Cache-Control: no-cache\r\n"
192 "Connection: close\r\n"
193 "Content-Type: text/html\r\n"
194 "\r\n"
195 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
196
197 [HTTP_ERR_504] =
198 "HTTP/1.0 504 Gateway Time-out\r\n"
199 "Cache-Control: no-cache\r\n"
200 "Connection: close\r\n"
201 "Content-Type: text/html\r\n"
202 "\r\n"
203 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
204
205};
206
Cyril Bonté19979e12012-04-04 12:57:21 +0200207/* status codes available for the stats admin page (strictly 4 chars length) */
208const char *stat_status_codes[STAT_STATUS_SIZE] = {
209 [STAT_STATUS_DENY] = "DENY",
210 [STAT_STATUS_DONE] = "DONE",
211 [STAT_STATUS_ERRP] = "ERRP",
212 [STAT_STATUS_EXCD] = "EXCD",
213 [STAT_STATUS_NONE] = "NONE",
214 [STAT_STATUS_PART] = "PART",
215 [STAT_STATUS_UNKN] = "UNKN",
216};
217
218
Willy Tarreau80587432006-12-24 17:47:20 +0100219/* We must put the messages here since GCC cannot initialize consts depending
220 * on strlen().
221 */
222struct chunk http_err_chunks[HTTP_ERR_SIZE];
223
Willy Tarreaua890d072013-04-02 12:01:06 +0200224/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
225static struct hdr_ctx static_hdr_ctx;
226
Willy Tarreau42250582007-04-01 01:30:43 +0200227#define FD_SETS_ARE_BITFIELDS
228#ifdef FD_SETS_ARE_BITFIELDS
229/*
230 * This map is used with all the FD_* macros to check whether a particular bit
231 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
232 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
233 * byte should be encoded. Be careful to always pass bytes from 0 to 255
234 * exclusively to the macros.
235 */
236fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
237fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100238fd_set http_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
Willy Tarreau42250582007-04-01 01:30:43 +0200239
240#else
241#error "Check if your OS uses bitfields for fd_sets"
242#endif
243
Willy Tarreau80587432006-12-24 17:47:20 +0100244void init_proto_http()
245{
Willy Tarreau42250582007-04-01 01:30:43 +0200246 int i;
247 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100248 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200249
Willy Tarreau80587432006-12-24 17:47:20 +0100250 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
251 if (!http_err_msgs[msg]) {
252 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
253 abort();
254 }
255
256 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
257 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
258 }
Willy Tarreau42250582007-04-01 01:30:43 +0200259
260 /* initialize the log header encoding map : '{|}"#' should be encoded with
261 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
262 * URL encoding only requires '"', '#' to be encoded as well as non-
263 * printable characters above.
264 */
265 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
266 memset(url_encode_map, 0, sizeof(url_encode_map));
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100267 memset(http_encode_map, 0, sizeof(url_encode_map));
Willy Tarreau42250582007-04-01 01:30:43 +0200268 for (i = 0; i < 32; i++) {
269 FD_SET(i, hdr_encode_map);
270 FD_SET(i, url_encode_map);
271 }
272 for (i = 127; i < 256; i++) {
273 FD_SET(i, hdr_encode_map);
274 FD_SET(i, url_encode_map);
275 }
276
277 tmp = "\"#{|}";
278 while (*tmp) {
279 FD_SET(*tmp, hdr_encode_map);
280 tmp++;
281 }
282
283 tmp = "\"#";
284 while (*tmp) {
285 FD_SET(*tmp, url_encode_map);
286 tmp++;
287 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200288
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +0100289 /* initialize the http header encoding map. The draft httpbis define the
290 * header content as:
291 *
292 * HTTP-message = start-line
293 * *( header-field CRLF )
294 * CRLF
295 * [ message-body ]
296 * header-field = field-name ":" OWS field-value OWS
297 * field-value = *( field-content / obs-fold )
298 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
299 * obs-fold = CRLF 1*( SP / HTAB )
300 * field-vchar = VCHAR / obs-text
301 * VCHAR = %x21-7E
302 * obs-text = %x80-FF
303 *
304 * All the chars are encoded except "VCHAR", "obs-text", SP and HTAB.
305 * The encoded chars are form 0x00 to 0x08, 0x0a to 0x1f and 0x7f. The
306 * "obs-fold" is volontary forgotten because haproxy remove this.
307 */
308 memset(http_encode_map, 0, sizeof(http_encode_map));
309 for (i = 0x00; i <= 0x08; i++)
310 FD_SET(i, http_encode_map);
311 for (i = 0x0a; i <= 0x1f; i++)
312 FD_SET(i, http_encode_map);
313 FD_SET(0x7f, http_encode_map);
314
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200315 /* memory allocations */
316 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100317 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100318}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200319
Willy Tarreau53b6c742006-12-17 13:37:46 +0100320/*
321 * We have 26 list of methods (1 per first letter), each of which can have
322 * up to 3 entries (2 valid, 1 null).
323 */
324struct http_method_desc {
Willy Tarreauc8987b32013-12-06 23:43:17 +0100325 enum http_meth_t meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100326 int len;
327 const char text[8];
328};
329
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100330const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100331 ['C' - 'A'] = {
332 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
333 },
334 ['D' - 'A'] = {
335 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
336 },
337 ['G' - 'A'] = {
338 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
339 },
340 ['H' - 'A'] = {
341 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
342 },
343 ['P' - 'A'] = {
344 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
345 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
346 },
347 ['T' - 'A'] = {
348 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
349 },
350 /* rest is empty like this :
351 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
352 */
353};
354
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100355const struct http_method_name http_known_methods[HTTP_METH_OTHER] = {
356 [HTTP_METH_NONE] = { "", 0 },
357 [HTTP_METH_OPTIONS] = { "OPTIONS", 7 },
358 [HTTP_METH_GET] = { "GET", 3 },
359 [HTTP_METH_HEAD] = { "HEAD", 4 },
360 [HTTP_METH_POST] = { "POST", 4 },
361 [HTTP_METH_PUT] = { "PUT", 3 },
362 [HTTP_METH_DELETE] = { "DELETE", 6 },
363 [HTTP_METH_TRACE] = { "TRACE", 5 },
364 [HTTP_METH_CONNECT] = { "CONNECT", 7 },
365};
366
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100367/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200368 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100369 * RFC2616 for those chars.
370 */
371
372const char http_is_spht[256] = {
373 [' '] = 1, ['\t'] = 1,
374};
375
376const char http_is_crlf[256] = {
377 ['\r'] = 1, ['\n'] = 1,
378};
379
380const char http_is_lws[256] = {
381 [' '] = 1, ['\t'] = 1,
382 ['\r'] = 1, ['\n'] = 1,
383};
384
385const char http_is_sep[256] = {
386 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
387 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
388 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
389 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
390 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
391};
392
393const char http_is_ctl[256] = {
394 [0 ... 31] = 1,
395 [127] = 1,
396};
397
398/*
399 * A token is any ASCII char that is neither a separator nor a CTL char.
400 * Do not overwrite values in assignment since gcc-2.95 will not handle
401 * them correctly. Instead, define every non-CTL char's status.
402 */
403const char http_is_token[256] = {
404 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
405 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
406 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
407 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
408 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
409 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
410 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
411 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
412 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
413 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
414 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
415 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
416 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
417 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
418 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
419 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
420 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
421 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
422 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
423 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
424 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
425 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
426 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
427 ['|'] = 1, ['}'] = 0, ['~'] = 1,
428};
429
430
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100431/*
432 * An http ver_token is any ASCII which can be found in an HTTP version,
433 * which includes 'H', 'T', 'P', '/', '.' and any digit.
434 */
435const char http_is_ver_token[256] = {
436 ['.'] = 1, ['/'] = 1,
437 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
438 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
439 ['H'] = 1, ['P'] = 1, ['T'] = 1,
440};
441
442
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100443/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100444 * Adds a header and its CRLF at the tail of the message's buffer, just before
445 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100446 * The header is also automatically added to the index <hdr_idx>, and the end
447 * of headers is automatically adjusted. The number of bytes added is returned
448 * on success, otherwise <0 is returned indicating an error.
449 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100450int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100451{
452 int bytes, len;
453
454 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200455 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100456 if (!bytes)
457 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100458 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100459 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
460}
461
462/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100463 * Adds a header and its CRLF at the tail of the message's buffer, just before
464 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100465 * the buffer is only opened and the space reserved, but nothing is copied.
466 * The header is also automatically added to the index <hdr_idx>, and the end
467 * of headers is automatically adjusted. The number of bytes added is returned
468 * on success, otherwise <0 is returned indicating an error.
469 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100470int http_header_add_tail2(struct http_msg *msg,
471 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100472{
473 int bytes;
474
Willy Tarreau9b28e032012-10-12 23:49:43 +0200475 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100476 if (!bytes)
477 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100478 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100479 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
480}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200481
482/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100483 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
484 * If so, returns the position of the first non-space character relative to
485 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
486 * to return a pointer to the place after the first space. Returns 0 if the
487 * header name does not match. Checks are case-insensitive.
488 */
489int http_header_match2(const char *hdr, const char *end,
490 const char *name, int len)
491{
492 const char *val;
493
494 if (hdr + len >= end)
495 return 0;
496 if (hdr[len] != ':')
497 return 0;
498 if (strncasecmp(hdr, name, len) != 0)
499 return 0;
500 val = hdr + len + 1;
501 while (val < end && HTTP_IS_SPHT(*val))
502 val++;
503 if ((val >= end) && (len + 2 <= end - hdr))
504 return len + 2; /* we may replace starting from second space */
505 return val - hdr;
506}
507
Willy Tarreau04ff9f12013-06-10 18:39:42 +0200508/* Find the first or next occurrence of header <name> in message buffer <sol>
509 * using headers index <idx>, and return it in the <ctx> structure. This
510 * structure holds everything necessary to use the header and find next
511 * occurrence. If its <idx> member is 0, the header is searched from the
512 * beginning. Otherwise, the next occurrence is returned. The function returns
513 * 1 when it finds a value, and 0 when there is no more. It is very similar to
514 * http_find_header2() except that it is designed to work with full-line headers
515 * whose comma is not a delimiter but is part of the syntax. As a special case,
516 * if ctx->val is NULL when searching for a new values of a header, the current
517 * header is rescanned. This allows rescanning after a header deletion.
518 */
519int http_find_full_header2(const char *name, int len,
520 char *sol, struct hdr_idx *idx,
521 struct hdr_ctx *ctx)
522{
523 char *eol, *sov;
524 int cur_idx, old_idx;
525
526 cur_idx = ctx->idx;
527 if (cur_idx) {
528 /* We have previously returned a header, let's search another one */
529 sol = ctx->line;
530 eol = sol + idx->v[cur_idx].len;
531 goto next_hdr;
532 }
533
534 /* first request for this header */
535 sol += hdr_idx_first_pos(idx);
536 old_idx = 0;
537 cur_idx = hdr_idx_first_idx(idx);
538 while (cur_idx) {
539 eol = sol + idx->v[cur_idx].len;
540
541 if (len == 0) {
542 /* No argument was passed, we want any header.
543 * To achieve this, we simply build a fake request. */
544 while (sol + len < eol && sol[len] != ':')
545 len++;
546 name = sol;
547 }
548
549 if ((len < eol - sol) &&
550 (sol[len] == ':') &&
551 (strncasecmp(sol, name, len) == 0)) {
552 ctx->del = len;
553 sov = sol + len + 1;
554 while (sov < eol && http_is_lws[(unsigned char)*sov])
555 sov++;
556
557 ctx->line = sol;
558 ctx->prev = old_idx;
559 ctx->idx = cur_idx;
560 ctx->val = sov - sol;
561 ctx->tws = 0;
562 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
563 eol--;
564 ctx->tws++;
565 }
566 ctx->vlen = eol - sov;
567 return 1;
568 }
569 next_hdr:
570 sol = eol + idx->v[cur_idx].cr + 1;
571 old_idx = cur_idx;
572 cur_idx = idx->v[cur_idx].next;
573 }
574 return 0;
575}
576
Willy Tarreau68085d82010-01-18 14:54:04 +0100577/* Find the end of the header value contained between <s> and <e>. See RFC2616,
578 * par 2.2 for more information. Note that it requires a valid header to return
579 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200580 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100581char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200582{
583 int quoted, qdpair;
584
585 quoted = qdpair = 0;
586 for (; s < e; s++) {
587 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200588 else if (quoted) {
589 if (*s == '\\') qdpair = 1;
590 else if (*s == '"') quoted = 0;
591 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200592 else if (*s == '"') quoted = 1;
593 else if (*s == ',') return s;
594 }
595 return s;
596}
597
598/* Find the first or next occurrence of header <name> in message buffer <sol>
599 * using headers index <idx>, and return it in the <ctx> structure. This
600 * structure holds everything necessary to use the header and find next
601 * occurrence. If its <idx> member is 0, the header is searched from the
602 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100603 * 1 when it finds a value, and 0 when there is no more. It is designed to work
604 * with headers defined as comma-separated lists. As a special case, if ctx->val
605 * is NULL when searching for a new values of a header, the current header is
606 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200607 */
608int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100609 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200610 struct hdr_ctx *ctx)
611{
Willy Tarreau68085d82010-01-18 14:54:04 +0100612 char *eol, *sov;
613 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200614
Willy Tarreau68085d82010-01-18 14:54:04 +0100615 cur_idx = ctx->idx;
616 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200617 /* We have previously returned a value, let's search
618 * another one on the same line.
619 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200620 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200621 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100622 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200623 eol = sol + idx->v[cur_idx].len;
624
625 if (sov >= eol)
626 /* no more values in this header */
627 goto next_hdr;
628
Willy Tarreau68085d82010-01-18 14:54:04 +0100629 /* values remaining for this header, skip the comma but save it
630 * for later use (eg: for header deletion).
631 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200632 sov++;
633 while (sov < eol && http_is_lws[(unsigned char)*sov])
634 sov++;
635
636 goto return_hdr;
637 }
638
639 /* first request for this header */
640 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100641 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200642 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200643 while (cur_idx) {
644 eol = sol + idx->v[cur_idx].len;
645
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200646 if (len == 0) {
647 /* No argument was passed, we want any header.
648 * To achieve this, we simply build a fake request. */
649 while (sol + len < eol && sol[len] != ':')
650 len++;
651 name = sol;
652 }
653
Willy Tarreau33a7e692007-06-10 19:45:56 +0200654 if ((len < eol - sol) &&
655 (sol[len] == ':') &&
656 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100657 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200658 sov = sol + len + 1;
659 while (sov < eol && http_is_lws[(unsigned char)*sov])
660 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100661
Willy Tarreau33a7e692007-06-10 19:45:56 +0200662 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100663 ctx->prev = old_idx;
664 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200665 ctx->idx = cur_idx;
666 ctx->val = sov - sol;
667
668 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200669 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200670 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200671 eol--;
672 ctx->tws++;
673 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200674 ctx->vlen = eol - sov;
675 return 1;
676 }
677 next_hdr:
678 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100679 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200680 cur_idx = idx->v[cur_idx].next;
681 }
682 return 0;
683}
684
685int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100686 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200687 struct hdr_ctx *ctx)
688{
689 return http_find_header2(name, strlen(name), sol, idx, ctx);
690}
691
Willy Tarreau68085d82010-01-18 14:54:04 +0100692/* Remove one value of a header. This only works on a <ctx> returned by one of
693 * the http_find_header functions. The value is removed, as well as surrounding
694 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100695 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100696 * message <msg>. The new index is returned. If it is zero, it means there is
697 * no more header, so any processing may stop. The ctx is always left in a form
698 * that can be handled by http_find_header2() to find next occurrence.
699 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100700int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100701{
702 int cur_idx = ctx->idx;
703 char *sol = ctx->line;
704 struct hdr_idx_elem *hdr;
705 int delta, skip_comma;
706
707 if (!cur_idx)
708 return 0;
709
710 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200711 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100712 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200713 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100714 http_msg_move_end(msg, delta);
715 idx->used--;
716 hdr->len = 0; /* unused entry */
717 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100718 if (idx->tail == ctx->idx)
719 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100720 ctx->idx = ctx->prev; /* walk back to the end of previous header */
721 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
722 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200723 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100724 return ctx->idx;
725 }
726
727 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200728 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
729 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100730 */
731
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200732 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200733 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200734 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100735 NULL, 0);
736 hdr->len += delta;
737 http_msg_move_end(msg, delta);
738 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200739 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100740 return ctx->idx;
741}
742
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100743/* This function handles a server error at the stream interface level. The
744 * stream interface is assumed to be already in a closed state. An optional
745 * message is copied into the input buffer, and an HTTP status code stored.
746 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100747 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200748 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200749static void http_server_error(struct session *s, struct stream_interface *si,
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100750 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200751{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200752 channel_auto_read(si->ob);
753 channel_abort(si->ob);
754 channel_auto_close(si->ob);
755 channel_erase(si->ob);
756 channel_auto_close(si->ib);
757 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100758 if (status > 0 && msg) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200759 s->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200760 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200761 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +0200762 if (!(s->flags & SN_ERR_MASK))
763 s->flags |= err;
764 if (!(s->flags & SN_FINST_MASK))
765 s->flags |= finst;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200766}
767
Willy Tarreau80587432006-12-24 17:47:20 +0100768/* This function returns the appropriate error location for the given session
769 * and message.
770 */
771
Willy Tarreau783f2582012-09-04 12:19:04 +0200772struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100773{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200774 if (s->be->errmsg[msgnum].str)
775 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100776 else if (s->fe->errmsg[msgnum].str)
777 return &s->fe->errmsg[msgnum];
778 else
779 return &http_err_chunks[msgnum];
780}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200781
Willy Tarreau53b6c742006-12-17 13:37:46 +0100782/*
783 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
784 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
785 */
Thierry FOURNIERd4373142013-12-17 01:10:10 +0100786enum http_meth_t find_http_meth(const char *str, const int len)
Willy Tarreau53b6c742006-12-17 13:37:46 +0100787{
788 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100789 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100790
791 m = ((unsigned)*str - 'A');
792
793 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100794 for (h = http_methods[m]; h->len > 0; h++) {
795 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100796 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100797 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100798 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100799 };
800 return HTTP_METH_OTHER;
801 }
802 return HTTP_METH_NONE;
803
804}
805
Willy Tarreau21d2af32008-02-14 20:25:24 +0100806/* Parse the URI from the given transaction (which is assumed to be in request
807 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
808 * It is returned otherwise.
809 */
810static char *
811http_get_path(struct http_txn *txn)
812{
813 char *ptr, *end;
814
Willy Tarreau9b28e032012-10-12 23:49:43 +0200815 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100816 end = ptr + txn->req.sl.rq.u_l;
817
818 if (ptr >= end)
819 return NULL;
820
821 /* RFC2616, par. 5.1.2 :
822 * Request-URI = "*" | absuri | abspath | authority
823 */
824
825 if (*ptr == '*')
826 return NULL;
827
828 if (isalpha((unsigned char)*ptr)) {
829 /* this is a scheme as described by RFC3986, par. 3.1 */
830 ptr++;
831 while (ptr < end &&
832 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
833 ptr++;
834 /* skip '://' */
835 if (ptr == end || *ptr++ != ':')
836 return NULL;
837 if (ptr == end || *ptr++ != '/')
838 return NULL;
839 if (ptr == end || *ptr++ != '/')
840 return NULL;
841 }
842 /* skip [user[:passwd]@]host[:[port]] */
843
844 while (ptr < end && *ptr != '/')
845 ptr++;
846
847 if (ptr == end)
848 return NULL;
849
850 /* OK, we got the '/' ! */
851 return ptr;
852}
853
William Lallemand65ad6e12014-01-31 15:08:02 +0100854/* Parse the URI from the given string and look for the "/" beginning the PATH.
855 * If not found, return NULL. It is returned otherwise.
856 */
857static char *
858http_get_path_from_string(char *str)
859{
860 char *ptr = str;
861
862 /* RFC2616, par. 5.1.2 :
863 * Request-URI = "*" | absuri | abspath | authority
864 */
865
866 if (*ptr == '*')
867 return NULL;
868
869 if (isalpha((unsigned char)*ptr)) {
870 /* this is a scheme as described by RFC3986, par. 3.1 */
871 ptr++;
872 while (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')
873 ptr++;
874 /* skip '://' */
875 if (*ptr == '\0' || *ptr++ != ':')
876 return NULL;
877 if (*ptr == '\0' || *ptr++ != '/')
878 return NULL;
879 if (*ptr == '\0' || *ptr++ != '/')
880 return NULL;
881 }
882 /* skip [user[:passwd]@]host[:[port]] */
883
884 while (*ptr != '\0' && *ptr != ' ' && *ptr != '/')
885 ptr++;
886
887 if (*ptr == '\0' || *ptr == ' ')
888 return NULL;
889
890 /* OK, we got the '/' ! */
891 return ptr;
892}
893
Willy Tarreau71241ab2012-12-27 11:30:54 +0100894/* Returns a 302 for a redirectable request that reaches a server working in
895 * in redirect mode. This may only be called just after the stream interface
896 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
897 * follow normal proxy processing. NOTE: this function is designed to support
898 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100899 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100900void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100901{
902 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100903 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100904 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200905 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100906
907 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100908 trash.len = strlen(HTTP_302);
909 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100910
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100911 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100912
Willy Tarreauefb453c2008-10-26 20:49:47 +0100913 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100914 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100915 return;
916
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100917 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100918 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100919 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
920 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100921 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100922
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200923 /* 3: add the request URI. Since it was already forwarded, we need
924 * to temporarily rewind the buffer.
925 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100926 txn = &s->txn;
Willy Tarreau211cdec2014-04-17 20:18:08 +0200927 b_rew(s->req->buf, rewind = http_hdr_rewind(&txn->req));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200928
Willy Tarreauefb453c2008-10-26 20:49:47 +0100929 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200930 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 +0200931
Willy Tarreau9b28e032012-10-12 23:49:43 +0200932 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200933
Willy Tarreauefb453c2008-10-26 20:49:47 +0100934 if (!path)
935 return;
936
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100937 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100938 return;
939
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100940 memcpy(trash.str + trash.len, path, len);
941 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100942
943 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100944 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
945 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100946 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100947 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
948 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100949 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100950
951 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200952 si_shutr(si);
953 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100954 si->err_type = SI_ET_NONE;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100955 si->state = SI_ST_CLO;
956
957 /* send the message */
Willy Tarreau570f2212013-06-10 16:42:09 +0200958 http_server_error(s, si, SN_ERR_LOCAL, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100959
960 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100961 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500962 srv_set_sess_last(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100963}
964
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100965/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100966 * that the server side is closed. Note that err_type is actually a
967 * bitmask, where almost only aborts may be cumulated with other
968 * values. We consider that aborted operations are more important
969 * than timeouts or errors due to the fact that nobody else in the
970 * logs might explain incomplete retries. All others should avoid
971 * being cumulated. It should normally not be possible to have multiple
972 * aborts at once, but just in case, the first one in sequence is reported.
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100973 * Note that connection errors appearing on the second request of a keep-alive
974 * connection are not reported since this allows the client to retry.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100975 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100976void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100977{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100978 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100979
980 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100981 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200982 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100983 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100984 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100985 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
986 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100987 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100988 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200989 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100990 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100991 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200992 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100993 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100994 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +0100995 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
996 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100997 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100998 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau36346242014-02-24 18:26:30 +0100999 503, (s->flags & SN_SRV_REUSED) ? NULL :
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001000 http_error_message(s, HTTP_ERR_503));
Willy Tarreau2d400bb2012-05-14 12:11:47 +02001001 else if (err_type & SI_ET_CONN_RES)
1002 http_server_error(s, si, SN_ERR_RESOURCE, SN_FINST_C,
Willy Tarreau6b726ad2013-12-15 19:31:37 +01001003 503, (s->txn.flags & TX_NOT_FIRST) ? NULL :
1004 http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001005 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +01001006 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +02001007 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +01001008}
1009
Willy Tarreau42250582007-04-01 01:30:43 +02001010extern const char sess_term_cond[8];
1011extern const char sess_fin_state[8];
1012extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +02001013struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +01001014struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +01001015struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001016
Willy Tarreau117f59e2007-03-04 18:17:17 +01001017/*
1018 * Capture headers from message starting at <som> according to header list
1019 * <cap_hdr>, and fill the <idx> structure appropriately.
1020 */
1021void capture_headers(char *som, struct hdr_idx *idx,
1022 char **cap, struct cap_hdr *cap_hdr)
1023{
1024 char *eol, *sol, *col, *sov;
1025 int cur_idx;
1026 struct cap_hdr *h;
1027 int len;
1028
1029 sol = som + hdr_idx_first_pos(idx);
1030 cur_idx = hdr_idx_first_idx(idx);
1031
1032 while (cur_idx) {
1033 eol = sol + idx->v[cur_idx].len;
1034
1035 col = sol;
1036 while (col < eol && *col != ':')
1037 col++;
1038
1039 sov = col + 1;
1040 while (sov < eol && http_is_lws[(unsigned char)*sov])
1041 sov++;
1042
1043 for (h = cap_hdr; h; h = h->next) {
1044 if ((h->namelen == col - sol) &&
1045 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1046 if (cap[h->index] == NULL)
1047 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001048 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001049
1050 if (cap[h->index] == NULL) {
1051 Alert("HTTP capture : out of memory.\n");
1052 continue;
1053 }
1054
1055 len = eol - sov;
1056 if (len > h->len)
1057 len = h->len;
1058
1059 memcpy(cap[h->index], sov, len);
1060 cap[h->index][len]=0;
1061 }
1062 }
1063 sol = eol + idx->v[cur_idx].cr + 1;
1064 cur_idx = idx->v[cur_idx].next;
1065 }
1066}
1067
1068
Willy Tarreau42250582007-04-01 01:30:43 +02001069/* either we find an LF at <ptr> or we jump to <bad>.
1070 */
1071#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1072
1073/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1074 * otherwise to <http_msg_ood> with <state> set to <st>.
1075 */
1076#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1077 ptr++; \
1078 if (likely(ptr < end)) \
1079 goto good; \
1080 else { \
1081 state = (st); \
1082 goto http_msg_ood; \
1083 } \
1084 } while (0)
1085
1086
Willy Tarreaubaaee002006-06-26 02:48:02 +02001087/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001088 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001089 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1090 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1091 * will give undefined results.
1092 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1093 * and that msg->sol points to the beginning of the response.
1094 * If a complete line is found (which implies that at least one CR or LF is
1095 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1096 * returned indicating an incomplete line (which does not mean that parts have
1097 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1098 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1099 * upon next call.
1100 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001101 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001102 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1103 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001104 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001105 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001106const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001107 enum ht_state state, const char *ptr, const char *end,
1108 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001109{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001110 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001111
Willy Tarreau8973c702007-01-21 23:58:29 +01001112 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001113 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001114 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001115 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001116 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1117
1118 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001119 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001120 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1121 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001122 state = HTTP_MSG_ERROR;
1123 break;
1124
Willy Tarreau8973c702007-01-21 23:58:29 +01001125 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001126 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001127 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001128 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001129 goto http_msg_rpcode;
1130 }
1131 if (likely(HTTP_IS_SPHT(*ptr)))
1132 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1133 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001134 state = HTTP_MSG_ERROR;
1135 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001136
Willy Tarreau8973c702007-01-21 23:58:29 +01001137 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001138 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001139 if (likely(!HTTP_IS_LWS(*ptr)))
1140 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1141
1142 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001143 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001144 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1145 }
1146
1147 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001148 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001149 http_msg_rsp_reason:
1150 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001151 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001152 msg->sl.st.r_l = 0;
1153 goto http_msg_rpline_eol;
1154
Willy Tarreau8973c702007-01-21 23:58:29 +01001155 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001156 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001157 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001158 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001159 goto http_msg_rpreason;
1160 }
1161 if (likely(HTTP_IS_SPHT(*ptr)))
1162 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1163 /* so it's a CR/LF, so there is no reason phrase */
1164 goto http_msg_rsp_reason;
1165
Willy Tarreau8973c702007-01-21 23:58:29 +01001166 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001167 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001168 if (likely(!HTTP_IS_CRLF(*ptr)))
1169 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001170 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001171 http_msg_rpline_eol:
1172 /* We have seen the end of line. Note that we do not
1173 * necessarily have the \n yet, but at least we know that we
1174 * have EITHER \r OR \n, otherwise the response would not be
1175 * complete. We can then record the response length and return
1176 * to the caller which will be able to register it.
1177 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001178 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001179 return ptr;
1180
Willy Tarreau8973c702007-01-21 23:58:29 +01001181 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001182#ifdef DEBUG_FULL
Willy Tarreau8973c702007-01-21 23:58:29 +01001183 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1184 exit(1);
1185#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001186 ;
Willy Tarreau8973c702007-01-21 23:58:29 +01001187 }
1188
1189 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001190 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001191 if (ret_state)
1192 *ret_state = state;
1193 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001194 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001195 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001196}
1197
Willy Tarreau8973c702007-01-21 23:58:29 +01001198/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001199 * This function parses a request line between <ptr> and <end>, starting with
1200 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1201 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1202 * will give undefined results.
1203 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1204 * and that msg->sol points to the beginning of the request.
1205 * If a complete line is found (which implies that at least one CR or LF is
1206 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1207 * returned indicating an incomplete line (which does not mean that parts have
1208 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1209 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1210 * upon next call.
1211 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001212 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001213 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1214 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001215 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001216 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001217const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01001218 enum ht_state state, const char *ptr, const char *end,
1219 unsigned int *ret_ptr, enum ht_state *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001220{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001221 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001222
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001223 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001224 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001225 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001226 if (likely(HTTP_IS_TOKEN(*ptr)))
1227 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001228
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001229 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001230 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001231 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1232 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001233
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001234 if (likely(HTTP_IS_CRLF(*ptr))) {
1235 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001236 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001237 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001238 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001239 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001240 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001241 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001242 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001243 msg->sl.rq.v_l = 0;
1244 goto http_msg_rqline_eol;
1245 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001246 state = HTTP_MSG_ERROR;
1247 break;
1248
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001249 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001250 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001251 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001252 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001253 goto http_msg_rquri;
1254 }
1255 if (likely(HTTP_IS_SPHT(*ptr)))
1256 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1257 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1258 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001259
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001260 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001261 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001262 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001263 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001264
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001265 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001266 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001267 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1268 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001269
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001270 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001271 /* non-ASCII chars are forbidden unless option
1272 * accept-invalid-http-request is enabled in the frontend.
1273 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001274 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001275 if (msg->err_pos < -1)
1276 goto invalid_char;
1277 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001278 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001279 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1280 }
1281
1282 if (likely(HTTP_IS_CRLF(*ptr))) {
1283 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1284 goto http_msg_req09_uri_e;
1285 }
1286
1287 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001288 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001289 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001290 state = HTTP_MSG_ERROR;
1291 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001292
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001293 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001294 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001295 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001296 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001297 goto http_msg_rqver;
1298 }
1299 if (likely(HTTP_IS_SPHT(*ptr)))
1300 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1301 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1302 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001303
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001304 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001305 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001306 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001307 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001308
1309 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001310 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001311 http_msg_rqline_eol:
1312 /* We have seen the end of line. Note that we do not
1313 * necessarily have the \n yet, but at least we know that we
1314 * have EITHER \r OR \n, otherwise the request would not be
1315 * complete. We can then record the request length and return
1316 * to the caller which will be able to register it.
1317 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001318 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001319 return ptr;
1320 }
1321
1322 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001323 state = HTTP_MSG_ERROR;
1324 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001326 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001327#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001328 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1329 exit(1);
1330#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001331 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001332 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001333
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001334 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001335 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001336 if (ret_state)
1337 *ret_state = state;
1338 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001339 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001340 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001341}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001342
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001343/*
1344 * Returns the data from Authorization header. Function may be called more
1345 * than once so data is stored in txn->auth_data. When no header is found
1346 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
Thierry FOURNIER98d96952014-01-23 12:13:02 +01001347 * searching again for something we are unable to find anyway. However, if
1348 * the result if valid, the cache is not reused because we would risk to
1349 * have the credentials overwritten by another session in parallel.
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001350 */
1351
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01001352/* This bufffer is initialized in the file 'src/haproxy.c'. This length is
1353 * set according to global.tune.bufsize.
1354 */
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001355char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001356
1357int
1358get_http_auth(struct session *s)
1359{
1360
1361 struct http_txn *txn = &s->txn;
1362 struct chunk auth_method;
1363 struct hdr_ctx ctx;
1364 char *h, *p;
1365 int len;
1366
1367#ifdef DEBUG_AUTH
1368 printf("Auth for session %p: %d\n", s, txn->auth.method);
1369#endif
1370
1371 if (txn->auth.method == HTTP_AUTH_WRONG)
1372 return 0;
1373
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001374 txn->auth.method = HTTP_AUTH_WRONG;
1375
1376 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001377
1378 if (txn->flags & TX_USE_PX_CONN) {
1379 h = "Proxy-Authorization";
1380 len = strlen(h);
1381 } else {
1382 h = "Authorization";
1383 len = strlen(h);
1384 }
1385
Willy Tarreau9b28e032012-10-12 23:49:43 +02001386 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001387 return 0;
1388
1389 h = ctx.line + ctx.val;
1390
1391 p = memchr(h, ' ', ctx.vlen);
1392 if (!p || p == h)
1393 return 0;
1394
1395 chunk_initlen(&auth_method, h, 0, p-h);
1396 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1397
1398 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1399
1400 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001401 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001402
1403 if (len < 0)
1404 return 0;
1405
1406
1407 get_http_auth_buff[len] = '\0';
1408
1409 p = strchr(get_http_auth_buff, ':');
1410
1411 if (!p)
1412 return 0;
1413
1414 txn->auth.user = get_http_auth_buff;
1415 *p = '\0';
1416 txn->auth.pass = p+1;
1417
1418 txn->auth.method = HTTP_AUTH_BASIC;
1419 return 1;
1420 }
1421
1422 return 0;
1423}
1424
Willy Tarreau58f10d72006-12-04 02:26:12 +01001425
Willy Tarreau8973c702007-01-21 23:58:29 +01001426/*
1427 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001428 * depending on the initial msg->msg_state. The caller is responsible for
1429 * ensuring that the message does not wrap. The function can be preempted
1430 * everywhere when data are missing and recalled at the exact same location
1431 * with no information loss. The message may even be realigned between two
1432 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001433 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001434 * fields. Note that msg->sol will be initialized after completing the first
1435 * state, so that none of the msg pointers has to be initialized prior to the
1436 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001437 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001438void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439{
Willy Tarreau3770f232013-12-07 00:01:53 +01001440 enum ht_state state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001442 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001443
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001444 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001445 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001446 ptr = buf->p + msg->next;
1447 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001448
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 if (unlikely(ptr >= end))
1450 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001451
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001453 /*
1454 * First, states that are specific to the response only.
1455 * We check them first so that request and headers are
1456 * closer to each other (accessed more often).
1457 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001458 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001459 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001460 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001461 /* we have a start of message, but we have to check
1462 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001463 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001464 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001465 if (unlikely(ptr != buf->p)) {
1466 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001467 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001468 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001469 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001470 }
Willy Tarreau26927362012-05-18 23:22:52 +02001471 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001472 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001473 hdr_idx_init(idx);
1474 state = HTTP_MSG_RPVER;
1475 goto http_msg_rpver;
1476 }
1477
1478 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1479 goto http_msg_invalid;
1480
1481 if (unlikely(*ptr == '\n'))
1482 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1483 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1484 /* stop here */
1485
Willy Tarreau8973c702007-01-21 23:58:29 +01001486 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001487 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001488 EXPECT_LF_HERE(ptr, http_msg_invalid);
1489 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1490 /* stop here */
1491
Willy Tarreau8973c702007-01-21 23:58:29 +01001492 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001493 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001494 case HTTP_MSG_RPVER_SP:
1495 case HTTP_MSG_RPCODE:
1496 case HTTP_MSG_RPCODE_SP:
1497 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001498 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001499 state, ptr, end,
1500 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001501 if (unlikely(!ptr))
1502 return;
1503
1504 /* we have a full response and we know that we have either a CR
1505 * or an LF at <ptr>.
1506 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001507 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1508
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001509 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001510 if (likely(*ptr == '\r'))
1511 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1512 goto http_msg_rpline_end;
1513
Willy Tarreau8973c702007-01-21 23:58:29 +01001514 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001515 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001516 /* msg->sol must point to the first of CR or LF. */
1517 EXPECT_LF_HERE(ptr, http_msg_invalid);
1518 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1519 /* stop here */
1520
1521 /*
1522 * Second, states that are specific to the request only
1523 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001525 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001527 /* we have a start of message, but we have to check
1528 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001529 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001530 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001531 if (likely(ptr != buf->p)) {
1532 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001533 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001534 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001535 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001536 }
Willy Tarreau26927362012-05-18 23:22:52 +02001537 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001538 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001539 state = HTTP_MSG_RQMETH;
1540 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001542
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1544 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001545
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 if (unlikely(*ptr == '\n'))
1547 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1548 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001549 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001550
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001552 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 EXPECT_LF_HERE(ptr, http_msg_invalid);
1554 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001555 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001556
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001558 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001559 case HTTP_MSG_RQMETH_SP:
1560 case HTTP_MSG_RQURI:
1561 case HTTP_MSG_RQURI_SP:
1562 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001563 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001564 state, ptr, end,
1565 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566 if (unlikely(!ptr))
1567 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 /* we have a full request and we know that we have either a CR
1570 * or an LF at <ptr>.
1571 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001572 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001573
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001574 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 if (likely(*ptr == '\r'))
1576 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001577 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001578
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001580 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 /* check for HTTP/0.9 request : no version information available.
1582 * msg->sol must point to the first of CR or LF.
1583 */
1584 if (unlikely(msg->sl.rq.v_l == 0))
1585 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001586
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001587 EXPECT_LF_HERE(ptr, http_msg_invalid);
1588 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001589 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001590
Willy Tarreau8973c702007-01-21 23:58:29 +01001591 /*
1592 * Common states below
1593 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001595 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001596 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001597 if (likely(!HTTP_IS_CRLF(*ptr))) {
1598 goto http_msg_hdr_name;
1599 }
1600
1601 if (likely(*ptr == '\r'))
1602 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1603 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001604
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001605 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001606 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001607 /* assumes msg->sol points to the first char */
1608 if (likely(HTTP_IS_TOKEN(*ptr)))
1609 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001610
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001611 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001612 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001613
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001614 if (likely(msg->err_pos < -1) || *ptr == '\n')
1615 goto http_msg_invalid;
1616
1617 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001618 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001619
1620 /* and we still accept this non-token character */
1621 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001622
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001623 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001624 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001625 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001626 if (likely(HTTP_IS_SPHT(*ptr)))
1627 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001628
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001629 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001630 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001631
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001632 if (likely(!HTTP_IS_CRLF(*ptr))) {
1633 goto http_msg_hdr_val;
1634 }
1635
1636 if (likely(*ptr == '\r'))
1637 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1638 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001640 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001641 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001642 EXPECT_LF_HERE(ptr, http_msg_invalid);
1643 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001644
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001645 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001646 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001647 if (likely(HTTP_IS_SPHT(*ptr))) {
1648 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001649 for (; buf->p + msg->sov < ptr; msg->sov++)
1650 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001651 goto http_msg_hdr_l1_sp;
1652 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001653 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001654 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001655 goto http_msg_complete_header;
1656
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001657 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001658 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001659 /* assumes msg->sol points to the first char, and msg->sov
1660 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001661 */
1662 if (likely(!HTTP_IS_CRLF(*ptr)))
1663 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001664
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001665 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001666 /* Note: we could also copy eol into ->eoh so that we have the
1667 * real header end in case it ends with lots of LWS, but is this
1668 * really needed ?
1669 */
1670 if (likely(*ptr == '\r'))
1671 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1672 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001673
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001674 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001675 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001676 EXPECT_LF_HERE(ptr, http_msg_invalid);
1677 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001678
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001679 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001680 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001681 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1682 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001683 for (; buf->p + msg->eol < ptr; msg->eol++)
1684 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001685 goto http_msg_hdr_val;
1686 }
1687 http_msg_complete_header:
1688 /*
1689 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001690 * Assumes msg->sol points to the first char, msg->sov points
1691 * to the first character of the value and msg->eol to the
1692 * first CR or LF so we know how the line ends. We insert last
1693 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001694 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001695 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001696 idx, idx->tail) < 0))
1697 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001698
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001699 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001700 if (likely(!HTTP_IS_CRLF(*ptr))) {
1701 goto http_msg_hdr_name;
1702 }
1703
1704 if (likely(*ptr == '\r'))
1705 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1706 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001707
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001708 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001709 http_msg_last_lf:
Willy Tarreau0558a022014-03-13 15:48:45 +01001710 /* Assumes msg->sol points to the first of either CR or LF.
1711 * Sets ->sov and ->next to the total header length, ->eoh to
1712 * the last CRLF, and ->eol to the last CRLF length (1 or 2).
1713 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001714 EXPECT_LF_HERE(ptr, http_msg_invalid);
1715 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001716 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001717 msg->eoh = msg->sol;
1718 msg->sol = 0;
Willy Tarreau0558a022014-03-13 15:48:45 +01001719 msg->eol = msg->sov - msg->eoh;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001720 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001721 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001722
1723 case HTTP_MSG_ERROR:
1724 /* this may only happen if we call http_msg_analyser() twice with an error */
1725 break;
1726
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001727 default:
Willy Tarreau3770f232013-12-07 00:01:53 +01001728#ifdef DEBUG_FULL
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1730 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001731#endif
Willy Tarreau3770f232013-12-07 00:01:53 +01001732 ;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001733 }
1734 http_msg_ood:
1735 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001736 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001737 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001738 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001739
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 http_msg_invalid:
1741 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001742 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001743 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001744 return;
1745}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001746
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001747/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1748 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1749 * nothing is done and 1 is returned.
1750 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001751static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001752{
1753 int delta;
1754 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001755 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001756
1757 if (msg->sl.rq.v_l != 0)
1758 return 1;
1759
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001760 /* RFC 1945 allows only GET for HTTP/0.9 requests */
1761 if (txn->meth != HTTP_METH_GET)
1762 return 0;
1763
Willy Tarreau9b28e032012-10-12 23:49:43 +02001764 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001765 delta = 0;
1766
1767 if (msg->sl.rq.u_l == 0) {
Apollon Oikonomopoulos25a15222014-04-06 02:46:00 +03001768 /* HTTP/0.9 requests *must* have a request URI, per RFC 1945 */
1769 return 0;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001770 }
1771 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001772 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001773 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001774 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001775 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001776 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001777 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001778 NULL, NULL);
1779 if (unlikely(!cur_end))
1780 return 0;
1781
1782 /* we have a full HTTP/1.0 request now and we know that
1783 * we have either a CR or an LF at <ptr>.
1784 */
1785 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1786 return 1;
1787}
1788
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001789/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001790 * and "keep-alive" values. If we already know that some headers may safely
1791 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001792 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1793 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001794 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001795 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1796 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1797 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001798 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001799 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001800void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001801{
Willy Tarreau5b154472009-12-21 20:11:07 +01001802 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001803 const char *hdr_val = "Connection";
1804 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001805
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001806 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001807 return;
1808
Willy Tarreau88d349d2010-01-25 12:15:43 +01001809 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1810 hdr_val = "Proxy-Connection";
1811 hdr_len = 16;
1812 }
1813
Willy Tarreau5b154472009-12-21 20:11:07 +01001814 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001815 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001816 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001817 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1818 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001819 if (to_del & 2)
1820 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001821 else
1822 txn->flags |= TX_CON_KAL_SET;
1823 }
1824 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1825 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001826 if (to_del & 1)
1827 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001828 else
1829 txn->flags |= TX_CON_CLO_SET;
1830 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001831 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1832 txn->flags |= TX_HDR_CONN_UPG;
1833 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001834 }
1835
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001836 txn->flags |= TX_HDR_CONN_PRS;
1837 return;
1838}
Willy Tarreau5b154472009-12-21 20:11:07 +01001839
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001840/* Apply desired changes on the Connection: header. Values may be removed and/or
1841 * added depending on the <wanted> flags, which are exclusively composed of
1842 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1843 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1844 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001845void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001846{
1847 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001848 const char *hdr_val = "Connection";
1849 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001850
1851 ctx.idx = 0;
1852
Willy Tarreau88d349d2010-01-25 12:15:43 +01001853
1854 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1855 hdr_val = "Proxy-Connection";
1856 hdr_len = 16;
1857 }
1858
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001859 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001860 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001861 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1862 if (wanted & TX_CON_KAL_SET)
1863 txn->flags |= TX_CON_KAL_SET;
1864 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001865 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001866 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001867 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1868 if (wanted & TX_CON_CLO_SET)
1869 txn->flags |= TX_CON_CLO_SET;
1870 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001871 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001872 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001873 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001874
1875 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1876 return;
1877
1878 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1879 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001880 hdr_val = "Connection: close";
1881 hdr_len = 17;
1882 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1883 hdr_val = "Proxy-Connection: close";
1884 hdr_len = 23;
1885 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001886 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001887 }
1888
1889 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1890 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001891 hdr_val = "Connection: keep-alive";
1892 hdr_len = 22;
1893 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1894 hdr_val = "Proxy-Connection: keep-alive";
1895 hdr_len = 28;
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 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001900}
1901
Willy Tarreauc24715e2014-04-17 15:21:20 +02001902/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to
1903 * the first byte of data after the chunk size, so that we know we can forward
1904 * exactly msg->next bytes. msg->sol contains the exact number of bytes forming
1905 * the chunk size. That way it is always possible to differentiate between the
1906 * start of the body and the start of the data.
Willy Tarreau115acb92009-12-26 13:56:06 +01001907 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001908 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001909 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001910static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001911{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001912 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001913 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001914 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001915 const char *end = buf->data + buf->size;
1916 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001917 unsigned int chunk = 0;
1918
1919 /* The chunk size is in the following form, though we are only
1920 * interested in the size and CRLF :
1921 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1922 */
1923 while (1) {
1924 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001925 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001926 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001927 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001928 if (c < 0) /* not a hex digit anymore */
1929 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001930 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001931 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001932 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001933 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001934 chunk = (chunk << 4) + c;
1935 }
1936
Willy Tarreaud98cf932009-12-27 22:54:55 +01001937 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001938 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001939 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940
1941 while (http_is_spht[(unsigned char)*ptr]) {
1942 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001943 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001944 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001945 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001946 }
1947
Willy Tarreaud98cf932009-12-27 22:54:55 +01001948 /* Up to there, we know that at least one byte is present at *ptr. Check
1949 * for the end of chunk size.
1950 */
1951 while (1) {
1952 if (likely(HTTP_IS_CRLF(*ptr))) {
1953 /* we now have a CR or an LF at ptr */
1954 if (likely(*ptr == '\r')) {
1955 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001956 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001957 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001958 return 0;
1959 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001960
Willy Tarreaud98cf932009-12-27 22:54:55 +01001961 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001962 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001963 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001964 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001965 /* done */
1966 break;
1967 }
1968 else if (*ptr == ';') {
1969 /* chunk extension, ends at next CRLF */
1970 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001971 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001972 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001973 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001974
1975 while (!HTTP_IS_CRLF(*ptr)) {
1976 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001977 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001978 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001979 return 0;
1980 }
1981 /* we have a CRLF now, loop above */
1982 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001983 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001984 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001985 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001986 }
1987
Willy Tarreaud98cf932009-12-27 22:54:55 +01001988 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreauc24715e2014-04-17 15:21:20 +02001989 * which may or may not be present. We save that into ->next,
1990 * and the number of bytes parsed into msg->sol.
Willy Tarreau115acb92009-12-26 13:56:06 +01001991 */
Willy Tarreauc24715e2014-04-17 15:21:20 +02001992 msg->sol = ptr - ptr_old;
Willy Tarreau0161d622013-04-02 01:26:55 +02001993 if (unlikely(ptr < ptr_old))
Willy Tarreauc24715e2014-04-17 15:21:20 +02001994 msg->sol += buf->size;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001995 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001996 msg->chunk_len = chunk;
1997 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001998 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001999 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002000 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002001 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002002 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002003}
2004
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002005/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01002006 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01002007 * the trailers is found, it is automatically scheduled to be forwarded,
2008 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2009 * If not enough data are available, the function does not change anything
Willy Tarreauc24715e2014-04-17 15:21:20 +02002010 * except maybe msg->next if it could parse some lines, and returns zero.
2011 * If a parse error is encountered, the function returns < 0 and does not
2012 * change anything except maybe msg->next. Note that the message must
2013 * already be in HTTP_MSG_TRAILERS state before calling this function,
Willy Tarreau638cd022010-01-03 07:42:04 +01002014 * which implies that all non-trailers data have already been scheduled for
Willy Tarreauc24715e2014-04-17 15:21:20 +02002015 * forwarding, and that msg->next exactly matches the length of trailers
2016 * already parsed and not forwarded. It is also important to note that this
2017 * function is designed to be able to parse wrapped headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002018 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002019static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002020{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002021 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002022
Willy Tarreaua458b672012-03-05 11:17:50 +01002023 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002024 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002025 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002026 const char *ptr = b_ptr(buf, msg->next);
2027 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01002028 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002029
2030 /* scan current line and stop at LF or CRLF */
2031 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002032 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002033 return 0;
2034
2035 if (*ptr == '\n') {
2036 if (!p1)
2037 p1 = ptr;
2038 p2 = ptr;
2039 break;
2040 }
2041
2042 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002043 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002044 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002045 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002046 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002047 p1 = ptr;
2048 }
2049
2050 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002051 if (ptr >= buf->data + buf->size)
2052 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002053 }
2054
2055 /* after LF; point to beginning of next line */
2056 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002057 if (p2 >= buf->data + buf->size)
2058 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002059
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002060 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01002061 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002062 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01002063
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002064 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01002065 /* LF/CRLF at beginning of line => end of trailers at p2.
2066 * Everything was scheduled for forwarding, there's nothing
2067 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002068 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002069 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002070 msg->msg_state = HTTP_MSG_DONE;
2071 return 1;
2072 }
2073 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002074 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002075 }
2076}
2077
Willy Tarreauc24715e2014-04-17 15:21:20 +02002078/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF
2079 * or a possible LF alone at the end of a chunk. It automatically adjusts
2080 * msg->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01002081 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002082 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2083 * not enough data are available, the function does not change anything and
2084 * returns zero. If a parse error is encountered, the function returns < 0 and
2085 * does not change anything. Note: this function is designed to parse wrapped
2086 * CRLF at the end of the buffer.
2087 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02002088static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002089{
Willy Tarreau9b28e032012-10-12 23:49:43 +02002090 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01002091 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002092 int bytes;
2093
2094 /* NB: we'll check data availabilty at the end. It's not a
2095 * problem because whatever we match first will be checked
2096 * against the correct length.
2097 */
2098 bytes = 1;
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002099 ptr = b_ptr(buf, msg->next);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002100 if (*ptr == '\r') {
2101 bytes++;
2102 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002103 if (ptr >= buf->data + buf->size)
2104 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002105 }
2106
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002107 if (msg->next + bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002108 return 0;
2109
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002110 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002111 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002112 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002113 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002114
2115 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02002116 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02002117 ptr = buf->data;
Willy Tarreauc24715e2014-04-17 15:21:20 +02002118 /* Advance ->next to allow the CRLF to be forwarded */
Willy Tarreau0669d7d2014-04-17 11:40:10 +02002119 msg->next += bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002120 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2121 return 1;
2122}
Willy Tarreau5b154472009-12-21 20:11:07 +01002123
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002124/* Parses a qvalue and returns it multipled by 1000, from 0 to 1000. If the
2125 * value is larger than 1000, it is bound to 1000. The parser consumes up to
2126 * 1 digit, one dot and 3 digits and stops on the first invalid character.
2127 * Unparsable qvalues return 1000 as "q=1.000".
2128 */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002129int parse_qvalue(const char *qvalue, const char **end)
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002130{
2131 int q = 1000;
2132
2133 if (!isdigit(*qvalue))
2134 goto out;
2135 q = (*qvalue++ - '0') * 1000;
2136
2137 if (*qvalue++ != '.')
2138 goto out;
2139
2140 if (!isdigit(*qvalue))
2141 goto out;
2142 q += (*qvalue++ - '0') * 100;
2143
2144 if (!isdigit(*qvalue))
2145 goto out;
2146 q += (*qvalue++ - '0') * 10;
2147
2148 if (!isdigit(*qvalue))
2149 goto out;
2150 q += (*qvalue++ - '0') * 1;
2151 out:
2152 if (q > 1000)
2153 q = 1000;
Willy Tarreau38b3aa52014-04-22 23:32:05 +02002154 if (end)
Thierry FOURNIERad903512014-04-11 17:51:01 +02002155 *end = qvalue;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002156 return q;
2157}
William Lallemand82fe75c2012-10-23 10:25:10 +02002158
2159/*
2160 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02002161 */
William Lallemand82fe75c2012-10-23 10:25:10 +02002162int select_compression_request_header(struct session *s, struct buffer *req)
2163{
2164 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02002165 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02002166 struct hdr_ctx ctx;
2167 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002168 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002169
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002170 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
2171 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02002172 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
2173 */
2174 ctx.idx = 0;
2175 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2176 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002177 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2178 (ctx.vlen < 31 ||
2179 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2180 ctx.line[ctx.val + 30] < '6' ||
2181 (ctx.line[ctx.val + 30] == '6' &&
2182 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2183 s->comp_algo = NULL;
2184 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002185 }
2186
William Lallemand82fe75c2012-10-23 10:25:10 +02002187 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002188 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 +01002189 int best_q = 0;
2190
William Lallemand82fe75c2012-10-23 10:25:10 +02002191 ctx.idx = 0;
2192 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002193 const char *qval;
2194 int q;
2195 int toklen;
2196
2197 /* try to isolate the token from the optional q-value */
2198 toklen = 0;
2199 while (toklen < ctx.vlen && http_is_token[(unsigned char)*(ctx.line + ctx.val + toklen)])
2200 toklen++;
2201
2202 qval = ctx.line + ctx.val + toklen;
2203 while (1) {
2204 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2205 qval++;
2206
2207 if (qval >= ctx.line + ctx.val + ctx.vlen || *qval != ';') {
2208 qval = NULL;
2209 break;
2210 }
2211 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002212
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002213 while (qval < ctx.line + ctx.val + ctx.vlen && http_is_lws[(unsigned char)*qval])
2214 qval++;
Willy Tarreau70737d12012-10-27 00:34:28 +02002215
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002216 if (qval >= ctx.line + ctx.val + ctx.vlen) {
2217 qval = NULL;
2218 break;
William Lallemand82fe75c2012-10-23 10:25:10 +02002219 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002220 if (strncmp(qval, "q=", MIN(ctx.line + ctx.val + ctx.vlen - qval, 2)) == 0)
2221 break;
2222
2223 while (qval < ctx.line + ctx.val + ctx.vlen && *qval != ';')
2224 qval++;
2225 }
2226
2227 /* here we have qval pointing to the first "q=" attribute or NULL if not found */
Thierry FOURNIERad903512014-04-11 17:51:01 +02002228 q = qval ? parse_qvalue(qval + 2, NULL) : 1000;
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002229
2230 if (q <= best_q)
2231 continue;
2232
2233 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
2234 if (*(ctx.line + ctx.val) == '*' ||
2235 word_match(ctx.line + ctx.val, toklen, comp_algo->name, comp_algo->name_len)) {
2236 s->comp_algo = comp_algo;
2237 best_q = q;
2238 break;
2239 }
2240 }
2241 }
2242 }
2243
2244 /* remove all occurrences of the header when "compression offload" is set */
2245 if (s->comp_algo) {
2246 if ((s->be->comp && s->be->comp->offload) || (s->fe->comp && s->fe->comp->offload)) {
2247 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2248 ctx.idx = 0;
2249 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2250 http_remove_header2(msg, &txn->hdr_idx, &ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002251 }
2252 }
Willy Tarreau0e9b1b42014-03-19 12:07:52 +01002253 return 1;
William Lallemand82fe75c2012-10-23 10:25:10 +02002254 }
2255
2256 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002257 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2258 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002259 if (comp_algo->add_data == identity_add_data) {
2260 s->comp_algo = comp_algo;
2261 return 1;
2262 }
2263 }
2264 }
2265
2266 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002267 return 0;
2268}
2269
2270/*
2271 * Selects a comression algorithm depending of the server response.
2272 */
2273int select_compression_response_header(struct session *s, struct buffer *res)
2274{
2275 struct http_txn *txn = &s->txn;
2276 struct http_msg *msg = &txn->rsp;
2277 struct hdr_ctx ctx;
2278 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002279
2280 /* no common compression algorithm was found in request header */
2281 if (s->comp_algo == NULL)
2282 goto fail;
2283
2284 /* HTTP < 1.1 should not be compressed */
Willy Tarreau72575502013-12-24 14:41:35 +01002285 if (!(msg->flags & HTTP_MSGF_VER_11) || !(txn->req.flags & HTTP_MSGF_VER_11))
William Lallemand82fe75c2012-10-23 10:25:10 +02002286 goto fail;
2287
William Lallemandd3002612012-11-26 14:34:47 +01002288 /* 200 only */
2289 if (txn->status != 200)
2290 goto fail;
2291
William Lallemand82fe75c2012-10-23 10:25:10 +02002292 /* Content-Length is null */
2293 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2294 goto fail;
2295
2296 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002297 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002298 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2299 goto fail;
2300
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002301 /* no compression when Cache-Control: no-transform is present in the message */
2302 ctx.idx = 0;
2303 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2304 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2305 goto fail;
2306 }
2307
William Lallemand82fe75c2012-10-23 10:25:10 +02002308 comp_type = NULL;
2309
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002310 /* we don't want to compress multipart content-types, nor content-types that are
2311 * not listed in the "compression type" directive if any. If no content-type was
2312 * found but configuration requires one, we don't compress either. Backend has
2313 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002314 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002315 ctx.idx = 0;
2316 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2317 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2318 goto fail;
2319
2320 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2321 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002322 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002323 if (ctx.vlen >= comp_type->name_len &&
2324 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002325 /* this Content-Type should be compressed */
2326 break;
2327 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002328 /* this Content-Type should not be compressed */
2329 if (comp_type == NULL)
2330 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002331 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002332 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002333 else { /* no content-type header */
2334 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2335 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002336 }
2337
William Lallemandd85f9172012-11-09 17:05:39 +01002338 /* limit compression rate */
2339 if (global.comp_rate_lim > 0)
2340 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2341 goto fail;
2342
William Lallemand072a2bf2012-11-20 17:01:01 +01002343 /* limit cpu usage */
2344 if (idle_pct < compress_min_idle)
2345 goto fail;
2346
William Lallemand4c49fae2012-11-07 15:00:23 +01002347 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002348 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002349 goto fail;
2350
William Lallemandec3e3892012-11-12 17:02:18 +01002351 s->flags |= SN_COMP_READY;
2352
William Lallemand82fe75c2012-10-23 10:25:10 +02002353 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002354 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002355 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2356 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2357
2358 /* add Transfer-Encoding header */
2359 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2360 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2361
2362 /*
2363 * Add Content-Encoding header when it's not identity encoding.
2364 * RFC 2616 : Identity encoding: This content-coding is used only in the
2365 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2366 * header.
2367 */
2368 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002369 trash.len = 18;
2370 memcpy(trash.str, "Content-Encoding: ", trash.len);
2371 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2372 trash.len += s->comp_algo->name_len;
2373 trash.str[trash.len] = '\0';
2374 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002375 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002376 return 1;
2377
2378fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002379 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002380 return 0;
2381}
2382
2383
Willy Tarreaud787e662009-07-07 10:14:51 +02002384/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2385 * processing can continue on next analysers, or zero if it either needs more
2386 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2387 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2388 * when it has nothing left to do, and may remove any analyser when it wants to
2389 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002390 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002391int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002392{
Willy Tarreau59234e92008-11-30 23:51:27 +01002393 /*
2394 * We will parse the partial (or complete) lines.
2395 * We will check the request syntax, and also join multi-line
2396 * headers. An index of all the lines will be elaborated while
2397 * parsing.
2398 *
2399 * For the parsing, we use a 28 states FSM.
2400 *
2401 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002402 * req->buf->p = beginning of request
2403 * req->buf->p + msg->eoh = end of processed headers / start of current one
2404 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002405 * msg->eol = end of current header or line (LF or CRLF)
2406 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002407 *
2408 * At end of parsing, we may perform a capture of the error (if any), and
Willy Tarreau877e78d2013-04-07 18:48:08 +02002409 * we will set a few fields (txn->meth, sn->flags/SN_REDIRECTABLE).
Willy Tarreaud787e662009-07-07 10:14:51 +02002410 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2411 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002412 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002413
Willy Tarreau59234e92008-11-30 23:51:27 +01002414 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002415 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002416 struct http_txn *txn = &s->txn;
2417 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002418 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002419
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002420 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 +01002421 now_ms, __FUNCTION__,
2422 s,
2423 req,
2424 req->rex, req->wex,
2425 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002426 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002427 req->analysers);
2428
Willy Tarreau52a0c602009-08-16 22:45:38 +02002429 /* we're speaking HTTP here, so let's speak HTTP to the client */
2430 s->srv_error = http_return_srv_error;
2431
Willy Tarreau83e3af02009-12-28 17:39:57 +01002432 /* There's a protected area at the end of the buffer for rewriting
2433 * purposes. We don't want to start to parse the request if the
2434 * protected area is affected, because we may have to move processed
2435 * data later, which is much more complicated.
2436 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002437 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002438 if (txn->flags & TX_NOT_FIRST) {
2439 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002440 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002441 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002442 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002443 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002444 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002445 req->flags |= CF_WAKE_WRITE;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002446 return 0;
2447 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002448 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2449 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2450 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002451 }
2452
Willy Tarreau065e8332010-01-08 00:30:20 +01002453 /* Note that we have the same problem with the response ; we
2454 * may want to send a redirect, error or anything which requires
2455 * some spare space. So we'll ensure that we have at least
2456 * maxrewrite bytes available in the response buffer before
2457 * processing that one. This will only affect pipelined
2458 * keep-alive requests.
2459 */
2460 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002461 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002462 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2463 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2464 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002465 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002466 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002467 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002468 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002469 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01002470 s->rep->flags |= CF_WAKE_WRITE;
Willy Tarreau0499e352010-12-17 07:13:42 +01002471 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002472 return 0;
2473 }
2474 }
2475
Willy Tarreau9b28e032012-10-12 23:49:43 +02002476 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002477 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002478 }
2479
Willy Tarreau59234e92008-11-30 23:51:27 +01002480 /* 1: we might have to print this header in debug mode */
2481 if (unlikely((global.mode & MODE_DEBUG) &&
2482 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002483 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002484 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002485
Willy Tarreau9b28e032012-10-12 23:49:43 +02002486 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002487 /* this is a bit complex : in case of error on the request line,
2488 * we know that rq.l is still zero, so we display only the part
2489 * up to the end of the line (truncated by debug_hdr).
2490 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002491 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002492 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002493
Willy Tarreau59234e92008-11-30 23:51:27 +01002494 sol += hdr_idx_first_pos(&txn->hdr_idx);
2495 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002496
Willy Tarreau59234e92008-11-30 23:51:27 +01002497 while (cur_idx) {
2498 eol = sol + txn->hdr_idx.v[cur_idx].len;
2499 debug_hdr("clihdr", s, sol, eol);
2500 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2501 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002502 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002503 }
2504
Willy Tarreau58f10d72006-12-04 02:26:12 +01002505
Willy Tarreau59234e92008-11-30 23:51:27 +01002506 /*
2507 * Now we quickly check if we have found a full valid request.
2508 * If not so, we check the FD and buffer states before leaving.
2509 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002510 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002511 * requests are checked first. When waiting for a second request
2512 * on a keep-alive session, if we encounter and error, close, t/o,
2513 * we note the error in the session flags but don't set any state.
2514 * Since the error will be noted there, it will not be counted by
2515 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002516 * Last, we may increase some tracked counters' http request errors on
2517 * the cases that are deliberately the client's fault. For instance,
2518 * a timeout or connection reset is not counted as an error. However
2519 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002520 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002521
Willy Tarreau655dce92009-11-08 13:10:58 +01002522 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002523 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002524 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002525 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002526 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002527 session_inc_http_req_ctr(s);
2528 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002529 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002530 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002531 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002532
Willy Tarreau59234e92008-11-30 23:51:27 +01002533 /* 1: Since we are in header mode, if there's no space
2534 * left for headers, we won't be able to free more
2535 * later, so the session will never terminate. We
2536 * must terminate it now.
2537 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002538 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002539 /* FIXME: check if URI is set and return Status
2540 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002541 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002542 session_inc_http_req_ctr(s);
2543 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002544 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002545 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002546 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002547 goto return_bad_req;
2548 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002549
Willy Tarreau59234e92008-11-30 23:51:27 +01002550 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002551 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002552 if (!(s->flags & SN_ERR_MASK))
2553 s->flags |= SN_ERR_CLICL;
2554
Willy Tarreaufcffa692010-01-10 14:21:19 +01002555 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002556 goto failed_keep_alive;
2557
Willy Tarreau59234e92008-11-30 23:51:27 +01002558 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002559 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002560 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002561 session_inc_http_err_ctr(s);
2562 }
2563
Willy Tarreaudc979f22012-12-04 10:39:01 +01002564 txn->status = 400;
2565 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002566 msg->msg_state = HTTP_MSG_ERROR;
2567 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002568
Willy Tarreauda7ff642010-06-23 11:44:09 +02002569 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002570 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002571 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002572 if (s->listener->counters)
2573 s->listener->counters->failed_req++;
2574
Willy Tarreau59234e92008-11-30 23:51:27 +01002575 if (!(s->flags & SN_FINST_MASK))
2576 s->flags |= SN_FINST_R;
2577 return 0;
2578 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002579
Willy Tarreau59234e92008-11-30 23:51:27 +01002580 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002581 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002582 if (!(s->flags & SN_ERR_MASK))
2583 s->flags |= SN_ERR_CLITO;
2584
Willy Tarreaufcffa692010-01-10 14:21:19 +01002585 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002586 goto failed_keep_alive;
2587
Willy Tarreau59234e92008-11-30 23:51:27 +01002588 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002589 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002590 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002591 session_inc_http_err_ctr(s);
2592 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002593 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002594 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002595 msg->msg_state = HTTP_MSG_ERROR;
2596 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002597
Willy Tarreauda7ff642010-06-23 11:44:09 +02002598 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002599 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002600 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002601 if (s->listener->counters)
2602 s->listener->counters->failed_req++;
2603
Willy Tarreau59234e92008-11-30 23:51:27 +01002604 if (!(s->flags & SN_FINST_MASK))
2605 s->flags |= SN_FINST_R;
2606 return 0;
2607 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002608
Willy Tarreau59234e92008-11-30 23:51:27 +01002609 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002610 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002611 if (!(s->flags & SN_ERR_MASK))
2612 s->flags |= SN_ERR_CLICL;
2613
Willy Tarreaufcffa692010-01-10 14:21:19 +01002614 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002615 goto failed_keep_alive;
2616
Willy Tarreau4076a152009-04-02 15:18:36 +02002617 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002618 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002619 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002620 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002621 msg->msg_state = HTTP_MSG_ERROR;
2622 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002623
Willy Tarreauda7ff642010-06-23 11:44:09 +02002624 session_inc_http_err_ctr(s);
2625 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002626 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002627 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002628 if (s->listener->counters)
2629 s->listener->counters->failed_req++;
2630
Willy Tarreau59234e92008-11-30 23:51:27 +01002631 if (!(s->flags & SN_FINST_MASK))
2632 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002633 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002634 }
2635
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002636 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002637 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2638 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002639#ifdef TCP_QUICKACK
Willy Tarreau3c728722014-01-23 13:50:42 +01002640 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 +01002641 /* We need more data, we have to re-enable quick-ack in case we
2642 * previously disabled it, otherwise we might cause the client
2643 * to delay next data.
2644 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002645 setsockopt(__objt_conn(s->req->prod->end)->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002646 }
2647#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002648
Willy Tarreaufcffa692010-01-10 14:21:19 +01002649 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2650 /* If the client starts to talk, let's fall back to
2651 * request timeout processing.
2652 */
2653 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002654 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002655 }
2656
Willy Tarreau59234e92008-11-30 23:51:27 +01002657 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002658 if (!tick_isset(req->analyse_exp)) {
2659 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2660 (txn->flags & TX_WAIT_NEXT_RQ) &&
2661 tick_isset(s->be->timeout.httpka))
2662 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2663 else
2664 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2665 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002666
Willy Tarreau59234e92008-11-30 23:51:27 +01002667 /* we're not ready yet */
2668 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002669
2670 failed_keep_alive:
2671 /* Here we process low-level errors for keep-alive requests. In
2672 * short, if the request is not the first one and it experiences
2673 * a timeout, read error or shutdown, we just silently close so
2674 * that the client can try again.
2675 */
2676 txn->status = 0;
2677 msg->msg_state = HTTP_MSG_RQBEFORE;
2678 req->analysers = 0;
2679 s->logs.logwait = 0;
Willy Tarreauabcd5142013-06-11 17:18:02 +02002680 s->logs.level = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002681 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002682 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002683 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002684 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002685
Willy Tarreaud787e662009-07-07 10:14:51 +02002686 /* OK now we have a complete HTTP request with indexed headers. Let's
2687 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002688 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002689 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002690 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002691 * byte after the last LF. msg->sov points to the first byte of data.
2692 * msg->eol cannot be trusted because it may have been left uninitialized
2693 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002694 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002695
Willy Tarreauda7ff642010-06-23 11:44:09 +02002696 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002697 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2698
Willy Tarreaub16a5742010-01-10 14:46:16 +01002699 if (txn->flags & TX_WAIT_NEXT_RQ) {
2700 /* kill the pending keep-alive timeout */
2701 txn->flags &= ~TX_WAIT_NEXT_RQ;
2702 req->analyse_exp = TICK_ETERNITY;
2703 }
2704
2705
Willy Tarreaud787e662009-07-07 10:14:51 +02002706 /* Maybe we found in invalid header name while we were configured not
2707 * to block on that, so we have to capture it now.
2708 */
2709 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002710 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002711
Willy Tarreau59234e92008-11-30 23:51:27 +01002712 /*
2713 * 1: identify the method
2714 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002715 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002716
2717 /* we can make use of server redirect on GET and HEAD */
2718 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2719 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002720
Willy Tarreau59234e92008-11-30 23:51:27 +01002721 /*
2722 * 2: check if the URI matches the monitor_uri.
2723 * We have to do this for every request which gets in, because
2724 * the monitor-uri is defined by the frontend.
2725 */
2726 if (unlikely((s->fe->monitor_uri_len != 0) &&
2727 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002728 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002729 s->fe->monitor_uri,
2730 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002731 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002732 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002733 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002734 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002735
Willy Tarreau59234e92008-11-30 23:51:27 +01002736 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002737 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002738
Willy Tarreau59234e92008-11-30 23:51:27 +01002739 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002740 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002741 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002742
Willy Tarreau59234e92008-11-30 23:51:27 +01002743 ret = acl_pass(ret);
2744 if (cond->pol == ACL_COND_UNLESS)
2745 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002746
Willy Tarreau59234e92008-11-30 23:51:27 +01002747 if (ret) {
2748 /* we fail this request, let's return 503 service unavail */
2749 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002750 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau570f2212013-06-10 16:42:09 +02002751 if (!(s->flags & SN_ERR_MASK))
2752 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002753 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002754 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002755 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002756
Willy Tarreau59234e92008-11-30 23:51:27 +01002757 /* nothing to fail, let's reply normaly */
2758 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002759 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau570f2212013-06-10 16:42:09 +02002760 if (!(s->flags & SN_ERR_MASK))
2761 s->flags |= SN_ERR_LOCAL; /* we don't want a real error here */
Willy Tarreau59234e92008-11-30 23:51:27 +01002762 goto return_prx_cond;
2763 }
2764
2765 /*
2766 * 3: Maybe we have to copy the original REQURI for the logs ?
2767 * Note: we cannot log anymore if the request has been
2768 * classified as invalid.
2769 */
2770 if (unlikely(s->logs.logwait & LW_REQ)) {
2771 /* we have a complete HTTP request that we must log */
2772 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2773 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002774
Willy Tarreau59234e92008-11-30 23:51:27 +01002775 if (urilen >= REQURI_LEN)
2776 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002777 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002778 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002779
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002780 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002781 s->do_log(s);
2782 } else {
2783 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002784 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002785 }
Willy Tarreau06619262006-12-17 08:37:22 +01002786
Willy Tarreau59234e92008-11-30 23:51:27 +01002787 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002788 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002789 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002790
Willy Tarreau5b154472009-12-21 20:11:07 +01002791 /* ... and check if the request is HTTP/1.1 or above */
2792 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002793 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2794 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2795 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002796 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002797
2798 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002799 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 +01002800
Willy Tarreau88d349d2010-01-25 12:15:43 +01002801 /* if the frontend has "option http-use-proxy-header", we'll check if
2802 * we have what looks like a proxied connection instead of a connection,
2803 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2804 * Note that this is *not* RFC-compliant, however browsers and proxies
2805 * happen to do that despite being non-standard :-(
2806 * We consider that a request not beginning with either '/' or '*' is
2807 * a proxied connection, which covers both "scheme://location" and
2808 * CONNECT ip:port.
2809 */
2810 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002811 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002812 txn->flags |= TX_USE_PX_CONN;
2813
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002814 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002815 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002816
Willy Tarreau59234e92008-11-30 23:51:27 +01002817 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002818 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002819 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002820 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002821
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002822 /* 6: determine the transfer-length.
2823 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2824 * the presence of a message-body in a REQUEST and its transfer length
2825 * must be determined that way (in order of precedence) :
2826 * 1. The presence of a message-body in a request is signaled by the
2827 * inclusion of a Content-Length or Transfer-Encoding header field
2828 * in the request's header fields. When a request message contains
2829 * both a message-body of non-zero length and a method that does
2830 * not define any semantics for that request message-body, then an
2831 * origin server SHOULD either ignore the message-body or respond
2832 * with an appropriate error message (e.g., 413). A proxy or
2833 * gateway, when presented the same request, SHOULD either forward
2834 * the request inbound with the message- body or ignore the
2835 * message-body when determining a response.
2836 *
2837 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2838 * and the "chunked" transfer-coding (Section 6.2) is used, the
2839 * transfer-length is defined by the use of this transfer-coding.
2840 * If a Transfer-Encoding header field is present and the "chunked"
2841 * transfer-coding is not present, the transfer-length is defined
2842 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002843 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002844 * 3. If a Content-Length header field is present, its decimal value in
2845 * OCTETs represents both the entity-length and the transfer-length.
2846 * If a message is received with both a Transfer-Encoding header
2847 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002848 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002849 * 4. By the server closing the connection. (Closing the connection
2850 * cannot be used to indicate the end of a request body, since that
2851 * would leave no possibility for the server to send back a response.)
2852 *
2853 * Whenever a transfer-coding is applied to a message-body, the set of
2854 * transfer-codings MUST include "chunked", unless the message indicates
2855 * it is terminated by closing the connection. When the "chunked"
2856 * transfer-coding is used, it MUST be the last transfer-coding applied
2857 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002858 */
2859
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002860 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002861 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002862 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002863 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002864 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002865 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002866 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2867 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002868 /* bad transfer-encoding (chunked followed by something else) */
2869 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002870 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002871 break;
2872 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002873 }
2874
Willy Tarreau32b47f42009-10-18 20:55:02 +02002875 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002876 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002877 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002878 signed long long cl;
2879
Willy Tarreauad14f752011-09-02 20:33:27 +02002880 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002881 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002882 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002883 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002884
Willy Tarreauad14f752011-09-02 20:33:27 +02002885 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002886 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002887 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002888 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002889
Willy Tarreauad14f752011-09-02 20:33:27 +02002890 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002891 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002892 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002893 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002894
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002895 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002896 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002897 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002898 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002899
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002900 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002901 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002902 }
2903
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002904 /* bodyless requests have a known length */
2905 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002906 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002907
Willy Tarreaud787e662009-07-07 10:14:51 +02002908 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002909 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002910 req->analyse_exp = TICK_ETERNITY;
2911 return 1;
2912
2913 return_bad_req:
2914 /* We centralize bad requests processing here */
2915 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2916 /* we detected a parsing error. We want to archive this request
2917 * in the dedicated proxy area for later troubleshooting.
2918 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002919 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002920 }
2921
2922 txn->req.msg_state = HTTP_MSG_ERROR;
2923 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002924 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002925
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002926 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002927 if (s->listener->counters)
2928 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002929
2930 return_prx_cond:
2931 if (!(s->flags & SN_ERR_MASK))
2932 s->flags |= SN_ERR_PRXCOND;
2933 if (!(s->flags & SN_FINST_MASK))
2934 s->flags |= SN_FINST_R;
2935
2936 req->analysers = 0;
2937 req->analyse_exp = TICK_ETERNITY;
2938 return 0;
2939}
2940
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002941
Willy Tarreau347a35d2013-11-22 17:51:09 +01002942/* This function prepares an applet to handle the stats. It can deal with the
2943 * "100-continue" expectation, check that admin rules are met for POST requests,
2944 * and program a response message if something was unexpected. It cannot fail
2945 * and always relies on the stats applet to complete the job. It does not touch
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002946 * analysers nor counters, which are left to the caller. It does not touch
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002947 * s->target which is supposed to already point to the stats applet. The caller
2948 * is expected to have already assigned an appctx to the session.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002949 */
2950int http_handle_stats(struct session *s, struct channel *req)
2951{
2952 struct stats_admin_rule *stats_admin_rule;
2953 struct stream_interface *si = s->rep->prod;
2954 struct http_txn *txn = &s->txn;
2955 struct http_msg *msg = &txn->req;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002956 struct uri_auth *uri_auth = s->be->uri_auth;
2957 const char *uri, *h, *lookup;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002958 struct appctx *appctx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002959
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002960 appctx = si_appctx(si);
2961 memset(&appctx->ctx.stats, 0, sizeof(appctx->ctx.stats));
2962 appctx->st1 = appctx->st2 = 0;
2963 appctx->ctx.stats.st_code = STAT_STATUS_INIT;
2964 appctx->ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreauaf3cf702014-04-22 22:19:53 +02002965 if ((msg->flags & HTTP_MSGF_VER_11) && (s->txn.meth != HTTP_METH_HEAD))
2966 appctx->ctx.stats.flags |= STAT_CHUNKED;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002967
2968 uri = msg->chn->buf->p + msg->sl.rq.u;
2969 lookup = uri + uri_auth->uri_len;
2970
2971 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2972 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002973 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002974 break;
2975 }
2976 }
2977
2978 if (uri_auth->refresh) {
2979 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2980 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002981 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002982 break;
2983 }
2984 }
2985 }
2986
2987 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2988 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002989 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002990 break;
2991 }
2992 }
2993
2994 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2995 if (memcmp(h, ";st=", 4) == 0) {
2996 int i;
2997 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002998 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002999 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
3000 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003001 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003002 break;
3003 }
3004 }
3005 break;
3006 }
3007 }
3008
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003009 appctx->ctx.stats.scope_str = 0;
3010 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003011 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3012 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3013 int itx = 0;
3014 const char *h2;
3015 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3016 const char *err;
3017
3018 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3019 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003020 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003021 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3022 itx++;
3023 h++;
3024 }
3025
3026 if (itx > STAT_SCOPE_TXT_MAXLEN)
3027 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003028 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003029
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003030 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003031 memcpy(scope_txt, h2, itx);
3032 scope_txt[itx] = '\0';
3033 err = invalid_char(scope_txt);
3034 if (err) {
3035 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003036 appctx->ctx.stats.scope_str = 0;
3037 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003038 }
3039 break;
3040 }
3041 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003042
3043 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003044 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003045 int ret = 1;
3046
3047 if (stats_admin_rule->cond) {
3048 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3049 ret = acl_pass(ret);
3050 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3051 ret = !ret;
3052 }
3053
3054 if (ret) {
3055 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003056 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003057 break;
3058 }
3059 }
3060
3061 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003062 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003063 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003064 if (msg->msg_state < HTTP_MSG_100_SENT) {
3065 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3066 * send an HTTP/1.1 100 Continue intermediate response.
3067 */
3068 if (msg->flags & HTTP_MSGF_VER_11) {
3069 struct hdr_ctx ctx;
3070 ctx.idx = 0;
3071 /* Expect is allowed in 1.1, look for it */
3072 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
3073 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3074 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
3075 }
3076 }
3077 msg->msg_state = HTTP_MSG_100_SENT;
3078 s->logs.tv_request = now; /* update the request timer to reflect full request */
3079 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003080 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003081 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003082 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003083 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3084 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003085 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003086 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003087 else {
3088 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003089 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003090 }
3091
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003092 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003093 return 1;
3094}
3095
Lukas Tribus67db8df2013-06-23 17:37:13 +02003096/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3097 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3098 */
3099static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3100{
3101#ifdef IP_TOS
3102 if (from.ss_family == AF_INET)
3103 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3104#endif
3105#ifdef IPV6_TCLASS
3106 if (from.ss_family == AF_INET6) {
3107 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3108 /* v4-mapped addresses need IP_TOS */
3109 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3110 else
3111 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3112 }
3113#endif
3114}
3115
Willy Tarreau20b0de52012-12-24 15:45:22 +01003116/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003117 * transaction <txn>. Returns the first rule that prevents further processing
3118 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3119 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3120 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003121 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003122static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003123http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003124{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003125 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003126 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003127 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003128
Willy Tarreauff011f22011-01-06 17:51:27 +01003129 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003130 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003131 continue;
3132
Willy Tarreau96257ec2012-12-27 10:46:37 +01003133 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003134 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003135 int ret;
3136
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003137 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003138 ret = acl_pass(ret);
3139
Willy Tarreauff011f22011-01-06 17:51:27 +01003140 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003141 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003142
3143 if (!ret) /* condition not matched */
3144 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003145 }
3146
Willy Tarreau20b0de52012-12-24 15:45:22 +01003147
Willy Tarreau96257ec2012-12-27 10:46:37 +01003148 switch (rule->action) {
3149 case HTTP_REQ_ACT_ALLOW:
3150 return NULL; /* "allow" rules are OK */
3151
3152 case HTTP_REQ_ACT_DENY:
3153 txn->flags |= TX_CLDENY;
3154 return rule;
3155
Willy Tarreauccbcc372012-12-27 12:37:57 +01003156 case HTTP_REQ_ACT_TARPIT:
3157 txn->flags |= TX_CLTARPIT;
3158 return rule;
3159
Willy Tarreau96257ec2012-12-27 10:46:37 +01003160 case HTTP_REQ_ACT_AUTH:
3161 return rule;
3162
Willy Tarreau81499eb2012-12-27 12:19:02 +01003163 case HTTP_REQ_ACT_REDIR:
3164 return rule;
3165
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003166 case HTTP_REQ_ACT_SET_NICE:
3167 s->task->nice = rule->arg.nice;
3168 break;
3169
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003170 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003171 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003172 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003173 break;
3174
Willy Tarreau51347ed2013-06-11 19:34:13 +02003175 case HTTP_REQ_ACT_SET_MARK:
3176#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003177 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003178 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003179#endif
3180 break;
3181
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003182 case HTTP_REQ_ACT_SET_LOGL:
3183 s->logs.level = rule->arg.loglevel;
3184 break;
3185
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003186 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003187 case HTTP_REQ_ACT_SET_HDR:
3188 ctx.idx = 0;
3189 /* remove all occurrences of the header */
3190 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3191 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3192 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003193 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003194 if (rule->action == HTTP_REQ_ACT_DEL_HDR)
3195 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003196 /* now fall through to header addition */
3197
3198 case HTTP_REQ_ACT_ADD_HDR:
3199 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3200 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3201 trash.len = rule->arg.hdr_add.name_len;
3202 trash.str[trash.len++] = ':';
3203 trash.str[trash.len++] = ' ';
3204 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3205 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3206 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003207 }
3208 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003209
3210 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003211 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003212}
3213
Willy Tarreau71241ab2012-12-27 11:30:54 +01003214
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003215/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3216 * transaction <txn>. Returns the first rule that prevents further processing
3217 * of the response (deny, ...) or NULL if it executed all rules or stopped
3218 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3219 * rule.
3220 */
3221static struct http_res_rule *
3222http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3223{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003224 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003225 struct http_res_rule *rule;
3226 struct hdr_ctx ctx;
3227
3228 list_for_each_entry(rule, rules, list) {
3229 if (rule->action >= HTTP_RES_ACT_MAX)
3230 continue;
3231
3232 /* check optional condition */
3233 if (rule->cond) {
3234 int ret;
3235
3236 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3237 ret = acl_pass(ret);
3238
3239 if (rule->cond->pol == ACL_COND_UNLESS)
3240 ret = !ret;
3241
3242 if (!ret) /* condition not matched */
3243 continue;
3244 }
3245
3246
3247 switch (rule->action) {
3248 case HTTP_RES_ACT_ALLOW:
3249 return NULL; /* "allow" rules are OK */
3250
3251 case HTTP_RES_ACT_DENY:
3252 txn->flags |= TX_SVDENY;
3253 return rule;
3254
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003255 case HTTP_RES_ACT_SET_NICE:
3256 s->task->nice = rule->arg.nice;
3257 break;
3258
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003259 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003260 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003261 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003262 break;
3263
Willy Tarreau51347ed2013-06-11 19:34:13 +02003264 case HTTP_RES_ACT_SET_MARK:
3265#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003266 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003267 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003268#endif
3269 break;
3270
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003271 case HTTP_RES_ACT_SET_LOGL:
3272 s->logs.level = rule->arg.loglevel;
3273 break;
3274
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003275 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003276 case HTTP_RES_ACT_SET_HDR:
3277 ctx.idx = 0;
3278 /* remove all occurrences of the header */
3279 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3280 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3281 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3282 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003283 if (rule->action == HTTP_RES_ACT_DEL_HDR)
3284 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003285 /* now fall through to header addition */
3286
3287 case HTTP_RES_ACT_ADD_HDR:
3288 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3289 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3290 trash.len = rule->arg.hdr_add.name_len;
3291 trash.str[trash.len++] = ':';
3292 trash.str[trash.len++] = ' ';
3293 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3294 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3295 break;
3296 }
3297 }
3298
3299 /* we reached the end of the rules, nothing to report */
3300 return NULL;
3301}
3302
3303
Willy Tarreau71241ab2012-12-27 11:30:54 +01003304/* Perform an HTTP redirect based on the information in <rule>. The function
3305 * returns non-zero on success, or zero in case of a, irrecoverable error such
3306 * as too large a request to build a valid response.
3307 */
3308static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3309{
3310 struct http_msg *msg = &txn->req;
3311 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003312 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003313
3314 /* build redirect message */
3315 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003316 case 308:
3317 msg_fmt = HTTP_308;
3318 break;
3319 case 307:
3320 msg_fmt = HTTP_307;
3321 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003322 case 303:
3323 msg_fmt = HTTP_303;
3324 break;
3325 case 301:
3326 msg_fmt = HTTP_301;
3327 break;
3328 case 302:
3329 default:
3330 msg_fmt = HTTP_302;
3331 break;
3332 }
3333
3334 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3335 return 0;
3336
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003337 location = trash.str + trash.len;
3338
Willy Tarreau71241ab2012-12-27 11:30:54 +01003339 switch(rule->type) {
3340 case REDIRECT_TYPE_SCHEME: {
3341 const char *path;
3342 const char *host;
3343 struct hdr_ctx ctx;
3344 int pathlen;
3345 int hostlen;
3346
3347 host = "";
3348 hostlen = 0;
3349 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003350 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003351 host = ctx.line + ctx.val;
3352 hostlen = ctx.vlen;
3353 }
3354
3355 path = http_get_path(txn);
3356 /* build message using path */
3357 if (path) {
3358 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3359 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3360 int qs = 0;
3361 while (qs < pathlen) {
3362 if (path[qs] == '?') {
3363 pathlen = qs;
3364 break;
3365 }
3366 qs++;
3367 }
3368 }
3369 } else {
3370 path = "/";
3371 pathlen = 1;
3372 }
3373
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003374 if (rule->rdr_str) { /* this is an old "redirect" rule */
3375 /* check if we can add scheme + "://" + host + path */
3376 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3377 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003378
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003379 /* add scheme */
3380 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3381 trash.len += rule->rdr_len;
3382 }
3383 else {
3384 /* add scheme with executing log format */
3385 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003386
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003387 /* check if we can add scheme + "://" + host + path */
3388 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3389 return 0;
3390 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003391 /* add "://" */
3392 memcpy(trash.str + trash.len, "://", 3);
3393 trash.len += 3;
3394
3395 /* add host */
3396 memcpy(trash.str + trash.len, host, hostlen);
3397 trash.len += hostlen;
3398
3399 /* add path */
3400 memcpy(trash.str + trash.len, path, pathlen);
3401 trash.len += pathlen;
3402
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003403 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003404 if (trash.len && trash.str[trash.len - 1] != '/' &&
3405 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3406 if (trash.len > trash.size - 5)
3407 return 0;
3408 trash.str[trash.len] = '/';
3409 trash.len++;
3410 }
3411
3412 break;
3413 }
3414 case REDIRECT_TYPE_PREFIX: {
3415 const char *path;
3416 int pathlen;
3417
3418 path = http_get_path(txn);
3419 /* build message using path */
3420 if (path) {
3421 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3422 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3423 int qs = 0;
3424 while (qs < pathlen) {
3425 if (path[qs] == '?') {
3426 pathlen = qs;
3427 break;
3428 }
3429 qs++;
3430 }
3431 }
3432 } else {
3433 path = "/";
3434 pathlen = 1;
3435 }
3436
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003437 if (rule->rdr_str) { /* this is an old "redirect" rule */
3438 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3439 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003440
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003441 /* add prefix. Note that if prefix == "/", we don't want to
3442 * add anything, otherwise it makes it hard for the user to
3443 * configure a self-redirection.
3444 */
3445 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3446 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3447 trash.len += rule->rdr_len;
3448 }
3449 }
3450 else {
3451 /* add prefix with executing log format */
3452 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3453
3454 /* Check length */
3455 if (trash.len + pathlen > trash.size - 4)
3456 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003457 }
3458
3459 /* add path */
3460 memcpy(trash.str + trash.len, path, pathlen);
3461 trash.len += pathlen;
3462
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003463 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003464 if (trash.len && trash.str[trash.len - 1] != '/' &&
3465 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3466 if (trash.len > trash.size - 5)
3467 return 0;
3468 trash.str[trash.len] = '/';
3469 trash.len++;
3470 }
3471
3472 break;
3473 }
3474 case REDIRECT_TYPE_LOCATION:
3475 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003476 if (rule->rdr_str) { /* this is an old "redirect" rule */
3477 if (trash.len + rule->rdr_len > trash.size - 4)
3478 return 0;
3479
3480 /* add location */
3481 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3482 trash.len += rule->rdr_len;
3483 }
3484 else {
3485 /* add location with executing log format */
3486 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003487
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003488 /* Check left length */
3489 if (trash.len > trash.size - 4)
3490 return 0;
3491 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003492 break;
3493 }
3494
3495 if (rule->cookie_len) {
3496 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3497 trash.len += 14;
3498 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3499 trash.len += rule->cookie_len;
3500 memcpy(trash.str + trash.len, "\r\n", 2);
3501 trash.len += 2;
3502 }
3503
3504 /* add end of headers and the keep-alive/close status.
3505 * We may choose to set keep-alive if the Location begins
3506 * with a slash, because the client will come back to the
3507 * same server.
3508 */
3509 txn->status = rule->code;
3510 /* let's log the request time */
3511 s->logs.tv_request = now;
3512
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003513 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003514 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3515 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3516 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3517 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3518 /* keep-alive possible */
3519 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3520 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3521 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3522 trash.len += 30;
3523 } else {
3524 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3525 trash.len += 24;
3526 }
3527 }
3528 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3529 trash.len += 4;
3530 bo_inject(txn->rsp.chn, trash.str, trash.len);
3531 /* "eat" the request */
3532 bi_fast_delete(txn->req.chn->buf, msg->sov);
Willy Tarreau6d8bac72014-04-25 12:19:32 +02003533 msg->next -= msg->sov;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003534 msg->sov = 0;
3535 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3536 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3537 txn->req.msg_state = HTTP_MSG_CLOSED;
3538 txn->rsp.msg_state = HTTP_MSG_DONE;
3539 } else {
3540 /* keep-alive not possible */
3541 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3542 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3543 trash.len += 29;
3544 } else {
3545 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3546 trash.len += 23;
3547 }
3548 stream_int_retnclose(txn->req.chn->prod, &trash);
3549 txn->req.chn->analysers = 0;
3550 }
3551
3552 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003553 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003554 if (!(s->flags & SN_FINST_MASK))
3555 s->flags |= SN_FINST_R;
3556
3557 return 1;
3558}
3559
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003560/* This stream analyser runs all HTTP request processing which is common to
3561 * frontends and backends, which means blocking ACLs, filters, connection-close,
3562 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003563 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003564 * either needs more data or wants to immediately abort the request (eg: deny,
3565 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003566 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003567int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003568{
Willy Tarreaud787e662009-07-07 10:14:51 +02003569 struct http_txn *txn = &s->txn;
3570 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003571 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003572 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003573 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003574 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003575
Willy Tarreau655dce92009-11-08 13:10:58 +01003576 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003577 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003578 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003579 return 0;
3580 }
3581
Willy Tarreau3a816292009-07-07 10:55:49 +02003582 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003583 req->analyse_exp = TICK_ETERNITY;
3584
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003585 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 +02003586 now_ms, __FUNCTION__,
3587 s,
3588 req,
3589 req->rex, req->wex,
3590 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003591 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003592 req->analysers);
3593
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003594 /* first check whether we have some ACLs set to block this request */
3595 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003596 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003597
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003598 ret = acl_pass(ret);
3599 if (cond->pol == ACL_COND_UNLESS)
3600 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003601
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003602 if (ret) {
3603 txn->status = 403;
3604 /* let's log the request time */
3605 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003606 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003607 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003608 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003609 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003610 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003611
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003612 /* just in case we have some per-backend tracking */
3613 session_inc_be_http_req_ctr(s);
3614
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003615 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003616 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003617
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003618 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003619 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003620 if (stats_check_uri(s->rep->prod, txn, px)) {
3621 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003622 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3623 txn->status = 500;
3624 s->logs.tv_request = now;
3625 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003626
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003627 if (!(s->flags & SN_ERR_MASK))
3628 s->flags |= SN_ERR_RESOURCE;
3629 goto return_prx_cond;
3630 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003631 /* parse the whole stats request and extract the relevant information */
3632 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003633 http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003634 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003635 }
3636
Willy Tarreau3b44e722013-11-16 10:28:23 +01003637 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3638 * blocked by an http-request rule.
3639 */
3640 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003641 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003642 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003643 }
Willy Tarreau06619262006-12-17 08:37:22 +01003644
Willy Tarreau3b44e722013-11-16 10:28:23 +01003645 /* return a 403 if either rule has blocked */
3646 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003647 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003648 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003649 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003650 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003651 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003652 s->fe->fe_counters.denied_req++;
3653 if (s->fe != s->be)
3654 s->be->be_counters.denied_req++;
3655 if (s->listener->counters)
3656 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003657 goto return_prx_cond;
3658 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003659
3660 /* When a connection is tarpitted, we use the tarpit timeout,
3661 * which may be the same as the connect timeout if unspecified.
3662 * If unset, then set it to zero because we really want it to
3663 * eventually expire. We build the tarpit as an analyser.
3664 */
3665 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003666 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003667 /* wipe the request out so that we can drop the connection early
3668 * if the client closes first.
3669 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003670 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003671 req->analysers = 0; /* remove switching rules etc... */
3672 req->analysers |= AN_REQ_HTTP_TARPIT;
3673 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3674 if (!req->analyse_exp)
3675 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003676 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003677 s->fe->fe_counters.denied_req++;
3678 if (s->fe != s->be)
3679 s->be->be_counters.denied_req++;
3680 if (s->listener->counters)
3681 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003682 return 1;
3683 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003684 }
Willy Tarreau06619262006-12-17 08:37:22 +01003685
Willy Tarreau70dffda2014-01-30 03:07:23 +01003686 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
Willy Tarreau5b154472009-12-21 20:11:07 +01003687 * only change if both the request and the config reference something else.
Willy Tarreau70dffda2014-01-30 03:07:23 +01003688 * Option httpclose by itself sets tunnel mode where headers are mangled.
3689 * However, if another mode is set, it will affect it (eg: server-close/
3690 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3691 * if FE and BE have the same settings (common). The method consists in
3692 * checking if options changed between the two calls (implying that either
3693 * one is non-null, or one of them is non-null and we are there for the first
3694 * time.
Willy Tarreau42736642009-10-18 21:04:35 +02003695 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003696
Willy Tarreau416ce612014-01-31 15:45:34 +01003697 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003698 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
Willy Tarreau70dffda2014-01-30 03:07:23 +01003699 int tmp = TX_CON_WANT_KAL;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003700
Willy Tarreau70dffda2014-01-30 03:07:23 +01003701 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
3702 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
3703 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
3704 tmp = TX_CON_WANT_TUN;
3705
3706 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3707 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3708 tmp = TX_CON_WANT_TUN;
3709 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003710
3711 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau70dffda2014-01-30 03:07:23 +01003712 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
3713 /* option httpclose + server_close => forceclose */
3714 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3715 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3716 tmp = TX_CON_WANT_CLO;
3717 else
3718 tmp = TX_CON_WANT_SCL;
3719 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003720
3721 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
3722 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
Willy Tarreau5b154472009-12-21 20:11:07 +01003723 tmp = TX_CON_WANT_CLO;
3724
Willy Tarreau5b154472009-12-21 20:11:07 +01003725 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3726 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003727
Willy Tarreau416ce612014-01-31 15:45:34 +01003728 if (!(txn->flags & TX_HDR_CONN_PRS) &&
3729 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003730 /* parse the Connection header and possibly clean it */
3731 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003732 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003733 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3734 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003735 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003736 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003737 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003738 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003739 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003740
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003741 /* check if client or config asks for explicit close in KAL/SCL */
3742 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3743 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3744 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003745 (!(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 +01003746 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003747 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003748 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3749 }
Willy Tarreau78599912009-10-17 20:12:21 +02003750
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003751 /* we can be blocked here because the request needs to be authenticated,
3752 * either to pass or to access stats.
3753 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003754 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003755 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003756
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003757 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003758 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003759
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003760 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003761 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003762 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003763 /* on 401 we still count one error, because normal browsing
3764 * won't significantly increase the counter but brute force
3765 * attempts will.
3766 */
3767 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003768 goto return_prx_cond;
3769 }
3770
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003771 /* add request headers from the rule sets in the same order */
3772 list_for_each_entry(wl, &px->req_add, list) {
3773 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003774 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003775 ret = acl_pass(ret);
3776 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3777 ret = !ret;
3778 if (!ret)
3779 continue;
3780 }
3781
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003782 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003783 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003784 }
3785
3786 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3787 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3788 goto return_bad_req;
3789 req->analyse_exp = TICK_ETERNITY;
3790 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003791 }
3792
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003793 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003794 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003795 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3796 s->fe->fe_counters.intercepted_req++;
3797
3798 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3799 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3800 if (!(s->flags & SN_FINST_MASK))
3801 s->flags |= SN_FINST_R;
3802
3803 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau70730dd2014-04-24 18:06:27 +02003804
3805 /* we may want to compress the stats page */
3806 if (s->fe->comp || s->be->comp)
3807 select_compression_request_header(s, req->buf);
3808
3809 /* enable the minimally required analyzers to handle keep-alive and compression on the HTTP response */
3810 req->analysers = AN_REQ_HTTP_XFER_BODY | AN_RES_WAIT_HTTP | AN_RES_HTTP_PROCESS_BE | AN_RES_HTTP_XFER_BODY;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003811 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003812 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003813
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003814 /* check whether we have some ACLs set to redirect this request */
3815 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003816 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003817 int ret;
3818
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003819 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003820 ret = acl_pass(ret);
3821 if (rule->cond->pol == ACL_COND_UNLESS)
3822 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003823 if (!ret)
3824 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003825 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003826 if (!http_apply_redirect_rule(rule, s, txn))
3827 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003828
Willy Tarreau71241ab2012-12-27 11:30:54 +01003829 req->analyse_exp = TICK_ETERNITY;
3830 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003831 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003832
Willy Tarreau2be39392010-01-03 17:24:51 +01003833 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3834 * If this happens, then the data will not come immediately, so we must
3835 * send all what we have without waiting. Note that due to the small gain
3836 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003837 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003838 * itself once used.
3839 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003840 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003841
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003842 /* that's OK for us now, let's move on to next analysers */
3843 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003844
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003845 return_bad_req:
3846 /* We centralize bad requests processing here */
3847 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3848 /* we detected a parsing error. We want to archive this request
3849 * in the dedicated proxy area for later troubleshooting.
3850 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003851 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003852 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003853
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003854 txn->req.msg_state = HTTP_MSG_ERROR;
3855 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003856 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003857
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003858 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003859 if (s->listener->counters)
3860 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003861
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003862 return_prx_cond:
3863 if (!(s->flags & SN_ERR_MASK))
3864 s->flags |= SN_ERR_PRXCOND;
3865 if (!(s->flags & SN_FINST_MASK))
3866 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003867
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003868 req->analysers = 0;
3869 req->analyse_exp = TICK_ETERNITY;
3870 return 0;
3871}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003872
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003873/* This function performs all the processing enabled for the current request.
3874 * It returns 1 if the processing can continue on next analysers, or zero if it
3875 * needs more data, encounters an error, or wants to immediately abort the
3876 * request. It relies on buffers flags, and updates s->req->analysers.
3877 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003878int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003879{
3880 struct http_txn *txn = &s->txn;
3881 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003882 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003883
Willy Tarreau655dce92009-11-08 13:10:58 +01003884 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003885 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003886 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003887 return 0;
3888 }
3889
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003890 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 +02003891 now_ms, __FUNCTION__,
3892 s,
3893 req,
3894 req->rex, req->wex,
3895 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003896 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003897 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003898
William Lallemand82fe75c2012-10-23 10:25:10 +02003899 if (s->fe->comp || s->be->comp)
3900 select_compression_request_header(s, req->buf);
3901
Willy Tarreau59234e92008-11-30 23:51:27 +01003902 /*
3903 * Right now, we know that we have processed the entire headers
3904 * and that unwanted requests have been filtered out. We can do
3905 * whatever we want with the remaining request. Also, now we
3906 * may have separate values for ->fe, ->be.
3907 */
Willy Tarreau06619262006-12-17 08:37:22 +01003908
Willy Tarreau59234e92008-11-30 23:51:27 +01003909 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003910 * If HTTP PROXY is set we simply get remote server address parsing
3911 * incoming request. Note that this requires that a connection is
3912 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003913 */
3914 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003915 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003916 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003917
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003918 /* Note that for now we don't reuse existing proxy connections */
3919 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003920 txn->req.msg_state = HTTP_MSG_ERROR;
3921 txn->status = 500;
3922 req->analysers = 0;
3923 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3924
3925 if (!(s->flags & SN_ERR_MASK))
3926 s->flags |= SN_ERR_RESOURCE;
3927 if (!(s->flags & SN_FINST_MASK))
3928 s->flags |= SN_FINST_R;
3929
3930 return 0;
3931 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003932
3933 path = http_get_path(txn);
3934 url2sa(req->buf->p + msg->sl.rq.u,
3935 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01003936 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003937 /* if the path was found, we have to remove everything between
3938 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3939 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3940 * u_l characters by a single "/".
3941 */
3942 if (path) {
3943 char *cur_ptr = req->buf->p;
3944 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3945 int delta;
3946
3947 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3948 http_msg_move_end(&txn->req, delta);
3949 cur_end += delta;
3950 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3951 goto return_bad_req;
3952 }
3953 else {
3954 char *cur_ptr = req->buf->p;
3955 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3956 int delta;
3957
3958 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3959 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3960 http_msg_move_end(&txn->req, delta);
3961 cur_end += delta;
3962 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3963 goto return_bad_req;
3964 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003965 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003966
Willy Tarreau59234e92008-11-30 23:51:27 +01003967 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003968 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003969 * Note that doing so might move headers in the request, but
3970 * the fields will stay coherent and the URI will not move.
3971 * This should only be performed in the backend.
3972 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003973 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003974 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3975 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003976
Willy Tarreau59234e92008-11-30 23:51:27 +01003977 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003978 * 8: the appsession cookie was looked up very early in 1.2,
3979 * so let's do the same now.
3980 */
3981
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003982 /* It needs to look into the URI unless persistence must be ignored */
3983 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003984 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 +01003985 }
3986
William Lallemanda73203e2012-03-12 12:48:57 +01003987 /* add unique-id if "header-unique-id" is specified */
3988
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003989 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3990 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3991 goto return_bad_req;
3992 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003993 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003994 }
William Lallemanda73203e2012-03-12 12:48:57 +01003995
3996 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003997 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3998 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003999 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004000 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01004001 goto return_bad_req;
4002 }
4003
Cyril Bontéb21570a2009-11-29 20:04:48 +01004004 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01004005 * 9: add X-Forwarded-For if either the frontend or the backend
4006 * asks for it.
4007 */
4008 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004009 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004010 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004011 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4012 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004013 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004014 /* The header is set to be added only if none is present
4015 * and we found it, so don't do anything.
4016 */
4017 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004018 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004019 /* Add an X-Forwarded-For header unless the source IP is
4020 * in the 'except' network range.
4021 */
4022 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004023 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004024 != s->fe->except_net.s_addr) &&
4025 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004026 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004027 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004028 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004029 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004030 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004031
4032 /* Note: we rely on the backend to get the header name to be used for
4033 * x-forwarded-for, because the header is really meant for the backends.
4034 * However, if the backend did not specify any option, we have to rely
4035 * on the frontend's header name.
4036 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004037 if (s->be->fwdfor_hdr_len) {
4038 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004039 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004040 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004041 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004042 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004043 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004044 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 +01004045
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004046 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004047 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004048 }
4049 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004050 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004051 /* FIXME: for the sake of completeness, we should also support
4052 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004053 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004054 int len;
4055 char pn[INET6_ADDRSTRLEN];
4056 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004057 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004058 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004059
Willy Tarreau59234e92008-11-30 23:51:27 +01004060 /* Note: we rely on the backend to get the header name to be used for
4061 * x-forwarded-for, because the header is really meant for the backends.
4062 * However, if the backend did not specify any option, we have to rely
4063 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004064 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004065 if (s->be->fwdfor_hdr_len) {
4066 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004067 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004068 } else {
4069 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004070 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004071 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004072 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004073
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004074 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004075 goto return_bad_req;
4076 }
4077 }
4078
4079 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004080 * 10: add X-Original-To if either the frontend or the backend
4081 * asks for it.
4082 */
4083 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4084
4085 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004086 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004087 /* Add an X-Original-To header unless the destination IP is
4088 * in the 'except' network range.
4089 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004090 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004091
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004092 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004093 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004094 (((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 +02004095 != s->fe->except_to.s_addr) &&
4096 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004097 (((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 +02004098 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004099 int len;
4100 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004101 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004102
4103 /* Note: we rely on the backend to get the header name to be used for
4104 * x-original-to, because the header is really meant for the backends.
4105 * However, if the backend did not specify any option, we have to rely
4106 * on the frontend's header name.
4107 */
4108 if (s->be->orgto_hdr_len) {
4109 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004110 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004111 } else {
4112 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004113 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004114 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004115 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 +02004116
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004117 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004118 goto return_bad_req;
4119 }
4120 }
4121 }
4122
Willy Tarreau50fc7772012-11-11 22:19:57 +01004123 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4124 * If an "Upgrade" token is found, the header is left untouched in order not to have
4125 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4126 * "Upgrade" is present in the Connection header.
4127 */
4128 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4129 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004130 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4131 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004132 unsigned int want_flags = 0;
4133
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004134 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004135 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004136 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4137 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004138 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004139 want_flags |= TX_CON_CLO_SET;
4140 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004141 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004142 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4143 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004144 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004145 want_flags |= TX_CON_KAL_SET;
4146 }
4147
4148 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004149 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004150 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004151
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004152
Willy Tarreau522d6c02009-12-06 18:49:18 +01004153 /* If we have no server assigned yet and we're balancing on url_param
4154 * with a POST request, we may be interested in checking the body for
4155 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004156 */
4157 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4158 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004159 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004160 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004161 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004162 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004163
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004164 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004165 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004166#ifdef TCP_QUICKACK
4167 /* We expect some data from the client. Unless we know for sure
4168 * we already have a full request, we have to re-enable quick-ack
4169 * in case we previously disabled it, otherwise we might cause
4170 * the client to delay further data.
4171 */
4172 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004173 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004174 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004175 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004176 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004177#endif
4178 }
Willy Tarreau03945942009-12-22 16:50:27 +01004179
Willy Tarreau59234e92008-11-30 23:51:27 +01004180 /*************************************************************
4181 * OK, that's finished for the headers. We have done what we *
4182 * could. Let's switch to the DATA state. *
4183 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004184 req->analyse_exp = TICK_ETERNITY;
4185 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004186
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004187 /* if the server closes the connection, we want to immediately react
4188 * and close the socket to save packets and syscalls.
4189 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004190 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4191 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004192
Willy Tarreau59234e92008-11-30 23:51:27 +01004193 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004194 /* OK let's go on with the BODY now */
4195 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004196
Willy Tarreau59234e92008-11-30 23:51:27 +01004197 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004198 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004199 /* we detected a parsing error. We want to archive this request
4200 * in the dedicated proxy area for later troubleshooting.
4201 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004202 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004203 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004204
Willy Tarreau59234e92008-11-30 23:51:27 +01004205 txn->req.msg_state = HTTP_MSG_ERROR;
4206 txn->status = 400;
4207 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004208 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004209
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004210 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004211 if (s->listener->counters)
4212 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004213
Willy Tarreau59234e92008-11-30 23:51:27 +01004214 if (!(s->flags & SN_ERR_MASK))
4215 s->flags |= SN_ERR_PRXCOND;
4216 if (!(s->flags & SN_FINST_MASK))
4217 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004218 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004219}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004220
Willy Tarreau60b85b02008-11-30 23:28:40 +01004221/* This function is an analyser which processes the HTTP tarpit. It always
4222 * returns zero, at the beginning because it prevents any other processing
4223 * from occurring, and at the end because it terminates the request.
4224 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004225int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004226{
4227 struct http_txn *txn = &s->txn;
4228
4229 /* This connection is being tarpitted. The CLIENT side has
4230 * already set the connect expiration date to the right
4231 * timeout. We just have to check that the client is still
4232 * there and that the timeout has not expired.
4233 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004234 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004235 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004236 !tick_is_expired(req->analyse_exp, now_ms))
4237 return 0;
4238
4239 /* We will set the queue timer to the time spent, just for
4240 * logging purposes. We fake a 500 server error, so that the
4241 * attacker will not suspect his connection has been tarpitted.
4242 * It will not cause trouble to the logs because we can exclude
4243 * the tarpitted connections by filtering on the 'PT' status flags.
4244 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004245 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4246
4247 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004248 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004249 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004250
4251 req->analysers = 0;
4252 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004253
Willy Tarreau60b85b02008-11-30 23:28:40 +01004254 if (!(s->flags & SN_ERR_MASK))
4255 s->flags |= SN_ERR_PRXCOND;
4256 if (!(s->flags & SN_FINST_MASK))
4257 s->flags |= SN_FINST_T;
4258 return 0;
4259}
4260
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004261/* This function is an analyser which waits for the HTTP request body. It waits
4262 * for either the buffer to be full, or the full advertised contents to have
4263 * reached the buffer. It must only be called after the standard HTTP request
4264 * processing has occurred, because it expects the request to be parsed and will
4265 * look for the Expect header. It may send a 100-Continue interim response. It
4266 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4267 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4268 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004269 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004270int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004271{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004272 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004273 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004274
4275 /* We have to parse the HTTP request body to find any required data.
4276 * "balance url_param check_post" should have been the only way to get
4277 * into this. We were brought here after HTTP header analysis, so all
4278 * related structures are ready.
4279 */
4280
Willy Tarreau890988f2014-04-10 11:59:33 +02004281 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4282 /* This is the first call */
4283 if (msg->msg_state < HTTP_MSG_BODY)
4284 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004285
Willy Tarreau890988f2014-04-10 11:59:33 +02004286 if (msg->msg_state < HTTP_MSG_100_SENT) {
4287 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4288 * send an HTTP/1.1 100 Continue intermediate response.
4289 */
4290 if (msg->flags & HTTP_MSGF_VER_11) {
4291 struct hdr_ctx ctx;
4292 ctx.idx = 0;
4293 /* Expect is allowed in 1.1, look for it */
4294 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4295 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4296 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4297 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004298 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004299 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004300 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004301
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004302 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004303 * req->buf->p still points to the beginning of the message. We
4304 * must save the body in msg->next because it survives buffer
4305 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004306 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004307 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004308
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004309 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004310 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4311 else
4312 msg->msg_state = HTTP_MSG_DATA;
4313 }
4314
Willy Tarreau890988f2014-04-10 11:59:33 +02004315 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4316 /* We're in content-length mode, we just have to wait for enough data. */
4317 if (req->buf->i - msg->sov < msg->body_len)
4318 goto missing_data;
4319
4320 /* OK we have everything we need now */
4321 goto http_end;
4322 }
4323
4324 /* OK here we're parsing a chunked-encoded message */
4325
Willy Tarreau522d6c02009-12-06 18:49:18 +01004326 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004327 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004328 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004329 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004330 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004331 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004332
Willy Tarreau115acb92009-12-26 13:56:06 +01004333 if (!ret)
4334 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004335 else if (ret < 0) {
4336 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004337 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004338 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004339 }
4340
Willy Tarreaud98cf932009-12-27 22:54:55 +01004341 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004342 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004343 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004344 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004345 if (msg->msg_state == HTTP_MSG_TRAILERS)
4346 goto http_end;
4347
Willy Tarreau226071e2014-04-10 11:55:45 +02004348 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004349 goto http_end;
4350
4351 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004352 /* we get here if we need to wait for more data. If the buffer is full,
4353 * we have the maximum we can expect.
4354 */
4355 if (buffer_full(req->buf, global.tune.maxrewrite))
4356 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004357
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004358 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004359 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004360 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004361
4362 if (!(s->flags & SN_ERR_MASK))
4363 s->flags |= SN_ERR_CLITO;
4364 if (!(s->flags & SN_FINST_MASK))
4365 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004366 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004367 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004368
4369 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004370 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004371 /* Not enough data. We'll re-use the http-request
4372 * timeout here. Ideally, we should set the timeout
4373 * relative to the accept() date. We just set the
4374 * request timeout once at the beginning of the
4375 * request.
4376 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004377 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004378 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004379 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004380 return 0;
4381 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004382
4383 http_end:
4384 /* The situation will not evolve, so let's give up on the analysis. */
4385 s->logs.tv_request = now; /* update the request timer to reflect full request */
4386 req->analysers &= ~an_bit;
4387 req->analyse_exp = TICK_ETERNITY;
4388 return 1;
4389
4390 return_bad_req: /* let's centralize all bad requests */
4391 txn->req.msg_state = HTTP_MSG_ERROR;
4392 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004393 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004394
Willy Tarreau79ebac62010-06-07 13:47:49 +02004395 if (!(s->flags & SN_ERR_MASK))
4396 s->flags |= SN_ERR_PRXCOND;
4397 if (!(s->flags & SN_FINST_MASK))
4398 s->flags |= SN_FINST_R;
4399
Willy Tarreau522d6c02009-12-06 18:49:18 +01004400 return_err_msg:
4401 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004402 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004403 if (s->listener->counters)
4404 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004405 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004406}
4407
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004408/* send a server's name with an outgoing request over an established connection.
4409 * Note: this function is designed to be called once the request has been scheduled
4410 * for being forwarded. This is the reason why it rewinds the buffer before
4411 * proceeding.
4412 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004413int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004414
4415 struct hdr_ctx ctx;
4416
Mark Lamourinec2247f02012-01-04 13:02:01 -05004417 char *hdr_name = be->server_id_hdr_name;
4418 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004419 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004420 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004421 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004422
William Lallemandd9e90662012-01-30 17:27:17 +01004423 ctx.idx = 0;
4424
Willy Tarreau211cdec2014-04-17 20:18:08 +02004425 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004426 if (old_o) {
4427 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004428 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004429 txn->req.next += old_o;
4430 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004431 }
4432
Willy Tarreau9b28e032012-10-12 23:49:43 +02004433 old_i = chn->buf->i;
4434 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 -05004435 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004436 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004437 }
4438
4439 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004440 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004441 memcpy(hdr_val, hdr_name, hdr_name_len);
4442 hdr_val += hdr_name_len;
4443 *hdr_val++ = ':';
4444 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004445 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4446 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004447
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004448 if (old_o) {
4449 /* If this was a forwarded request, we must readjust the amount of
4450 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004451 * variations. Note that the current state is >= HTTP_MSG_BODY,
4452 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004453 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004454 old_o += chn->buf->i - old_i;
4455 b_adv(chn->buf, old_o);
4456 txn->req.next -= old_o;
4457 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004458 }
4459
Mark Lamourinec2247f02012-01-04 13:02:01 -05004460 return 0;
4461}
4462
Willy Tarreau610ecce2010-01-04 21:15:02 +01004463/* Terminate current transaction and prepare a new one. This is very tricky
4464 * right now but it works.
4465 */
4466void http_end_txn_clean_session(struct session *s)
4467{
Willy Tarreau068621e2013-12-23 15:11:25 +01004468 int prev_status = s->txn.status;
4469
Willy Tarreau610ecce2010-01-04 21:15:02 +01004470 /* FIXME: We need a more portable way of releasing a backend's and a
4471 * server's connections. We need a safer way to reinitialize buffer
4472 * flags. We also need a more accurate method for computing per-request
4473 * data.
4474 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004475
Willy Tarreau4213a112013-12-15 10:25:42 +01004476 /* unless we're doing keep-alive, we want to quickly close the connection
4477 * to the server.
4478 */
4479 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4480 !si_conn_ready(s->req->cons)) {
4481 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4482 si_shutr(s->req->cons);
4483 si_shutw(s->req->cons);
4484 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004485
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004486 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004487 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004488 if (unlikely(s->srv_conn))
4489 sess_change_server(s, NULL);
4490 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004491
4492 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4493 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004494 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004495
4496 if (s->txn.status) {
4497 int n;
4498
4499 n = s->txn.status / 100;
4500 if (n < 1 || n > 5)
4501 n = 0;
4502
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004503 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004504 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004505 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004506 s->fe->fe_counters.p.http.comp_rsp++;
4507 }
Willy Tarreau24657792010-02-26 10:30:28 +01004508 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004509 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004510 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004511 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004512 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004513 s->be->be_counters.p.http.comp_rsp++;
4514 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004515 }
4516
4517 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004518 s->logs.bytes_in -= s->req->buf->i;
4519 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004520
4521 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004522 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004523 !(s->flags & SN_MONITOR) &&
4524 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4525 s->do_log(s);
4526 }
4527
4528 s->logs.accept_date = date; /* user-visible date for logging */
4529 s->logs.tv_accept = now; /* corrected date for internal use */
4530 tv_zero(&s->logs.tv_request);
4531 s->logs.t_queue = -1;
4532 s->logs.t_connect = -1;
4533 s->logs.t_data = -1;
4534 s->logs.t_close = 0;
4535 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4536 s->logs.srv_queue_size = 0; /* we will get this number soon */
4537
Willy Tarreau9b28e032012-10-12 23:49:43 +02004538 s->logs.bytes_in = s->req->total = s->req->buf->i;
4539 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004540
4541 if (s->pend_pos)
4542 pendconn_free(s->pend_pos);
4543
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004544 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004545 if (s->flags & SN_CURR_SESS) {
4546 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004547 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004548 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004549 if (may_dequeue_tasks(objt_server(s->target), s->be))
4550 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004551 }
4552
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004553 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004554
Willy Tarreau4213a112013-12-15 10:25:42 +01004555 /* only release our endpoint if we don't intend to reuse the
4556 * connection.
4557 */
4558 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4559 !si_conn_ready(s->req->cons)) {
4560 si_release_endpoint(s->req->cons);
4561 }
4562
Willy Tarreau610ecce2010-01-04 21:15:02 +01004563 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004564 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004565 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004566 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004567 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004568 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4569 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 +02004570 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 +01004571 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004572
Willy Tarreau610ecce2010-01-04 21:15:02 +01004573 s->txn.meth = 0;
4574 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004575 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004576
4577 if (prev_status == 401 || prev_status == 407) {
4578 /* In HTTP keep-alive mode, if we receive a 401, we still have
4579 * a chance of being able to send the visitor again to the same
4580 * server over the same connection. This is required by some
4581 * broken protocols such as NTLM, and anyway whenever there is
4582 * an opportunity for sending the challenge to the proper place,
4583 * it's better to do it (at least it helps with debugging).
4584 */
4585 s->txn.flags |= TX_PREFER_LAST;
4586 }
4587
Willy Tarreauee55dc02010-06-01 10:56:34 +02004588 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004589 s->req->cons->flags |= SI_FL_INDEP_STR;
4590
Willy Tarreau96e31212011-05-30 18:10:30 +02004591 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004592 s->req->flags |= CF_NEVER_WAIT;
4593 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004594 }
4595
Willy Tarreau610ecce2010-01-04 21:15:02 +01004596 /* if the request buffer is not empty, it means we're
4597 * about to process another request, so send pending
4598 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004599 * Just don't do this if the buffer is close to be full,
4600 * because the request will wait for it to flush a little
4601 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004602 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004603 if (s->req->buf->i) {
4604 if (s->rep->buf->o &&
4605 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4606 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004607 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004608 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004609
4610 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004611 channel_auto_read(s->req);
4612 channel_auto_close(s->req);
4613 channel_auto_read(s->rep);
4614 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004615
Willy Tarreau27375622013-12-17 00:00:28 +01004616 /* we're in keep-alive with an idle connection, monitor it */
4617 si_idle_conn(s->req->cons);
4618
Willy Tarreau342b11c2010-11-24 16:22:09 +01004619 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004620 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004621}
4622
4623
4624/* This function updates the request state machine according to the response
4625 * state machine and buffer flags. It returns 1 if it changes anything (flag
4626 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4627 * it is only used to find when a request/response couple is complete. Both
4628 * this function and its equivalent should loop until both return zero. It
4629 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4630 */
4631int http_sync_req_state(struct session *s)
4632{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004633 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004634 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004635 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004636 unsigned int old_state = txn->req.msg_state;
4637
Willy Tarreau610ecce2010-01-04 21:15:02 +01004638 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4639 return 0;
4640
4641 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004642 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004643 * We can shut the read side unless we want to abort_on_close,
4644 * or we have a POST request. The issue with POST requests is
4645 * that some browsers still send a CRLF after the request, and
4646 * this CRLF must be read so that it does not remain in the kernel
4647 * buffers, otherwise a close could cause an RST on some systems
4648 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004649 * Note that if we're using keep-alive on the client side, we'd
4650 * rather poll now and keep the polling enabled for the whole
4651 * session's life than enabling/disabling it between each
4652 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004653 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004654 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4655 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4656 !(s->be->options & PR_O_ABRT_CLOSE) &&
4657 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004658 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004659
Willy Tarreau40f151a2012-12-20 12:10:09 +01004660 /* if the server closes the connection, we want to immediately react
4661 * and close the socket to save packets and syscalls.
4662 */
4663 chn->cons->flags |= SI_FL_NOHALF;
4664
Willy Tarreau610ecce2010-01-04 21:15:02 +01004665 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4666 goto wait_other_side;
4667
4668 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4669 /* The server has not finished to respond, so we
4670 * don't want to move in order not to upset it.
4671 */
4672 goto wait_other_side;
4673 }
4674
4675 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4676 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004677 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004678 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004679 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004680 goto wait_other_side;
4681 }
4682
4683 /* When we get here, it means that both the request and the
4684 * response have finished receiving. Depending on the connection
4685 * mode, we'll have to wait for the last bytes to leave in either
4686 * direction, and sometimes for a close to be effective.
4687 */
4688
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004689 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4690 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004691 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4692 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004693 }
4694 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4695 /* Option forceclose is set, or either side wants to close,
4696 * let's enforce it now that we're not expecting any new
4697 * data to come. The caller knows the session is complete
4698 * once both states are CLOSED.
4699 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004700 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4701 channel_shutr_now(chn);
4702 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004703 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004704 }
4705 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004706 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4707 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004708 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004709 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4710 channel_auto_read(chn);
4711 txn->req.msg_state = HTTP_MSG_TUNNEL;
4712 chn->flags |= CF_NEVER_WAIT;
4713 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004714 }
4715
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004716 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004717 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004718 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004719
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004720 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004721 txn->req.msg_state = HTTP_MSG_CLOSING;
4722 goto http_msg_closing;
4723 }
4724 else {
4725 txn->req.msg_state = HTTP_MSG_CLOSED;
4726 goto http_msg_closed;
4727 }
4728 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004729 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004730 }
4731
4732 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4733 http_msg_closing:
4734 /* nothing else to forward, just waiting for the output buffer
4735 * to be empty and for the shutw_now to take effect.
4736 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004737 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004738 txn->req.msg_state = HTTP_MSG_CLOSED;
4739 goto http_msg_closed;
4740 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004741 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004742 txn->req.msg_state = HTTP_MSG_ERROR;
4743 goto wait_other_side;
4744 }
4745 }
4746
4747 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4748 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004749 /* see above in MSG_DONE why we only do this in these states */
4750 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4751 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4752 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004753 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004754 goto wait_other_side;
4755 }
4756
4757 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004758 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004759}
4760
4761
4762/* This function updates the response state machine according to the request
4763 * state machine and buffer flags. It returns 1 if it changes anything (flag
4764 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4765 * it is only used to find when a request/response couple is complete. Both
4766 * this function and its equivalent should loop until both return zero. It
4767 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4768 */
4769int http_sync_res_state(struct session *s)
4770{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004771 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004772 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004773 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004774 unsigned int old_state = txn->rsp.msg_state;
4775
Willy Tarreau610ecce2010-01-04 21:15:02 +01004776 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4777 return 0;
4778
4779 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4780 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004781 * still monitor the server connection for a possible close
4782 * while the request is being uploaded, so we don't disable
4783 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004784 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004785 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004786
4787 if (txn->req.msg_state == HTTP_MSG_ERROR)
4788 goto wait_other_side;
4789
4790 if (txn->req.msg_state < HTTP_MSG_DONE) {
4791 /* The client seems to still be sending data, probably
4792 * because we got an error response during an upload.
4793 * We have the choice of either breaking the connection
4794 * or letting it pass through. Let's do the later.
4795 */
4796 goto wait_other_side;
4797 }
4798
4799 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4800 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004801 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004802 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004803 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004804 goto wait_other_side;
4805 }
4806
4807 /* When we get here, it means that both the request and the
4808 * response have finished receiving. Depending on the connection
4809 * mode, we'll have to wait for the last bytes to leave in either
4810 * direction, and sometimes for a close to be effective.
4811 */
4812
4813 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4814 /* Server-close mode : shut read and wait for the request
4815 * side to close its output buffer. The caller will detect
4816 * when we're in DONE and the other is in CLOSED and will
4817 * catch that for the final cleanup.
4818 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004819 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4820 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004821 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004822 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4823 /* Option forceclose is set, or either side wants to close,
4824 * let's enforce it now that we're not expecting any new
4825 * data to come. The caller knows the session is complete
4826 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004827 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004828 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4829 channel_shutr_now(chn);
4830 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004831 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004832 }
4833 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004834 /* The last possible modes are keep-alive and tunnel. Tunnel will
4835 * need to forward remaining data. Keep-alive will need to monitor
4836 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004837 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004838 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004839 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004840 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4841 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004842 }
4843
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004844 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004845 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004846 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004847 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4848 goto http_msg_closing;
4849 }
4850 else {
4851 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4852 goto http_msg_closed;
4853 }
4854 }
4855 goto wait_other_side;
4856 }
4857
4858 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4859 http_msg_closing:
4860 /* nothing else to forward, just waiting for the output buffer
4861 * to be empty and for the shutw_now to take effect.
4862 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004863 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004864 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4865 goto http_msg_closed;
4866 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004867 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004868 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004869 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004870 if (objt_server(s->target))
4871 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004872 goto wait_other_side;
4873 }
4874 }
4875
4876 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4877 http_msg_closed:
4878 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004879 bi_erase(chn);
4880 channel_auto_close(chn);
4881 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004882 goto wait_other_side;
4883 }
4884
4885 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004886 /* We force the response to leave immediately if we're waiting for the
4887 * other side, since there is no pending shutdown to push it out.
4888 */
4889 if (!channel_is_empty(chn))
4890 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004891 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004892}
4893
4894
4895/* Resync the request and response state machines. Return 1 if either state
4896 * changes.
4897 */
4898int http_resync_states(struct session *s)
4899{
4900 struct http_txn *txn = &s->txn;
4901 int old_req_state = txn->req.msg_state;
4902 int old_res_state = txn->rsp.msg_state;
4903
Willy Tarreau610ecce2010-01-04 21:15:02 +01004904 http_sync_req_state(s);
4905 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004906 if (!http_sync_res_state(s))
4907 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004908 if (!http_sync_req_state(s))
4909 break;
4910 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004911
Willy Tarreau610ecce2010-01-04 21:15:02 +01004912 /* OK, both state machines agree on a compatible state.
4913 * There are a few cases we're interested in :
4914 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4915 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4916 * directions, so let's simply disable both analysers.
4917 * - HTTP_MSG_CLOSED on the response only means we must abort the
4918 * request.
4919 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4920 * with server-close mode means we've completed one request and we
4921 * must re-initialize the server connection.
4922 */
4923
4924 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4925 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4926 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4927 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4928 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004929 channel_auto_close(s->req);
4930 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004931 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004932 channel_auto_close(s->rep);
4933 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004934 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004935 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4936 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004937 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004938 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004939 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004940 channel_auto_close(s->rep);
4941 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004942 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004943 channel_abort(s->req);
4944 channel_auto_close(s->req);
4945 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004946 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004947 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004948 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4949 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004950 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004951 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4952 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4953 /* server-close/keep-alive: terminate this transaction,
4954 * possibly killing the server connection and reinitialize
4955 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004956 */
4957 http_end_txn_clean_session(s);
4958 }
4959
Willy Tarreau610ecce2010-01-04 21:15:02 +01004960 return txn->req.msg_state != old_req_state ||
4961 txn->rsp.msg_state != old_res_state;
4962}
4963
Willy Tarreaud98cf932009-12-27 22:54:55 +01004964/* This function is an analyser which forwards request body (including chunk
4965 * sizes if any). It is called as soon as we must forward, even if we forward
4966 * zero byte. The only situation where it must not be called is when we're in
4967 * tunnel mode and we want to forward till the close. It's used both to forward
4968 * remaining data and to resync after end of body. It expects the msg_state to
4969 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4970 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004971 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004972 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004973 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004974int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004975{
4976 struct http_txn *txn = &s->txn;
4977 struct http_msg *msg = &s->txn.req;
4978
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004979 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4980 return 0;
4981
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004982 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004983 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004984 /* Output closed while we were sending data. We must abort and
4985 * wake the other side up.
4986 */
4987 msg->msg_state = HTTP_MSG_ERROR;
4988 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004989 return 1;
4990 }
4991
Willy Tarreaud98cf932009-12-27 22:54:55 +01004992 /* Note that we don't have to send 100-continue back because we don't
4993 * need the data to complete our job, and it's up to the server to
4994 * decide whether to return 100, 417 or anything else in return of
4995 * an "Expect: 100-continue" header.
4996 */
4997
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02004998 if (msg->sov) {
4999 /* we have msg->sov which points to the first byte of message
5000 * body, and req->buf.p still points to the beginning of the
5001 * message. We forward the headers now, as we don't need them
5002 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005003 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005004 b_adv(req->buf, msg->sov);
5005 msg->next -= msg->sov;
5006 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005007
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02005008 /* The previous analysers guarantee that the state is somewhere
5009 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5010 * msg->next are always correct.
5011 */
5012 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5013 if (msg->flags & HTTP_MSGF_TE_CHNK)
5014 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5015 else
5016 msg->msg_state = HTTP_MSG_DATA;
5017 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005018 }
5019
Willy Tarreau7ba23542014-04-17 21:50:00 +02005020 /* Some post-connect processing might want us to refrain from starting to
5021 * forward data. Currently, the only reason for this is "balance url_param"
5022 * whichs need to parse/process the request after we've enabled forwarding.
5023 */
5024 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5025 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5026 channel_auto_connect(req);
5027 goto missing_data;
5028 }
5029 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5030 }
5031
Willy Tarreau80a92c02014-03-12 10:41:13 +01005032 /* in most states, we should abort in case of early close */
5033 channel_auto_close(req);
5034
Willy Tarreauefdf0942014-04-24 20:08:57 +02005035 if (req->to_forward) {
5036 /* We can't process the buffer's contents yet */
5037 req->flags |= CF_WAKE_WRITE;
5038 goto missing_data;
5039 }
5040
Willy Tarreaud98cf932009-12-27 22:54:55 +01005041 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005042 if (msg->msg_state == HTTP_MSG_DATA) {
5043 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005044 /* we may have some pending data starting at req->buf->p */
5045 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005046 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005047 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005048 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005049 msg->next += msg->chunk_len;
5050 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005051
5052 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005053 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005054 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005055 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005056 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005057 }
5058 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005059 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005060 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005061 * TRAILERS state.
5062 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005063 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005064
Willy Tarreau54d23df2012-10-25 19:04:45 +02005065 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005066 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005067 else if (ret < 0) {
5068 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005069 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005070 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005071 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005072 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005073 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005074 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005075 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005076 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005077 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005078
5079 if (ret == 0)
5080 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005081 else if (ret < 0) {
5082 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005083 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005084 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005085 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005086 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005087 /* we're in MSG_CHUNK_SIZE now */
5088 }
5089 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005090 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005091
5092 if (ret == 0)
5093 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005094 else if (ret < 0) {
5095 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005096 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005097 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005098 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005099 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005100 /* we're in HTTP_MSG_DONE now */
5101 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005102 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005103 int old_state = msg->msg_state;
5104
Willy Tarreau610ecce2010-01-04 21:15:02 +01005105 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005106
5107 /* we may have some pending data starting at req->buf->p
5108 * such as last chunk of data or trailers.
5109 */
5110 b_adv(req->buf, msg->next);
5111 msg->next = 0;
5112
Willy Tarreau4fe41902010-06-07 22:27:41 +02005113 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005114 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5115 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005116 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005117 if (http_resync_states(s)) {
5118 /* some state changes occurred, maybe the analyser
5119 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005120 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005121 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005122 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005123 /* request errors are most likely due to
5124 * the server aborting the transfer.
5125 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005126 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005127 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005128 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005129 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005130 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005131 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005132 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005133 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005134
5135 /* If "option abortonclose" is set on the backend, we
5136 * want to monitor the client's connection and forward
5137 * any shutdown notification to the server, which will
5138 * decide whether to close or to go on processing the
5139 * request.
5140 */
5141 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005142 channel_auto_read(req);
5143 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005144 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005145 else if (s->txn.meth == HTTP_METH_POST) {
5146 /* POST requests may require to read extra CRLF
5147 * sent by broken browsers and which could cause
5148 * an RST to be sent upon close on some systems
5149 * (eg: Linux).
5150 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005151 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005152 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005153
Willy Tarreau610ecce2010-01-04 21:15:02 +01005154 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005155 }
5156 }
5157
Willy Tarreaud98cf932009-12-27 22:54:55 +01005158 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005159 /* we may have some pending data starting at req->buf->p */
5160 b_adv(req->buf, msg->next);
5161 msg->next = 0;
5162 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5163
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005164 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005165 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005166 if (!(s->flags & SN_ERR_MASK))
5167 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005168 if (!(s->flags & SN_FINST_MASK)) {
5169 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5170 s->flags |= SN_FINST_H;
5171 else
5172 s->flags |= SN_FINST_D;
5173 }
5174
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005175 s->fe->fe_counters.cli_aborts++;
5176 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005177 if (objt_server(s->target))
5178 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005179
5180 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005181 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005182
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005183 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005184 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005185 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005186
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005187 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005188 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005189 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005190 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005191 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005192
Willy Tarreau5c620922011-05-11 19:56:11 +02005193 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005194 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005195 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005196 * modes are already handled by the stream sock layer. We must not do
5197 * this in content-length mode because it could present the MSG_MORE
5198 * flag with the last block of forwarded data, which would cause an
5199 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005200 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005201 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005202 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005203
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005204 return 0;
5205
Willy Tarreaud98cf932009-12-27 22:54:55 +01005206 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005207 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005208 if (s->listener->counters)
5209 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005210
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005211 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005212 /* we may have some pending data starting at req->buf->p */
5213 b_adv(req->buf, msg->next);
5214 msg->next = 0;
5215
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005216 txn->req.msg_state = HTTP_MSG_ERROR;
5217 if (txn->status) {
5218 /* Note: we don't send any error if some data were already sent */
5219 stream_int_retnclose(req->prod, NULL);
5220 } else {
5221 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005222 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005223 }
5224 req->analysers = 0;
5225 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005226
5227 if (!(s->flags & SN_ERR_MASK))
5228 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005229 if (!(s->flags & SN_FINST_MASK)) {
5230 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5231 s->flags |= SN_FINST_H;
5232 else
5233 s->flags |= SN_FINST_D;
5234 }
5235 return 0;
5236
5237 aborted_xfer:
5238 txn->req.msg_state = HTTP_MSG_ERROR;
5239 if (txn->status) {
5240 /* Note: we don't send any error if some data were already sent */
5241 stream_int_retnclose(req->prod, NULL);
5242 } else {
5243 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005244 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005245 }
5246 req->analysers = 0;
5247 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5248
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005249 s->fe->fe_counters.srv_aborts++;
5250 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005251 if (objt_server(s->target))
5252 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005253
5254 if (!(s->flags & SN_ERR_MASK))
5255 s->flags |= SN_ERR_SRVCL;
5256 if (!(s->flags & SN_FINST_MASK)) {
5257 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5258 s->flags |= SN_FINST_H;
5259 else
5260 s->flags |= SN_FINST_D;
5261 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005262 return 0;
5263}
5264
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005265/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5266 * processing can continue on next analysers, or zero if it either needs more
5267 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5268 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5269 * when it has nothing left to do, and may remove any analyser when it wants to
5270 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005271 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005272int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005273{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005274 struct http_txn *txn = &s->txn;
5275 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005276 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005277 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005278 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005279 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005280
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005281 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 +02005282 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005283 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005284 rep,
5285 rep->rex, rep->wex,
5286 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005287 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005288 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005289
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005290 /*
5291 * Now parse the partial (or complete) lines.
5292 * We will check the response syntax, and also join multi-line
5293 * headers. An index of all the lines will be elaborated while
5294 * parsing.
5295 *
5296 * For the parsing, we use a 28 states FSM.
5297 *
5298 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005299 * rep->buf->p = beginning of response
5300 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5301 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005302 * msg->eol = end of current header or line (LF or CRLF)
5303 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005304 */
5305
Willy Tarreau628c40c2014-04-24 19:11:26 +02005306 next_one:
Willy Tarreau83e3af02009-12-28 17:39:57 +01005307 /* There's a protected area at the end of the buffer for rewriting
5308 * purposes. We don't want to start to parse the request if the
5309 * protected area is affected, because we may have to move processed
5310 * data later, which is much more complicated.
5311 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005312 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005313 if (unlikely(!channel_reserved(rep))) {
5314 /* some data has still not left the buffer, wake us once that's done */
5315 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5316 goto abort_response;
5317 channel_dont_close(rep);
5318 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005319 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005320 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005321 }
5322
Willy Tarreau379357a2013-06-08 12:55:46 +02005323 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5324 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5325 buffer_slow_realign(rep->buf);
5326
Willy Tarreau9b28e032012-10-12 23:49:43 +02005327 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005328 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005329 }
5330
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005331 /* 1: we might have to print this header in debug mode */
5332 if (unlikely((global.mode & MODE_DEBUG) &&
5333 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005334 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005335 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005336
Willy Tarreau9b28e032012-10-12 23:49:43 +02005337 sol = rep->buf->p;
5338 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005339 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005340
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005341 sol += hdr_idx_first_pos(&txn->hdr_idx);
5342 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005343
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005344 while (cur_idx) {
5345 eol = sol + txn->hdr_idx.v[cur_idx].len;
5346 debug_hdr("srvhdr", s, sol, eol);
5347 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5348 cur_idx = txn->hdr_idx.v[cur_idx].next;
5349 }
5350 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005351
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005352 /*
5353 * Now we quickly check if we have found a full valid response.
5354 * If not so, we check the FD and buffer states before leaving.
5355 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005356 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005357 * responses are checked first.
5358 *
5359 * Depending on whether the client is still there or not, we
5360 * may send an error response back or not. Note that normally
5361 * we should only check for HTTP status there, and check I/O
5362 * errors somewhere else.
5363 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005364
Willy Tarreau655dce92009-11-08 13:10:58 +01005365 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005366 /* Invalid response */
5367 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5368 /* we detected a parsing error. We want to archive this response
5369 * in the dedicated proxy area for later troubleshooting.
5370 */
5371 hdr_response_bad:
5372 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005373 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005374
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005375 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005376 if (objt_server(s->target)) {
5377 objt_server(s->target)->counters.failed_resp++;
5378 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005379 }
Willy Tarreau64648412010-03-05 10:41:54 +01005380 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005381 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005382 rep->analysers = 0;
5383 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005384 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005385 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005386 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005387
5388 if (!(s->flags & SN_ERR_MASK))
5389 s->flags |= SN_ERR_PRXCOND;
5390 if (!(s->flags & SN_FINST_MASK))
5391 s->flags |= SN_FINST_H;
5392
5393 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005394 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005395
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005396 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005397 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005398 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005399 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005400 goto hdr_response_bad;
5401 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005402
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005403 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005404 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005405 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005406 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005407 else if (txn->flags & TX_NOT_FIRST)
5408 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005409
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005410 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005411 if (objt_server(s->target)) {
5412 objt_server(s->target)->counters.failed_resp++;
5413 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005414 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005415
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005416 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005417 rep->analysers = 0;
5418 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005419 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005420 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005421 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005422
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005423 if (!(s->flags & SN_ERR_MASK))
5424 s->flags |= SN_ERR_SRVCL;
5425 if (!(s->flags & SN_FINST_MASK))
5426 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005427 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005428 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005429
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005430 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005431 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005432 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005433 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005434 else if (txn->flags & TX_NOT_FIRST)
5435 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005436
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005437 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005438 if (objt_server(s->target)) {
5439 objt_server(s->target)->counters.failed_resp++;
5440 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005441 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005442
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005443 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005444 rep->analysers = 0;
5445 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005446 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005447 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005448 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005449
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005450 if (!(s->flags & SN_ERR_MASK))
5451 s->flags |= SN_ERR_SRVTO;
5452 if (!(s->flags & SN_FINST_MASK))
5453 s->flags |= SN_FINST_H;
5454 return 0;
5455 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005456
Willy Tarreauf003d372012-11-26 13:35:37 +01005457 /* client abort with an abortonclose */
5458 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5459 s->fe->fe_counters.cli_aborts++;
5460 s->be->be_counters.cli_aborts++;
5461 if (objt_server(s->target))
5462 objt_server(s->target)->counters.cli_aborts++;
5463
5464 rep->analysers = 0;
5465 channel_auto_close(rep);
5466
5467 txn->status = 400;
5468 bi_erase(rep);
5469 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5470
5471 if (!(s->flags & SN_ERR_MASK))
5472 s->flags |= SN_ERR_CLICL;
5473 if (!(s->flags & SN_FINST_MASK))
5474 s->flags |= SN_FINST_H;
5475
5476 /* process_session() will take care of the error */
5477 return 0;
5478 }
5479
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005480 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005481 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005482 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005483 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005484 else if (txn->flags & TX_NOT_FIRST)
5485 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005486
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005487 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005488 if (objt_server(s->target)) {
5489 objt_server(s->target)->counters.failed_resp++;
5490 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005491 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005492
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005493 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005494 rep->analysers = 0;
5495 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005496 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005497 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005498 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005499
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005500 if (!(s->flags & SN_ERR_MASK))
5501 s->flags |= SN_ERR_SRVCL;
5502 if (!(s->flags & SN_FINST_MASK))
5503 s->flags |= SN_FINST_H;
5504 return 0;
5505 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005506
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005507 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005508 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005509 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005510 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005511 else if (txn->flags & TX_NOT_FIRST)
5512 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005513
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005514 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005515 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005516 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005517
5518 if (!(s->flags & SN_ERR_MASK))
5519 s->flags |= SN_ERR_CLICL;
5520 if (!(s->flags & SN_FINST_MASK))
5521 s->flags |= SN_FINST_H;
5522
5523 /* process_session() will take care of the error */
5524 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005525 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005526
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005527 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005528 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005529 return 0;
5530 }
5531
5532 /* More interesting part now : we know that we have a complete
5533 * response which at least looks like HTTP. We have an indicator
5534 * of each header's length, so we can parse them quickly.
5535 */
5536
5537 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005538 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005539
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005540 /*
5541 * 1: get the status code
5542 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005543 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005544 if (n < 1 || n > 5)
5545 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005546 /* when the client triggers a 4xx from the server, it's most often due
5547 * to a missing object or permission. These events should be tracked
5548 * because if they happen often, it may indicate a brute force or a
5549 * vulnerability scan.
5550 */
5551 if (n == 4)
5552 session_inc_http_err_ctr(s);
5553
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005554 if (objt_server(s->target))
5555 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005556
Willy Tarreau5b154472009-12-21 20:11:07 +01005557 /* check if the response is HTTP/1.1 or above */
5558 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005559 ((rep->buf->p[5] > '1') ||
5560 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005561 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005562
5563 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005564 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 +01005565
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005566 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005567 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005568
Willy Tarreau9b28e032012-10-12 23:49:43 +02005569 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005570
Willy Tarreau39650402010-03-15 19:44:39 +01005571 /* Adjust server's health based on status code. Note: status codes 501
5572 * and 505 are triggered on demand by client request, so we must not
5573 * count them as server failures.
5574 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005575 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005576 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005577 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005578 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005579 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005580 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005581
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005582 /*
5583 * 2: check for cacheability.
5584 */
5585
5586 switch (txn->status) {
Willy Tarreau628c40c2014-04-24 19:11:26 +02005587 case 100:
5588 /*
5589 * We may be facing a 100-continue response, in which case this
5590 * is not the right response, and we're waiting for the next one.
5591 * Let's allow this response to go to the client and wait for the
5592 * next one.
5593 */
5594 hdr_idx_init(&txn->hdr_idx);
5595 msg->next -= channel_forward(rep, msg->next);
5596 msg->msg_state = HTTP_MSG_RPBEFORE;
5597 txn->status = 0;
5598 s->logs.t_data = -1; /* was not a response yet */
5599 goto next_one;
5600
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005601 case 200:
5602 case 203:
5603 case 206:
5604 case 300:
5605 case 301:
5606 case 410:
5607 /* RFC2616 @13.4:
5608 * "A response received with a status code of
5609 * 200, 203, 206, 300, 301 or 410 MAY be stored
5610 * by a cache (...) unless a cache-control
5611 * directive prohibits caching."
5612 *
5613 * RFC2616 @9.5: POST method :
5614 * "Responses to this method are not cacheable,
5615 * unless the response includes appropriate
5616 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005617 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005618 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005619 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005620 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5621 break;
5622 default:
5623 break;
5624 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005625
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005626 /*
5627 * 3: we may need to capture headers
5628 */
5629 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005630 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005631 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005632 txn->rsp.cap, s->fe->rsp_cap);
5633
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005634 /* 4: determine the transfer-length.
5635 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5636 * the presence of a message-body in a RESPONSE and its transfer length
5637 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005638 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005639 * All responses to the HEAD request method MUST NOT include a
5640 * message-body, even though the presence of entity-header fields
5641 * might lead one to believe they do. All 1xx (informational), 204
5642 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5643 * message-body. All other responses do include a message-body,
5644 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005645 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005646 * 1. Any response which "MUST NOT" include a message-body (such as the
5647 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5648 * always terminated by the first empty line after the header fields,
5649 * regardless of the entity-header fields present in the message.
5650 *
5651 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5652 * the "chunked" transfer-coding (Section 6.2) is used, the
5653 * transfer-length is defined by the use of this transfer-coding.
5654 * If a Transfer-Encoding header field is present and the "chunked"
5655 * transfer-coding is not present, the transfer-length is defined by
5656 * the sender closing the connection.
5657 *
5658 * 3. If a Content-Length header field is present, its decimal value in
5659 * OCTETs represents both the entity-length and the transfer-length.
5660 * If a message is received with both a Transfer-Encoding header
5661 * field and a Content-Length header field, the latter MUST be ignored.
5662 *
5663 * 4. If the message uses the media type "multipart/byteranges", and
5664 * the transfer-length is not otherwise specified, then this self-
5665 * delimiting media type defines the transfer-length. This media
5666 * type MUST NOT be used unless the sender knows that the recipient
5667 * can parse it; the presence in a request of a Range header with
5668 * multiple byte-range specifiers from a 1.1 client implies that the
5669 * client can parse multipart/byteranges responses.
5670 *
5671 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005672 */
5673
5674 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005675 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005676 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005677 * FIXME: should we parse anyway and return an error on chunked encoding ?
5678 */
5679 if (txn->meth == HTTP_METH_HEAD ||
5680 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005681 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005682 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005683 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005684 goto skip_content_length;
5685 }
5686
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005687 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005688 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005689 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005690 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005691 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005692 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5693 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005694 /* bad transfer-encoding (chunked followed by something else) */
5695 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005696 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005697 break;
5698 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005699 }
5700
5701 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5702 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005703 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005704 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005705 signed long long cl;
5706
Willy Tarreauad14f752011-09-02 20:33:27 +02005707 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005708 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005709 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005710 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005711
Willy Tarreauad14f752011-09-02 20:33:27 +02005712 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005713 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005714 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005715 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005716
Willy Tarreauad14f752011-09-02 20:33:27 +02005717 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005718 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005719 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005720 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005721
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005722 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005723 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005724 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005725 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005726
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005727 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005728 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005729 }
5730
William Lallemand82fe75c2012-10-23 10:25:10 +02005731 if (s->fe->comp || s->be->comp)
5732 select_compression_response_header(s, rep->buf);
5733
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005734skip_content_length:
Willy Tarreau5b154472009-12-21 20:11:07 +01005735 /* Now we have to check if we need to modify the Connection header.
5736 * This is more difficult on the response than it is on the request,
5737 * because we can have two different HTTP versions and we don't know
5738 * how the client will interprete a response. For instance, let's say
5739 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5740 * HTTP/1.1 response without any header. Maybe it will bound itself to
5741 * HTTP/1.0 because it only knows about it, and will consider the lack
5742 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5743 * the lack of header as a keep-alive. Thus we will use two flags
5744 * indicating how a request MAY be understood by the client. In case
5745 * of multiple possibilities, we'll fix the header to be explicit. If
5746 * ambiguous cases such as both close and keepalive are seen, then we
5747 * will fall back to explicit close. Note that we won't take risks with
5748 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005749 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005750 */
5751
Willy Tarreaudc008c52010-02-01 16:20:08 +01005752 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5753 txn->status == 101)) {
5754 /* Either we've established an explicit tunnel, or we're
5755 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005756 * to understand the next protocols. We have to switch to tunnel
5757 * mode, so that we transfer the request and responses then let
5758 * this protocol pass unmodified. When we later implement specific
5759 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005760 * header which contains information about that protocol for
5761 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005762 */
5763 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5764 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005765 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5766 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005767 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5768 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005769 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005770
Willy Tarreau70dffda2014-01-30 03:07:23 +01005771 /* this situation happens when combining pretend-keepalive with httpclose. */
5772 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005773 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5774 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
Willy Tarreau70dffda2014-01-30 03:07:23 +01005775 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5776
Willy Tarreau60466522010-01-18 19:08:45 +01005777 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005778 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005779 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5780 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005781
Willy Tarreau60466522010-01-18 19:08:45 +01005782 /* now adjust header transformations depending on current state */
5783 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5784 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5785 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005786 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005787 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005788 }
Willy Tarreau60466522010-01-18 19:08:45 +01005789 else { /* SCL / KAL */
5790 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005791 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005792 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005793 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005794
Willy Tarreau60466522010-01-18 19:08:45 +01005795 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005796 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005797
Willy Tarreau60466522010-01-18 19:08:45 +01005798 /* Some keep-alive responses are converted to Server-close if
5799 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005800 */
Willy Tarreau60466522010-01-18 19:08:45 +01005801 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5802 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005803 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005804 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005805 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005806 }
5807
Willy Tarreau7959a552013-09-23 16:44:27 +02005808 /* we want to have the response time before we start processing it */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02005809 s->logs.t_data = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau7959a552013-09-23 16:44:27 +02005810
Willy Tarreauf118d9f2014-04-24 18:26:08 +02005811 /* end of job, return OK */
5812 rep->analysers &= ~an_bit;
5813 rep->analyse_exp = TICK_ETERNITY;
5814 channel_auto_close(rep);
5815 return 1;
5816
5817 abort_keep_alive:
5818 /* A keep-alive request to the server failed on a network error.
5819 * The client is required to retry. We need to close without returning
5820 * any other information so that the client retries.
5821 */
5822 txn->status = 0;
5823 rep->analysers = 0;
5824 s->req->analysers = 0;
5825 channel_auto_close(rep);
5826 s->logs.logwait = 0;
5827 s->logs.level = 0;
5828 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
5829 bi_erase(rep);
5830 stream_int_retnclose(rep->cons, NULL);
5831 return 0;
5832}
5833
5834/* This function performs all the processing enabled for the current response.
5835 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5836 * and updates s->rep->analysers. It might make sense to explode it into several
5837 * other functions. It works like process_request (see indications above).
5838 */
5839int http_process_res_common(struct session *s, struct channel *rep, int an_bit, struct proxy *px)
5840{
5841 struct http_txn *txn = &s->txn;
5842 struct http_msg *msg = &txn->rsp;
5843 struct proxy *cur_proxy;
5844 struct cond_wordlist *wl;
5845 struct http_res_rule *http_res_last_rule = NULL;
5846
5847 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
5848 now_ms, __FUNCTION__,
5849 s,
5850 rep,
5851 rep->rex, rep->wex,
5852 rep->flags,
5853 rep->buf->i,
5854 rep->analysers);
5855
5856 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
5857 return 0;
5858
5859 rep->analysers &= ~an_bit;
5860 rep->analyse_exp = TICK_ETERNITY;
5861
Willy Tarreau70730dd2014-04-24 18:06:27 +02005862 /* The stats applet needs to adjust the Connection header but we don't
5863 * apply any filter there.
5864 */
5865 if (unlikely(objt_applet(s->target) == &http_stats_applet))
5866 goto skip_filters;
5867
Willy Tarreau58975672014-04-24 21:13:57 +02005868 /*
5869 * We will have to evaluate the filters.
5870 * As opposed to version 1.2, now they will be evaluated in the
5871 * filters order and not in the header order. This means that
5872 * each filter has to be validated among all headers.
5873 *
5874 * Filters are tried with ->be first, then with ->fe if it is
5875 * different from ->be.
5876 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005877
Willy Tarreau58975672014-04-24 21:13:57 +02005878 cur_proxy = s->be;
5879 while (1) {
5880 struct proxy *rule_set = cur_proxy;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005881
Willy Tarreau58975672014-04-24 21:13:57 +02005882 /* evaluate http-response rules */
5883 if (!http_res_last_rule)
5884 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 +02005885
Willy Tarreau58975672014-04-24 21:13:57 +02005886 /* try headers filters */
5887 if (rule_set->rsp_exp != NULL) {
5888 if (apply_filters_to_response(s, rep, rule_set) < 0) {
5889 return_bad_resp:
5890 if (objt_server(s->target)) {
5891 objt_server(s->target)->counters.failed_resp++;
5892 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_RSP);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005893 }
Willy Tarreau58975672014-04-24 21:13:57 +02005894 s->be->be_counters.failed_resp++;
5895 return_srv_prx_502:
5896 rep->analysers = 0;
5897 txn->status = 502;
5898 s->logs.t_data = -1; /* was not a valid response */
5899 rep->prod->flags |= SI_FL_NOLINGER;
5900 bi_erase(rep);
5901 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
5902 if (!(s->flags & SN_ERR_MASK))
5903 s->flags |= SN_ERR_PRXCOND;
5904 if (!(s->flags & SN_FINST_MASK))
5905 s->flags |= SN_FINST_H;
5906 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005907 }
Willy Tarreau58975672014-04-24 21:13:57 +02005908 }
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005909
Willy Tarreau58975672014-04-24 21:13:57 +02005910 /* has the response been denied ? */
5911 if (txn->flags & TX_SVDENY) {
5912 if (objt_server(s->target))
5913 objt_server(s->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005914
Willy Tarreau58975672014-04-24 21:13:57 +02005915 s->be->be_counters.denied_resp++;
5916 s->fe->fe_counters.denied_resp++;
5917 if (s->listener->counters)
5918 s->listener->counters->denied_resp++;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005919
Willy Tarreau58975672014-04-24 21:13:57 +02005920 goto return_srv_prx_502;
5921 }
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005922
Willy Tarreau58975672014-04-24 21:13:57 +02005923 /* add response headers from the rule sets in the same order */
5924 list_for_each_entry(wl, &rule_set->rsp_add, list) {
5925 if (txn->status < 200)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005926 break;
Willy Tarreau58975672014-04-24 21:13:57 +02005927 if (wl->cond) {
5928 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
5929 ret = acl_pass(ret);
5930 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5931 ret = !ret;
5932 if (!ret)
5933 continue;
5934 }
5935 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
5936 goto return_bad_resp;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005937 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005938
Willy Tarreau58975672014-04-24 21:13:57 +02005939 /* check whether we're already working on the frontend */
5940 if (cur_proxy == s->fe)
5941 break;
5942 cur_proxy = s->fe;
5943 }
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005944
Willy Tarreau58975672014-04-24 21:13:57 +02005945 /* OK that's all we can do for 1xx responses */
5946 if (unlikely(txn->status < 200))
5947 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005948
Willy Tarreau58975672014-04-24 21:13:57 +02005949 /*
5950 * Now check for a server cookie.
5951 */
5952 if (s->be->cookie_name || s->be->appsession_name || s->fe->capture_name ||
5953 (s->be->options & PR_O_CHK_CACHE))
5954 manage_server_side_cookies(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005955
Willy Tarreau58975672014-04-24 21:13:57 +02005956 /*
5957 * Check for cache-control or pragma headers if required.
5958 */
5959 if ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC))
5960 check_response_for_cacheability(s, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005961
Willy Tarreau58975672014-04-24 21:13:57 +02005962 /*
5963 * Add server cookie in the response if needed
5964 */
5965 if (objt_server(s->target) && (s->be->ck_opts & PR_CK_INS) &&
5966 !((txn->flags & TX_SCK_FOUND) && (s->be->ck_opts & PR_CK_PSV)) &&
5967 (!(s->flags & SN_DIRECT) ||
5968 ((s->be->cookie_maxidle || txn->cookie_last_date) &&
5969 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5970 (s->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5971 (!s->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
5972 (!(s->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
5973 !(s->flags & SN_IGNORE_PRST)) {
5974 /* the server is known, it's not the one the client requested, or the
5975 * cookie's last seen date needs to be refreshed. We have to
5976 * insert a set-cookie here, except if we want to insert only on POST
5977 * requests and this one isn't. Note that servers which don't have cookies
5978 * (eg: some backup servers) will return a full cookie removal request.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005979 */
Willy Tarreau58975672014-04-24 21:13:57 +02005980 if (!objt_server(s->target)->cookie) {
5981 chunk_printf(&trash,
5982 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5983 s->be->cookie_name);
5984 }
5985 else {
5986 chunk_printf(&trash, "Set-Cookie: %s=%s", s->be->cookie_name, objt_server(s->target)->cookie);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005987
Willy Tarreau58975672014-04-24 21:13:57 +02005988 if (s->be->cookie_maxidle || s->be->cookie_maxlife) {
5989 /* emit last_date, which is mandatory */
5990 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5991 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5992 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005993
Willy Tarreau58975672014-04-24 21:13:57 +02005994 if (s->be->cookie_maxlife) {
5995 /* emit first_date, which is either the original one or
5996 * the current date.
5997 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005998 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreau58975672014-04-24 21:13:57 +02005999 s30tob64(txn->cookie_first_date ?
6000 txn->cookie_first_date >> 2 :
6001 (date.tv_sec+3) >> 2, trash.str + trash.len);
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006002 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006003 }
Willy Tarreauef4f3912010-10-07 21:00:29 +02006004 }
Willy Tarreau58975672014-04-24 21:13:57 +02006005 chunk_appendf(&trash, "; path=/");
6006 }
Willy Tarreau4992dd22012-05-31 21:02:17 +02006007
Willy Tarreau58975672014-04-24 21:13:57 +02006008 if (s->be->cookie_domain)
6009 chunk_appendf(&trash, "; domain=%s", s->be->cookie_domain);
Willy Tarreauef4f3912010-10-07 21:00:29 +02006010
Willy Tarreau58975672014-04-24 21:13:57 +02006011 if (s->be->ck_opts & PR_CK_HTTPONLY)
6012 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006013
Willy Tarreau58975672014-04-24 21:13:57 +02006014 if (s->be->ck_opts & PR_CK_SECURE)
6015 chunk_appendf(&trash, "; Secure");
Willy Tarreaubaaee002006-06-26 02:48:02 +02006016
Willy Tarreau58975672014-04-24 21:13:57 +02006017 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
6018 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006019
Willy Tarreau58975672014-04-24 21:13:57 +02006020 txn->flags &= ~TX_SCK_MASK;
6021 if (objt_server(s->target)->cookie && (s->flags & SN_DIRECT))
6022 /* the server did not change, only the date was updated */
6023 txn->flags |= TX_SCK_UPDATED;
6024 else
6025 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006026
Willy Tarreau58975672014-04-24 21:13:57 +02006027 /* Here, we will tell an eventual cache on the client side that we don't
6028 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6029 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6030 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006031 */
Willy Tarreau58975672014-04-24 21:13:57 +02006032 if ((s->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006033
Willy Tarreau58975672014-04-24 21:13:57 +02006034 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006035
Willy Tarreau58975672014-04-24 21:13:57 +02006036 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
6037 "Cache-control: private", 22) < 0))
6038 goto return_bad_resp;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006039 }
Willy Tarreau58975672014-04-24 21:13:57 +02006040 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006041
Willy Tarreau58975672014-04-24 21:13:57 +02006042 /*
6043 * Check if result will be cacheable with a cookie.
6044 * We'll block the response if security checks have caught
6045 * nasty things such as a cacheable cookie.
6046 */
6047 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6048 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
6049 (s->be->options & PR_O_CHK_CACHE)) {
6050 /* we're in presence of a cacheable response containing
6051 * a set-cookie header. We'll block it as requested by
6052 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006053 */
Willy Tarreau58975672014-04-24 21:13:57 +02006054 if (objt_server(s->target))
6055 objt_server(s->target)->counters.failed_secu++;
Willy Tarreau60466522010-01-18 19:08:45 +01006056
Willy Tarreau58975672014-04-24 21:13:57 +02006057 s->be->be_counters.denied_resp++;
6058 s->fe->fe_counters.denied_resp++;
6059 if (s->listener->counters)
6060 s->listener->counters->denied_resp++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006061
Willy Tarreau58975672014-04-24 21:13:57 +02006062 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
6063 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6064 send_log(s->be, LOG_ALERT,
6065 "Blocking cacheable cookie in response from instance %s, server %s.\n",
6066 s->be->id, objt_server(s->target) ? objt_server(s->target)->id : "<dispatch>");
6067 goto return_srv_prx_502;
6068 }
Willy Tarreau03945942009-12-22 16:50:27 +01006069
Willy Tarreau70730dd2014-04-24 18:06:27 +02006070 skip_filters:
Willy Tarreau58975672014-04-24 21:13:57 +02006071 /*
6072 * Adjust "Connection: close" or "Connection: keep-alive" if needed.
6073 * If an "Upgrade" token is found, the header is left untouched in order
6074 * not to have to deal with some client bugs : some of them fail an upgrade
6075 * if anything but "Upgrade" is present in the Connection header.
6076 */
6077 if (!(txn->flags & TX_HDR_CONN_UPG) &&
6078 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
6079 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6080 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
6081 unsigned int want_flags = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006082
Willy Tarreau58975672014-04-24 21:13:57 +02006083 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6084 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6085 /* we want a keep-alive response here. Keep-alive header
6086 * required if either side is not 1.1.
6087 */
6088 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
6089 want_flags |= TX_CON_KAL_SET;
6090 }
6091 else {
6092 /* we want a close response here. Close header required if
6093 * the server is 1.1, regardless of the client.
6094 */
6095 if (msg->flags & HTTP_MSGF_VER_11)
6096 want_flags |= TX_CON_CLO_SET;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006097 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006098
Willy Tarreau58975672014-04-24 21:13:57 +02006099 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
6100 http_change_connection_header(txn, msg, want_flags);
6101 }
6102
6103 skip_header_mangling:
6104 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
6105 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
6106 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006107
Willy Tarreau58975672014-04-24 21:13:57 +02006108 /* if the user wants to log as soon as possible, without counting
6109 * bytes from the server, then this is the right moment. We have
6110 * to temporarily assign bytes_out to log what we currently have.
6111 */
6112 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
6113 s->logs.t_close = s->logs.t_data; /* to get a valid end date */
6114 s->logs.bytes_out = txn->rsp.eoh;
6115 s->do_log(s);
6116 s->logs.bytes_out = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006117 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006118 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006119}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006120
Willy Tarreaud98cf932009-12-27 22:54:55 +01006121/* This function is an analyser which forwards response body (including chunk
6122 * sizes if any). It is called as soon as we must forward, even if we forward
6123 * zero byte. The only situation where it must not be called is when we're in
6124 * tunnel mode and we want to forward till the close. It's used both to forward
6125 * remaining data and to resync after end of body. It expects the msg_state to
6126 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6127 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006128 *
6129 * It is capable of compressing response data both in content-length mode and
6130 * in chunked mode. The state machines follows different flows depending on
6131 * whether content-length and chunked modes are used, since there are no
6132 * trailers in content-length :
6133 *
6134 * chk-mode cl-mode
6135 * ,----- BODY -----.
6136 * / \
6137 * V size > 0 V chk-mode
6138 * .--> SIZE -------------> DATA -------------> CRLF
6139 * | | size == 0 | last byte |
6140 * | v final crlf v inspected |
6141 * | TRAILERS -----------> DONE |
6142 * | |
6143 * `----------------------------------------------'
6144 *
6145 * Compression only happens in the DATA state, and must be flushed in final
6146 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6147 * is performed at once on final states for all bytes parsed, or when leaving
6148 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006149 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006150int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006151{
6152 struct http_txn *txn = &s->txn;
6153 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006154 static struct buffer *tmpbuf = NULL;
6155 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006156 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006157
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006158 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6159 return 0;
6160
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006161 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006162 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006163 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006164 /* Output closed while we were sending data. We must abort and
6165 * wake the other side up.
6166 */
6167 msg->msg_state = HTTP_MSG_ERROR;
6168 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006169 return 1;
6170 }
6171
Willy Tarreau4fe41902010-06-07 22:27:41 +02006172 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006173 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006174
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006175 if (msg->sov) {
6176 /* we have msg->sov which points to the first byte of message
6177 * body, and res->buf.p still points to the beginning of the
6178 * message. We forward the headers now, as we don't need them
6179 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006180 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006181 b_adv(res->buf, msg->sov);
6182 msg->next -= msg->sov;
6183 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006184
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006185 /* The previous analysers guarantee that the state is somewhere
6186 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6187 * msg->next are always correct.
6188 */
6189 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6190 if (msg->flags & HTTP_MSGF_TE_CHNK)
6191 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6192 else
6193 msg->msg_state = HTTP_MSG_DATA;
6194 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006195 }
6196
Willy Tarreauefdf0942014-04-24 20:08:57 +02006197 if (res->to_forward) {
6198 /* We can't process the buffer's contents yet */
6199 res->flags |= CF_WAKE_WRITE;
6200 goto missing_data;
6201 }
6202
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006203 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6204 /* We need a compression buffer in the DATA state to put the
6205 * output of compressed data, and in CRLF state to let the
6206 * TRAILERS state finish the job of removing the trailing CRLF.
6207 */
6208 if (unlikely(tmpbuf == NULL)) {
6209 /* this is the first time we need the compression buffer */
6210 tmpbuf = pool_alloc2(pool2_buffer);
6211 if (tmpbuf == NULL)
6212 goto aborted_xfer; /* no memory */
6213 }
6214
6215 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006216 if (ret < 0) {
6217 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006218 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006219 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006220 compressing = 1;
6221 }
6222
Willy Tarreaud98cf932009-12-27 22:54:55 +01006223 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006224 switch (msg->msg_state - HTTP_MSG_DATA) {
6225 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006226 /* we may have some pending data starting at res->buf->p */
6227 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006228 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006229 if (ret < 0)
6230 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006231
Willy Tarreaud5a67832014-04-21 10:54:27 +02006232 if (msg->chunk_len) {
6233 /* input empty or output full */
6234 if (res->buf->i > msg->next)
6235 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006236 goto missing_data;
6237 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006238 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006239 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006240 if (msg->chunk_len > res->buf->i - msg->next) {
6241 /* output full */
6242 res->flags |= CF_WAKE_WRITE;
6243 goto missing_data;
6244 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006245 msg->next += msg->chunk_len;
6246 msg->chunk_len = 0;
6247 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006248
6249 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006250 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006251 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006252 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006253 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006254 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006255 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006256 /* fall through for HTTP_MSG_CHUNK_CRLF */
6257
6258 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6259 /* we want the CRLF after the data */
6260
6261 ret = http_skip_chunk_crlf(msg);
6262 if (ret == 0)
6263 goto missing_data;
6264 else if (ret < 0) {
6265 if (msg->err_pos >= 0)
6266 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6267 goto return_bad_res;
6268 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006269 /* we're in MSG_CHUNK_SIZE now, fall through */
6270
6271 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006272 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006273 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006274 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006275 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006276
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006277 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006278 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006279 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006280 else if (ret < 0) {
6281 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006282 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006283 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006284 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006285 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006286 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006287
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006288 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006289 if (unlikely(compressing)) {
6290 /* we need to flush output contents before syncing FSMs */
6291 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6292 compressing = 0;
6293 }
6294
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006295 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006296 if (ret == 0)
6297 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006298 else if (ret < 0) {
6299 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006300 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006301 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006302 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006303 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006304
6305 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006306 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006307 if (unlikely(compressing)) {
6308 /* we need to flush output contents before syncing FSMs */
6309 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6310 compressing = 0;
6311 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006312
Willy Tarreauc623c172014-04-18 09:53:50 +02006313 /* we may have some pending data starting at res->buf->p
6314 * such as a last chunk of data or trailers.
6315 */
6316 b_adv(res->buf, msg->next);
6317 msg->next = 0;
6318
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006319 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006320 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006321 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6322 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006323 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006324
Willy Tarreau610ecce2010-01-04 21:15:02 +01006325 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006326 /* some state changes occurred, maybe the analyser
6327 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006328 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006329 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006330 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006331 /* response errors are most likely due to
6332 * the client aborting the transfer.
6333 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006334 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006335 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006336 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006337 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006338 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006339 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006340 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006341 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006342 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006343 }
6344 }
6345
Willy Tarreaud98cf932009-12-27 22:54:55 +01006346 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006347 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006348 if (unlikely(compressing)) {
6349 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006350 compressing = 0;
6351 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006352
Willy Tarreauc623c172014-04-18 09:53:50 +02006353 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6354 b_adv(res->buf, msg->next);
6355 msg->next = 0;
6356 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6357 }
6358
Willy Tarreauf003d372012-11-26 13:35:37 +01006359 if (res->flags & CF_SHUTW)
6360 goto aborted_xfer;
6361
6362 /* stop waiting for data if the input is closed before the end. If the
6363 * client side was already closed, it means that the client has aborted,
6364 * so we don't want to count this as a server abort. Otherwise it's a
6365 * server abort.
6366 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006367 if (res->flags & CF_SHUTR) {
Willy Tarreaub2c6a782014-04-23 20:29:01 +02006368 if ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))
Willy Tarreauf003d372012-11-26 13:35:37 +01006369 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006370 if (!(s->flags & SN_ERR_MASK))
6371 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006372 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006373 if (objt_server(s->target))
6374 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006375 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006376 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006377
Willy Tarreau40dba092010-03-04 18:14:51 +01006378 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006379 if (!s->req->analysers)
6380 goto return_bad_res;
6381
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006382 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006383 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006384 * Similarly, with keep-alive on the client side, we don't want to forward a
6385 * close.
6386 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006387 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006388 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6389 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006390 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006391
Willy Tarreau5c620922011-05-11 19:56:11 +02006392 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006393 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006394 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006395 * modes are already handled by the stream sock layer. We must not do
6396 * this in content-length mode because it could present the MSG_MORE
6397 * flag with the last block of forwarded data, which would cause an
6398 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006399 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006400 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006401 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006402
Willy Tarreaud98cf932009-12-27 22:54:55 +01006403 /* the session handler will take care of timeouts and errors */
6404 return 0;
6405
Willy Tarreau40dba092010-03-04 18:14:51 +01006406 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006407 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006408 if (objt_server(s->target))
6409 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006410
6411 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006412 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006413 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006414 compressing = 0;
6415 }
6416
Willy Tarreauc623c172014-04-18 09:53:50 +02006417 /* we may have some pending data starting at res->buf->p */
6418 if (s->comp_algo == NULL) {
6419 b_adv(res->buf, msg->next);
6420 msg->next = 0;
6421 }
6422
Willy Tarreaud98cf932009-12-27 22:54:55 +01006423 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006424 /* don't send any error message as we're in the body */
6425 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006426 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006427 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006428 if (objt_server(s->target))
6429 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006430
6431 if (!(s->flags & SN_ERR_MASK))
6432 s->flags |= SN_ERR_PRXCOND;
6433 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006434 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006435 return 0;
6436
6437 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006438 if (unlikely(compressing)) {
6439 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6440 compressing = 0;
6441 }
6442
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006443 txn->rsp.msg_state = HTTP_MSG_ERROR;
6444 /* don't send any error message as we're in the body */
6445 stream_int_retnclose(res->cons, NULL);
6446 res->analysers = 0;
6447 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6448
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006449 s->fe->fe_counters.cli_aborts++;
6450 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006451 if (objt_server(s->target))
6452 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006453
6454 if (!(s->flags & SN_ERR_MASK))
6455 s->flags |= SN_ERR_CLICL;
6456 if (!(s->flags & SN_FINST_MASK))
6457 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006458 return 0;
6459}
6460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006461/* Iterate the same filter through all request headers.
6462 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006463 * Since it can manage the switch to another backend, it updates the per-proxy
6464 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006465 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006466int apply_filter_to_req_headers(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006467{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006468 char term;
6469 char *cur_ptr, *cur_end, *cur_next;
6470 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006471 struct http_txn *txn = &s->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006472 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006473 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006475 last_hdr = 0;
6476
Willy Tarreau9b28e032012-10-12 23:49:43 +02006477 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006478 old_idx = 0;
6479
6480 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006481 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006482 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006483 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006484 (exp->action == ACT_ALLOW ||
6485 exp->action == ACT_DENY ||
6486 exp->action == ACT_TARPIT))
6487 return 0;
6488
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006489 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006490 if (!cur_idx)
6491 break;
6492
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006493 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006494 cur_ptr = cur_next;
6495 cur_end = cur_ptr + cur_hdr->len;
6496 cur_next = cur_end + cur_hdr->cr + 1;
6497
6498 /* Now we have one header between cur_ptr and cur_end,
6499 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006500 */
6501
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006502 /* The annoying part is that pattern matching needs
6503 * that we modify the contents to null-terminate all
6504 * strings before testing them.
6505 */
6506
6507 term = *cur_end;
6508 *cur_end = '\0';
6509
6510 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6511 switch (exp->action) {
6512 case ACT_SETBE:
6513 /* It is not possible to jump a second time.
6514 * FIXME: should we return an HTTP/500 here so that
6515 * the admin knows there's a problem ?
6516 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006517 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006518 break;
6519
6520 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006521 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006522 last_hdr = 1;
6523 break;
6524
6525 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006526 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006527 last_hdr = 1;
6528 break;
6529
6530 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006531 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006532 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006533 break;
6534
6535 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006536 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006537 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006538 break;
6539
6540 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006541 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6542 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006543 /* FIXME: if the user adds a newline in the replacement, the
6544 * index will not be recalculated for now, and the new line
6545 * will not be counted as a new header.
6546 */
6547
6548 cur_end += delta;
6549 cur_next += delta;
6550 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006551 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006552 break;
6553
6554 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006555 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006556 cur_next += delta;
6557
Willy Tarreaufa355d42009-11-29 18:12:29 +01006558 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006559 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6560 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006561 cur_hdr->len = 0;
6562 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006563 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006564 break;
6565
6566 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006567 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006568 if (cur_end)
6569 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006571 /* keep the link from this header to next one in case of later
6572 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006573 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006574 old_idx = cur_idx;
6575 }
6576 return 0;
6577}
6578
6579
6580/* Apply the filter to the request line.
6581 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6582 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006583 * Since it can manage the switch to another backend, it updates the per-proxy
6584 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006585 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006586int apply_filter_to_req_line(struct session *s, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006587{
6588 char term;
6589 char *cur_ptr, *cur_end;
6590 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006591 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006592 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006593
Willy Tarreau3d300592007-03-18 18:34:41 +01006594 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006595 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006596 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006597 (exp->action == ACT_ALLOW ||
6598 exp->action == ACT_DENY ||
6599 exp->action == ACT_TARPIT))
6600 return 0;
6601 else if (exp->action == ACT_REMOVE)
6602 return 0;
6603
6604 done = 0;
6605
Willy Tarreau9b28e032012-10-12 23:49:43 +02006606 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006607 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006608
6609 /* Now we have the request line between cur_ptr and cur_end */
6610
6611 /* The annoying part is that pattern matching needs
6612 * that we modify the contents to null-terminate all
6613 * strings before testing them.
6614 */
6615
6616 term = *cur_end;
6617 *cur_end = '\0';
6618
6619 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6620 switch (exp->action) {
6621 case ACT_SETBE:
6622 /* It is not possible to jump a second time.
6623 * FIXME: should we return an HTTP/500 here so that
6624 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006625 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006626 if (s->be != s->fe)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006627 break;
6628
6629 /* Swithing Proxy */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006630 session_set_backend(s, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006631 done = 1;
6632 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006633
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006634 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006635 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006636 done = 1;
6637 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006638
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006639 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006640 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006641 done = 1;
6642 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006643
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006644 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006645 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006646 done = 1;
6647 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006648
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006649 case ACT_REPLACE:
6650 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006651 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6652 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006653 /* FIXME: if the user adds a newline in the replacement, the
6654 * index will not be recalculated for now, and the new line
6655 * will not be counted as a new header.
6656 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006657
Willy Tarreaufa355d42009-11-29 18:12:29 +01006658 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006659 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006660 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006661 HTTP_MSG_RQMETH,
6662 cur_ptr, cur_end + 1,
6663 NULL, NULL);
6664 if (unlikely(!cur_end))
6665 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006666
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006667 /* we have a full request and we know that we have either a CR
6668 * or an LF at <ptr>.
6669 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006670 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6671 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006672 /* there is no point trying this regex on headers */
6673 return 1;
6674 }
6675 }
6676 *cur_end = term; /* restore the string terminator */
6677 return done;
6678}
Willy Tarreau97de6242006-12-27 17:18:38 +01006679
Willy Tarreau58f10d72006-12-04 02:26:12 +01006680
Willy Tarreau58f10d72006-12-04 02:26:12 +01006681
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006682/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006683 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006684 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006685 * unparsable request. Since it can manage the switch to another backend, it
6686 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006687 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006688int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006689{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006690 struct http_txn *txn = &s->txn;
6691 struct hdr_exp *exp;
6692
6693 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006694 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006695
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006696 /*
6697 * The interleaving of transformations and verdicts
6698 * makes it difficult to decide to continue or stop
6699 * the evaluation.
6700 */
6701
Willy Tarreau6c123b12010-01-28 20:22:06 +01006702 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6703 break;
6704
Willy Tarreau3d300592007-03-18 18:34:41 +01006705 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006706 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006707 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006708 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006709
6710 /* if this filter had a condition, evaluate it now and skip to
6711 * next filter if the condition does not match.
6712 */
6713 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006714 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006715 ret = acl_pass(ret);
6716 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6717 ret = !ret;
6718
6719 if (!ret)
6720 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006721 }
6722
6723 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006724 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006725 if (unlikely(ret < 0))
6726 return -1;
6727
6728 if (likely(ret == 0)) {
6729 /* The filter did not match the request, it can be
6730 * iterated through all headers.
6731 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006732 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006733 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006734 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006735 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006736}
6737
6738
Willy Tarreaua15645d2007-03-18 16:22:39 +01006739
Willy Tarreau58f10d72006-12-04 02:26:12 +01006740/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006741 * Try to retrieve the server associated to the appsession.
6742 * If the server is found, it's assigned to the session.
6743 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006744void manage_client_side_appsession(struct session *s, const char *buf, int len) {
6745 struct http_txn *txn = &s->txn;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006746 appsess *asession = NULL;
6747 char *sessid_temp = NULL;
6748
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006749 if (len > s->be->appsession_len) {
6750 len = s->be->appsession_len;
Cyril Bontéb21570a2009-11-29 20:04:48 +01006751 }
6752
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006753 if (s->be->options2 & PR_O2_AS_REQL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006754 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006755 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006756 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006757 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006758 }
6759
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006760 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006761 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006762 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006763 return;
6764 }
6765
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006766 memcpy(txn->sessid, buf, len);
6767 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006768 }
6769
6770 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6771 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006772 send_log(s->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006773 return;
6774 }
6775
Cyril Bontéb21570a2009-11-29 20:04:48 +01006776 memcpy(sessid_temp, buf, len);
6777 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006778
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006779 asession = appsession_hash_lookup(&(s->be->htbl_proxy), sessid_temp);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006780 /* free previously allocated memory */
6781 pool_free2(apools.sessid, sessid_temp);
6782
6783 if (asession != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006784 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
6785 if (!(s->be->options2 & PR_O2_AS_REQL))
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006786 asession->request_count++;
6787
6788 if (asession->serverid != NULL) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006789 struct server *srv = s->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006790
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006791 while (srv) {
6792 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006793 if ((srv->state & SRV_RUNNING) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006794 (s->be->options & PR_O_PERSIST) ||
6795 (s->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006796 /* we found the server and it's usable */
6797 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006798 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006799 s->flags |= SN_DIRECT | SN_ASSIGNED;
6800 s->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006801
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006802 break;
6803 } else {
6804 txn->flags &= ~TX_CK_MASK;
6805 txn->flags |= TX_CK_DOWN;
6806 }
6807 }
6808 srv = srv->next;
6809 }
6810 }
6811 }
6812}
6813
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006814/* Find the end of a cookie value contained between <s> and <e>. It works the
6815 * same way as with headers above except that the semi-colon also ends a token.
6816 * See RFC2965 for more information. Note that it requires a valid header to
6817 * return a valid result.
6818 */
6819char *find_cookie_value_end(char *s, const char *e)
6820{
6821 int quoted, qdpair;
6822
6823 quoted = qdpair = 0;
6824 for (; s < e; s++) {
6825 if (qdpair) qdpair = 0;
6826 else if (quoted) {
6827 if (*s == '\\') qdpair = 1;
6828 else if (*s == '"') quoted = 0;
6829 }
6830 else if (*s == '"') quoted = 1;
6831 else if (*s == ',' || *s == ';') return s;
6832 }
6833 return s;
6834}
6835
6836/* Delete a value in a header between delimiters <from> and <next> in buffer
6837 * <buf>. The number of characters displaced is returned, and the pointer to
6838 * the first delimiter is updated if required. The function tries as much as
6839 * possible to respect the following principles :
6840 * - replace <from> delimiter by the <next> one unless <from> points to a
6841 * colon, in which case <next> is simply removed
6842 * - set exactly one space character after the new first delimiter, unless
6843 * there are not enough characters in the block being moved to do so.
6844 * - remove unneeded spaces before the previous delimiter and after the new
6845 * one.
6846 *
6847 * It is the caller's responsibility to ensure that :
6848 * - <from> points to a valid delimiter or the colon ;
6849 * - <next> points to a valid delimiter or the final CR/LF ;
6850 * - there are non-space chars before <from> ;
6851 * - there is a CR/LF at or after <next>.
6852 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006853int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006854{
6855 char *prev = *from;
6856
6857 if (*prev == ':') {
6858 /* We're removing the first value, preserve the colon and add a
6859 * space if possible.
6860 */
6861 if (!http_is_crlf[(unsigned char)*next])
6862 next++;
6863 prev++;
6864 if (prev < next)
6865 *prev++ = ' ';
6866
6867 while (http_is_spht[(unsigned char)*next])
6868 next++;
6869 } else {
6870 /* Remove useless spaces before the old delimiter. */
6871 while (http_is_spht[(unsigned char)*(prev-1)])
6872 prev--;
6873 *from = prev;
6874
6875 /* copy the delimiter and if possible a space if we're
6876 * not at the end of the line.
6877 */
6878 if (!http_is_crlf[(unsigned char)*next]) {
6879 *prev++ = *next++;
6880 if (prev + 1 < next)
6881 *prev++ = ' ';
6882 while (http_is_spht[(unsigned char)*next])
6883 next++;
6884 }
6885 }
6886 return buffer_replace2(buf, prev, next, NULL, 0);
6887}
6888
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006889/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006890 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006891 * desirable to call it only when needed. This code is quite complex because
6892 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6893 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006894 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006895void manage_client_side_cookies(struct session *s, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006896{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02006897 struct http_txn *txn = &s->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006898 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006899 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006900 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6901 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006902
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006903 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006904 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006905 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006906
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006907 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006908 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006909 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006910
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006911 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006912 hdr_beg = hdr_next;
6913 hdr_end = hdr_beg + cur_hdr->len;
6914 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006915
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006916 /* We have one full header between hdr_beg and hdr_end, and the
6917 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006918 * "Cookie:" headers.
6919 */
6920
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006921 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006922 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006923 old_idx = cur_idx;
6924 continue;
6925 }
6926
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006927 del_from = NULL; /* nothing to be deleted */
6928 preserve_hdr = 0; /* assume we may kill the whole header */
6929
Willy Tarreau58f10d72006-12-04 02:26:12 +01006930 /* Now look for cookies. Conforming to RFC2109, we have to support
6931 * attributes whose name begin with a '$', and associate them with
6932 * the right cookie, if we want to delete this cookie.
6933 * So there are 3 cases for each cookie read :
6934 * 1) it's a special attribute, beginning with a '$' : ignore it.
6935 * 2) it's a server id cookie that we *MAY* want to delete : save
6936 * some pointers on it (last semi-colon, beginning of cookie...)
6937 * 3) it's an application cookie : we *MAY* have to delete a previous
6938 * "special" cookie.
6939 * At the end of loop, if a "special" cookie remains, we may have to
6940 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006941 * *MUST* delete it.
6942 *
6943 * Note: RFC2965 is unclear about the processing of spaces around
6944 * the equal sign in the ATTR=VALUE form. A careful inspection of
6945 * the RFC explicitly allows spaces before it, and not within the
6946 * tokens (attrs or values). An inspection of RFC2109 allows that
6947 * too but section 10.1.3 lets one think that spaces may be allowed
6948 * after the equal sign too, resulting in some (rare) buggy
6949 * implementations trying to do that. So let's do what servers do.
6950 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6951 * allowed quoted strings in values, with any possible character
6952 * after a backslash, including control chars and delimitors, which
6953 * causes parsing to become ambiguous. Browsers also allow spaces
6954 * within values even without quotes.
6955 *
6956 * We have to keep multiple pointers in order to support cookie
6957 * removal at the beginning, middle or end of header without
6958 * corrupting the header. All of these headers are valid :
6959 *
6960 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6961 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6962 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6963 * | | | | | | | | |
6964 * | | | | | | | | hdr_end <--+
6965 * | | | | | | | +--> next
6966 * | | | | | | +----> val_end
6967 * | | | | | +-----------> val_beg
6968 * | | | | +--------------> equal
6969 * | | | +----------------> att_end
6970 * | | +---------------------> att_beg
6971 * | +--------------------------> prev
6972 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006973 */
6974
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006975 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6976 /* Iterate through all cookies on this line */
6977
6978 /* find att_beg */
6979 att_beg = prev + 1;
6980 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6981 att_beg++;
6982
6983 /* find att_end : this is the first character after the last non
6984 * space before the equal. It may be equal to hdr_end.
6985 */
6986 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006987
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006988 while (equal < hdr_end) {
6989 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006990 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006991 if (http_is_spht[(unsigned char)*equal++])
6992 continue;
6993 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006994 }
6995
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006996 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6997 * is between <att_beg> and <equal>, both may be identical.
6998 */
6999
7000 /* look for end of cookie if there is an equal sign */
7001 if (equal < hdr_end && *equal == '=') {
7002 /* look for the beginning of the value */
7003 val_beg = equal + 1;
7004 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7005 val_beg++;
7006
7007 /* find the end of the value, respecting quotes */
7008 next = find_cookie_value_end(val_beg, hdr_end);
7009
7010 /* make val_end point to the first white space or delimitor after the value */
7011 val_end = next;
7012 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7013 val_end--;
7014 } else {
7015 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007016 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007017
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007018 /* We have nothing to do with attributes beginning with '$'. However,
7019 * they will automatically be removed if a header before them is removed,
7020 * since they're supposed to be linked together.
7021 */
7022 if (*att_beg == '$')
7023 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007024
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007025 /* Ignore cookies with no equal sign */
7026 if (equal == next) {
7027 /* This is not our cookie, so we must preserve it. But if we already
7028 * scheduled another cookie for removal, we cannot remove the
7029 * complete header, but we can remove the previous block itself.
7030 */
7031 preserve_hdr = 1;
7032 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007033 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007034 val_end += delta;
7035 next += delta;
7036 hdr_end += delta;
7037 hdr_next += delta;
7038 cur_hdr->len += delta;
7039 http_msg_move_end(&txn->req, delta);
7040 prev = del_from;
7041 del_from = NULL;
7042 }
7043 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007044 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007045
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007046 /* if there are spaces around the equal sign, we need to
7047 * strip them otherwise we'll get trouble for cookie captures,
7048 * or even for rewrites. Since this happens extremely rarely,
7049 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007050 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007051 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7052 int stripped_before = 0;
7053 int stripped_after = 0;
7054
7055 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007056 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007057 equal += stripped_before;
7058 val_beg += stripped_before;
7059 }
7060
7061 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007062 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007063 val_beg += stripped_after;
7064 stripped_before += stripped_after;
7065 }
7066
7067 val_end += stripped_before;
7068 next += stripped_before;
7069 hdr_end += stripped_before;
7070 hdr_next += stripped_before;
7071 cur_hdr->len += stripped_before;
7072 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007073 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007074 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007075
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007076 /* First, let's see if we want to capture this cookie. We check
7077 * that we don't already have a client side cookie, because we
7078 * can only capture one. Also as an optimisation, we ignore
7079 * cookies shorter than the declared name.
7080 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007081 if (s->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7082 (val_end - att_beg >= s->fe->capture_namelen) &&
7083 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007084 int log_len = val_end - att_beg;
7085
7086 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7087 Alert("HTTP logging : out of memory.\n");
7088 } else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007089 if (log_len > s->fe->capture_len)
7090 log_len = s->fe->capture_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007091 memcpy(txn->cli_cookie, att_beg, log_len);
7092 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007093 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007094 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007095
Willy Tarreaubca99692010-10-06 19:25:55 +02007096 /* Persistence cookies in passive, rewrite or insert mode have the
7097 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007098 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007099 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007100 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007101 * For cookies in prefix mode, the form is :
7102 *
7103 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007104 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007105 if ((att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7106 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
7107 struct server *srv = s->be->srv;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007108 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007109
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007110 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7111 * have the server ID between val_beg and delim, and the original cookie between
7112 * delim+1 and val_end. Otherwise, delim==val_end :
7113 *
7114 * Cookie: NAME=SRV; # in all but prefix modes
7115 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7116 * | || || | |+-> next
7117 * | || || | +--> val_end
7118 * | || || +---------> delim
7119 * | || |+------------> val_beg
7120 * | || +-------------> att_end = equal
7121 * | |+-----------------> att_beg
7122 * | +------------------> prev
7123 * +-------------------------> hdr_beg
7124 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007125
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007126 if (s->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007127 for (delim = val_beg; delim < val_end; delim++)
7128 if (*delim == COOKIE_DELIM)
7129 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007130 } else {
7131 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007132 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007133 /* Now check if the cookie contains a date field, which would
7134 * appear after a vertical bar ('|') just after the server name
7135 * and before the delimiter.
7136 */
7137 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7138 if (vbar1) {
7139 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007140 * right is the last seen date. It is a base64 encoded
7141 * 30-bit value representing the UNIX date since the
7142 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007143 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007144 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007145 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007146 if (val_end - vbar1 >= 5) {
7147 val = b64tos30(vbar1);
7148 if (val > 0)
7149 txn->cookie_last_date = val << 2;
7150 }
7151 /* look for a second vertical bar */
7152 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7153 if (vbar1 && (val_end - vbar1 > 5)) {
7154 val = b64tos30(vbar1 + 1);
7155 if (val > 0)
7156 txn->cookie_first_date = val << 2;
7157 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007158 }
7159 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007160
Willy Tarreauf64d1412010-10-07 20:06:11 +02007161 /* if the cookie has an expiration date and the proxy wants to check
7162 * it, then we do that now. We first check if the cookie is too old,
7163 * then only if it has expired. We detect strict overflow because the
7164 * time resolution here is not great (4 seconds). Cookies with dates
7165 * in the future are ignored if their offset is beyond one day. This
7166 * allows an admin to fix timezone issues without expiring everyone
7167 * and at the same time avoids keeping unwanted side effects for too
7168 * long.
7169 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007170 if (txn->cookie_first_date && s->be->cookie_maxlife &&
7171 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)s->be->cookie_maxlife) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007172 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007173 txn->flags &= ~TX_CK_MASK;
7174 txn->flags |= TX_CK_OLD;
7175 delim = val_beg; // let's pretend we have not found the cookie
7176 txn->cookie_first_date = 0;
7177 txn->cookie_last_date = 0;
7178 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007179 else if (txn->cookie_last_date && s->be->cookie_maxidle &&
7180 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)s->be->cookie_maxidle) ||
Willy Tarreauef4f3912010-10-07 21:00:29 +02007181 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007182 txn->flags &= ~TX_CK_MASK;
7183 txn->flags |= TX_CK_EXPIRED;
7184 delim = val_beg; // let's pretend we have not found the cookie
7185 txn->cookie_first_date = 0;
7186 txn->cookie_last_date = 0;
7187 }
7188
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007189 /* Here, we'll look for the first running server which supports the cookie.
7190 * This allows to share a same cookie between several servers, for example
7191 * to dedicate backup servers to specific servers only.
7192 * However, to prevent clients from sticking to cookie-less backup server
7193 * when they have incidentely learned an empty cookie, we simply ignore
7194 * empty cookies and mark them as invalid.
7195 * The same behaviour is applied when persistence must be ignored.
7196 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007197 if ((delim == val_beg) || (s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007198 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007199
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007200 while (srv) {
7201 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7202 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7203 if ((srv->state & SRV_RUNNING) ||
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007204 (s->be->options & PR_O_PERSIST) ||
7205 (s->flags & SN_FORCE_PRST)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007206 /* we found the server and we can use it */
7207 txn->flags &= ~TX_CK_MASK;
7208 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007209 s->flags |= SN_DIRECT | SN_ASSIGNED;
7210 s->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007211 break;
7212 } else {
7213 /* we found a server, but it's down,
7214 * mark it as such and go on in case
7215 * another one is available.
7216 */
7217 txn->flags &= ~TX_CK_MASK;
7218 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007219 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007220 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007221 srv = srv->next;
7222 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007223
Willy Tarreauf64d1412010-10-07 20:06:11 +02007224 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007225 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007226 txn->flags &= ~TX_CK_MASK;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007227 if ((s->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007228 txn->flags |= TX_CK_UNUSED;
7229 else
7230 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007231 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007232
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007233 /* depending on the cookie mode, we may have to either :
7234 * - delete the complete cookie if we're in insert+indirect mode, so that
7235 * the server never sees it ;
7236 * - remove the server id from the cookie value, and tag the cookie as an
7237 * application cookie so that it does not get accidentely removed later,
7238 * if we're in cookie prefix mode
7239 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007240 if ((s->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007241 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007242
Willy Tarreau9b28e032012-10-12 23:49:43 +02007243 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007244 val_end += delta;
7245 next += delta;
7246 hdr_end += delta;
7247 hdr_next += delta;
7248 cur_hdr->len += delta;
7249 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007250
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007251 del_from = NULL;
7252 preserve_hdr = 1; /* we want to keep this cookie */
7253 }
7254 else if (del_from == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007255 (s->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007256 del_from = prev;
7257 }
7258 } else {
7259 /* This is not our cookie, so we must preserve it. But if we already
7260 * scheduled another cookie for removal, we cannot remove the
7261 * complete header, but we can remove the previous block itself.
7262 */
7263 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007264
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007265 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007266 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007267 if (att_beg >= del_from)
7268 att_beg += delta;
7269 if (att_end >= del_from)
7270 att_end += delta;
7271 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007272 val_end += delta;
7273 next += delta;
7274 hdr_end += delta;
7275 hdr_next += delta;
7276 cur_hdr->len += delta;
7277 http_msg_move_end(&txn->req, delta);
7278 prev = del_from;
7279 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007280 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007281 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007282
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007283 /* Look for the appsession cookie unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007284 if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007285 int cmp_len, value_len;
7286 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007287
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007288 if (s->be->options2 & PR_O2_AS_PFX) {
7289 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7290 value_begin = att_beg + s->be->appsession_name_len;
7291 value_len = val_end - att_beg - s->be->appsession_name_len;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007292 } else {
7293 cmp_len = att_end - att_beg;
7294 value_begin = val_beg;
7295 value_len = val_end - val_beg;
7296 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007297
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007298 /* let's see if the cookie is our appcookie */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007299 if (cmp_len == s->be->appsession_name_len &&
7300 memcmp(att_beg, s->be->appsession_name, cmp_len) == 0) {
7301 manage_client_side_appsession(s, value_begin, value_len);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007302 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007303 }
7304
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007305 /* continue with next cookie on this header line */
7306 att_beg = next;
7307 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007308
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007309 /* There are no more cookies on this line.
7310 * We may still have one (or several) marked for deletion at the
7311 * end of the line. We must do this now in two ways :
7312 * - if some cookies must be preserved, we only delete from the
7313 * mark to the end of line ;
7314 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007315 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007316 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007317 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007318 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007319 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007320 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007321 cur_hdr->len += delta;
7322 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007323 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007324
7325 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007326 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7327 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007328 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007329 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007330 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007331 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007332 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007333 }
7334
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007335 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007336 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007337 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007338}
7339
7340
Willy Tarreaua15645d2007-03-18 16:22:39 +01007341/* Iterate the same filter through all response headers contained in <rtr>.
7342 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7343 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007344int apply_filter_to_resp_headers(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007345{
7346 char term;
7347 char *cur_ptr, *cur_end, *cur_next;
7348 int cur_idx, old_idx, last_hdr;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007349 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007350 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007351 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007352
7353 last_hdr = 0;
7354
Willy Tarreau9b28e032012-10-12 23:49:43 +02007355 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007356 old_idx = 0;
7357
7358 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007359 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007360 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007361 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007362 (exp->action == ACT_ALLOW ||
7363 exp->action == ACT_DENY))
7364 return 0;
7365
7366 cur_idx = txn->hdr_idx.v[old_idx].next;
7367 if (!cur_idx)
7368 break;
7369
7370 cur_hdr = &txn->hdr_idx.v[cur_idx];
7371 cur_ptr = cur_next;
7372 cur_end = cur_ptr + cur_hdr->len;
7373 cur_next = cur_end + cur_hdr->cr + 1;
7374
7375 /* Now we have one header between cur_ptr and cur_end,
7376 * and the next header starts at cur_next.
7377 */
7378
7379 /* The annoying part is that pattern matching needs
7380 * that we modify the contents to null-terminate all
7381 * strings before testing them.
7382 */
7383
7384 term = *cur_end;
7385 *cur_end = '\0';
7386
7387 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7388 switch (exp->action) {
7389 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007390 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007391 last_hdr = 1;
7392 break;
7393
7394 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007395 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007396 last_hdr = 1;
7397 break;
7398
7399 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007400 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7401 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007402 /* FIXME: if the user adds a newline in the replacement, the
7403 * index will not be recalculated for now, and the new line
7404 * will not be counted as a new header.
7405 */
7406
7407 cur_end += delta;
7408 cur_next += delta;
7409 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007410 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007411 break;
7412
7413 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007414 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007415 cur_next += delta;
7416
Willy Tarreaufa355d42009-11-29 18:12:29 +01007417 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007418 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7419 txn->hdr_idx.used--;
7420 cur_hdr->len = 0;
7421 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007422 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007423 break;
7424
7425 }
7426 }
7427 if (cur_end)
7428 *cur_end = term; /* restore the string terminator */
7429
7430 /* keep the link from this header to next one in case of later
7431 * removal of next header.
7432 */
7433 old_idx = cur_idx;
7434 }
7435 return 0;
7436}
7437
7438
7439/* Apply the filter to the status line in the response buffer <rtr>.
7440 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7441 * or -1 if a replacement resulted in an invalid status line.
7442 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007443int apply_filter_to_sts_line(struct session *s, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007444{
7445 char term;
7446 char *cur_ptr, *cur_end;
7447 int done;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007448 struct http_txn *txn = &s->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007449 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007450
7451
Willy Tarreau3d300592007-03-18 18:34:41 +01007452 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007453 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007454 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007455 (exp->action == ACT_ALLOW ||
7456 exp->action == ACT_DENY))
7457 return 0;
7458 else if (exp->action == ACT_REMOVE)
7459 return 0;
7460
7461 done = 0;
7462
Willy Tarreau9b28e032012-10-12 23:49:43 +02007463 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007464 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007465
7466 /* Now we have the status line between cur_ptr and cur_end */
7467
7468 /* The annoying part is that pattern matching needs
7469 * that we modify the contents to null-terminate all
7470 * strings before testing them.
7471 */
7472
7473 term = *cur_end;
7474 *cur_end = '\0';
7475
7476 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7477 switch (exp->action) {
7478 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007479 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007480 done = 1;
7481 break;
7482
7483 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007484 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007485 done = 1;
7486 break;
7487
7488 case ACT_REPLACE:
7489 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007490 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7491 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007492 /* FIXME: if the user adds a newline in the replacement, the
7493 * index will not be recalculated for now, and the new line
7494 * will not be counted as a new header.
7495 */
7496
Willy Tarreaufa355d42009-11-29 18:12:29 +01007497 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007498 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007499 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007500 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007501 cur_ptr, cur_end + 1,
7502 NULL, NULL);
7503 if (unlikely(!cur_end))
7504 return -1;
7505
7506 /* we have a full respnse and we know that we have either a CR
7507 * or an LF at <ptr>.
7508 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007509 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007510 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007511 /* there is no point trying this regex on headers */
7512 return 1;
7513 }
7514 }
7515 *cur_end = term; /* restore the string terminator */
7516 return done;
7517}
7518
7519
7520
7521/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007522 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007523 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7524 * unparsable response.
7525 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007526int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007527{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007528 struct http_txn *txn = &s->txn;
7529 struct hdr_exp *exp;
7530
7531 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007532 int ret;
7533
7534 /*
7535 * The interleaving of transformations and verdicts
7536 * makes it difficult to decide to continue or stop
7537 * the evaluation.
7538 */
7539
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007540 if (txn->flags & TX_SVDENY)
7541 break;
7542
Willy Tarreau3d300592007-03-18 18:34:41 +01007543 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007544 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7545 exp->action == ACT_PASS)) {
7546 exp = exp->next;
7547 continue;
7548 }
7549
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007550 /* if this filter had a condition, evaluate it now and skip to
7551 * next filter if the condition does not match.
7552 */
7553 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007554 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007555 ret = acl_pass(ret);
7556 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7557 ret = !ret;
7558 if (!ret)
7559 continue;
7560 }
7561
Willy Tarreaua15645d2007-03-18 16:22:39 +01007562 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007563 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007564 if (unlikely(ret < 0))
7565 return -1;
7566
7567 if (likely(ret == 0)) {
7568 /* The filter did not match the response, it can be
7569 * iterated through all headers.
7570 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007571 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007572 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007573 }
7574 return 0;
7575}
7576
7577
Willy Tarreaua15645d2007-03-18 16:22:39 +01007578/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007579 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007580 * desirable to call it only when needed. This function is also used when we
7581 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007582 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007583void manage_server_side_cookies(struct session *s, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007584{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007585 struct http_txn *txn = &s->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007586 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007587 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007588 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007589 char *hdr_beg, *hdr_end, *hdr_next;
7590 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007591
Willy Tarreaua15645d2007-03-18 16:22:39 +01007592 /* Iterate through the headers.
7593 * we start with the start line.
7594 */
7595 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007596 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007597
7598 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7599 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007600 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007601
7602 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007603 hdr_beg = hdr_next;
7604 hdr_end = hdr_beg + cur_hdr->len;
7605 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007606
Willy Tarreau24581ba2010-08-31 22:39:35 +02007607 /* We have one full header between hdr_beg and hdr_end, and the
7608 * next header starts at hdr_next. We're only interested in
7609 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007610 */
7611
Willy Tarreau24581ba2010-08-31 22:39:35 +02007612 is_cookie2 = 0;
7613 prev = hdr_beg + 10;
7614 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007615 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007616 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7617 if (!val) {
7618 old_idx = cur_idx;
7619 continue;
7620 }
7621 is_cookie2 = 1;
7622 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007623 }
7624
Willy Tarreau24581ba2010-08-31 22:39:35 +02007625 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7626 * <prev> points to the colon.
7627 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007628 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007629
Willy Tarreau24581ba2010-08-31 22:39:35 +02007630 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7631 * check-cache is enabled) and we are not interested in checking
7632 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007633 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007634 if (s->be->cookie_name == NULL &&
7635 s->be->appsession_name == NULL &&
7636 s->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007637 return;
7638
Willy Tarreau24581ba2010-08-31 22:39:35 +02007639 /* OK so now we know we have to process this response cookie.
7640 * The format of the Set-Cookie header is slightly different
7641 * from the format of the Cookie header in that it does not
7642 * support the comma as a cookie delimiter (thus the header
7643 * cannot be folded) because the Expires attribute described in
7644 * the original Netscape's spec may contain an unquoted date
7645 * with a comma inside. We have to live with this because
7646 * many browsers don't support Max-Age and some browsers don't
7647 * support quoted strings. However the Set-Cookie2 header is
7648 * clean.
7649 *
7650 * We have to keep multiple pointers in order to support cookie
7651 * removal at the beginning, middle or end of header without
7652 * corrupting the header (in case of set-cookie2). A special
7653 * pointer, <scav> points to the beginning of the set-cookie-av
7654 * fields after the first semi-colon. The <next> pointer points
7655 * either to the end of line (set-cookie) or next unquoted comma
7656 * (set-cookie2). All of these headers are valid :
7657 *
7658 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7659 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7660 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7661 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7662 * | | | | | | | | | |
7663 * | | | | | | | | +-> next hdr_end <--+
7664 * | | | | | | | +------------> scav
7665 * | | | | | | +--------------> val_end
7666 * | | | | | +--------------------> val_beg
7667 * | | | | +----------------------> equal
7668 * | | | +------------------------> att_end
7669 * | | +----------------------------> att_beg
7670 * | +------------------------------> prev
7671 * +-----------------------------------------> hdr_beg
7672 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007673
Willy Tarreau24581ba2010-08-31 22:39:35 +02007674 for (; prev < hdr_end; prev = next) {
7675 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007676
Willy Tarreau24581ba2010-08-31 22:39:35 +02007677 /* find att_beg */
7678 att_beg = prev + 1;
7679 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7680 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007681
Willy Tarreau24581ba2010-08-31 22:39:35 +02007682 /* find att_end : this is the first character after the last non
7683 * space before the equal. It may be equal to hdr_end.
7684 */
7685 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007686
Willy Tarreau24581ba2010-08-31 22:39:35 +02007687 while (equal < hdr_end) {
7688 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7689 break;
7690 if (http_is_spht[(unsigned char)*equal++])
7691 continue;
7692 att_end = equal;
7693 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007694
Willy Tarreau24581ba2010-08-31 22:39:35 +02007695 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7696 * is between <att_beg> and <equal>, both may be identical.
7697 */
7698
7699 /* look for end of cookie if there is an equal sign */
7700 if (equal < hdr_end && *equal == '=') {
7701 /* look for the beginning of the value */
7702 val_beg = equal + 1;
7703 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7704 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007705
Willy Tarreau24581ba2010-08-31 22:39:35 +02007706 /* find the end of the value, respecting quotes */
7707 next = find_cookie_value_end(val_beg, hdr_end);
7708
7709 /* make val_end point to the first white space or delimitor after the value */
7710 val_end = next;
7711 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7712 val_end--;
7713 } else {
7714 /* <equal> points to next comma, semi-colon or EOL */
7715 val_beg = val_end = next = equal;
7716 }
7717
7718 if (next < hdr_end) {
7719 /* Set-Cookie2 supports multiple cookies, and <next> points to
7720 * a colon or semi-colon before the end. So skip all attr-value
7721 * pairs and look for the next comma. For Set-Cookie, since
7722 * commas are permitted in values, skip to the end.
7723 */
7724 if (is_cookie2)
7725 next = find_hdr_value_end(next, hdr_end);
7726 else
7727 next = hdr_end;
7728 }
7729
7730 /* Now everything is as on the diagram above */
7731
7732 /* Ignore cookies with no equal sign */
7733 if (equal == val_end)
7734 continue;
7735
7736 /* If there are spaces around the equal sign, we need to
7737 * strip them otherwise we'll get trouble for cookie captures,
7738 * or even for rewrites. Since this happens extremely rarely,
7739 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007740 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007741 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7742 int stripped_before = 0;
7743 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007744
Willy Tarreau24581ba2010-08-31 22:39:35 +02007745 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007746 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007747 equal += stripped_before;
7748 val_beg += stripped_before;
7749 }
7750
7751 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007752 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007753 val_beg += stripped_after;
7754 stripped_before += stripped_after;
7755 }
7756
7757 val_end += stripped_before;
7758 next += stripped_before;
7759 hdr_end += stripped_before;
7760 hdr_next += stripped_before;
7761 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007762 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007763 }
7764
7765 /* First, let's see if we want to capture this cookie. We check
7766 * that we don't already have a server side cookie, because we
7767 * can only capture one. Also as an optimisation, we ignore
7768 * cookies shorter than the declared name.
7769 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007770 if (s->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007771 txn->srv_cookie == NULL &&
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007772 (val_end - att_beg >= s->fe->capture_namelen) &&
7773 memcmp(att_beg, s->fe->capture_name, s->fe->capture_namelen) == 0) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007774 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007775 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007776 Alert("HTTP logging : out of memory.\n");
7777 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007778 else {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007779 if (log_len > s->fe->capture_len)
7780 log_len = s->fe->capture_len;
Willy Tarreauf70fc752010-11-19 11:27:18 +01007781 memcpy(txn->srv_cookie, att_beg, log_len);
7782 txn->srv_cookie[log_len] = 0;
7783 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007784 }
7785
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007786 srv = objt_server(s->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007787 /* now check if we need to process it for persistence */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007788 if (!(s->flags & SN_IGNORE_PRST) &&
7789 (att_end - att_beg == s->be->cookie_len) && (s->be->cookie_name != NULL) &&
7790 (memcmp(att_beg, s->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007791 /* assume passive cookie by default */
7792 txn->flags &= ~TX_SCK_MASK;
7793 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007794
7795 /* If the cookie is in insert mode on a known server, we'll delete
7796 * this occurrence because we'll insert another one later.
7797 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007798 * a direct access.
7799 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007800 if (s->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007801 /* The "preserve" flag was set, we don't want to touch the
7802 * server's cookie.
7803 */
7804 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007805 else if ((srv && (s->be->ck_opts & PR_CK_INS)) ||
7806 ((s->flags & SN_DIRECT) && (s->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007807 /* this cookie must be deleted */
7808 if (*prev == ':' && next == hdr_end) {
7809 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007810 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007811 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7812 txn->hdr_idx.used--;
7813 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007814 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007815 hdr_next += delta;
7816 http_msg_move_end(&txn->rsp, delta);
7817 /* note: while both invalid now, <next> and <hdr_end>
7818 * are still equal, so the for() will stop as expected.
7819 */
7820 } else {
7821 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007822 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007823 next = prev;
7824 hdr_end += delta;
7825 hdr_next += delta;
7826 cur_hdr->len += delta;
7827 http_msg_move_end(&txn->rsp, delta);
7828 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007829 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007830 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007831 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007832 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007833 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007834 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007835 * with this server since we know it.
7836 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007837 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007838 next += delta;
7839 hdr_end += delta;
7840 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007841 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007842 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007843
Willy Tarreauf1348312010-10-07 15:54:11 +02007844 txn->flags &= ~TX_SCK_MASK;
7845 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007846 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007847 else if (srv && srv->cookie && (s->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007848 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007849 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007850 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007851 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007852 next += delta;
7853 hdr_end += delta;
7854 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007855 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007856 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007857
Willy Tarreau827aee92011-03-10 16:55:02 +01007858 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007859 txn->flags &= ~TX_SCK_MASK;
7860 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007861 }
7862 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007863 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007864 else if (!(s->flags & SN_IGNORE_PRST) && (s->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007865 int cmp_len, value_len;
7866 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007867
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007868 if (s->be->options2 & PR_O2_AS_PFX) {
7869 cmp_len = MIN(val_end - att_beg, s->be->appsession_name_len);
7870 value_begin = att_beg + s->be->appsession_name_len;
7871 value_len = MIN(s->be->appsession_len, val_end - att_beg - s->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007872 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007873 cmp_len = att_end - att_beg;
7874 value_begin = val_beg;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007875 value_len = MIN(s->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007876 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007877
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007878 if ((cmp_len == s->be->appsession_name_len) &&
7879 (memcmp(att_beg, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007880 /* free a possibly previously allocated memory */
7881 pool_free2(apools.sessid, txn->sessid);
7882
Cyril Bontéb21570a2009-11-29 20:04:48 +01007883 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007884 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007885 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007886 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bontéb21570a2009-11-29 20:04:48 +01007887 return;
7888 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007889 memcpy(txn->sessid, value_begin, value_len);
7890 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007891 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007892 }
7893 /* that's done for this cookie, check the next one on the same
7894 * line when next != hdr_end (only if is_cookie2).
7895 */
7896 }
7897 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007898 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007899 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007900
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007901 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007902 appsess *asession = NULL;
7903 /* only do insert, if lookup fails */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007904 asession = appsession_hash_lookup(&(s->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007905 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007906 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007907 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7908 Alert("Not enough Memory process_srv():asession:calloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007909 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007910 return;
7911 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007912 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7913
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007914 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7915 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007916 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7917 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007918 return;
7919 }
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007920 memcpy(asession->sessid, txn->sessid, s->be->appsession_len);
7921 asession->sessid[s->be->appsession_len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007922
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007923 server_id_len = strlen(objt_server(s->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007924 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007925 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007926 send_log(s->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7927 s->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007928 return;
7929 }
7930 asession->serverid[0] = '\0';
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007931 memcpy(asession->serverid, objt_server(s->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007932
7933 asession->request_count = 0;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007934 appsession_hash_insert(&(s->be->htbl_proxy), asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007935 }
7936
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007937 asession->expire = tick_add_ifset(now_ms, s->be->timeout.appsession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007938 asession->request_count++;
7939 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007940}
7941
7942
Willy Tarreaua15645d2007-03-18 16:22:39 +01007943/*
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007944 * Check if response is cacheable or not. Updates s->flags.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007945 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007946void check_response_for_cacheability(struct session *s, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007947{
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02007948 struct http_txn *txn = &s->txn;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007949 char *p1, *p2;
7950
7951 char *cur_ptr, *cur_end, *cur_next;
7952 int cur_idx;
7953
Willy Tarreau5df51872007-11-25 16:20:08 +01007954 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007955 return;
7956
7957 /* Iterate through the headers.
7958 * we start with the start line.
7959 */
7960 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007961 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007962
7963 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7964 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007965 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007966
7967 cur_hdr = &txn->hdr_idx.v[cur_idx];
7968 cur_ptr = cur_next;
7969 cur_end = cur_ptr + cur_hdr->len;
7970 cur_next = cur_end + cur_hdr->cr + 1;
7971
7972 /* We have one full header between cur_ptr and cur_end, and the
7973 * next header starts at cur_next. We're only interested in
7974 * "Cookie:" headers.
7975 */
7976
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007977 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7978 if (val) {
7979 if ((cur_end - (cur_ptr + val) >= 8) &&
7980 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7981 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7982 return;
7983 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007984 }
7985
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007986 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7987 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007988 continue;
7989
7990 /* OK, right now we know we have a cache-control header at cur_ptr */
7991
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007992 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007993
7994 if (p1 >= cur_end) /* no more info */
7995 continue;
7996
7997 /* p1 is at the beginning of the value */
7998 p2 = p1;
7999
Willy Tarreau8f8e6452007-06-17 21:51:38 +02008000 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01008001 p2++;
8002
8003 /* we have a complete value between p1 and p2 */
8004 if (p2 < cur_end && *p2 == '=') {
8005 /* we have something of the form no-cache="set-cookie" */
8006 if ((cur_end - p1 >= 21) &&
8007 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8008 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008009 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008010 continue;
8011 }
8012
8013 /* OK, so we know that either p2 points to the end of string or to a comma */
8014 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008015 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008016 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8017 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8018 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008019 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008020 return;
8021 }
8022
8023 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008024 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008025 continue;
8026 }
8027 }
8028}
8029
8030
Willy Tarreau58f10d72006-12-04 02:26:12 +01008031/*
8032 * Try to retrieve a known appsession in the URI, then the associated server.
8033 * If the server is found, it's assigned to the session.
8034 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008035void get_srv_from_appsession(struct session *s, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008036{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008037 char *end_params, *first_param, *cur_param, *next_param;
8038 char separator;
8039 int value_len;
8040
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008041 int mode = s->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008042
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008043 if (s->be->appsession_name == NULL ||
8044 (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 +01008045 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008046 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008047
Cyril Bontéb21570a2009-11-29 20:04:48 +01008048 first_param = NULL;
8049 switch (mode) {
8050 case PR_O2_AS_M_PP:
8051 first_param = memchr(begin, ';', len);
8052 break;
8053 case PR_O2_AS_M_QS:
8054 first_param = memchr(begin, '?', len);
8055 break;
8056 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008057
Cyril Bontéb21570a2009-11-29 20:04:48 +01008058 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008059 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008060 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008061
Cyril Bontéb21570a2009-11-29 20:04:48 +01008062 switch (mode) {
8063 case PR_O2_AS_M_PP:
8064 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8065 end_params = (char *) begin + len;
8066 }
8067 separator = ';';
8068 break;
8069 case PR_O2_AS_M_QS:
8070 end_params = (char *) begin + len;
8071 separator = '&';
8072 break;
8073 default:
8074 /* unknown mode, shouldn't happen */
8075 return;
8076 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008077
Cyril Bontéb21570a2009-11-29 20:04:48 +01008078 cur_param = next_param = end_params;
8079 while (cur_param > first_param) {
8080 cur_param--;
8081 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8082 /* let's see if this is the appsession parameter */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008083 if ((cur_param + s->be->appsession_name_len + 1 < next_param) &&
8084 ((s->be->options2 & PR_O2_AS_PFX) || cur_param[s->be->appsession_name_len + 1] == '=') &&
8085 (strncasecmp(cur_param + 1, s->be->appsession_name, s->be->appsession_name_len) == 0)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01008086 /* Cool... it's the right one */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008087 cur_param += s->be->appsession_name_len + (s->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8088 value_len = MIN(s->be->appsession_len, next_param - cur_param);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008089 if (value_len > 0) {
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008090 manage_client_side_appsession(s, cur_param, value_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01008091 }
8092 break;
8093 }
8094 next_param = cur_param;
8095 }
8096 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008097#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008098 Alert("get_srv_from_appsession\n");
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008099 appsession_hash_dump(&(s->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008100#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008101}
8102
Willy Tarreaub2513902006-12-17 14:52:38 +01008103/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008104 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008105 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008106 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008107 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008108 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008109 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008110 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008111 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008112int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008113{
8114 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008115 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008116 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008117
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008118 if (!uri_auth)
8119 return 0;
8120
Cyril Bonté70be45d2010-10-12 00:14:35 +02008121 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008122 return 0;
8123
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008124 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008125 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008126 return 0;
8127
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008128 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008129 return 0;
8130
Willy Tarreaub2513902006-12-17 14:52:38 +01008131 return 1;
8132}
8133
Willy Tarreau4076a152009-04-02 15:18:36 +02008134/*
8135 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008136 * By default it tries to report the error position as msg->err_pos. However if
8137 * this one is not set, it will then report msg->next, which is the last known
8138 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008139 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008140 */
8141void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008142 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008143 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008144{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008145 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008146 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008147
Willy Tarreau9b28e032012-10-12 23:49:43 +02008148 es->len = MIN(chn->buf->i, sizeof(es->buf));
8149 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008150 len1 = MIN(len1, es->len);
8151 len2 = es->len - len1; /* remaining data if buffer wraps */
8152
Willy Tarreau9b28e032012-10-12 23:49:43 +02008153 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008154 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008155 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008156
Willy Tarreau4076a152009-04-02 15:18:36 +02008157 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008158 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008159 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008160 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008161
Willy Tarreau4076a152009-04-02 15:18:36 +02008162 es->when = date; // user-visible date
8163 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008164 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008165 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008166 if (objt_conn(s->req->prod->end))
8167 es->src = __objt_conn(s->req->prod->end)->addr.from;
8168 else
8169 memset(&es->src, 0, sizeof(es->src));
8170
Willy Tarreau078272e2010-12-12 12:46:33 +01008171 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008172 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008173 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008174 es->s_flags = s->flags;
8175 es->t_flags = s->txn.flags;
8176 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008177 es->b_out = chn->buf->o;
8178 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008179 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008180 es->m_clen = msg->chunk_len;
8181 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008182}
Willy Tarreaub2513902006-12-17 14:52:38 +01008183
Willy Tarreau294c4732011-12-16 21:35:50 +01008184/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8185 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8186 * performed over the whole headers. Otherwise it must contain a valid header
8187 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8188 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8189 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8190 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008191 * -1. The value fetch stops at commas, so this function is suited for use with
8192 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008193 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008194 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008195unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008196 struct hdr_idx *idx, int occ,
8197 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008198{
Willy Tarreau294c4732011-12-16 21:35:50 +01008199 struct hdr_ctx local_ctx;
8200 char *ptr_hist[MAX_HDR_HISTORY];
8201 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008202 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008203 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008204
Willy Tarreau294c4732011-12-16 21:35:50 +01008205 if (!ctx) {
8206 local_ctx.idx = 0;
8207 ctx = &local_ctx;
8208 }
8209
Willy Tarreaubce70882009-09-07 11:51:47 +02008210 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008211 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008212 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008213 occ--;
8214 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008215 *vptr = ctx->line + ctx->val;
8216 *vlen = ctx->vlen;
8217 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008218 }
8219 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008220 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008221 }
8222
8223 /* negative occurrence, we scan all the list then walk back */
8224 if (-occ > MAX_HDR_HISTORY)
8225 return 0;
8226
Willy Tarreau294c4732011-12-16 21:35:50 +01008227 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008228 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008229 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8230 len_hist[hist_ptr] = ctx->vlen;
8231 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008232 hist_ptr = 0;
8233 found++;
8234 }
8235 if (-occ > found)
8236 return 0;
8237 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008238 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8239 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8240 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008241 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008242 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008243 if (hist_ptr >= MAX_HDR_HISTORY)
8244 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008245 *vptr = ptr_hist[hist_ptr];
8246 *vlen = len_hist[hist_ptr];
8247 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008248}
8249
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008250/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8251 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8252 * performed over the whole headers. Otherwise it must contain a valid header
8253 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8254 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8255 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8256 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8257 * -1. This function differs from http_get_hdr() in that it only returns full
8258 * line header values and does not stop at commas.
8259 * The return value is 0 if nothing was found, or non-zero otherwise.
8260 */
8261unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8262 struct hdr_idx *idx, int occ,
8263 struct hdr_ctx *ctx, char **vptr, int *vlen)
8264{
8265 struct hdr_ctx local_ctx;
8266 char *ptr_hist[MAX_HDR_HISTORY];
8267 int len_hist[MAX_HDR_HISTORY];
8268 unsigned int hist_ptr;
8269 int found;
8270
8271 if (!ctx) {
8272 local_ctx.idx = 0;
8273 ctx = &local_ctx;
8274 }
8275
8276 if (occ >= 0) {
8277 /* search from the beginning */
8278 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8279 occ--;
8280 if (occ <= 0) {
8281 *vptr = ctx->line + ctx->val;
8282 *vlen = ctx->vlen;
8283 return 1;
8284 }
8285 }
8286 return 0;
8287 }
8288
8289 /* negative occurrence, we scan all the list then walk back */
8290 if (-occ > MAX_HDR_HISTORY)
8291 return 0;
8292
8293 found = hist_ptr = 0;
8294 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8295 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8296 len_hist[hist_ptr] = ctx->vlen;
8297 if (++hist_ptr >= MAX_HDR_HISTORY)
8298 hist_ptr = 0;
8299 found++;
8300 }
8301 if (-occ > found)
8302 return 0;
8303 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8304 * find occurrence -occ, so we have to check [hist_ptr+occ].
8305 */
8306 hist_ptr += occ;
8307 if (hist_ptr >= MAX_HDR_HISTORY)
8308 hist_ptr -= MAX_HDR_HISTORY;
8309 *vptr = ptr_hist[hist_ptr];
8310 *vlen = len_hist[hist_ptr];
8311 return 1;
8312}
8313
Willy Tarreaubaaee002006-06-26 02:48:02 +02008314/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008315 * Print a debug line with a header. Always stop at the first CR or LF char,
8316 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8317 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008318 */
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008319void debug_hdr(const char *dir, struct session *s, const char *start, const char *end)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008320{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008321 int max;
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008322 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", s->uniq_id, s->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008323 dir,
Willy Tarreauf1fd9dc2014-04-24 20:47:57 +02008324 objt_conn(s->req->prod->end) ? (unsigned short)objt_conn(s->req->prod->end)->t.sock.fd : -1,
8325 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 +02008326
8327 for (max = 0; start + max < end; max++)
8328 if (start[max] == '\r' || start[max] == '\n')
8329 break;
8330
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008331 UBOUND(max, trash.size - trash.len - 3);
8332 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8333 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008334 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008335}
8336
Willy Tarreau0937bc42009-12-22 15:03:09 +01008337/*
8338 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8339 * the required fields are properly allocated and that we only need to (re)init
8340 * them. This should be used before processing any new request.
8341 */
8342void http_init_txn(struct session *s)
8343{
8344 struct http_txn *txn = &s->txn;
8345 struct proxy *fe = s->fe;
8346
8347 txn->flags = 0;
8348 txn->status = -1;
8349
Willy Tarreauf64d1412010-10-07 20:06:11 +02008350 txn->cookie_first_date = 0;
8351 txn->cookie_last_date = 0;
8352
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008353 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008354 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008355 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008356 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008357 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008358 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008359 txn->req.chunk_len = 0LL;
8360 txn->req.body_len = 0LL;
8361 txn->rsp.chunk_len = 0LL;
8362 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008363 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8364 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008365 txn->req.chn = s->req;
8366 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008367
8368 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008369
8370 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8371 if (fe->options2 & PR_O2_REQBUG_OK)
8372 txn->req.err_pos = -1; /* let buggy requests pass */
8373
Willy Tarreau46023632010-01-07 22:51:47 +01008374 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008375 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8376
Willy Tarreau46023632010-01-07 22:51:47 +01008377 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008378 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8379
8380 if (txn->hdr_idx.v)
8381 hdr_idx_init(&txn->hdr_idx);
8382}
8383
8384/* to be used at the end of a transaction */
8385void http_end_txn(struct session *s)
8386{
8387 struct http_txn *txn = &s->txn;
8388
Willy Tarreau75195602014-03-11 15:48:55 +01008389 /* release any possible compression context */
8390 if (s->flags & SN_COMP_READY)
8391 s->comp_algo->end(&s->comp_ctx);
8392 s->comp_algo = NULL;
8393 s->flags &= ~SN_COMP_READY;
8394
Willy Tarreau0937bc42009-12-22 15:03:09 +01008395 /* these ones will have been dynamically allocated */
8396 pool_free2(pool2_requri, txn->uri);
8397 pool_free2(pool2_capture, txn->cli_cookie);
8398 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008399 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008400 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008401
William Lallemanda73203e2012-03-12 12:48:57 +01008402 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008403 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008404 txn->uri = NULL;
8405 txn->srv_cookie = NULL;
8406 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008407
8408 if (txn->req.cap) {
8409 struct cap_hdr *h;
8410 for (h = s->fe->req_cap; h; h = h->next)
8411 pool_free2(h->pool, txn->req.cap[h->index]);
8412 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8413 }
8414
8415 if (txn->rsp.cap) {
8416 struct cap_hdr *h;
8417 for (h = s->fe->rsp_cap; h; h = h->next)
8418 pool_free2(h->pool, txn->rsp.cap[h->index]);
8419 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8420 }
8421
Willy Tarreau0937bc42009-12-22 15:03:09 +01008422}
8423
8424/* to be used at the end of a transaction to prepare a new one */
8425void http_reset_txn(struct session *s)
8426{
8427 http_end_txn(s);
8428 http_init_txn(s);
8429
8430 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008431 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008432 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008433 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008434 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008435 /* re-init store persistence */
8436 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008437 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008438
Willy Tarreau0937bc42009-12-22 15:03:09 +01008439 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008440
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008441 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008442
Willy Tarreau739cfba2010-01-25 23:11:14 +01008443 /* We must trim any excess data from the response buffer, because we
8444 * may have blocked an invalid response from a server that we don't
8445 * want to accidentely forward once we disable the analysers, nor do
8446 * we want those data to come along with next response. A typical
8447 * example of such data would be from a buggy server responding to
8448 * a HEAD with some data, or sending more than the advertised
8449 * content-length.
8450 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008451 if (unlikely(s->rep->buf->i))
8452 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008453
Willy Tarreau0937bc42009-12-22 15:03:09 +01008454 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008455 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008456
Willy Tarreaud04e8582010-05-31 12:31:35 +02008457 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008458 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008459
8460 s->req->rex = TICK_ETERNITY;
8461 s->req->wex = TICK_ETERNITY;
8462 s->req->analyse_exp = TICK_ETERNITY;
8463 s->rep->rex = TICK_ETERNITY;
8464 s->rep->wex = TICK_ETERNITY;
8465 s->rep->analyse_exp = TICK_ETERNITY;
8466}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008467
Willy Tarreauff011f22011-01-06 17:51:27 +01008468void free_http_req_rules(struct list *r) {
8469 struct http_req_rule *tr, *pr;
8470
8471 list_for_each_entry_safe(pr, tr, r, list) {
8472 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008473 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008474 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008475
8476 free(pr);
8477 }
8478}
8479
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008480/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008481struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8482{
8483 struct http_req_rule *rule;
8484 int cur_arg;
8485
8486 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8487 if (!rule) {
8488 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008489 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008490 }
8491
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008492 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008493 rule->action = HTTP_REQ_ACT_ALLOW;
8494 cur_arg = 1;
8495 } else if (!strcmp(args[0], "deny")) {
8496 rule->action = HTTP_REQ_ACT_DENY;
8497 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008498 } else if (!strcmp(args[0], "tarpit")) {
8499 rule->action = HTTP_REQ_ACT_TARPIT;
8500 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008501 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008502 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008503 cur_arg = 1;
8504
8505 while(*args[cur_arg]) {
8506 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008507 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008508 cur_arg+=2;
8509 continue;
8510 } else
8511 break;
8512 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008513 } else if (!strcmp(args[0], "set-nice")) {
8514 rule->action = HTTP_REQ_ACT_SET_NICE;
8515 cur_arg = 1;
8516
8517 if (!*args[cur_arg] ||
8518 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8519 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8520 file, linenum, args[0]);
8521 goto out_err;
8522 }
8523 rule->arg.nice = atoi(args[cur_arg]);
8524 if (rule->arg.nice < -1024)
8525 rule->arg.nice = -1024;
8526 else if (rule->arg.nice > 1024)
8527 rule->arg.nice = 1024;
8528 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008529 } else if (!strcmp(args[0], "set-tos")) {
8530#ifdef IP_TOS
8531 char *err;
8532 rule->action = HTTP_REQ_ACT_SET_TOS;
8533 cur_arg = 1;
8534
8535 if (!*args[cur_arg] ||
8536 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8537 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8538 file, linenum, args[0]);
8539 goto out_err;
8540 }
8541
8542 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8543 if (err && *err != '\0') {
8544 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8545 file, linenum, err, args[0]);
8546 goto out_err;
8547 }
8548 cur_arg++;
8549#else
8550 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8551 goto out_err;
8552#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008553 } else if (!strcmp(args[0], "set-mark")) {
8554#ifdef SO_MARK
8555 char *err;
8556 rule->action = HTTP_REQ_ACT_SET_MARK;
8557 cur_arg = 1;
8558
8559 if (!*args[cur_arg] ||
8560 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8561 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8562 file, linenum, args[0]);
8563 goto out_err;
8564 }
8565
8566 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8567 if (err && *err != '\0') {
8568 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8569 file, linenum, err, args[0]);
8570 goto out_err;
8571 }
8572 cur_arg++;
8573 global.last_checks |= LSTCHK_NETADM;
8574#else
8575 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8576 goto out_err;
8577#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008578 } else if (!strcmp(args[0], "set-log-level")) {
8579 rule->action = HTTP_REQ_ACT_SET_LOGL;
8580 cur_arg = 1;
8581
8582 if (!*args[cur_arg] ||
8583 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8584 bad_log_level:
8585 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8586 file, linenum, args[0]);
8587 goto out_err;
8588 }
8589 if (strcmp(args[cur_arg], "silent") == 0)
8590 rule->arg.loglevel = -1;
8591 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8592 goto bad_log_level;
8593 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008594 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8595 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8596 cur_arg = 1;
8597
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008598 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8599 (*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 +01008600 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8601 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008602 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008603 }
8604
8605 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8606 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8607 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008608
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008609 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008610 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008611 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8612 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008613 free(proxy->conf.lfs_file);
8614 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8615 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008616 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008617 } else if (strcmp(args[0], "del-header") == 0) {
8618 rule->action = HTTP_REQ_ACT_DEL_HDR;
8619 cur_arg = 1;
8620
8621 if (!*args[cur_arg] ||
8622 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8623 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8624 file, linenum, args[0]);
8625 goto out_err;
8626 }
8627
8628 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8629 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8630
8631 proxy->conf.args.ctx = ARGC_HRQ;
8632 free(proxy->conf.lfs_file);
8633 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8634 proxy->conf.lfs_line = proxy->conf.args.line;
8635 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008636 } else if (strcmp(args[0], "redirect") == 0) {
8637 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008638 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008639
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008640 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008641 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8642 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8643 goto out_err;
8644 }
8645
8646 /* this redirect rule might already contain a parsed condition which
8647 * we'll pass to the http-request rule.
8648 */
8649 rule->action = HTTP_REQ_ACT_REDIR;
8650 rule->arg.redir = redir;
8651 rule->cond = redir->cond;
8652 redir->cond = NULL;
8653 cur_arg = 2;
8654 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008655 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008656 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', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008657 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008658 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008659 }
8660
8661 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8662 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008663 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008664
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008665 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8666 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8667 file, linenum, args[0], errmsg);
8668 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008669 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008670 }
8671 rule->cond = cond;
8672 }
8673 else if (*args[cur_arg]) {
8674 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8675 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8676 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008677 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008678 }
8679
8680 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008681 out_err:
8682 free(rule);
8683 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008684}
8685
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008686/* parse an "http-respose" rule */
8687struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8688{
8689 struct http_res_rule *rule;
8690 int cur_arg;
8691
8692 rule = calloc(1, sizeof(*rule));
8693 if (!rule) {
8694 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8695 goto out_err;
8696 }
8697
8698 if (!strcmp(args[0], "allow")) {
8699 rule->action = HTTP_RES_ACT_ALLOW;
8700 cur_arg = 1;
8701 } else if (!strcmp(args[0], "deny")) {
8702 rule->action = HTTP_RES_ACT_DENY;
8703 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008704 } else if (!strcmp(args[0], "set-nice")) {
8705 rule->action = HTTP_RES_ACT_SET_NICE;
8706 cur_arg = 1;
8707
8708 if (!*args[cur_arg] ||
8709 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8710 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8711 file, linenum, args[0]);
8712 goto out_err;
8713 }
8714 rule->arg.nice = atoi(args[cur_arg]);
8715 if (rule->arg.nice < -1024)
8716 rule->arg.nice = -1024;
8717 else if (rule->arg.nice > 1024)
8718 rule->arg.nice = 1024;
8719 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008720 } else if (!strcmp(args[0], "set-tos")) {
8721#ifdef IP_TOS
8722 char *err;
8723 rule->action = HTTP_RES_ACT_SET_TOS;
8724 cur_arg = 1;
8725
8726 if (!*args[cur_arg] ||
8727 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8728 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8729 file, linenum, args[0]);
8730 goto out_err;
8731 }
8732
8733 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8734 if (err && *err != '\0') {
8735 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8736 file, linenum, err, args[0]);
8737 goto out_err;
8738 }
8739 cur_arg++;
8740#else
8741 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8742 goto out_err;
8743#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008744 } else if (!strcmp(args[0], "set-mark")) {
8745#ifdef SO_MARK
8746 char *err;
8747 rule->action = HTTP_RES_ACT_SET_MARK;
8748 cur_arg = 1;
8749
8750 if (!*args[cur_arg] ||
8751 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8752 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8753 file, linenum, args[0]);
8754 goto out_err;
8755 }
8756
8757 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8758 if (err && *err != '\0') {
8759 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8760 file, linenum, err, args[0]);
8761 goto out_err;
8762 }
8763 cur_arg++;
8764 global.last_checks |= LSTCHK_NETADM;
8765#else
8766 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8767 goto out_err;
8768#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008769 } else if (!strcmp(args[0], "set-log-level")) {
8770 rule->action = HTTP_RES_ACT_SET_LOGL;
8771 cur_arg = 1;
8772
8773 if (!*args[cur_arg] ||
8774 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8775 bad_log_level:
8776 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8777 file, linenum, args[0]);
8778 goto out_err;
8779 }
8780 if (strcmp(args[cur_arg], "silent") == 0)
8781 rule->arg.loglevel = -1;
8782 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8783 goto bad_log_level;
8784 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008785 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8786 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8787 cur_arg = 1;
8788
8789 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8790 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8791 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8792 file, linenum, args[0]);
8793 goto out_err;
8794 }
8795
8796 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8797 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8798 LIST_INIT(&rule->arg.hdr_add.fmt);
8799
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008800 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008801 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008802 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
8803 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008804 free(proxy->conf.lfs_file);
8805 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8806 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008807 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008808 } else if (strcmp(args[0], "del-header") == 0) {
8809 rule->action = HTTP_RES_ACT_DEL_HDR;
8810 cur_arg = 1;
8811
8812 if (!*args[cur_arg] ||
8813 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8814 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8815 file, linenum, args[0]);
8816 goto out_err;
8817 }
8818
8819 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8820 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8821
8822 proxy->conf.args.ctx = ARGC_HRS;
8823 free(proxy->conf.lfs_file);
8824 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8825 proxy->conf.lfs_line = proxy->conf.args.line;
8826 cur_arg += 1;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008827 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008828 Alert("parsing [%s:%d]: 'http-response' expects 'allow', 'deny', 'redirect', 'add-header', 'set-header', 'set-nice', 'set-tos', 'set-mark', 'set-log-level', but got '%s'%s.\n",
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008829 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8830 goto out_err;
8831 }
8832
8833 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8834 struct acl_cond *cond;
8835 char *errmsg = NULL;
8836
8837 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8838 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8839 file, linenum, args[0], errmsg);
8840 free(errmsg);
8841 goto out_err;
8842 }
8843 rule->cond = cond;
8844 }
8845 else if (*args[cur_arg]) {
8846 Alert("parsing [%s:%d]: 'http-response %s' expects"
8847 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8848 file, linenum, args[0], args[cur_arg]);
8849 goto out_err;
8850 }
8851
8852 return rule;
8853 out_err:
8854 free(rule);
8855 return NULL;
8856}
8857
Willy Tarreau4baae242012-12-27 12:00:31 +01008858/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008859 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8860 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008861 */
8862struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008863 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008864{
8865 struct redirect_rule *rule;
8866 int cur_arg;
8867 int type = REDIRECT_TYPE_NONE;
8868 int code = 302;
8869 const char *destination = NULL;
8870 const char *cookie = NULL;
8871 int cookie_set = 0;
8872 unsigned int flags = REDIRECT_FLAG_NONE;
8873 struct acl_cond *cond = NULL;
8874
8875 cur_arg = 0;
8876 while (*(args[cur_arg])) {
8877 if (strcmp(args[cur_arg], "location") == 0) {
8878 if (!*args[cur_arg + 1])
8879 goto missing_arg;
8880
8881 type = REDIRECT_TYPE_LOCATION;
8882 cur_arg++;
8883 destination = args[cur_arg];
8884 }
8885 else if (strcmp(args[cur_arg], "prefix") == 0) {
8886 if (!*args[cur_arg + 1])
8887 goto missing_arg;
8888
8889 type = REDIRECT_TYPE_PREFIX;
8890 cur_arg++;
8891 destination = args[cur_arg];
8892 }
8893 else if (strcmp(args[cur_arg], "scheme") == 0) {
8894 if (!*args[cur_arg + 1])
8895 goto missing_arg;
8896
8897 type = REDIRECT_TYPE_SCHEME;
8898 cur_arg++;
8899 destination = args[cur_arg];
8900 }
8901 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8902 if (!*args[cur_arg + 1])
8903 goto missing_arg;
8904
8905 cur_arg++;
8906 cookie = args[cur_arg];
8907 cookie_set = 1;
8908 }
8909 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8910 if (!*args[cur_arg + 1])
8911 goto missing_arg;
8912
8913 cur_arg++;
8914 cookie = args[cur_arg];
8915 cookie_set = 0;
8916 }
8917 else if (strcmp(args[cur_arg], "code") == 0) {
8918 if (!*args[cur_arg + 1])
8919 goto missing_arg;
8920
8921 cur_arg++;
8922 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008923 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008924 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008925 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008926 args[cur_arg - 1], args[cur_arg]);
8927 return NULL;
8928 }
8929 }
8930 else if (!strcmp(args[cur_arg],"drop-query")) {
8931 flags |= REDIRECT_FLAG_DROP_QS;
8932 }
8933 else if (!strcmp(args[cur_arg],"append-slash")) {
8934 flags |= REDIRECT_FLAG_APPEND_SLASH;
8935 }
8936 else if (strcmp(args[cur_arg], "if") == 0 ||
8937 strcmp(args[cur_arg], "unless") == 0) {
8938 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8939 if (!cond) {
8940 memprintf(errmsg, "error in condition: %s", *errmsg);
8941 return NULL;
8942 }
8943 break;
8944 }
8945 else {
8946 memprintf(errmsg,
8947 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8948 args[cur_arg]);
8949 return NULL;
8950 }
8951 cur_arg++;
8952 }
8953
8954 if (type == REDIRECT_TYPE_NONE) {
8955 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8956 return NULL;
8957 }
8958
8959 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8960 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008961 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008962
8963 if (!use_fmt) {
8964 /* old-style static redirect rule */
8965 rule->rdr_str = strdup(destination);
8966 rule->rdr_len = strlen(destination);
8967 }
8968 else {
8969 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008970
8971 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8972 * if prefix == "/", we don't want to add anything, otherwise it
8973 * makes it hard for the user to configure a self-redirection.
8974 */
8975 proxy->conf.args.ctx = ARGC_RDR;
8976 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008977 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008978 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8979 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008980 free(curproxy->conf.lfs_file);
8981 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
8982 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008983 }
8984 }
8985
Willy Tarreau4baae242012-12-27 12:00:31 +01008986 if (cookie) {
8987 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8988 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8989 */
8990 rule->cookie_len = strlen(cookie);
8991 if (cookie_set) {
8992 rule->cookie_str = malloc(rule->cookie_len + 10);
8993 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8994 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8995 rule->cookie_len += 9;
8996 } else {
8997 rule->cookie_str = malloc(rule->cookie_len + 21);
8998 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8999 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
9000 rule->cookie_len += 20;
9001 }
9002 }
9003 rule->type = type;
9004 rule->code = code;
9005 rule->flags = flags;
9006 LIST_INIT(&rule->list);
9007 return rule;
9008
9009 missing_arg:
9010 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9011 return NULL;
9012}
9013
Willy Tarreau8797c062007-05-07 00:55:35 +02009014/************************************************************************/
9015/* The code below is dedicated to ACL parsing and matching */
9016/************************************************************************/
9017
9018
Willy Tarreau14174bc2012-04-16 14:34:04 +02009019/* This function ensures that the prerequisites for an L7 fetch are ready,
9020 * which means that a request or response is ready. If some data is missing,
9021 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009022 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9023 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009024 *
9025 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009026 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9027 * decide whether or not an HTTP message is present ;
9028 * 0 if the requested data cannot be fetched or if it is certain that
9029 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009030 * 1 if an HTTP message is ready
9031 */
9032static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009033smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009034 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009035{
9036 struct http_txn *txn = l7;
9037 struct http_msg *msg = &txn->req;
9038
9039 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9040 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9041 */
9042
9043 if (unlikely(!s || !txn))
9044 return 0;
9045
9046 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009047 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009048
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009049 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009050 if (unlikely(!s->req))
9051 return 0;
9052
Willy Tarreauaae75e32013-03-29 12:31:49 +01009053 /* If the buffer does not leave enough free space at the end,
9054 * we must first realign it.
9055 */
9056 if (s->req->buf->p > s->req->buf->data &&
9057 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9058 buffer_slow_realign(s->req->buf);
9059
Willy Tarreau14174bc2012-04-16 14:34:04 +02009060 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009061 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009062 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009063
9064 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009065 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009066 http_msg_analyzer(msg, &txn->hdr_idx);
9067
9068 /* Still no valid request ? */
9069 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009070 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009071 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009072 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009073 }
9074 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009075 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009076 return 0;
9077 }
9078
9079 /* OK we just got a valid HTTP request. We have some minor
9080 * preparation to perform so that further checks can rely
9081 * on HTTP tests.
9082 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009083
9084 /* If the request was parsed but was too large, we must absolutely
9085 * return an error so that it is not processed. At the moment this
9086 * cannot happen, but if the parsers are to change in the future,
9087 * we want this check to be maintained.
9088 */
9089 if (unlikely(s->req->buf->i + s->req->buf->p >
9090 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9091 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009092 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009093 return 1;
9094 }
9095
Willy Tarreau9b28e032012-10-12 23:49:43 +02009096 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009097 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9098 s->flags |= SN_REDIRECTABLE;
9099
Willy Tarreau506d0502013-07-06 13:29:24 +02009100 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9101 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009102 }
9103
Willy Tarreau506d0502013-07-06 13:29:24 +02009104 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009105 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009106 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009107
9108 /* otherwise everything's ready for the request */
9109 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009110 else {
9111 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009112 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9113 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009114 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009115 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009116 }
9117
9118 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009119 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009120 return 1;
9121}
Willy Tarreau8797c062007-05-07 00:55:35 +02009122
Willy Tarreauc0239e02012-04-16 14:42:55 +02009123#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009124 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 +02009125
Willy Tarreau24e32d82012-04-23 23:55:44 +02009126#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009127 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 +02009128
Willy Tarreau8797c062007-05-07 00:55:35 +02009129
9130/* 1. Check on METHOD
9131 * We use the pre-parsed method if it is known, and store its number as an
9132 * integer. If it is unknown, we use the pointer and the length.
9133 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009134static int pat_parse_meth(const char *text, struct pattern *pattern, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009135{
9136 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009137 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02009138
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009139 len = strlen(text);
9140 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009141
9142 pattern->val.i = meth;
9143 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009144 trash = get_trash_chunk();
9145 if (trash->size < len) {
9146 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
9147 len, trash->size);
9148 return 0;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009149 }
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009150 pattern->ptr.str = trash->str;
Willy Tarreau8797c062007-05-07 00:55:35 +02009151 pattern->len = len;
9152 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009153 else {
9154 pattern->ptr.str = NULL;
9155 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009156 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009157 return 1;
9158}
9159
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009160/* This function fetches the method of current HTTP request and stores
9161 * it in the global pattern struct as a chunk. There are two possibilities :
9162 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9163 * in <len> and <ptr> is NULL ;
9164 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9165 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009166 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009167 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009168static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009169smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009170 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009171{
9172 int meth;
9173 struct http_txn *txn = l7;
9174
Willy Tarreau24e32d82012-04-23 23:55:44 +02009175 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009176
Willy Tarreau8797c062007-05-07 00:55:35 +02009177 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009178 smp->type = SMP_T_METH;
9179 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009180 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009181 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9182 /* ensure the indexes are not affected */
9183 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009184 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009185 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9186 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009187 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009188 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009189 return 1;
9190}
9191
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009192/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009193static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009194{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009195 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009196 struct pattern_list *lst;
9197 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009198
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009199 list_for_each_entry(lst, &expr->patterns, list) {
9200 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009201
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009202 /* well-known method */
9203 if (pattern->val.i != HTTP_METH_OTHER) {
9204 if (smp->data.meth.meth == pattern->val.i)
9205 return pattern;
9206 else
9207 continue;
9208 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009209
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009210 /* Other method, we must compare the strings */
9211 if (pattern->len != smp->data.meth.str.len)
9212 continue;
9213
9214 icase = pattern->flags & PAT_F_IGNORE_CASE;
9215 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0) ||
9216 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0))
9217 return pattern;
9218 }
9219 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009220}
9221
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009222static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009223smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009224 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009225{
9226 struct http_txn *txn = l7;
9227 char *ptr;
9228 int len;
9229
Willy Tarreauc0239e02012-04-16 14:42:55 +02009230 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009231
Willy Tarreau8797c062007-05-07 00:55:35 +02009232 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009233 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009234
9235 while ((len-- > 0) && (*ptr++ != '/'));
9236 if (len <= 0)
9237 return 0;
9238
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009239 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009240 smp->data.str.str = ptr;
9241 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009242
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009243 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009244 return 1;
9245}
9246
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009247static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009248smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009249 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009250{
9251 struct http_txn *txn = l7;
9252 char *ptr;
9253 int len;
9254
Willy Tarreauc0239e02012-04-16 14:42:55 +02009255 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009256
Willy Tarreauf26b2522012-12-14 08:33:14 +01009257 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9258 return 0;
9259
Willy Tarreau8797c062007-05-07 00:55:35 +02009260 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009261 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009262
9263 while ((len-- > 0) && (*ptr++ != '/'));
9264 if (len <= 0)
9265 return 0;
9266
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009267 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009268 smp->data.str.str = ptr;
9269 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009270
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009271 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009272 return 1;
9273}
9274
9275/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009276static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009277smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009278 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009279{
9280 struct http_txn *txn = l7;
9281 char *ptr;
9282 int len;
9283
Willy Tarreauc0239e02012-04-16 14:42:55 +02009284 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009285
Willy Tarreauf26b2522012-12-14 08:33:14 +01009286 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9287 return 0;
9288
Willy Tarreau8797c062007-05-07 00:55:35 +02009289 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009290 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009291
Willy Tarreauf853c462012-04-23 18:53:56 +02009292 smp->type = SMP_T_UINT;
9293 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009294 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009295 return 1;
9296}
9297
9298/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009299static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009300smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009301 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009302{
9303 struct http_txn *txn = l7;
9304
Willy Tarreauc0239e02012-04-16 14:42:55 +02009305 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009306
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009307 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009308 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009309 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009310 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009311 return 1;
9312}
9313
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009314static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009315smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009316 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009317{
9318 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009319 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009320
Willy Tarreauc0239e02012-04-16 14:42:55 +02009321 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009322
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009323 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 +02009324 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009325 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009326
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009327 smp->type = SMP_T_IPV4;
9328 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009329 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009330 return 1;
9331}
9332
9333static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009334smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009335 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009336{
9337 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009338 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009339
Willy Tarreauc0239e02012-04-16 14:42:55 +02009340 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009341
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009342 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 +02009343 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9344 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009345
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009346 smp->type = SMP_T_UINT;
9347 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009348 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009349 return 1;
9350}
9351
Willy Tarreau185b5c42012-04-26 15:11:51 +02009352/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9353 * Accepts an optional argument of type string containing the header field name,
9354 * and an optional argument of type signed or unsigned integer to request an
9355 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009356 * headers are considered from the first one. It does not stop on commas and
9357 * returns full lines instead (useful for User-Agent or Date for example).
9358 */
9359static int
9360smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009361 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009362{
9363 struct http_txn *txn = l7;
9364 struct hdr_idx *idx = &txn->hdr_idx;
9365 struct hdr_ctx *ctx = smp->ctx.a[0];
9366 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9367 int occ = 0;
9368 const char *name_str = NULL;
9369 int name_len = 0;
9370
9371 if (!ctx) {
9372 /* first call */
9373 ctx = &static_hdr_ctx;
9374 ctx->idx = 0;
9375 smp->ctx.a[0] = ctx;
9376 }
9377
9378 if (args) {
9379 if (args[0].type != ARGT_STR)
9380 return 0;
9381 name_str = args[0].data.str.str;
9382 name_len = args[0].data.str.len;
9383
9384 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9385 occ = args[1].data.uint;
9386 }
9387
9388 CHECK_HTTP_MESSAGE_FIRST();
9389
9390 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9391 /* search for header from the beginning */
9392 ctx->idx = 0;
9393
9394 if (!occ && !(opt & SMP_OPT_ITERATE))
9395 /* no explicit occurrence and single fetch => last header by default */
9396 occ = -1;
9397
9398 if (!occ)
9399 /* prepare to report multiple occurrences for ACL fetches */
9400 smp->flags |= SMP_F_NOT_LAST;
9401
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009402 smp->type = SMP_T_STR;
9403 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009404 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9405 return 1;
9406
9407 smp->flags &= ~SMP_F_NOT_LAST;
9408 return 0;
9409}
9410
9411/* 6. Check on HTTP header count. The number of occurrences is returned.
9412 * Accepts exactly 1 argument of type string. It does not stop on commas and
9413 * returns full lines instead (useful for User-Agent or Date for example).
9414 */
9415static int
9416smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009417 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009418{
9419 struct http_txn *txn = l7;
9420 struct hdr_idx *idx = &txn->hdr_idx;
9421 struct hdr_ctx ctx;
9422 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9423 int cnt;
9424
9425 if (!args || args->type != ARGT_STR)
9426 return 0;
9427
9428 CHECK_HTTP_MESSAGE_FIRST();
9429
9430 ctx.idx = 0;
9431 cnt = 0;
9432 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9433 cnt++;
9434
9435 smp->type = SMP_T_UINT;
9436 smp->data.uint = cnt;
9437 smp->flags = SMP_F_VOL_HDR;
9438 return 1;
9439}
9440
9441/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9442 * Accepts an optional argument of type string containing the header field name,
9443 * and an optional argument of type signed or unsigned integer to request an
9444 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009445 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009446 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009447static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009448smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009449 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009450{
9451 struct http_txn *txn = l7;
9452 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009453 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009454 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009455 int occ = 0;
9456 const char *name_str = NULL;
9457 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009458
Willy Tarreaua890d072013-04-02 12:01:06 +02009459 if (!ctx) {
9460 /* first call */
9461 ctx = &static_hdr_ctx;
9462 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009463 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009464 }
9465
Willy Tarreau185b5c42012-04-26 15:11:51 +02009466 if (args) {
9467 if (args[0].type != ARGT_STR)
9468 return 0;
9469 name_str = args[0].data.str.str;
9470 name_len = args[0].data.str.len;
9471
9472 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9473 occ = args[1].data.uint;
9474 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009475
Willy Tarreaue333ec92012-04-16 16:26:40 +02009476 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009477
Willy Tarreau185b5c42012-04-26 15:11:51 +02009478 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009479 /* search for header from the beginning */
9480 ctx->idx = 0;
9481
Willy Tarreau185b5c42012-04-26 15:11:51 +02009482 if (!occ && !(opt & SMP_OPT_ITERATE))
9483 /* no explicit occurrence and single fetch => last header by default */
9484 occ = -1;
9485
9486 if (!occ)
9487 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009488 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009489
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009490 smp->type = SMP_T_STR;
9491 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009492 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 +02009493 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009494
Willy Tarreau37406352012-04-23 16:16:37 +02009495 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009496 return 0;
9497}
9498
Willy Tarreauc11416f2007-06-17 16:58:38 +02009499/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009500 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009501 */
9502static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009503smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009504 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009505{
9506 struct http_txn *txn = l7;
9507 struct hdr_idx *idx = &txn->hdr_idx;
9508 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009509 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009510 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009511
Willy Tarreau24e32d82012-04-23 23:55:44 +02009512 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009513 return 0;
9514
Willy Tarreaue333ec92012-04-16 16:26:40 +02009515 CHECK_HTTP_MESSAGE_FIRST();
9516
Willy Tarreau33a7e692007-06-10 19:45:56 +02009517 ctx.idx = 0;
9518 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009519 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 +02009520 cnt++;
9521
Willy Tarreauf853c462012-04-23 18:53:56 +02009522 smp->type = SMP_T_UINT;
9523 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009524 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009525 return 1;
9526}
9527
Willy Tarreau185b5c42012-04-26 15:11:51 +02009528/* Fetch an HTTP header's integer value. The integer value is returned. It
9529 * takes a mandatory argument of type string and an optional one of type int
9530 * to designate a specific occurrence. It returns an unsigned integer, which
9531 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009532 */
9533static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009534smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009535 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009536{
Willy Tarreauef38c392013-07-22 16:29:32 +02009537 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009538
Willy Tarreauf853c462012-04-23 18:53:56 +02009539 if (ret > 0) {
9540 smp->type = SMP_T_UINT;
9541 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9542 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009543
Willy Tarreaud53e2422012-04-16 17:21:11 +02009544 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009545}
9546
Cyril Bonté69fa9922012-10-25 00:01:06 +02009547/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9548 * and an optional one of type int to designate a specific occurrence.
9549 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009550 */
9551static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009552smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009553 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009554{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009555 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009556
Willy Tarreauef38c392013-07-22 16:29:32 +02009557 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009558 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9559 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009560 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009561 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009562 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009563 if (smp->data.str.len < temp->size - 1) {
9564 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9565 temp->str[smp->data.str.len] = '\0';
9566 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9567 smp->type = SMP_T_IPV6;
9568 break;
9569 }
9570 }
9571 }
9572
Willy Tarreaud53e2422012-04-16 17:21:11 +02009573 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009574 if (!(smp->flags & SMP_F_NOT_LAST))
9575 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009576 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009577 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009578}
9579
Willy Tarreau737b0c12007-06-10 21:28:46 +02009580/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9581 * the first '/' after the possible hostname, and ends before the possible '?'.
9582 */
9583static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009584smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009585 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009586{
9587 struct http_txn *txn = l7;
9588 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009589
Willy Tarreauc0239e02012-04-16 14:42:55 +02009590 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009591
Willy Tarreau9b28e032012-10-12 23:49:43 +02009592 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009593 ptr = http_get_path(txn);
9594 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009595 return 0;
9596
9597 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009598 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009599 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009600
9601 while (ptr < end && *ptr != '?')
9602 ptr++;
9603
Willy Tarreauf853c462012-04-23 18:53:56 +02009604 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009605 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009606 return 1;
9607}
9608
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009609/* This produces a concatenation of the first occurrence of the Host header
9610 * followed by the path component if it begins with a slash ('/'). This means
9611 * that '*' will not be added, resulting in exactly the first Host entry.
9612 * If no Host header is found, then the path is returned as-is. The returned
9613 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009614 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009615 */
9616static int
9617smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009618 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009619{
9620 struct http_txn *txn = l7;
9621 char *ptr, *end, *beg;
9622 struct hdr_ctx ctx;
9623
9624 CHECK_HTTP_MESSAGE_FIRST();
9625
9626 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02009627 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009628 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009629
9630 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009631 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009632 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009633 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009634 smp->data.str.len = ctx.vlen;
9635
9636 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +02009637 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009638 beg = http_get_path(txn);
9639 if (!beg)
9640 beg = end;
9641
9642 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9643
9644 if (beg < ptr && *beg == '/') {
9645 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9646 smp->data.str.len += ptr - beg;
9647 }
9648
9649 smp->flags = SMP_F_VOL_1ST;
9650 return 1;
9651}
9652
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009653/* This produces a 32-bit hash of the concatenation of the first occurrence of
9654 * the Host header followed by the path component if it begins with a slash ('/').
9655 * This means that '*' will not be added, resulting in exactly the first Host
9656 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009657 * is hashed using the path hash followed by a full avalanche hash and provides a
9658 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009659 * high-traffic sites without having to store whole paths.
9660 */
9661static int
9662smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009663 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009664{
9665 struct http_txn *txn = l7;
9666 struct hdr_ctx ctx;
9667 unsigned int hash = 0;
9668 char *ptr, *beg, *end;
9669 int len;
9670
9671 CHECK_HTTP_MESSAGE_FIRST();
9672
9673 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02009674 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009675 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9676 ptr = ctx.line + ctx.val;
9677 len = ctx.vlen;
9678 while (len--)
9679 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9680 }
9681
9682 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +02009683 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009684 beg = http_get_path(txn);
9685 if (!beg)
9686 beg = end;
9687
9688 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9689
9690 if (beg < ptr && *beg == '/') {
9691 while (beg < ptr)
9692 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9693 }
9694 hash = full_hash(hash);
9695
9696 smp->type = SMP_T_UINT;
9697 smp->data.uint = hash;
9698 smp->flags = SMP_F_VOL_1ST;
9699 return 1;
9700}
9701
Willy Tarreau4a550602012-12-09 14:53:32 +01009702/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009703 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9704 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9705 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009706 * that in environments where IPv6 is insignificant, truncating the output to
9707 * 8 bytes would still work.
9708 */
9709static int
9710smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009711 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009712{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009713 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009714 struct connection *cli_conn = objt_conn(l4->si[0].end);
9715
9716 if (!cli_conn)
9717 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009718
Willy Tarreauef38c392013-07-22 16:29:32 +02009719 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009720 return 0;
9721
Willy Tarreau47ca5452012-12-23 20:22:19 +01009722 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009723 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9724 temp->len += sizeof(smp->data.uint);
9725
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009726 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009727 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009728 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009729 temp->len += 4;
9730 break;
9731 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009732 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009733 temp->len += 16;
9734 break;
9735 default:
9736 return 0;
9737 }
9738
9739 smp->data.str = *temp;
9740 smp->type = SMP_T_BIN;
9741 return 1;
9742}
9743
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009744static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009745smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009746 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009747{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009748 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9749 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9750 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009751
Willy Tarreau24e32d82012-04-23 23:55:44 +02009752 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009753
Willy Tarreauf853c462012-04-23 18:53:56 +02009754 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009755 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009756 return 1;
9757}
9758
Willy Tarreau7f18e522010-10-22 20:04:13 +02009759/* return a valid test if the current request is the first one on the connection */
9760static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009761smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009762 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009763{
9764 if (!s)
9765 return 0;
9766
Willy Tarreauf853c462012-04-23 18:53:56 +02009767 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009768 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009769 return 1;
9770}
9771
Willy Tarreau34db1082012-04-19 17:16:54 +02009772/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009773static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009774smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009775 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009776{
9777
Willy Tarreau24e32d82012-04-23 23:55:44 +02009778 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009779 return 0;
9780
Willy Tarreauc0239e02012-04-16 14:42:55 +02009781 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009782
Willy Tarreauc0239e02012-04-16 14:42:55 +02009783 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009784 return 0;
9785
Willy Tarreauf853c462012-04-23 18:53:56 +02009786 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009787 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009788 return 1;
9789}
Willy Tarreau8797c062007-05-07 00:55:35 +02009790
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009791/* Accepts exactly 1 argument of type userlist */
9792static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009793smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009794 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009795{
9796
9797 if (!args || args->type != ARGT_USR)
9798 return 0;
9799
9800 CHECK_HTTP_MESSAGE_FIRST();
9801
9802 if (!get_http_auth(l4))
9803 return 0;
9804
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009805 /* if the user does not belong to the userlist or has a wrong password,
9806 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009807 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009808 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009809 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
9810 return 0;
9811
9812 /* pat_match_auth() will need the user list */
9813 smp->ctx.a[0] = args->data.usr;
9814
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009815 smp->type = SMP_T_STR;
9816 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009817 smp->data.str.str = l4->txn.auth.user;
9818 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009819
9820 return 1;
9821}
9822
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009823/* Try to find the next occurrence of a cookie name in a cookie header value.
9824 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9825 * the cookie value is returned into *value and *value_l, and the function
9826 * returns a pointer to the next pointer to search from if the value was found.
9827 * Otherwise if the cookie was not found, NULL is returned and neither value
9828 * nor value_l are touched. The input <hdr> string should first point to the
9829 * header's value, and the <hdr_end> pointer must point to the first character
9830 * not part of the value. <list> must be non-zero if value may represent a list
9831 * of values (cookie headers). This makes it faster to abort parsing when no
9832 * list is expected.
9833 */
9834static char *
9835extract_cookie_value(char *hdr, const char *hdr_end,
9836 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009837 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009838{
9839 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9840 char *next;
9841
9842 /* we search at least a cookie name followed by an equal, and more
9843 * generally something like this :
9844 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9845 */
9846 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9847 /* Iterate through all cookies on this line */
9848
9849 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9850 att_beg++;
9851
9852 /* find att_end : this is the first character after the last non
9853 * space before the equal. It may be equal to hdr_end.
9854 */
9855 equal = att_end = att_beg;
9856
9857 while (equal < hdr_end) {
9858 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9859 break;
9860 if (http_is_spht[(unsigned char)*equal++])
9861 continue;
9862 att_end = equal;
9863 }
9864
9865 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9866 * is between <att_beg> and <equal>, both may be identical.
9867 */
9868
9869 /* look for end of cookie if there is an equal sign */
9870 if (equal < hdr_end && *equal == '=') {
9871 /* look for the beginning of the value */
9872 val_beg = equal + 1;
9873 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9874 val_beg++;
9875
9876 /* find the end of the value, respecting quotes */
9877 next = find_cookie_value_end(val_beg, hdr_end);
9878
9879 /* make val_end point to the first white space or delimitor after the value */
9880 val_end = next;
9881 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9882 val_end--;
9883 } else {
9884 val_beg = val_end = next = equal;
9885 }
9886
9887 /* We have nothing to do with attributes beginning with '$'. However,
9888 * they will automatically be removed if a header before them is removed,
9889 * since they're supposed to be linked together.
9890 */
9891 if (*att_beg == '$')
9892 continue;
9893
9894 /* Ignore cookies with no equal sign */
9895 if (equal == next)
9896 continue;
9897
9898 /* Now we have the cookie name between att_beg and att_end, and
9899 * its value between val_beg and val_end.
9900 */
9901
9902 if (att_end - att_beg == cookie_name_l &&
9903 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9904 /* let's return this value and indicate where to go on from */
9905 *value = val_beg;
9906 *value_l = val_end - val_beg;
9907 return next + 1;
9908 }
9909
9910 /* Set-Cookie headers only have the name in the first attr=value part */
9911 if (!list)
9912 break;
9913 }
9914
9915 return NULL;
9916}
9917
William Lallemanda43ba4e2014-01-28 18:14:25 +01009918/* Fetch a captured HTTP request header. The index is the position of
9919 * the "capture" option in the configuration file
9920 */
9921static int
9922smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9923 const struct arg *args, struct sample *smp, const char *kw)
9924{
9925 struct proxy *fe = l4->fe;
9926 struct http_txn *txn = l7;
9927 int idx;
9928
9929 if (!args || args->type != ARGT_UINT)
9930 return 0;
9931
9932 idx = args->data.uint;
9933
9934 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
9935 return 0;
9936
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009937 smp->type = SMP_T_STR;
9938 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +01009939 smp->data.str.str = txn->req.cap[idx];
9940 smp->data.str.len = strlen(txn->req.cap[idx]);
9941
9942 return 1;
9943}
9944
9945/* Fetch a captured HTTP response header. The index is the position of
9946 * the "capture" option in the configuration file
9947 */
9948static int
9949smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9950 const struct arg *args, struct sample *smp, const char *kw)
9951{
9952 struct proxy *fe = l4->fe;
9953 struct http_txn *txn = l7;
9954 int idx;
9955
9956 if (!args || args->type != ARGT_UINT)
9957 return 0;
9958
9959 idx = args->data.uint;
9960
9961 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
9962 return 0;
9963
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009964 smp->type = SMP_T_STR;
9965 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +01009966 smp->data.str.str = txn->rsp.cap[idx];
9967 smp->data.str.len = strlen(txn->rsp.cap[idx]);
9968
9969 return 1;
9970}
9971
William Lallemand65ad6e12014-01-31 15:08:02 +01009972/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
9973static int
9974smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9975 const struct arg *args, struct sample *smp, const char *kw)
9976{
9977 struct chunk *temp;
9978 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +01009979 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +01009980
9981 if (!txn->uri)
9982 return 0;
9983
William Lallemand96a77852014-02-05 00:30:02 +01009984 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009985
William Lallemand96a77852014-02-05 00:30:02 +01009986 while (*ptr != ' ' && *ptr != '\0') /* find first space */
9987 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +01009988
William Lallemand96a77852014-02-05 00:30:02 +01009989 temp = get_trash_chunk();
9990 temp->str = txn->uri;
9991 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009992 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009993 smp->type = SMP_T_STR;
9994 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +01009995
9996 return 1;
9997
9998}
9999
10000/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
10001static int
10002smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10003 const struct arg *args, struct sample *smp, const char *kw)
10004{
10005 struct chunk *temp;
10006 struct http_txn *txn = l7;
10007 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010008
10009 if (!txn->uri)
10010 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010011
William Lallemand65ad6e12014-01-31 15:08:02 +010010012 ptr = txn->uri;
10013
10014 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10015 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010016
William Lallemand65ad6e12014-01-31 15:08:02 +010010017 if (!*ptr)
10018 return 0;
10019
10020 ptr++; /* skip the space */
10021
10022 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010023 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010024 if (!ptr)
10025 return 0;
10026 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10027 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010028
10029 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010030 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010031 smp->type = SMP_T_STR;
10032 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010033
10034 return 1;
10035}
10036
Willy Tarreau3c1b5ec2014-04-24 23:41:57 +020010037/* Retrieves the HTTP version from the request (either 1.0 or 1.1) and emits it
10038 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10039 */
10040static int
10041smp_fetch_capture_req_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10042 const struct arg *args, struct sample *smp, const char *kw)
10043{
10044 struct http_txn *txn = l7;
10045
10046 if (txn->req.msg_state < HTTP_MSG_HDR_FIRST)
10047 return 0;
10048
10049 if (txn->req.flags & HTTP_MSGF_VER_11)
10050 smp->data.str.str = "HTTP/1.1";
10051 else
10052 smp->data.str.str = "HTTP/1.0";
10053
10054 smp->data.str.len = 8;
10055 smp->type = SMP_T_STR;
10056 smp->flags = SMP_F_CONST;
10057 return 1;
10058
10059}
10060
10061/* Retrieves the HTTP version from the response (either 1.0 or 1.1) and emits it
10062 * as a string (either "HTTP/1.0" or "HTTP/1.1").
10063 */
10064static int
10065smp_fetch_capture_res_ver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
10066 const struct arg *args, struct sample *smp, const char *kw)
10067{
10068 struct http_txn *txn = l7;
10069
10070 if (txn->rsp.msg_state < HTTP_MSG_HDR_FIRST)
10071 return 0;
10072
10073 if (txn->rsp.flags & HTTP_MSGF_VER_11)
10074 smp->data.str.str = "HTTP/1.1";
10075 else
10076 smp->data.str.str = "HTTP/1.0";
10077
10078 smp->data.str.len = 8;
10079 smp->type = SMP_T_STR;
10080 smp->flags = SMP_F_CONST;
10081 return 1;
10082
10083}
10084
William Lallemand65ad6e12014-01-31 15:08:02 +010010085
Willy Tarreaue333ec92012-04-16 16:26:40 +020010086/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010087 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010088 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010089 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010090 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010091 * Accepts exactly 1 argument of type string. If the input options indicate
10092 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010093 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010094 */
10095static int
Willy Tarreau51539362012-05-08 12:46:28 +020010096smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010097 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010098{
10099 struct http_txn *txn = l7;
10100 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010101 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010102 const struct http_msg *msg;
10103 const char *hdr_name;
10104 int hdr_name_len;
10105 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010106 int occ = 0;
10107 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010108
Willy Tarreau24e32d82012-04-23 23:55:44 +020010109 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010110 return 0;
10111
Willy Tarreaua890d072013-04-02 12:01:06 +020010112 if (!ctx) {
10113 /* first call */
10114 ctx = &static_hdr_ctx;
10115 ctx->idx = 0;
10116 smp->ctx.a[2] = ctx;
10117 }
10118
Willy Tarreaue333ec92012-04-16 16:26:40 +020010119 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010120
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010121 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010122 msg = &txn->req;
10123 hdr_name = "Cookie";
10124 hdr_name_len = 6;
10125 } else {
10126 msg = &txn->rsp;
10127 hdr_name = "Set-Cookie";
10128 hdr_name_len = 10;
10129 }
10130
Willy Tarreau28376d62012-04-26 21:26:10 +020010131 if (!occ && !(opt & SMP_OPT_ITERATE))
10132 /* no explicit occurrence and single fetch => last cookie by default */
10133 occ = -1;
10134
10135 /* OK so basically here, either we want only one value and it's the
10136 * last one, or we want to iterate over all of them and we fetch the
10137 * next one.
10138 */
10139
Willy Tarreau9b28e032012-10-12 23:49:43 +020010140 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010141 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010142 /* search for the header from the beginning, we must first initialize
10143 * the search parameters.
10144 */
Willy Tarreau37406352012-04-23 16:16:37 +020010145 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010146 ctx->idx = 0;
10147 }
10148
Willy Tarreau28376d62012-04-26 21:26:10 +020010149 smp->flags |= SMP_F_VOL_HDR;
10150
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010151 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010152 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10153 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010154 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10155 goto out;
10156
Willy Tarreau24e32d82012-04-23 23:55:44 +020010157 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010158 continue;
10159
Willy Tarreau37406352012-04-23 16:16:37 +020010160 smp->ctx.a[0] = ctx->line + ctx->val;
10161 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010162 }
10163
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010164 smp->type = SMP_T_STR;
10165 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010166 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010167 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010168 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010169 &smp->data.str.str,
10170 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010171 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010172 found = 1;
10173 if (occ >= 0) {
10174 /* one value was returned into smp->data.str.{str,len} */
10175 smp->flags |= SMP_F_NOT_LAST;
10176 return 1;
10177 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010178 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010179 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010180 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010181 /* all cookie headers and values were scanned. If we're looking for the
10182 * last occurrence, we may return it now.
10183 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010184 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010185 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010186 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010187}
10188
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010189/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010190 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010191 * multiple cookies may be parsed on the same line. The returned sample is of
10192 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010193 */
10194static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010195smp_fetch_cookie_cnt(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 Tarreau04aa6a92012-04-06 18:57:55 +020010197{
10198 struct http_txn *txn = l7;
10199 struct hdr_idx *idx = &txn->hdr_idx;
10200 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010201 const struct http_msg *msg;
10202 const char *hdr_name;
10203 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010204 int cnt;
10205 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010206 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010207
Willy Tarreau24e32d82012-04-23 23:55:44 +020010208 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010209 return 0;
10210
Willy Tarreaue333ec92012-04-16 16:26:40 +020010211 CHECK_HTTP_MESSAGE_FIRST();
10212
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010213 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010214 msg = &txn->req;
10215 hdr_name = "Cookie";
10216 hdr_name_len = 6;
10217 } else {
10218 msg = &txn->rsp;
10219 hdr_name = "Set-Cookie";
10220 hdr_name_len = 10;
10221 }
10222
Willy Tarreau9b28e032012-10-12 23:49:43 +020010223 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010224 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010225 ctx.idx = 0;
10226 cnt = 0;
10227
10228 while (1) {
10229 /* Note: val_beg == NULL every time we need to fetch a new header */
10230 if (!val_beg) {
10231 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10232 break;
10233
Willy Tarreau24e32d82012-04-23 23:55:44 +020010234 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010235 continue;
10236
10237 val_beg = ctx.line + ctx.val;
10238 val_end = val_beg + ctx.vlen;
10239 }
10240
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010241 smp->type = SMP_T_STR;
10242 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010243 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010244 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010245 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010246 &smp->data.str.str,
10247 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010248 cnt++;
10249 }
10250 }
10251
Willy Tarreaub169eba2013-12-16 15:14:43 +010010252 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010253 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010254 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010255 return 1;
10256}
10257
Willy Tarreau51539362012-05-08 12:46:28 +020010258/* Fetch an cookie's integer value. The integer value is returned. It
10259 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10260 */
10261static int
10262smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010263 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010264{
Willy Tarreauef38c392013-07-22 16:29:32 +020010265 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010266
10267 if (ret > 0) {
10268 smp->type = SMP_T_UINT;
10269 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10270 }
10271
10272 return ret;
10273}
10274
Willy Tarreau8797c062007-05-07 00:55:35 +020010275/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010276/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010277/************************************************************************/
10278
David Cournapeau16023ee2010-12-23 20:55:41 +090010279/*
10280 * Given a path string and its length, find the position of beginning of the
10281 * query string. Returns NULL if no query string is found in the path.
10282 *
10283 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10284 *
10285 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10286 */
bedis4c75cca2012-10-05 08:38:24 +020010287static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010288{
10289 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010290
bedis4c75cca2012-10-05 08:38:24 +020010291 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010292 return p ? p + 1 : NULL;
10293}
10294
bedis4c75cca2012-10-05 08:38:24 +020010295static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010296{
bedis4c75cca2012-10-05 08:38:24 +020010297 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010298}
10299
10300/*
10301 * Given a url parameter, find the starting position of the first occurence,
10302 * or NULL if the parameter is not found.
10303 *
10304 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10305 * the function will return query_string+8.
10306 */
10307static char*
10308find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010309 char* url_param_name, size_t url_param_name_l,
10310 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010311{
10312 char *pos, *last;
10313
10314 pos = query_string;
10315 last = query_string + query_string_l - url_param_name_l - 1;
10316
10317 while (pos <= last) {
10318 if (pos[url_param_name_l] == '=') {
10319 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10320 return pos;
10321 pos += url_param_name_l + 1;
10322 }
bedis4c75cca2012-10-05 08:38:24 +020010323 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010324 pos++;
10325 pos++;
10326 }
10327 return NULL;
10328}
10329
10330/*
10331 * Given a url parameter name, returns its value and size into *value and
10332 * *value_l respectively, and returns non-zero. If the parameter is not found,
10333 * zero is returned and value/value_l are not touched.
10334 */
10335static int
10336find_url_param_value(char* path, size_t path_l,
10337 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010338 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010339{
10340 char *query_string, *qs_end;
10341 char *arg_start;
10342 char *value_start, *value_end;
10343
bedis4c75cca2012-10-05 08:38:24 +020010344 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010345 if (!query_string)
10346 return 0;
10347
10348 qs_end = path + path_l;
10349 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010350 url_param_name, url_param_name_l,
10351 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010352 if (!arg_start)
10353 return 0;
10354
10355 value_start = arg_start + url_param_name_l + 1;
10356 value_end = value_start;
10357
bedis4c75cca2012-10-05 08:38:24 +020010358 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010359 value_end++;
10360
10361 *value = value_start;
10362 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010363 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010364}
10365
10366static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010367smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010368 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010369{
bedis4c75cca2012-10-05 08:38:24 +020010370 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010371 struct http_txn *txn = l7;
10372 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010373
bedis4c75cca2012-10-05 08:38:24 +020010374 if (!args || args[0].type != ARGT_STR ||
10375 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010376 return 0;
10377
10378 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010379
bedis4c75cca2012-10-05 08:38:24 +020010380 if (args[1].type)
10381 delim = *args[1].data.str.str;
10382
Willy Tarreau9b28e032012-10-12 23:49:43 +020010383 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010384 args->data.str.str, args->data.str.len,
10385 &smp->data.str.str, &smp->data.str.len,
10386 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010387 return 0;
10388
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010389 smp->type = SMP_T_STR;
10390 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010391 return 1;
10392}
10393
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010394/* Return the signed integer value for the specified url parameter (see url_param
10395 * above).
10396 */
10397static int
10398smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010399 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010400{
Willy Tarreauef38c392013-07-22 16:29:32 +020010401 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010402
10403 if (ret > 0) {
10404 smp->type = SMP_T_UINT;
10405 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10406 }
10407
10408 return ret;
10409}
10410
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010411/* This produces a 32-bit hash of the concatenation of the first occurrence of
10412 * the Host header followed by the path component if it begins with a slash ('/').
10413 * This means that '*' will not be added, resulting in exactly the first Host
10414 * entry. If no Host header is found, then the path is used. The resulting value
10415 * is hashed using the url hash followed by a full avalanche hash and provides a
10416 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10417 * high-traffic sites without having to store whole paths.
10418 * this differs from the base32 functions in that it includes the url parameters
10419 * as well as the path
10420 */
10421static int
10422smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010423 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010424{
10425 struct http_txn *txn = l7;
10426 struct hdr_ctx ctx;
10427 unsigned int hash = 0;
10428 char *ptr, *beg, *end;
10429 int len;
10430
10431 CHECK_HTTP_MESSAGE_FIRST();
10432
10433 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010434 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010435 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10436 ptr = ctx.line + ctx.val;
10437 len = ctx.vlen;
10438 while (len--)
10439 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10440 }
10441
10442 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010443 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 +000010444 beg = http_get_path(txn);
10445 if (!beg)
10446 beg = end;
10447
10448 for (ptr = beg; ptr < end ; ptr++);
10449
10450 if (beg < ptr && *beg == '/') {
10451 while (beg < ptr)
10452 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10453 }
10454 hash = full_hash(hash);
10455
10456 smp->type = SMP_T_UINT;
10457 smp->data.uint = hash;
10458 smp->flags = SMP_F_VOL_1ST;
10459 return 1;
10460}
10461
10462/* This concatenates the source address with the 32-bit hash of the Host and
10463 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10464 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10465 * on the source address length. The URL hash is stored before the address so
10466 * that in environments where IPv6 is insignificant, truncating the output to
10467 * 8 bytes would still work.
10468 */
10469static int
10470smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010471 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010472{
10473 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010474 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010475
Willy Tarreaue155ec22013-11-18 18:33:22 +010010476 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010477 return 0;
10478
10479 temp = get_trash_chunk();
10480 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10481 temp->len += sizeof(smp->data.uint);
10482
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010483 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010484 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010485 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010486 temp->len += 4;
10487 break;
10488 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010489 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010490 temp->len += 16;
10491 break;
10492 default:
10493 return 0;
10494 }
10495
10496 smp->data.str = *temp;
10497 smp->type = SMP_T_BIN;
10498 return 1;
10499}
10500
Willy Tarreau185b5c42012-04-26 15:11:51 +020010501/* This function is used to validate the arguments passed to any "hdr" fetch
10502 * keyword. These keywords support an optional positive or negative occurrence
10503 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10504 * is assumed that the types are already the correct ones. Returns 0 on error,
10505 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10506 * error message in case of error, that the caller is responsible for freeing.
10507 * The initial location must either be freeable or NULL.
10508 */
10509static int val_hdr(struct arg *arg, char **err_msg)
10510{
10511 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010512 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010513 return 0;
10514 }
10515 return 1;
10516}
10517
Willy Tarreau276fae92013-07-25 14:36:01 +020010518/* takes an UINT value on input supposed to represent the time since EPOCH,
10519 * adds an optional offset found in args[0] and emits a string representing
10520 * the date in RFC-1123/5322 format.
10521 */
10522static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10523{
10524 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10525 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10526 struct chunk *temp;
10527 struct tm *tm;
10528 time_t curr_date = smp->data.uint;
10529
10530 /* add offset */
10531 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10532 curr_date += args[0].data.sint;
10533
10534 tm = gmtime(&curr_date);
10535
10536 temp = get_trash_chunk();
10537 temp->len = snprintf(temp->str, temp->size - temp->len,
10538 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10539 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10540 tm->tm_hour, tm->tm_min, tm->tm_sec);
10541
10542 smp->data.str = *temp;
10543 smp->type = SMP_T_STR;
10544 return 1;
10545}
10546
Thierry FOURNIERad903512014-04-11 17:51:01 +020010547/* Match language range with language tag. RFC2616 14.4:
10548 *
10549 * A language-range matches a language-tag if it exactly equals
10550 * the tag, or if it exactly equals a prefix of the tag such
10551 * that the first tag character following the prefix is "-".
10552 *
10553 * Return 1 if the strings match, else return 0.
10554 */
10555static inline int language_range_match(const char *range, int range_len,
10556 const char *tag, int tag_len)
10557{
10558 const char *end = range + range_len;
10559 const char *tend = tag + tag_len;
10560 while (range < end) {
10561 if (*range == '-' && tag == tend)
10562 return 1;
10563 if (*range != *tag || tag == tend)
10564 return 0;
10565 range++;
10566 tag++;
10567 }
10568 /* Return true only if the last char of the tag is matched. */
10569 return tag == tend;
10570}
10571
10572/* Arguments: The list of expected value, the number of parts returned and the separator */
10573static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
10574{
10575 const char *al = smp->data.str.str;
10576 const char *end = al + smp->data.str.len;
10577 const char *token;
10578 int toklen;
10579 int qvalue;
10580 const char *str;
10581 const char *w;
10582 int best_q = 0;
10583
10584 /* Set the constant to the sample, because the output of the
10585 * function will be peek in the constant configuration string.
10586 */
10587 smp->flags |= SMP_F_CONST;
10588 smp->data.str.size = 0;
10589 smp->data.str.str = "";
10590 smp->data.str.len = 0;
10591
10592 /* Parse the accept language */
10593 while (1) {
10594
10595 /* Jump spaces, quit if the end is detected. */
10596 while (al < end && isspace(*al))
10597 al++;
10598 if (al >= end)
10599 break;
10600
10601 /* Start of the fisrt word. */
10602 token = al;
10603
10604 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
10605 while (al < end && *al != ';' && *al != ',' && !isspace(*al))
10606 al++;
10607 if (al == token)
10608 goto expect_comma;
10609
10610 /* Length of the token. */
10611 toklen = al - token;
10612 qvalue = 1000;
10613
10614 /* Check if the token exists in the list. If the token not exists,
10615 * jump to the next token.
10616 */
10617 str = args[0].data.str.str;
10618 w = str;
10619 while (1) {
10620 if (*str == ';' || *str == '\0') {
10621 if (language_range_match(token, toklen, w, str-w))
10622 goto look_for_q;
10623 if (*str == '\0')
10624 goto expect_comma;
10625 w = str + 1;
10626 }
10627 str++;
10628 }
10629 goto expect_comma;
10630
10631look_for_q:
10632
10633 /* Jump spaces, quit if the end is detected. */
10634 while (al < end && isspace(*al))
10635 al++;
10636 if (al >= end)
10637 goto process_value;
10638
10639 /* If ',' is found, process the result */
10640 if (*al == ',')
10641 goto process_value;
10642
10643 /* If the character is different from ';', look
10644 * for the end of the header part in best effort.
10645 */
10646 if (*al != ';')
10647 goto expect_comma;
10648
10649 /* Assumes that the char is ';', now expect "q=". */
10650 al++;
10651
10652 /* Jump spaces, process value if the end is detected. */
10653 while (al < end && isspace(*al))
10654 al++;
10655 if (al >= end)
10656 goto process_value;
10657
10658 /* Expect 'q'. If no 'q', continue in best effort */
10659 if (*al != 'q')
10660 goto process_value;
10661 al++;
10662
10663 /* Jump spaces, process value if the end is detected. */
10664 while (al < end && isspace(*al))
10665 al++;
10666 if (al >= end)
10667 goto process_value;
10668
10669 /* Expect '='. If no '=', continue in best effort */
10670 if (*al != '=')
10671 goto process_value;
10672 al++;
10673
10674 /* Jump spaces, process value if the end is detected. */
10675 while (al < end && isspace(*al))
10676 al++;
10677 if (al >= end)
10678 goto process_value;
10679
10680 /* Parse the q value. */
10681 qvalue = parse_qvalue(al, &al);
10682
10683process_value:
10684
10685 /* If the new q value is the best q value, then store the associated
10686 * language in the response. If qvalue is the biggest value (1000),
10687 * break the process.
10688 */
10689 if (qvalue > best_q) {
10690 smp->data.str.str = (char *)w;
10691 smp->data.str.len = str - w;
10692 if (qvalue >= 1000)
10693 break;
10694 best_q = qvalue;
10695 }
10696
10697expect_comma:
10698
10699 /* Expect comma or end. If the end is detected, quit the loop. */
10700 while (al < end && *al != ',')
10701 al++;
10702 if (al >= end)
10703 break;
10704
10705 /* Comma is found, jump it and restart the analyzer. */
10706 al++;
10707 }
10708
10709 /* Set default value if required. */
10710 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
10711 smp->data.str.str = args[1].data.str.str;
10712 smp->data.str.len = args[1].data.str.len;
10713 }
10714
10715 /* Return true only if a matching language was found. */
10716 return smp->data.str.len != 0;
10717}
10718
Willy Tarreau4a568972010-05-12 08:08:50 +020010719/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010720/* All supported ACL keywords must be declared here. */
10721/************************************************************************/
10722
10723/* Note: must not be declared <const> as its list will be overwritten.
10724 * Please take care of keeping this list alphabetically sorted.
10725 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010726static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010727 { "base", "base", PAT_MATCH_STR },
10728 { "base_beg", "base", PAT_MATCH_BEG },
10729 { "base_dir", "base", PAT_MATCH_DIR },
10730 { "base_dom", "base", PAT_MATCH_DOM },
10731 { "base_end", "base", PAT_MATCH_END },
10732 { "base_len", "base", PAT_MATCH_LEN },
10733 { "base_reg", "base", PAT_MATCH_REG },
10734 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010735
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010736 { "cook", "req.cook", PAT_MATCH_STR },
10737 { "cook_beg", "req.cook", PAT_MATCH_BEG },
10738 { "cook_dir", "req.cook", PAT_MATCH_DIR },
10739 { "cook_dom", "req.cook", PAT_MATCH_DOM },
10740 { "cook_end", "req.cook", PAT_MATCH_END },
10741 { "cook_len", "req.cook", PAT_MATCH_LEN },
10742 { "cook_reg", "req.cook", PAT_MATCH_REG },
10743 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010744
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010745 { "hdr", "req.hdr", PAT_MATCH_STR },
10746 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
10747 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
10748 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
10749 { "hdr_end", "req.hdr", PAT_MATCH_END },
10750 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
10751 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
10752 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010753
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010754 /* these two declarations uses strings with list storage (in place
10755 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
10756 * and delete functions are relative to the list management. The parse
10757 * and match method are related to the corresponding fetch methods. This
10758 * is very particular ACL declaration mode.
10759 */
10760 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
10761 { "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 +020010762
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010763 { "path", "path", PAT_MATCH_STR },
10764 { "path_beg", "path", PAT_MATCH_BEG },
10765 { "path_dir", "path", PAT_MATCH_DIR },
10766 { "path_dom", "path", PAT_MATCH_DOM },
10767 { "path_end", "path", PAT_MATCH_END },
10768 { "path_len", "path", PAT_MATCH_LEN },
10769 { "path_reg", "path", PAT_MATCH_REG },
10770 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010771
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010772 { "req_ver", "req.ver", PAT_MATCH_STR },
10773 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010774
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010775 { "scook", "res.cook", PAT_MATCH_STR },
10776 { "scook_beg", "res.cook", PAT_MATCH_BEG },
10777 { "scook_dir", "res.cook", PAT_MATCH_DIR },
10778 { "scook_dom", "res.cook", PAT_MATCH_DOM },
10779 { "scook_end", "res.cook", PAT_MATCH_END },
10780 { "scook_len", "res.cook", PAT_MATCH_LEN },
10781 { "scook_reg", "res.cook", PAT_MATCH_REG },
10782 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010783
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010784 { "shdr", "res.hdr", PAT_MATCH_STR },
10785 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
10786 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
10787 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
10788 { "shdr_end", "res.hdr", PAT_MATCH_END },
10789 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
10790 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
10791 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010792
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010793 { "url", "url", PAT_MATCH_STR },
10794 { "url_beg", "url", PAT_MATCH_BEG },
10795 { "url_dir", "url", PAT_MATCH_DIR },
10796 { "url_dom", "url", PAT_MATCH_DOM },
10797 { "url_end", "url", PAT_MATCH_END },
10798 { "url_len", "url", PAT_MATCH_LEN },
10799 { "url_reg", "url", PAT_MATCH_REG },
10800 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010801
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010802 { "urlp", "urlp", PAT_MATCH_STR },
10803 { "urlp_beg", "urlp", PAT_MATCH_BEG },
10804 { "urlp_dir", "urlp", PAT_MATCH_DIR },
10805 { "urlp_dom", "urlp", PAT_MATCH_DOM },
10806 { "urlp_end", "urlp", PAT_MATCH_END },
10807 { "urlp_len", "urlp", PAT_MATCH_LEN },
10808 { "urlp_reg", "urlp", PAT_MATCH_REG },
10809 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010810
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010811 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010812}};
10813
10814/************************************************************************/
10815/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010816/************************************************************************/
10817/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010818static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010819 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010820 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10821 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10822
William Lallemanda43ba4e2014-01-28 18:14:25 +010010823 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010824 { "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 +020010825
10826 /* retrieve these captures from the HTTP logs */
10827 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
10828 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
10829 { "capture.req.ver", smp_fetch_capture_req_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
10830
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010831 { "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 +020010832 { "capture.res.ver", smp_fetch_capture_res_ver, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010010833
Willy Tarreau409bcde2013-01-08 00:31:00 +010010834 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10835 * are only here to match the ACL's name, are request-only and are used
10836 * for ACL compatibility only.
10837 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010838 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
10839 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010840 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10841 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10842
10843 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10844 * only here to match the ACL's name, are request-only and are used for
10845 * ACL compatibility only.
10846 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010847 { "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 +010010848 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10849 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10850 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10851
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010852 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010853 { "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 +010010854 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010855 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010856 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010857
10858 /* HTTP protocol on the request path */
10859 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010860 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010861
10862 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010863 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
10864 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010865
10866 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010867 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
10868 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010869
Willy Tarreau18ed2562013-01-14 15:56:36 +010010870 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010871 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010872 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10873 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10874
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010875 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010876 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010877 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010878 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10879 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10880 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10881
10882 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010883 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010884 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10885 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10886
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010887 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010888 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010889 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010890 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10891 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10892 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10893
Willy Tarreau409bcde2013-01-08 00:31:00 +010010894 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010895 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010896 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10897 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010898 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010010899
10900 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010901 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010902 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10903 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10904 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10905
10906 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010907 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010908 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10909 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010910 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10911 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010912 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
10913 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010914 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10915 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010916}};
10917
Willy Tarreau8797c062007-05-07 00:55:35 +020010918
Willy Tarreau276fae92013-07-25 14:36:01 +020010919/* Note: must not be declared <const> as its list will be overwritten */
10920static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020010921 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
10922 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020010923 { NULL, NULL, 0, 0, 0 },
10924}};
10925
Willy Tarreau8797c062007-05-07 00:55:35 +020010926__attribute__((constructor))
10927static void __http_protocol_init(void)
10928{
10929 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010930 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010931 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010932}
10933
10934
Willy Tarreau58f10d72006-12-04 02:26:12 +010010935/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010936 * Local variables:
10937 * c-indent-level: 8
10938 * c-basic-offset: 8
10939 * End:
10940 */