blob: 1e038f46134cdadc04a115cb3efd283628657512 [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 Tarreau2d3d94c2008-11-30 20:20:08 +0100749static void http_server_error(struct session *t, struct stream_interface *si,
750 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 Tarreau3bac9ff2007-03-18 17:31:28 +0100759 t->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 }
762 if (!(t->flags & SN_ERR_MASK))
763 t->flags |= err;
764 if (!(t->flags & SN_FINST_MASK))
765 t->flags |= finst;
766}
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 Tarreau414e9bb2013-11-23 00:30:38 +01002965
2966 uri = msg->chn->buf->p + msg->sl.rq.u;
2967 lookup = uri + uri_auth->uri_len;
2968
2969 for (h = lookup; h <= uri + msg->sl.rq.u_l - 3; h++) {
2970 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002971 appctx->ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002972 break;
2973 }
2974 }
2975
2976 if (uri_auth->refresh) {
2977 for (h = lookup; h <= uri + msg->sl.rq.u_l - 10; h++) {
2978 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002979 appctx->ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002980 break;
2981 }
2982 }
2983 }
2984
2985 for (h = lookup; h <= uri + msg->sl.rq.u_l - 4; h++) {
2986 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002987 appctx->ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002988 break;
2989 }
2990 }
2991
2992 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
2993 if (memcmp(h, ";st=", 4) == 0) {
2994 int i;
2995 h += 4;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002996 appctx->ctx.stats.st_code = STAT_STATUS_UNKN;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01002997 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
2998 if (strncmp(stat_status_codes[i], h, 4) == 0) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01002999 appctx->ctx.stats.st_code = i;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003000 break;
3001 }
3002 }
3003 break;
3004 }
3005 }
3006
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003007 appctx->ctx.stats.scope_str = 0;
3008 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003009 for (h = lookup; h <= uri + msg->sl.rq.u_l - 8; h++) {
3010 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
3011 int itx = 0;
3012 const char *h2;
3013 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
3014 const char *err;
3015
3016 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
3017 h2 = h;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003018 appctx->ctx.stats.scope_str = h2 - msg->chn->buf->p;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003019 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
3020 itx++;
3021 h++;
3022 }
3023
3024 if (itx > STAT_SCOPE_TXT_MAXLEN)
3025 itx = STAT_SCOPE_TXT_MAXLEN;
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003026 appctx->ctx.stats.scope_len = itx;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003027
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003028 /* scope_txt = search query, appctx->ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003029 memcpy(scope_txt, h2, itx);
3030 scope_txt[itx] = '\0';
3031 err = invalid_char(scope_txt);
3032 if (err) {
3033 /* bad char in search text => clear scope */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003034 appctx->ctx.stats.scope_str = 0;
3035 appctx->ctx.stats.scope_len = 0;
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003036 }
3037 break;
3038 }
3039 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003040
3041 /* now check whether we have some admin rules for this request */
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003042 list_for_each_entry(stats_admin_rule, &uri_auth->admin_rules, list) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003043 int ret = 1;
3044
3045 if (stats_admin_rule->cond) {
3046 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
3047 ret = acl_pass(ret);
3048 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3049 ret = !ret;
3050 }
3051
3052 if (ret) {
3053 /* no rule, or the rule matches */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003054 appctx->ctx.stats.flags |= STAT_ADMIN;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003055 break;
3056 }
3057 }
3058
3059 /* Was the status page requested with a POST ? */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003060 if (unlikely(txn->meth == HTTP_METH_POST && txn->req.body_len > 0)) {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003061 if (appctx->ctx.stats.flags & STAT_ADMIN) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003062 if (msg->msg_state < HTTP_MSG_100_SENT) {
3063 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3064 * send an HTTP/1.1 100 Continue intermediate response.
3065 */
3066 if (msg->flags & HTTP_MSGF_VER_11) {
3067 struct hdr_ctx ctx;
3068 ctx.idx = 0;
3069 /* Expect is allowed in 1.1, look for it */
3070 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
3071 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3072 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
3073 }
3074 }
3075 msg->msg_state = HTTP_MSG_100_SENT;
3076 s->logs.tv_request = now; /* update the request timer to reflect full request */
3077 }
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003078 appctx->st0 = STAT_HTTP_POST;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003079 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003080 else {
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003081 appctx->ctx.stats.st_code = STAT_STATUS_DENY;
3082 appctx->st0 = STAT_HTTP_LAST;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003083 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003084 }
Willy Tarreau347a35d2013-11-22 17:51:09 +01003085 else {
3086 /* So it was another method (GET/HEAD) */
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003087 appctx->st0 = STAT_HTTP_HEAD;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003088 }
3089
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003090 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003091 return 1;
3092}
3093
Lukas Tribus67db8df2013-06-23 17:37:13 +02003094/* Sets the TOS header in IPv4 and the traffic class header in IPv6 packets
3095 * (as per RFC3260 #4 and BCP37 #4.2 and #5.2).
3096 */
3097static inline void inet_set_tos(int fd, struct sockaddr_storage from, int tos)
3098{
3099#ifdef IP_TOS
3100 if (from.ss_family == AF_INET)
3101 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3102#endif
3103#ifdef IPV6_TCLASS
3104 if (from.ss_family == AF_INET6) {
3105 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)&from)->sin6_addr))
3106 /* v4-mapped addresses need IP_TOS */
3107 setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
3108 else
3109 setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &tos, sizeof(tos));
3110 }
3111#endif
3112}
3113
Willy Tarreau20b0de52012-12-24 15:45:22 +01003114/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003115 * transaction <txn>. Returns the first rule that prevents further processing
3116 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3117 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3118 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003119 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003120static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003121http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003122{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003123 struct connection *cli_conn;
Willy Tarreauff011f22011-01-06 17:51:27 +01003124 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003125 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003126
Willy Tarreauff011f22011-01-06 17:51:27 +01003127 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003128 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003129 continue;
3130
Willy Tarreau96257ec2012-12-27 10:46:37 +01003131 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003132 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003133 int ret;
3134
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003135 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003136 ret = acl_pass(ret);
3137
Willy Tarreauff011f22011-01-06 17:51:27 +01003138 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003139 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003140
3141 if (!ret) /* condition not matched */
3142 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003143 }
3144
Willy Tarreau20b0de52012-12-24 15:45:22 +01003145
Willy Tarreau96257ec2012-12-27 10:46:37 +01003146 switch (rule->action) {
3147 case HTTP_REQ_ACT_ALLOW:
3148 return NULL; /* "allow" rules are OK */
3149
3150 case HTTP_REQ_ACT_DENY:
3151 txn->flags |= TX_CLDENY;
3152 return rule;
3153
Willy Tarreauccbcc372012-12-27 12:37:57 +01003154 case HTTP_REQ_ACT_TARPIT:
3155 txn->flags |= TX_CLTARPIT;
3156 return rule;
3157
Willy Tarreau96257ec2012-12-27 10:46:37 +01003158 case HTTP_REQ_ACT_AUTH:
3159 return rule;
3160
Willy Tarreau81499eb2012-12-27 12:19:02 +01003161 case HTTP_REQ_ACT_REDIR:
3162 return rule;
3163
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003164 case HTTP_REQ_ACT_SET_NICE:
3165 s->task->nice = rule->arg.nice;
3166 break;
3167
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003168 case HTTP_REQ_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003169 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003170 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003171 break;
3172
Willy Tarreau51347ed2013-06-11 19:34:13 +02003173 case HTTP_REQ_ACT_SET_MARK:
3174#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003175 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003176 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003177#endif
3178 break;
3179
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003180 case HTTP_REQ_ACT_SET_LOGL:
3181 s->logs.level = rule->arg.loglevel;
3182 break;
3183
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003184 case HTTP_REQ_ACT_DEL_HDR:
Willy Tarreau96257ec2012-12-27 10:46:37 +01003185 case HTTP_REQ_ACT_SET_HDR:
3186 ctx.idx = 0;
3187 /* remove all occurrences of the header */
3188 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3189 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3190 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003191 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003192 if (rule->action == HTTP_REQ_ACT_DEL_HDR)
3193 break;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003194 /* now fall through to header addition */
3195
3196 case HTTP_REQ_ACT_ADD_HDR:
3197 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3198 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3199 trash.len = rule->arg.hdr_add.name_len;
3200 trash.str[trash.len++] = ':';
3201 trash.str[trash.len++] = ' ';
3202 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3203 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3204 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003205 }
3206 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003207
3208 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003209 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003210}
3211
Willy Tarreau71241ab2012-12-27 11:30:54 +01003212
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003213/* Executes the http-response rules <rules> for session <s>, proxy <px> and
3214 * transaction <txn>. Returns the first rule that prevents further processing
3215 * of the response (deny, ...) or NULL if it executed all rules or stopped
3216 * on an allow. It may set the TX_SVDENY on txn->flags if it encounters a deny
3217 * rule.
3218 */
3219static struct http_res_rule *
3220http_res_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3221{
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003222 struct connection *cli_conn;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003223 struct http_res_rule *rule;
3224 struct hdr_ctx ctx;
3225
3226 list_for_each_entry(rule, rules, list) {
3227 if (rule->action >= HTTP_RES_ACT_MAX)
3228 continue;
3229
3230 /* check optional condition */
3231 if (rule->cond) {
3232 int ret;
3233
3234 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
3235 ret = acl_pass(ret);
3236
3237 if (rule->cond->pol == ACL_COND_UNLESS)
3238 ret = !ret;
3239
3240 if (!ret) /* condition not matched */
3241 continue;
3242 }
3243
3244
3245 switch (rule->action) {
3246 case HTTP_RES_ACT_ALLOW:
3247 return NULL; /* "allow" rules are OK */
3248
3249 case HTTP_RES_ACT_DENY:
3250 txn->flags |= TX_SVDENY;
3251 return rule;
3252
Willy Tarreauf4c43c12013-06-11 17:01:13 +02003253 case HTTP_RES_ACT_SET_NICE:
3254 s->task->nice = rule->arg.nice;
3255 break;
3256
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003257 case HTTP_RES_ACT_SET_TOS:
Willy Tarreau3c728722014-01-23 13:50:42 +01003258 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003259 inet_set_tos(cli_conn->t.sock.fd, cli_conn->addr.from, rule->arg.tos);
Willy Tarreau42cf39e2013-06-11 18:51:32 +02003260 break;
3261
Willy Tarreau51347ed2013-06-11 19:34:13 +02003262 case HTTP_RES_ACT_SET_MARK:
3263#ifdef SO_MARK
Willy Tarreau3c728722014-01-23 13:50:42 +01003264 if ((cli_conn = objt_conn(s->req->prod->end)) && conn_ctrl_ready(cli_conn))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003265 setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &rule->arg.mark, sizeof(rule->arg.mark));
Willy Tarreau51347ed2013-06-11 19:34:13 +02003266#endif
3267 break;
3268
Willy Tarreau9a355ec2013-06-11 17:45:46 +02003269 case HTTP_RES_ACT_SET_LOGL:
3270 s->logs.level = rule->arg.loglevel;
3271 break;
3272
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003273 case HTTP_RES_ACT_DEL_HDR:
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003274 case HTTP_RES_ACT_SET_HDR:
3275 ctx.idx = 0;
3276 /* remove all occurrences of the header */
3277 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3278 txn->rsp.chn->buf->p, &txn->hdr_idx, &ctx)) {
3279 http_remove_header2(&txn->rsp, &txn->hdr_idx, &ctx);
3280 }
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02003281 if (rule->action == HTTP_RES_ACT_DEL_HDR)
3282 break;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02003283 /* now fall through to header addition */
3284
3285 case HTTP_RES_ACT_ADD_HDR:
3286 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3287 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3288 trash.len = rule->arg.hdr_add.name_len;
3289 trash.str[trash.len++] = ':';
3290 trash.str[trash.len++] = ' ';
3291 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3292 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
3293 break;
3294 }
3295 }
3296
3297 /* we reached the end of the rules, nothing to report */
3298 return NULL;
3299}
3300
3301
Willy Tarreau71241ab2012-12-27 11:30:54 +01003302/* Perform an HTTP redirect based on the information in <rule>. The function
3303 * returns non-zero on success, or zero in case of a, irrecoverable error such
3304 * as too large a request to build a valid response.
3305 */
3306static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3307{
3308 struct http_msg *msg = &txn->req;
3309 const char *msg_fmt;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003310 const char *location;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003311
3312 /* build redirect message */
3313 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003314 case 308:
3315 msg_fmt = HTTP_308;
3316 break;
3317 case 307:
3318 msg_fmt = HTTP_307;
3319 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003320 case 303:
3321 msg_fmt = HTTP_303;
3322 break;
3323 case 301:
3324 msg_fmt = HTTP_301;
3325 break;
3326 case 302:
3327 default:
3328 msg_fmt = HTTP_302;
3329 break;
3330 }
3331
3332 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3333 return 0;
3334
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003335 location = trash.str + trash.len;
3336
Willy Tarreau71241ab2012-12-27 11:30:54 +01003337 switch(rule->type) {
3338 case REDIRECT_TYPE_SCHEME: {
3339 const char *path;
3340 const char *host;
3341 struct hdr_ctx ctx;
3342 int pathlen;
3343 int hostlen;
3344
3345 host = "";
3346 hostlen = 0;
3347 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02003348 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003349 host = ctx.line + ctx.val;
3350 hostlen = ctx.vlen;
3351 }
3352
3353 path = http_get_path(txn);
3354 /* build message using path */
3355 if (path) {
3356 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3357 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3358 int qs = 0;
3359 while (qs < pathlen) {
3360 if (path[qs] == '?') {
3361 pathlen = qs;
3362 break;
3363 }
3364 qs++;
3365 }
3366 }
3367 } else {
3368 path = "/";
3369 pathlen = 1;
3370 }
3371
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003372 if (rule->rdr_str) { /* this is an old "redirect" rule */
3373 /* check if we can add scheme + "://" + host + path */
3374 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3375 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003376
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003377 /* add scheme */
3378 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3379 trash.len += rule->rdr_len;
3380 }
3381 else {
3382 /* add scheme with executing log format */
3383 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003384
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003385 /* check if we can add scheme + "://" + host + path */
3386 if (trash.len + 3 + hostlen + pathlen > trash.size - 4)
3387 return 0;
3388 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003389 /* add "://" */
3390 memcpy(trash.str + trash.len, "://", 3);
3391 trash.len += 3;
3392
3393 /* add host */
3394 memcpy(trash.str + trash.len, host, hostlen);
3395 trash.len += hostlen;
3396
3397 /* add path */
3398 memcpy(trash.str + trash.len, path, pathlen);
3399 trash.len += pathlen;
3400
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003401 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003402 if (trash.len && trash.str[trash.len - 1] != '/' &&
3403 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3404 if (trash.len > trash.size - 5)
3405 return 0;
3406 trash.str[trash.len] = '/';
3407 trash.len++;
3408 }
3409
3410 break;
3411 }
3412 case REDIRECT_TYPE_PREFIX: {
3413 const char *path;
3414 int pathlen;
3415
3416 path = http_get_path(txn);
3417 /* build message using path */
3418 if (path) {
3419 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3420 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3421 int qs = 0;
3422 while (qs < pathlen) {
3423 if (path[qs] == '?') {
3424 pathlen = qs;
3425 break;
3426 }
3427 qs++;
3428 }
3429 }
3430 } else {
3431 path = "/";
3432 pathlen = 1;
3433 }
3434
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003435 if (rule->rdr_str) { /* this is an old "redirect" rule */
3436 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3437 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003438
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003439 /* add prefix. Note that if prefix == "/", we don't want to
3440 * add anything, otherwise it makes it hard for the user to
3441 * configure a self-redirection.
3442 */
3443 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3444 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3445 trash.len += rule->rdr_len;
3446 }
3447 }
3448 else {
3449 /* add prefix with executing log format */
3450 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
3451
3452 /* Check length */
3453 if (trash.len + pathlen > trash.size - 4)
3454 return 0;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003455 }
3456
3457 /* add path */
3458 memcpy(trash.str + trash.len, path, pathlen);
3459 trash.len += pathlen;
3460
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003461 /* append a slash at the end of the location if needed and missing */
Willy Tarreau71241ab2012-12-27 11:30:54 +01003462 if (trash.len && trash.str[trash.len - 1] != '/' &&
3463 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3464 if (trash.len > trash.size - 5)
3465 return 0;
3466 trash.str[trash.len] = '/';
3467 trash.len++;
3468 }
3469
3470 break;
3471 }
3472 case REDIRECT_TYPE_LOCATION:
3473 default:
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003474 if (rule->rdr_str) { /* this is an old "redirect" rule */
3475 if (trash.len + rule->rdr_len > trash.size - 4)
3476 return 0;
3477
3478 /* add location */
3479 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3480 trash.len += rule->rdr_len;
3481 }
3482 else {
3483 /* add location with executing log format */
3484 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->rdr_fmt);
Willy Tarreau71241ab2012-12-27 11:30:54 +01003485
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003486 /* Check left length */
3487 if (trash.len > trash.size - 4)
3488 return 0;
3489 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003490 break;
3491 }
3492
3493 if (rule->cookie_len) {
3494 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3495 trash.len += 14;
3496 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3497 trash.len += rule->cookie_len;
3498 memcpy(trash.str + trash.len, "\r\n", 2);
3499 trash.len += 2;
3500 }
3501
3502 /* add end of headers and the keep-alive/close status.
3503 * We may choose to set keep-alive if the Location begins
3504 * with a slash, because the client will come back to the
3505 * same server.
3506 */
3507 txn->status = rule->code;
3508 /* let's log the request time */
3509 s->logs.tv_request = now;
3510
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01003511 if (*location == '/' &&
Willy Tarreau71241ab2012-12-27 11:30:54 +01003512 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3513 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3514 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3515 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3516 /* keep-alive possible */
3517 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3518 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3519 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3520 trash.len += 30;
3521 } else {
3522 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3523 trash.len += 24;
3524 }
3525 }
3526 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3527 trash.len += 4;
3528 bo_inject(txn->rsp.chn, trash.str, trash.len);
3529 /* "eat" the request */
3530 bi_fast_delete(txn->req.chn->buf, msg->sov);
3531 msg->sov = 0;
3532 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3533 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3534 txn->req.msg_state = HTTP_MSG_CLOSED;
3535 txn->rsp.msg_state = HTTP_MSG_DONE;
3536 } else {
3537 /* keep-alive not possible */
3538 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3539 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3540 trash.len += 29;
3541 } else {
3542 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3543 trash.len += 23;
3544 }
3545 stream_int_retnclose(txn->req.chn->prod, &trash);
3546 txn->req.chn->analysers = 0;
3547 }
3548
3549 if (!(s->flags & SN_ERR_MASK))
Willy Tarreau570f2212013-06-10 16:42:09 +02003550 s->flags |= SN_ERR_LOCAL;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003551 if (!(s->flags & SN_FINST_MASK))
3552 s->flags |= SN_FINST_R;
3553
3554 return 1;
3555}
3556
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003557/* This stream analyser runs all HTTP request processing which is common to
3558 * frontends and backends, which means blocking ACLs, filters, connection-close,
3559 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003560 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003561 * either needs more data or wants to immediately abort the request (eg: deny,
3562 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003563 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003564int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003565{
Willy Tarreaud787e662009-07-07 10:14:51 +02003566 struct http_txn *txn = &s->txn;
3567 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003568 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003569 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003570 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003571 struct cond_wordlist *wl;
Willy Tarreaud787e662009-07-07 10:14:51 +02003572
Willy Tarreau655dce92009-11-08 13:10:58 +01003573 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003574 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003575 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003576 return 0;
3577 }
3578
Willy Tarreau3a816292009-07-07 10:55:49 +02003579 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003580 req->analyse_exp = TICK_ETERNITY;
3581
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003582 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 +02003583 now_ms, __FUNCTION__,
3584 s,
3585 req,
3586 req->rex, req->wex,
3587 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003588 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003589 req->analysers);
3590
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003591 /* first check whether we have some ACLs set to block this request */
3592 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003593 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003594
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003595 ret = acl_pass(ret);
3596 if (cond->pol == ACL_COND_UNLESS)
3597 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003598
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003599 if (ret) {
3600 txn->status = 403;
3601 /* let's log the request time */
3602 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003603 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003604 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003605 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003606 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003607 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003608
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003609 /* just in case we have some per-backend tracking */
3610 session_inc_be_http_req_ctr(s);
3611
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003612 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003613 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003614
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003615 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003616 if (!http_req_last_rule) {
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003617 if (stats_check_uri(s->rep->prod, txn, px)) {
3618 s->target = &http_stats_applet.obj_type;
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003619 if (unlikely(!stream_int_register_handler(s->rep->prod, objt_applet(s->target)))) {
3620 txn->status = 500;
3621 s->logs.tv_request = now;
3622 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau7b4b4992013-12-01 09:15:12 +01003623
Willy Tarreau1fbe1c92013-12-01 09:35:41 +01003624 if (!(s->flags & SN_ERR_MASK))
3625 s->flags |= SN_ERR_RESOURCE;
3626 goto return_prx_cond;
3627 }
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003628 /* parse the whole stats request and extract the relevant information */
3629 http_handle_stats(s, req);
Willy Tarreau96257ec2012-12-27 10:46:37 +01003630 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 +01003631 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003632 }
3633
Willy Tarreau3b44e722013-11-16 10:28:23 +01003634 /* only apply req{,i}{rep/deny/tarpit} if the request was not yet
3635 * blocked by an http-request rule.
3636 */
3637 if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT)) && (px->req_exp != NULL)) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003638 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003639 goto return_bad_req;
Willy Tarreau3b44e722013-11-16 10:28:23 +01003640 }
Willy Tarreau06619262006-12-17 08:37:22 +01003641
Willy Tarreau3b44e722013-11-16 10:28:23 +01003642 /* return a 403 if either rule has blocked */
3643 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003644 if (txn->flags & TX_CLDENY) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003645 txn->status = 403;
Willy Tarreau59234e92008-11-30 23:51:27 +01003646 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003647 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003648 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003649 s->fe->fe_counters.denied_req++;
3650 if (s->fe != s->be)
3651 s->be->be_counters.denied_req++;
3652 if (s->listener->counters)
3653 s->listener->counters->denied_req++;
Willy Tarreau59234e92008-11-30 23:51:27 +01003654 goto return_prx_cond;
3655 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003656
3657 /* When a connection is tarpitted, we use the tarpit timeout,
3658 * which may be the same as the connect timeout if unspecified.
3659 * If unset, then set it to zero because we really want it to
3660 * eventually expire. We build the tarpit as an analyser.
3661 */
3662 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003663 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003664 /* wipe the request out so that we can drop the connection early
3665 * if the client closes first.
3666 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003667 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003668 req->analysers = 0; /* remove switching rules etc... */
3669 req->analysers |= AN_REQ_HTTP_TARPIT;
3670 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3671 if (!req->analyse_exp)
3672 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003673 session_inc_http_err_ctr(s);
Willy Tarreau687ba132013-11-16 10:13:35 +01003674 s->fe->fe_counters.denied_req++;
3675 if (s->fe != s->be)
3676 s->be->be_counters.denied_req++;
3677 if (s->listener->counters)
3678 s->listener->counters->denied_req++;
Willy Tarreauc465fd72009-08-31 00:17:18 +02003679 return 1;
3680 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003681 }
Willy Tarreau06619262006-12-17 08:37:22 +01003682
Willy Tarreau70dffda2014-01-30 03:07:23 +01003683 /* Until set to anything else, the connection mode is set as Keep-Alive. It will
Willy Tarreau5b154472009-12-21 20:11:07 +01003684 * only change if both the request and the config reference something else.
Willy Tarreau70dffda2014-01-30 03:07:23 +01003685 * Option httpclose by itself sets tunnel mode where headers are mangled.
3686 * However, if another mode is set, it will affect it (eg: server-close/
3687 * keep-alive + httpclose = close). Note that we avoid to redo the same work
3688 * if FE and BE have the same settings (common). The method consists in
3689 * checking if options changed between the two calls (implying that either
3690 * one is non-null, or one of them is non-null and we are there for the first
3691 * time.
Willy Tarreau42736642009-10-18 21:04:35 +02003692 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003693
Willy Tarreau416ce612014-01-31 15:45:34 +01003694 if (!(txn->flags & TX_HDR_CONN_PRS) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003695 ((s->fe->options & PR_O_HTTP_MODE) != (s->be->options & PR_O_HTTP_MODE))) {
Willy Tarreau70dffda2014-01-30 03:07:23 +01003696 int tmp = TX_CON_WANT_KAL;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003697
Willy Tarreau70dffda2014-01-30 03:07:23 +01003698 if (!((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) {
3699 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN ||
3700 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_TUN)
3701 tmp = TX_CON_WANT_TUN;
3702
3703 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3704 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3705 tmp = TX_CON_WANT_TUN;
3706 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003707
3708 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL ||
Willy Tarreau70dffda2014-01-30 03:07:23 +01003709 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_SCL) {
3710 /* option httpclose + server_close => forceclose */
3711 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
3712 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)
3713 tmp = TX_CON_WANT_CLO;
3714 else
3715 tmp = TX_CON_WANT_SCL;
3716 }
Willy Tarreau02bce8b2014-01-30 00:15:28 +01003717
3718 if ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL ||
3719 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_FCL)
Willy Tarreau5b154472009-12-21 20:11:07 +01003720 tmp = TX_CON_WANT_CLO;
3721
Willy Tarreau5b154472009-12-21 20:11:07 +01003722 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3723 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003724
Willy Tarreau416ce612014-01-31 15:45:34 +01003725 if (!(txn->flags & TX_HDR_CONN_PRS) &&
3726 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003727 /* parse the Connection header and possibly clean it */
3728 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003729 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003730 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3731 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003732 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003733 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003734 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003735 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003736 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003737
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003738 /* check if client or config asks for explicit close in KAL/SCL */
3739 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3740 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3741 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003742 (!(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 +01003743 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003744 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003745 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3746 }
Willy Tarreau78599912009-10-17 20:12:21 +02003747
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003748 /* we can be blocked here because the request needs to be authenticated,
3749 * either to pass or to access stats.
3750 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003751 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003752 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003753
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003754 if (!realm)
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003755 realm = (objt_applet(s->target) == &http_stats_applet) ? STATS_DEFAULT_REALM : px->id;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003756
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003757 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003758 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003759 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003760 /* on 401 we still count one error, because normal browsing
3761 * won't significantly increase the counter but brute force
3762 * attempts will.
3763 */
3764 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003765 goto return_prx_cond;
3766 }
3767
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003768 /* add request headers from the rule sets in the same order */
3769 list_for_each_entry(wl, &px->req_add, list) {
3770 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003771 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003772 ret = acl_pass(ret);
3773 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3774 ret = !ret;
3775 if (!ret)
3776 continue;
3777 }
3778
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003779 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003780 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003781 }
3782
3783 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3784 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3785 goto return_bad_req;
3786 req->analyse_exp = TICK_ETERNITY;
3787 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003788 }
3789
Willy Tarreau414e9bb2013-11-23 00:30:38 +01003790 if (unlikely(objt_applet(s->target) == &http_stats_applet)) {
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003791 /* process the stats request now */
Willy Tarreau347a35d2013-11-22 17:51:09 +01003792 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3793 s->fe->fe_counters.intercepted_req++;
3794
3795 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3796 s->flags |= SN_ERR_LOCAL; // to mark that it comes from the proxy
3797 if (!(s->flags & SN_FINST_MASK))
3798 s->flags |= SN_FINST_R;
3799
3800 req->analyse_exp = TICK_ETERNITY;
Willy Tarreau51437d22013-12-29 00:43:40 +01003801 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003802 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003803 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003804
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003805 /* check whether we have some ACLs set to redirect this request */
3806 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003807 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003808 int ret;
3809
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003810 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003811 ret = acl_pass(ret);
3812 if (rule->cond->pol == ACL_COND_UNLESS)
3813 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003814 if (!ret)
3815 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003816 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003817 if (!http_apply_redirect_rule(rule, s, txn))
3818 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003819
Willy Tarreau71241ab2012-12-27 11:30:54 +01003820 req->analyse_exp = TICK_ETERNITY;
3821 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003822 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003823
Willy Tarreau2be39392010-01-03 17:24:51 +01003824 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3825 * If this happens, then the data will not come immediately, so we must
3826 * send all what we have without waiting. Note that due to the small gain
3827 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003828 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003829 * itself once used.
3830 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003831 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003832
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003833 /* that's OK for us now, let's move on to next analysers */
3834 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003835
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003836 return_bad_req:
3837 /* We centralize bad requests processing here */
3838 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3839 /* we detected a parsing error. We want to archive this request
3840 * in the dedicated proxy area for later troubleshooting.
3841 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003842 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003843 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003844
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003845 txn->req.msg_state = HTTP_MSG_ERROR;
3846 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003847 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003848
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003849 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003850 if (s->listener->counters)
3851 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003852
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003853 return_prx_cond:
3854 if (!(s->flags & SN_ERR_MASK))
3855 s->flags |= SN_ERR_PRXCOND;
3856 if (!(s->flags & SN_FINST_MASK))
3857 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003858
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003859 req->analysers = 0;
3860 req->analyse_exp = TICK_ETERNITY;
3861 return 0;
3862}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003863
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003864/* This function performs all the processing enabled for the current request.
3865 * It returns 1 if the processing can continue on next analysers, or zero if it
3866 * needs more data, encounters an error, or wants to immediately abort the
3867 * request. It relies on buffers flags, and updates s->req->analysers.
3868 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003869int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003870{
3871 struct http_txn *txn = &s->txn;
3872 struct http_msg *msg = &txn->req;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003873 struct connection *cli_conn = objt_conn(req->prod->end);
Willy Tarreau58f10d72006-12-04 02:26:12 +01003874
Willy Tarreau655dce92009-11-08 13:10:58 +01003875 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003876 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003877 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003878 return 0;
3879 }
3880
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003881 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 +02003882 now_ms, __FUNCTION__,
3883 s,
3884 req,
3885 req->rex, req->wex,
3886 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003887 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003888 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003889
William Lallemand82fe75c2012-10-23 10:25:10 +02003890 if (s->fe->comp || s->be->comp)
3891 select_compression_request_header(s, req->buf);
3892
Willy Tarreau59234e92008-11-30 23:51:27 +01003893 /*
3894 * Right now, we know that we have processed the entire headers
3895 * and that unwanted requests have been filtered out. We can do
3896 * whatever we want with the remaining request. Also, now we
3897 * may have separate values for ->fe, ->be.
3898 */
Willy Tarreau06619262006-12-17 08:37:22 +01003899
Willy Tarreau59234e92008-11-30 23:51:27 +01003900 /*
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003901 * If HTTP PROXY is set we simply get remote server address parsing
3902 * incoming request. Note that this requires that a connection is
3903 * allocated on the server side.
Willy Tarreau59234e92008-11-30 23:51:27 +01003904 */
3905 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003906 struct connection *conn;
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003907 char *path;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003908
Willy Tarreau9471b8c2013-12-15 13:31:35 +01003909 /* Note that for now we don't reuse existing proxy connections */
3910 if (unlikely((conn = si_alloc_conn(req->cons, 0)) == NULL)) {
Willy Tarreau32e3c6a2013-10-11 19:34:20 +02003911 txn->req.msg_state = HTTP_MSG_ERROR;
3912 txn->status = 500;
3913 req->analysers = 0;
3914 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
3915
3916 if (!(s->flags & SN_ERR_MASK))
3917 s->flags |= SN_ERR_RESOURCE;
3918 if (!(s->flags & SN_FINST_MASK))
3919 s->flags |= SN_FINST_R;
3920
3921 return 0;
3922 }
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003923
3924 path = http_get_path(txn);
3925 url2sa(req->buf->p + msg->sl.rq.u,
3926 path ? path - (req->buf->p + msg->sl.rq.u) : msg->sl.rq.u_l,
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01003927 &conn->addr.to, NULL);
Willy Tarreaue8df1e12013-12-16 14:30:55 +01003928 /* if the path was found, we have to remove everything between
3929 * req->buf->p + msg->sl.rq.u and path (excluded). If it was not
3930 * found, we need to replace from req->buf->p + msg->sl.rq.u for
3931 * u_l characters by a single "/".
3932 */
3933 if (path) {
3934 char *cur_ptr = req->buf->p;
3935 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3936 int delta;
3937
3938 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u, path, NULL, 0);
3939 http_msg_move_end(&txn->req, delta);
3940 cur_end += delta;
3941 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3942 goto return_bad_req;
3943 }
3944 else {
3945 char *cur_ptr = req->buf->p;
3946 char *cur_end = cur_ptr + txn->req.sl.rq.l;
3947 int delta;
3948
3949 delta = buffer_replace2(req->buf, req->buf->p + msg->sl.rq.u,
3950 req->buf->p + msg->sl.rq.u + msg->sl.rq.u_l, "/", 1);
3951 http_msg_move_end(&txn->req, delta);
3952 cur_end += delta;
3953 if (http_parse_reqline(&txn->req, HTTP_MSG_RQMETH, cur_ptr, cur_end + 1, NULL, NULL) == NULL)
3954 goto return_bad_req;
3955 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003956 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003957
Willy Tarreau59234e92008-11-30 23:51:27 +01003958 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003959 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003960 * Note that doing so might move headers in the request, but
3961 * the fields will stay coherent and the URI will not move.
3962 * This should only be performed in the backend.
3963 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003964 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003965 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3966 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003967
Willy Tarreau59234e92008-11-30 23:51:27 +01003968 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003969 * 8: the appsession cookie was looked up very early in 1.2,
3970 * so let's do the same now.
3971 */
3972
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003973 /* It needs to look into the URI unless persistence must be ignored */
3974 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003975 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 +01003976 }
3977
William Lallemanda73203e2012-03-12 12:48:57 +01003978 /* add unique-id if "header-unique-id" is specified */
3979
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003980 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
3981 if ((s->unique_id = pool_alloc2(pool2_uniqueid)) == NULL)
3982 goto return_bad_req;
3983 s->unique_id[0] = '\0';
William Lallemanda73203e2012-03-12 12:48:57 +01003984 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
William Lallemand5b7ea3a2013-08-28 15:44:19 +02003985 }
William Lallemanda73203e2012-03-12 12:48:57 +01003986
3987 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003988 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3989 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003990 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003991 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003992 goto return_bad_req;
3993 }
3994
Cyril Bontéb21570a2009-11-29 20:04:48 +01003995 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003996 * 9: add X-Forwarded-For if either the frontend or the backend
3997 * asks for it.
3998 */
3999 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004000 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02004001 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02004002 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
4003 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004004 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02004005 /* The header is set to be added only if none is present
4006 * and we found it, so don't do anything.
4007 */
4008 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004009 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004010 /* Add an X-Forwarded-For header unless the source IP is
4011 * in the 'except' network range.
4012 */
4013 if ((!s->fe->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004014 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004015 != s->fe->except_net.s_addr) &&
4016 (!s->be->except_mask.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004017 (((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01004018 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01004019 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01004020 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004021 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02004022
4023 /* Note: we rely on the backend to get the header name to be used for
4024 * x-forwarded-for, because the header is really meant for the backends.
4025 * However, if the backend did not specify any option, we have to rely
4026 * on the frontend's header name.
4027 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004028 if (s->be->fwdfor_hdr_len) {
4029 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004030 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02004031 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01004032 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004033 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004034 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004035 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 +01004036
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004037 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01004038 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01004039 }
4040 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004041 else if (cli_conn && cli_conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01004042 /* FIXME: for the sake of completeness, we should also support
4043 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004044 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004045 int len;
4046 char pn[INET6_ADDRSTRLEN];
4047 inet_ntop(AF_INET6,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004048 (const void *)&((struct sockaddr_in6 *)(&cli_conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01004049 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004050
Willy Tarreau59234e92008-11-30 23:51:27 +01004051 /* Note: we rely on the backend to get the header name to be used for
4052 * x-forwarded-for, because the header is really meant for the backends.
4053 * However, if the backend did not specify any option, we have to rely
4054 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004055 */
Willy Tarreau59234e92008-11-30 23:51:27 +01004056 if (s->be->fwdfor_hdr_len) {
4057 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004058 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01004059 } else {
4060 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004061 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02004062 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004063 len += snprintf(trash.str + len, trash.size - len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02004064
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004065 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01004066 goto return_bad_req;
4067 }
4068 }
4069
4070 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02004071 * 10: add X-Original-To if either the frontend or the backend
4072 * asks for it.
4073 */
4074 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
4075
4076 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004077 if (cli_conn && cli_conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004078 /* Add an X-Original-To header unless the destination IP is
4079 * in the 'except' network range.
4080 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004081 conn_get_to_addr(cli_conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02004082
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004083 if (cli_conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02004084 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004085 (((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 +02004086 != s->fe->except_to.s_addr) &&
4087 (!s->be->except_mask_to.s_addr ||
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004088 (((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 +02004089 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02004090 int len;
4091 unsigned char *pn;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004092 pn = (unsigned char *)&((struct sockaddr_in *)&cli_conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02004093
4094 /* Note: we rely on the backend to get the header name to be used for
4095 * x-original-to, because the header is really meant for the backends.
4096 * However, if the backend did not specify any option, we have to rely
4097 * on the frontend's header name.
4098 */
4099 if (s->be->orgto_hdr_len) {
4100 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004101 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02004102 } else {
4103 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004104 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01004105 }
Willy Tarreaue9187f82014-04-14 15:27:14 +02004106 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 +02004107
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004108 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02004109 goto return_bad_req;
4110 }
4111 }
4112 }
4113
Willy Tarreau50fc7772012-11-11 22:19:57 +01004114 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
4115 * If an "Upgrade" token is found, the header is left untouched in order not to have
4116 * to deal with some servers bugs : some of them fail an Upgrade if anything but
4117 * "Upgrade" is present in the Connection header.
4118 */
4119 if (!(txn->flags & TX_HDR_CONN_UPG) &&
4120 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004121 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4122 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004123 unsigned int want_flags = 0;
4124
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004125 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02004126 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004127 ((s->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
4128 (s->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL)) &&
Willy Tarreau22a95342010-09-29 14:31:41 +02004129 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004130 want_flags |= TX_CON_CLO_SET;
4131 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02004132 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
Willy Tarreau02bce8b2014-01-30 00:15:28 +01004133 ((s->fe->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL &&
4134 (s->be->options & PR_O_HTTP_MODE) != PR_O_HTTP_PCL)) ||
Willy Tarreau22a95342010-09-29 14:31:41 +02004135 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004136 want_flags |= TX_CON_KAL_SET;
4137 }
4138
4139 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004140 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01004141 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004142
Willy Tarreaubbf0b372010-01-18 16:54:40 +01004143
Willy Tarreau522d6c02009-12-06 18:49:18 +01004144 /* If we have no server assigned yet and we're balancing on url_param
4145 * with a POST request, we may be interested in checking the body for
4146 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01004147 */
4148 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
4149 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004150 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004151 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004152 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01004153 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004154
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004155 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004156 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01004157#ifdef TCP_QUICKACK
4158 /* We expect some data from the client. Unless we know for sure
4159 * we already have a full request, we have to re-enable quick-ack
4160 * in case we previously disabled it, otherwise we might cause
4161 * the client to delay further data.
4162 */
4163 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreau3c728722014-01-23 13:50:42 +01004164 cli_conn && conn_ctrl_ready(cli_conn) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004165 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004166 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaub363a1f2013-10-01 10:45:07 +02004167 setsockopt(cli_conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01004168#endif
4169 }
Willy Tarreau03945942009-12-22 16:50:27 +01004170
Willy Tarreau59234e92008-11-30 23:51:27 +01004171 /*************************************************************
4172 * OK, that's finished for the headers. We have done what we *
4173 * could. Let's switch to the DATA state. *
4174 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01004175 req->analyse_exp = TICK_ETERNITY;
4176 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004177
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004178 /* if the server closes the connection, we want to immediately react
4179 * and close the socket to save packets and syscalls.
4180 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01004181 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
4182 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004183
Willy Tarreau59234e92008-11-30 23:51:27 +01004184 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01004185 /* OK let's go on with the BODY now */
4186 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01004187
Willy Tarreau59234e92008-11-30 23:51:27 +01004188 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02004189 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01004190 /* we detected a parsing error. We want to archive this request
4191 * in the dedicated proxy area for later troubleshooting.
4192 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004193 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01004194 }
Willy Tarreau4076a152009-04-02 15:18:36 +02004195
Willy Tarreau59234e92008-11-30 23:51:27 +01004196 txn->req.msg_state = HTTP_MSG_ERROR;
4197 txn->status = 400;
4198 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02004199 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004200
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004201 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004202 if (s->listener->counters)
4203 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004204
Willy Tarreau59234e92008-11-30 23:51:27 +01004205 if (!(s->flags & SN_ERR_MASK))
4206 s->flags |= SN_ERR_PRXCOND;
4207 if (!(s->flags & SN_FINST_MASK))
4208 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02004209 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004210}
Willy Tarreauadfb8562008-08-11 15:24:42 +02004211
Willy Tarreau60b85b02008-11-30 23:28:40 +01004212/* This function is an analyser which processes the HTTP tarpit. It always
4213 * returns zero, at the beginning because it prevents any other processing
4214 * from occurring, and at the end because it terminates the request.
4215 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004216int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01004217{
4218 struct http_txn *txn = &s->txn;
4219
4220 /* This connection is being tarpitted. The CLIENT side has
4221 * already set the connect expiration date to the right
4222 * timeout. We just have to check that the client is still
4223 * there and that the timeout has not expired.
4224 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004225 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004226 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01004227 !tick_is_expired(req->analyse_exp, now_ms))
4228 return 0;
4229
4230 /* We will set the queue timer to the time spent, just for
4231 * logging purposes. We fake a 500 server error, so that the
4232 * attacker will not suspect his connection has been tarpitted.
4233 * It will not cause trouble to the logs because we can exclude
4234 * the tarpitted connections by filtering on the 'PT' status flags.
4235 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004236 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4237
4238 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004239 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004240 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004241
4242 req->analysers = 0;
4243 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004244
Willy Tarreau60b85b02008-11-30 23:28:40 +01004245 if (!(s->flags & SN_ERR_MASK))
4246 s->flags |= SN_ERR_PRXCOND;
4247 if (!(s->flags & SN_FINST_MASK))
4248 s->flags |= SN_FINST_T;
4249 return 0;
4250}
4251
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004252/* This function is an analyser which waits for the HTTP request body. It waits
4253 * for either the buffer to be full, or the full advertised contents to have
4254 * reached the buffer. It must only be called after the standard HTTP request
4255 * processing has occurred, because it expects the request to be parsed and will
4256 * look for the Expect header. It may send a 100-Continue interim response. It
4257 * takes in input any state starting from HTTP_MSG_BODY and leaves with one of
4258 * HTTP_MSG_CHK_SIZE, HTTP_MSG_DATA or HTTP_MSG_TRAILERS. It returns zero if it
4259 * needs to read more data, or 1 once it has completed its analysis.
Willy Tarreaud34af782008-11-30 23:36:37 +01004260 */
Willy Tarreau5a8f9472014-04-10 11:16:06 +02004261int http_wait_for_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004262{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004263 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004264 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004265
4266 /* We have to parse the HTTP request body to find any required data.
4267 * "balance url_param check_post" should have been the only way to get
4268 * into this. We were brought here after HTTP header analysis, so all
4269 * related structures are ready.
4270 */
4271
Willy Tarreau890988f2014-04-10 11:59:33 +02004272 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4273 /* This is the first call */
4274 if (msg->msg_state < HTTP_MSG_BODY)
4275 goto missing_data;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004276
Willy Tarreau890988f2014-04-10 11:59:33 +02004277 if (msg->msg_state < HTTP_MSG_100_SENT) {
4278 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4279 * send an HTTP/1.1 100 Continue intermediate response.
4280 */
4281 if (msg->flags & HTTP_MSGF_VER_11) {
4282 struct hdr_ctx ctx;
4283 ctx.idx = 0;
4284 /* Expect is allowed in 1.1, look for it */
4285 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
4286 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
4287 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
4288 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004289 }
Willy Tarreau890988f2014-04-10 11:59:33 +02004290 msg->msg_state = HTTP_MSG_100_SENT;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004291 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004292
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004293 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau877e78d2013-04-07 18:48:08 +02004294 * req->buf->p still points to the beginning of the message. We
4295 * must save the body in msg->next because it survives buffer
4296 * re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004297 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004298 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004299
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004300 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004301 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4302 else
4303 msg->msg_state = HTTP_MSG_DATA;
4304 }
4305
Willy Tarreau890988f2014-04-10 11:59:33 +02004306 if (!(msg->flags & HTTP_MSGF_TE_CHNK)) {
4307 /* We're in content-length mode, we just have to wait for enough data. */
4308 if (req->buf->i - msg->sov < msg->body_len)
4309 goto missing_data;
4310
4311 /* OK we have everything we need now */
4312 goto http_end;
4313 }
4314
4315 /* OK here we're parsing a chunked-encoded message */
4316
Willy Tarreau522d6c02009-12-06 18:49:18 +01004317 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004318 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004319 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004320 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004321 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004322 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004323
Willy Tarreau115acb92009-12-26 13:56:06 +01004324 if (!ret)
4325 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004326 else if (ret < 0) {
4327 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004328 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004329 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004330 }
4331
Willy Tarreaud98cf932009-12-27 22:54:55 +01004332 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004333 * We have the first data byte is in msg->sov. We're waiting for at
Willy Tarreau226071e2014-04-10 11:55:45 +02004334 * least a whole chunk or the whole content length bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004335 */
Willy Tarreau890988f2014-04-10 11:59:33 +02004336 if (msg->msg_state == HTTP_MSG_TRAILERS)
4337 goto http_end;
4338
Willy Tarreau226071e2014-04-10 11:55:45 +02004339 if (req->buf->i - msg->sov >= msg->body_len) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004340 goto http_end;
4341
4342 missing_data:
Willy Tarreau31a19952014-04-10 11:50:37 +02004343 /* we get here if we need to wait for more data. If the buffer is full,
4344 * we have the maximum we can expect.
4345 */
4346 if (buffer_full(req->buf, global.tune.maxrewrite))
4347 goto http_end;
Willy Tarreau115acb92009-12-26 13:56:06 +01004348
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004349 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004350 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004351 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004352
4353 if (!(s->flags & SN_ERR_MASK))
4354 s->flags |= SN_ERR_CLITO;
4355 if (!(s->flags & SN_FINST_MASK))
4356 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004357 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004358 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004359
4360 /* we get here if we need to wait for more data */
Willy Tarreau31a19952014-04-10 11:50:37 +02004361 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004362 /* Not enough data. We'll re-use the http-request
4363 * timeout here. Ideally, we should set the timeout
4364 * relative to the accept() date. We just set the
4365 * request timeout once at the beginning of the
4366 * request.
4367 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004368 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004369 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004370 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004371 return 0;
4372 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004373
4374 http_end:
4375 /* The situation will not evolve, so let's give up on the analysis. */
4376 s->logs.tv_request = now; /* update the request timer to reflect full request */
4377 req->analysers &= ~an_bit;
4378 req->analyse_exp = TICK_ETERNITY;
4379 return 1;
4380
4381 return_bad_req: /* let's centralize all bad requests */
4382 txn->req.msg_state = HTTP_MSG_ERROR;
4383 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004384 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004385
Willy Tarreau79ebac62010-06-07 13:47:49 +02004386 if (!(s->flags & SN_ERR_MASK))
4387 s->flags |= SN_ERR_PRXCOND;
4388 if (!(s->flags & SN_FINST_MASK))
4389 s->flags |= SN_FINST_R;
4390
Willy Tarreau522d6c02009-12-06 18:49:18 +01004391 return_err_msg:
4392 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004393 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004394 if (s->listener->counters)
4395 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004396 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004397}
4398
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004399/* send a server's name with an outgoing request over an established connection.
4400 * Note: this function is designed to be called once the request has been scheduled
4401 * for being forwarded. This is the reason why it rewinds the buffer before
4402 * proceeding.
4403 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004404int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004405
4406 struct hdr_ctx ctx;
4407
Mark Lamourinec2247f02012-01-04 13:02:01 -05004408 char *hdr_name = be->server_id_hdr_name;
4409 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004410 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004411 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004412 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004413
William Lallemandd9e90662012-01-30 17:27:17 +01004414 ctx.idx = 0;
4415
Willy Tarreau211cdec2014-04-17 20:18:08 +02004416 old_o = http_hdr_rewind(&txn->req);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004417 if (old_o) {
4418 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004419 b_rew(chn->buf, old_o);
Willy Tarreau877e78d2013-04-07 18:48:08 +02004420 txn->req.next += old_o;
4421 txn->req.sov += old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004422 }
4423
Willy Tarreau9b28e032012-10-12 23:49:43 +02004424 old_i = chn->buf->i;
4425 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 -05004426 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004427 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004428 }
4429
4430 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004431 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004432 memcpy(hdr_val, hdr_name, hdr_name_len);
4433 hdr_val += hdr_name_len;
4434 *hdr_val++ = ':';
4435 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004436 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4437 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004438
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004439 if (old_o) {
4440 /* If this was a forwarded request, we must readjust the amount of
4441 * data to be forwarded in order to take into account the size
Willy Tarreau877e78d2013-04-07 18:48:08 +02004442 * variations. Note that the current state is >= HTTP_MSG_BODY,
4443 * so we don't have to adjust ->sol.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004444 */
Willy Tarreau877e78d2013-04-07 18:48:08 +02004445 old_o += chn->buf->i - old_i;
4446 b_adv(chn->buf, old_o);
4447 txn->req.next -= old_o;
4448 txn->req.sov -= old_o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004449 }
4450
Mark Lamourinec2247f02012-01-04 13:02:01 -05004451 return 0;
4452}
4453
Willy Tarreau610ecce2010-01-04 21:15:02 +01004454/* Terminate current transaction and prepare a new one. This is very tricky
4455 * right now but it works.
4456 */
4457void http_end_txn_clean_session(struct session *s)
4458{
Willy Tarreau068621e2013-12-23 15:11:25 +01004459 int prev_status = s->txn.status;
4460
Willy Tarreau610ecce2010-01-04 21:15:02 +01004461 /* FIXME: We need a more portable way of releasing a backend's and a
4462 * server's connections. We need a safer way to reinitialize buffer
4463 * flags. We also need a more accurate method for computing per-request
4464 * data.
4465 */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004466
Willy Tarreau4213a112013-12-15 10:25:42 +01004467 /* unless we're doing keep-alive, we want to quickly close the connection
4468 * to the server.
4469 */
4470 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4471 !si_conn_ready(s->req->cons)) {
4472 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
4473 si_shutr(s->req->cons);
4474 si_shutw(s->req->cons);
4475 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004476
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004477 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004478 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004479 if (unlikely(s->srv_conn))
4480 sess_change_server(s, NULL);
4481 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004482
4483 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4484 session_process_counters(s);
Willy Tarreauf3338342014-01-28 21:40:28 +01004485 session_stop_content_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004486
4487 if (s->txn.status) {
4488 int n;
4489
4490 n = s->txn.status / 100;
4491 if (n < 1 || n > 5)
4492 n = 0;
4493
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004494 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004495 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004496 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004497 s->fe->fe_counters.p.http.comp_rsp++;
4498 }
Willy Tarreau24657792010-02-26 10:30:28 +01004499 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004500 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004501 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004502 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004503 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004504 s->be->be_counters.p.http.comp_rsp++;
4505 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004506 }
4507
4508 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004509 s->logs.bytes_in -= s->req->buf->i;
4510 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004511
4512 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004513 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004514 !(s->flags & SN_MONITOR) &&
4515 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4516 s->do_log(s);
4517 }
4518
4519 s->logs.accept_date = date; /* user-visible date for logging */
4520 s->logs.tv_accept = now; /* corrected date for internal use */
4521 tv_zero(&s->logs.tv_request);
4522 s->logs.t_queue = -1;
4523 s->logs.t_connect = -1;
4524 s->logs.t_data = -1;
4525 s->logs.t_close = 0;
4526 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4527 s->logs.srv_queue_size = 0; /* we will get this number soon */
4528
Willy Tarreau9b28e032012-10-12 23:49:43 +02004529 s->logs.bytes_in = s->req->total = s->req->buf->i;
4530 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004531
4532 if (s->pend_pos)
4533 pendconn_free(s->pend_pos);
4534
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004535 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004536 if (s->flags & SN_CURR_SESS) {
4537 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004538 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004539 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004540 if (may_dequeue_tasks(objt_server(s->target), s->be))
4541 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004542 }
4543
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004544 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004545
Willy Tarreau4213a112013-12-15 10:25:42 +01004546 /* only release our endpoint if we don't intend to reuse the
4547 * connection.
4548 */
4549 if (((s->txn.flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) ||
4550 !si_conn_ready(s->req->cons)) {
4551 si_release_endpoint(s->req->cons);
4552 }
4553
Willy Tarreau610ecce2010-01-04 21:15:02 +01004554 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004555 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004556 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004557 s->req->cons->exp = TICK_ETERNITY;
Willy Tarreauc9200962013-12-31 23:03:09 +01004558 s->req->cons->flags &= SI_FL_DONT_WAKE; /* we're in the context of process_session */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004559 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4560 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 +02004561 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 +01004562 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE|SN_SRV_REUSED);
Willy Tarreau543db622012-11-15 16:41:22 +01004563
Willy Tarreau610ecce2010-01-04 21:15:02 +01004564 s->txn.meth = 0;
4565 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004566 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreau068621e2013-12-23 15:11:25 +01004567
4568 if (prev_status == 401 || prev_status == 407) {
4569 /* In HTTP keep-alive mode, if we receive a 401, we still have
4570 * a chance of being able to send the visitor again to the same
4571 * server over the same connection. This is required by some
4572 * broken protocols such as NTLM, and anyway whenever there is
4573 * an opportunity for sending the challenge to the proper place,
4574 * it's better to do it (at least it helps with debugging).
4575 */
4576 s->txn.flags |= TX_PREFER_LAST;
4577 }
4578
Willy Tarreauee55dc02010-06-01 10:56:34 +02004579 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004580 s->req->cons->flags |= SI_FL_INDEP_STR;
4581
Willy Tarreau96e31212011-05-30 18:10:30 +02004582 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004583 s->req->flags |= CF_NEVER_WAIT;
4584 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004585 }
4586
Willy Tarreau610ecce2010-01-04 21:15:02 +01004587 /* if the request buffer is not empty, it means we're
4588 * about to process another request, so send pending
4589 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004590 * Just don't do this if the buffer is close to be full,
4591 * because the request will wait for it to flush a little
4592 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004593 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004594 if (s->req->buf->i) {
4595 if (s->rep->buf->o &&
4596 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4597 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004598 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004599 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004600
4601 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004602 channel_auto_read(s->req);
4603 channel_auto_close(s->req);
4604 channel_auto_read(s->rep);
4605 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004606
Willy Tarreau27375622013-12-17 00:00:28 +01004607 /* we're in keep-alive with an idle connection, monitor it */
4608 si_idle_conn(s->req->cons);
4609
Willy Tarreau342b11c2010-11-24 16:22:09 +01004610 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004611 s->rep->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004612}
4613
4614
4615/* This function updates the request state machine according to the response
4616 * state machine and buffer flags. It returns 1 if it changes anything (flag
4617 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4618 * it is only used to find when a request/response couple is complete. Both
4619 * this function and its equivalent should loop until both return zero. It
4620 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4621 */
4622int http_sync_req_state(struct session *s)
4623{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004624 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004625 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004626 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004627 unsigned int old_state = txn->req.msg_state;
4628
Willy Tarreau610ecce2010-01-04 21:15:02 +01004629 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4630 return 0;
4631
4632 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004633 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004634 * We can shut the read side unless we want to abort_on_close,
4635 * or we have a POST request. The issue with POST requests is
4636 * that some browsers still send a CRLF after the request, and
4637 * this CRLF must be read so that it does not remain in the kernel
4638 * buffers, otherwise a close could cause an RST on some systems
4639 * (eg: Linux).
Willy Tarreau3988d932013-12-27 23:03:08 +01004640 * Note that if we're using keep-alive on the client side, we'd
4641 * rather poll now and keep the polling enabled for the whole
4642 * session's life than enabling/disabling it between each
4643 * response and next request.
Willy Tarreau90deb182010-01-07 00:20:41 +01004644 */
Willy Tarreau3988d932013-12-27 23:03:08 +01004645 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4646 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4647 !(s->be->options & PR_O_ABRT_CLOSE) &&
4648 txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004649 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004650
Willy Tarreau40f151a2012-12-20 12:10:09 +01004651 /* if the server closes the connection, we want to immediately react
4652 * and close the socket to save packets and syscalls.
4653 */
4654 chn->cons->flags |= SI_FL_NOHALF;
4655
Willy Tarreau610ecce2010-01-04 21:15:02 +01004656 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4657 goto wait_other_side;
4658
4659 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4660 /* The server has not finished to respond, so we
4661 * don't want to move in order not to upset it.
4662 */
4663 goto wait_other_side;
4664 }
4665
4666 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4667 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004668 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004669 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004670 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004671 goto wait_other_side;
4672 }
4673
4674 /* When we get here, it means that both the request and the
4675 * response have finished receiving. Depending on the connection
4676 * mode, we'll have to wait for the last bytes to leave in either
4677 * direction, and sometimes for a close to be effective.
4678 */
4679
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004680 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4681 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004682 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4683 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004684 }
4685 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4686 /* Option forceclose is set, or either side wants to close,
4687 * let's enforce it now that we're not expecting any new
4688 * data to come. The caller knows the session is complete
4689 * once both states are CLOSED.
4690 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004691 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4692 channel_shutr_now(chn);
4693 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004694 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004695 }
4696 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004697 /* The last possible modes are keep-alive and tunnel. Tunnel mode
4698 * will not have any analyser so it needs to poll for reads.
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004699 */
Willy Tarreau4213a112013-12-15 10:25:42 +01004700 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) {
4701 channel_auto_read(chn);
4702 txn->req.msg_state = HTTP_MSG_TUNNEL;
4703 chn->flags |= CF_NEVER_WAIT;
4704 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004705 }
4706
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004707 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004708 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004709 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004710
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004711 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004712 txn->req.msg_state = HTTP_MSG_CLOSING;
4713 goto http_msg_closing;
4714 }
4715 else {
4716 txn->req.msg_state = HTTP_MSG_CLOSED;
4717 goto http_msg_closed;
4718 }
4719 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004720 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004721 }
4722
4723 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4724 http_msg_closing:
4725 /* nothing else to forward, just waiting for the output buffer
4726 * to be empty and for the shutw_now to take effect.
4727 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004728 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004729 txn->req.msg_state = HTTP_MSG_CLOSED;
4730 goto http_msg_closed;
4731 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004732 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004733 txn->req.msg_state = HTTP_MSG_ERROR;
4734 goto wait_other_side;
4735 }
4736 }
4737
4738 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4739 http_msg_closed:
Willy Tarreau3988d932013-12-27 23:03:08 +01004740 /* see above in MSG_DONE why we only do this in these states */
4741 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_SCL) &&
4742 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_KAL) &&
4743 !(s->be->options & PR_O_ABRT_CLOSE))
Willy Tarreau2e7a1652013-12-15 15:32:10 +01004744 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004745 goto wait_other_side;
4746 }
4747
4748 wait_other_side:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004749 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004750}
4751
4752
4753/* This function updates the response state machine according to the request
4754 * state machine and buffer flags. It returns 1 if it changes anything (flag
4755 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4756 * it is only used to find when a request/response couple is complete. Both
4757 * this function and its equivalent should loop until both return zero. It
4758 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4759 */
4760int http_sync_res_state(struct session *s)
4761{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004762 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004763 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004764 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004765 unsigned int old_state = txn->rsp.msg_state;
4766
Willy Tarreau610ecce2010-01-04 21:15:02 +01004767 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4768 return 0;
4769
4770 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4771 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004772 * still monitor the server connection for a possible close
4773 * while the request is being uploaded, so we don't disable
4774 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004775 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004776 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004777
4778 if (txn->req.msg_state == HTTP_MSG_ERROR)
4779 goto wait_other_side;
4780
4781 if (txn->req.msg_state < HTTP_MSG_DONE) {
4782 /* The client seems to still be sending data, probably
4783 * because we got an error response during an upload.
4784 * We have the choice of either breaking the connection
4785 * or letting it pass through. Let's do the later.
4786 */
4787 goto wait_other_side;
4788 }
4789
4790 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4791 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004792 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004793 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004794 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004795 goto wait_other_side;
4796 }
4797
4798 /* When we get here, it means that both the request and the
4799 * response have finished receiving. Depending on the connection
4800 * mode, we'll have to wait for the last bytes to leave in either
4801 * direction, and sometimes for a close to be effective.
4802 */
4803
4804 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4805 /* Server-close mode : shut read and wait for the request
4806 * side to close its output buffer. The caller will detect
4807 * when we're in DONE and the other is in CLOSED and will
4808 * catch that for the final cleanup.
4809 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004810 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4811 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004812 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004813 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4814 /* Option forceclose is set, or either side wants to close,
4815 * let's enforce it now that we're not expecting any new
4816 * data to come. The caller knows the session is complete
4817 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004818 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004819 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4820 channel_shutr_now(chn);
4821 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004822 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004823 }
4824 else {
Willy Tarreau4213a112013-12-15 10:25:42 +01004825 /* The last possible modes are keep-alive and tunnel. Tunnel will
4826 * need to forward remaining data. Keep-alive will need to monitor
4827 * for connection closing.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004828 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004829 channel_auto_read(chn);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004830 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau4213a112013-12-15 10:25:42 +01004831 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
4832 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004833 }
4834
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004835 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004836 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004837 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004838 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4839 goto http_msg_closing;
4840 }
4841 else {
4842 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4843 goto http_msg_closed;
4844 }
4845 }
4846 goto wait_other_side;
4847 }
4848
4849 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4850 http_msg_closing:
4851 /* nothing else to forward, just waiting for the output buffer
4852 * to be empty and for the shutw_now to take effect.
4853 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004854 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004855 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4856 goto http_msg_closed;
4857 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004858 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004859 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004860 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004861 if (objt_server(s->target))
4862 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004863 goto wait_other_side;
4864 }
4865 }
4866
4867 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4868 http_msg_closed:
4869 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004870 bi_erase(chn);
4871 channel_auto_close(chn);
4872 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004873 goto wait_other_side;
4874 }
4875
4876 wait_other_side:
Willy Tarreaufc47f912012-10-20 10:38:09 +02004877 /* We force the response to leave immediately if we're waiting for the
4878 * other side, since there is no pending shutdown to push it out.
4879 */
4880 if (!channel_is_empty(chn))
4881 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004882 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004883}
4884
4885
4886/* Resync the request and response state machines. Return 1 if either state
4887 * changes.
4888 */
4889int http_resync_states(struct session *s)
4890{
4891 struct http_txn *txn = &s->txn;
4892 int old_req_state = txn->req.msg_state;
4893 int old_res_state = txn->rsp.msg_state;
4894
Willy Tarreau610ecce2010-01-04 21:15:02 +01004895 http_sync_req_state(s);
4896 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004897 if (!http_sync_res_state(s))
4898 break;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004899 if (!http_sync_req_state(s))
4900 break;
4901 }
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02004902
Willy Tarreau610ecce2010-01-04 21:15:02 +01004903 /* OK, both state machines agree on a compatible state.
4904 * There are a few cases we're interested in :
4905 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4906 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4907 * directions, so let's simply disable both analysers.
4908 * - HTTP_MSG_CLOSED on the response only means we must abort the
4909 * request.
4910 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4911 * with server-close mode means we've completed one request and we
4912 * must re-initialize the server connection.
4913 */
4914
4915 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4916 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4917 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4918 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4919 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004920 channel_auto_close(s->req);
4921 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004922 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004923 channel_auto_close(s->rep);
4924 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004925 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004926 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4927 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004928 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004929 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004930 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004931 channel_auto_close(s->rep);
4932 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004933 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004934 channel_abort(s->req);
4935 channel_auto_close(s->req);
4936 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004937 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004938 }
Willy Tarreau4213a112013-12-15 10:25:42 +01004939 else if ((txn->req.msg_state == HTTP_MSG_DONE ||
4940 txn->req.msg_state == HTTP_MSG_CLOSED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004941 txn->rsp.msg_state == HTTP_MSG_DONE &&
Willy Tarreau4213a112013-12-15 10:25:42 +01004942 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
4943 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
4944 /* server-close/keep-alive: terminate this transaction,
4945 * possibly killing the server connection and reinitialize
4946 * a fresh-new transaction.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004947 */
4948 http_end_txn_clean_session(s);
4949 }
4950
Willy Tarreau610ecce2010-01-04 21:15:02 +01004951 return txn->req.msg_state != old_req_state ||
4952 txn->rsp.msg_state != old_res_state;
4953}
4954
Willy Tarreaud98cf932009-12-27 22:54:55 +01004955/* This function is an analyser which forwards request body (including chunk
4956 * sizes if any). It is called as soon as we must forward, even if we forward
4957 * zero byte. The only situation where it must not be called is when we're in
4958 * tunnel mode and we want to forward till the close. It's used both to forward
4959 * remaining data and to resync after end of body. It expects the msg_state to
4960 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4961 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004962 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreauc24715e2014-04-17 15:21:20 +02004963 * bytes of pending data + the headers if not already done.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004964 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004965int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004966{
4967 struct http_txn *txn = &s->txn;
4968 struct http_msg *msg = &s->txn.req;
4969
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004970 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4971 return 0;
4972
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004973 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004974 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004975 /* Output closed while we were sending data. We must abort and
4976 * wake the other side up.
4977 */
4978 msg->msg_state = HTTP_MSG_ERROR;
4979 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004980 return 1;
4981 }
4982
Willy Tarreaud98cf932009-12-27 22:54:55 +01004983 /* Note that we don't have to send 100-continue back because we don't
4984 * need the data to complete our job, and it's up to the server to
4985 * decide whether to return 100, 417 or anything else in return of
4986 * an "Expect: 100-continue" header.
4987 */
4988
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02004989 if (msg->sov) {
4990 /* we have msg->sov which points to the first byte of message
4991 * body, and req->buf.p still points to the beginning of the
4992 * message. We forward the headers now, as we don't need them
4993 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004994 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02004995 b_adv(req->buf, msg->sov);
4996 msg->next -= msg->sov;
4997 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01004998
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02004999 /* The previous analysers guarantee that the state is somewhere
5000 * between MSG_BODY and the first MSG_DATA. So msg->sol and
5001 * msg->next are always correct.
5002 */
5003 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5004 if (msg->flags & HTTP_MSGF_TE_CHNK)
5005 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5006 else
5007 msg->msg_state = HTTP_MSG_DATA;
5008 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005009 }
5010
Willy Tarreau7ba23542014-04-17 21:50:00 +02005011 /* Some post-connect processing might want us to refrain from starting to
5012 * forward data. Currently, the only reason for this is "balance url_param"
5013 * whichs need to parse/process the request after we've enabled forwarding.
5014 */
5015 if (unlikely(msg->flags & HTTP_MSGF_WAIT_CONN)) {
5016 if (!(s->rep->flags & CF_READ_ATTACHED)) {
5017 channel_auto_connect(req);
5018 goto missing_data;
5019 }
5020 msg->flags &= ~HTTP_MSGF_WAIT_CONN;
5021 }
5022
Willy Tarreau80a92c02014-03-12 10:41:13 +01005023 /* in most states, we should abort in case of early close */
5024 channel_auto_close(req);
5025
Willy Tarreaud98cf932009-12-27 22:54:55 +01005026 while (1) {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005027 if (msg->msg_state == HTTP_MSG_DATA) {
5028 /* must still forward */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005029 /* we may have some pending data starting at req->buf->p */
5030 if (msg->chunk_len > req->buf->i - msg->next) {
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005031 req->flags |= CF_WAKE_WRITE;
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005032 goto missing_data;
Willy Tarreau4afd70a2014-01-25 02:26:39 +01005033 }
Willy Tarreaubed410e2014-04-22 08:19:34 +02005034 msg->next += msg->chunk_len;
5035 msg->chunk_len = 0;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005036
5037 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005038 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005039 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005040 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01005041 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005042 }
5043 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005044 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02005045 * set ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01005046 * TRAILERS state.
5047 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005048 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005049
Willy Tarreau54d23df2012-10-25 19:04:45 +02005050 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005051 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005052 else if (ret < 0) {
5053 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005054 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005055 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005056 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005057 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005058 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005059 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005060 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005061 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005062 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005063
5064 if (ret == 0)
5065 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005066 else if (ret < 0) {
5067 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005068 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005069 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005070 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005071 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005072 /* we're in MSG_CHUNK_SIZE now */
5073 }
5074 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005075 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005076
5077 if (ret == 0)
5078 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005079 else if (ret < 0) {
5080 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005081 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005082 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005083 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005084 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005085 /* we're in HTTP_MSG_DONE now */
5086 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005087 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005088 int old_state = msg->msg_state;
5089
Willy Tarreau610ecce2010-01-04 21:15:02 +01005090 /* other states, DONE...TUNNEL */
Willy Tarreaubed410e2014-04-22 08:19:34 +02005091
5092 /* we may have some pending data starting at req->buf->p
5093 * such as last chunk of data or trailers.
5094 */
5095 b_adv(req->buf, msg->next);
5096 msg->next = 0;
5097
Willy Tarreau4fe41902010-06-07 22:27:41 +02005098 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005099 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5100 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005101 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005102 if (http_resync_states(s)) {
5103 /* some state changes occurred, maybe the analyser
5104 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01005105 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005106 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005107 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005108 /* request errors are most likely due to
5109 * the server aborting the transfer.
5110 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005111 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005112 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005113 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005114 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005115 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005116 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005117 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005118 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005119
5120 /* If "option abortonclose" is set on the backend, we
5121 * want to monitor the client's connection and forward
5122 * any shutdown notification to the server, which will
5123 * decide whether to close or to go on processing the
5124 * request.
5125 */
5126 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005127 channel_auto_read(req);
5128 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02005129 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005130 else if (s->txn.meth == HTTP_METH_POST) {
5131 /* POST requests may require to read extra CRLF
5132 * sent by broken browsers and which could cause
5133 * an RST to be sent upon close on some systems
5134 * (eg: Linux).
5135 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005136 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02005137 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02005138
Willy Tarreau610ecce2010-01-04 21:15:02 +01005139 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005140 }
5141 }
5142
Willy Tarreaud98cf932009-12-27 22:54:55 +01005143 missing_data:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005144 /* we may have some pending data starting at req->buf->p */
5145 b_adv(req->buf, msg->next);
5146 msg->next = 0;
5147 msg->chunk_len -= channel_forward(req, msg->chunk_len);
5148
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005149 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005150 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02005151 if (!(s->flags & SN_ERR_MASK))
5152 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005153 if (!(s->flags & SN_FINST_MASK)) {
5154 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5155 s->flags |= SN_FINST_H;
5156 else
5157 s->flags |= SN_FINST_D;
5158 }
5159
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005160 s->fe->fe_counters.cli_aborts++;
5161 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005162 if (objt_server(s->target))
5163 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005164
5165 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02005166 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005167
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005168 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005169 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005170 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005171
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005172 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005173 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005174 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005175 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005176 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005177
Willy Tarreau5c620922011-05-11 19:56:11 +02005178 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005179 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005180 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005181 * modes are already handled by the stream sock layer. We must not do
5182 * this in content-length mode because it could present the MSG_MORE
5183 * flag with the last block of forwarded data, which would cause an
5184 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005185 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005186 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005187 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005188
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005189 return 0;
5190
Willy Tarreaud98cf932009-12-27 22:54:55 +01005191 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005192 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005193 if (s->listener->counters)
5194 s->listener->counters->failed_req++;
Willy Tarreaubed410e2014-04-22 08:19:34 +02005195
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005196 return_bad_req_stats_ok:
Willy Tarreaubed410e2014-04-22 08:19:34 +02005197 /* we may have some pending data starting at req->buf->p */
5198 b_adv(req->buf, msg->next);
5199 msg->next = 0;
5200
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005201 txn->req.msg_state = HTTP_MSG_ERROR;
5202 if (txn->status) {
5203 /* Note: we don't send any error if some data were already sent */
5204 stream_int_retnclose(req->prod, NULL);
5205 } else {
5206 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02005207 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005208 }
5209 req->analysers = 0;
5210 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005211
5212 if (!(s->flags & SN_ERR_MASK))
5213 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005214 if (!(s->flags & SN_FINST_MASK)) {
5215 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5216 s->flags |= SN_FINST_H;
5217 else
5218 s->flags |= SN_FINST_D;
5219 }
5220 return 0;
5221
5222 aborted_xfer:
5223 txn->req.msg_state = HTTP_MSG_ERROR;
5224 if (txn->status) {
5225 /* Note: we don't send any error if some data were already sent */
5226 stream_int_retnclose(req->prod, NULL);
5227 } else {
5228 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02005229 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005230 }
5231 req->analysers = 0;
5232 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
5233
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005234 s->fe->fe_counters.srv_aborts++;
5235 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005236 if (objt_server(s->target))
5237 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005238
5239 if (!(s->flags & SN_ERR_MASK))
5240 s->flags |= SN_ERR_SRVCL;
5241 if (!(s->flags & SN_FINST_MASK)) {
5242 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
5243 s->flags |= SN_FINST_H;
5244 else
5245 s->flags |= SN_FINST_D;
5246 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005247 return 0;
5248}
5249
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005250/* This stream analyser waits for a complete HTTP response. It returns 1 if the
5251 * processing can continue on next analysers, or zero if it either needs more
5252 * data or wants to immediately abort the response (eg: timeout, error, ...). It
5253 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
5254 * when it has nothing left to do, and may remove any analyser when it wants to
5255 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005256 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005257int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02005258{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005259 struct http_txn *txn = &s->txn;
5260 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005261 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005262 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005263 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005264 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02005265
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005266 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 +02005267 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005268 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005269 rep,
5270 rep->rex, rep->wex,
5271 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005272 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02005273 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02005274
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005275 /*
5276 * Now parse the partial (or complete) lines.
5277 * We will check the response syntax, and also join multi-line
5278 * headers. An index of all the lines will be elaborated while
5279 * parsing.
5280 *
5281 * For the parsing, we use a 28 states FSM.
5282 *
5283 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02005284 * rep->buf->p = beginning of response
5285 * rep->buf->p + msg->eoh = end of processed headers / start of current one
5286 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02005287 * msg->eol = end of current header or line (LF or CRLF)
5288 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005289 */
5290
Willy Tarreau83e3af02009-12-28 17:39:57 +01005291 /* There's a protected area at the end of the buffer for rewriting
5292 * purposes. We don't want to start to parse the request if the
5293 * protected area is affected, because we may have to move processed
5294 * data later, which is much more complicated.
5295 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005296 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005297 if (unlikely(!channel_reserved(rep))) {
5298 /* some data has still not left the buffer, wake us once that's done */
5299 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5300 goto abort_response;
5301 channel_dont_close(rep);
5302 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaud7ad9f52013-12-31 17:26:25 +01005303 rep->flags |= CF_WAKE_WRITE;
Willy Tarreau379357a2013-06-08 12:55:46 +02005304 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005305 }
5306
Willy Tarreau379357a2013-06-08 12:55:46 +02005307 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5308 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5309 buffer_slow_realign(rep->buf);
5310
Willy Tarreau9b28e032012-10-12 23:49:43 +02005311 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005312 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005313 }
5314
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005315 /* 1: we might have to print this header in debug mode */
5316 if (unlikely((global.mode & MODE_DEBUG) &&
5317 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005318 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005319 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005320
Willy Tarreau9b28e032012-10-12 23:49:43 +02005321 sol = rep->buf->p;
5322 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005323 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005324
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005325 sol += hdr_idx_first_pos(&txn->hdr_idx);
5326 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005327
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005328 while (cur_idx) {
5329 eol = sol + txn->hdr_idx.v[cur_idx].len;
5330 debug_hdr("srvhdr", s, sol, eol);
5331 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5332 cur_idx = txn->hdr_idx.v[cur_idx].next;
5333 }
5334 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005335
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005336 /*
5337 * Now we quickly check if we have found a full valid response.
5338 * If not so, we check the FD and buffer states before leaving.
5339 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005340 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005341 * responses are checked first.
5342 *
5343 * Depending on whether the client is still there or not, we
5344 * may send an error response back or not. Note that normally
5345 * we should only check for HTTP status there, and check I/O
5346 * errors somewhere else.
5347 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005348
Willy Tarreau655dce92009-11-08 13:10:58 +01005349 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005350 /* Invalid response */
5351 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5352 /* we detected a parsing error. We want to archive this response
5353 * in the dedicated proxy area for later troubleshooting.
5354 */
5355 hdr_response_bad:
5356 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005357 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005358
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005359 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005360 if (objt_server(s->target)) {
5361 objt_server(s->target)->counters.failed_resp++;
5362 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005363 }
Willy Tarreau64648412010-03-05 10:41:54 +01005364 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005365 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005366 rep->analysers = 0;
5367 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005368 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005369 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005370 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005371
5372 if (!(s->flags & SN_ERR_MASK))
5373 s->flags |= SN_ERR_PRXCOND;
5374 if (!(s->flags & SN_FINST_MASK))
5375 s->flags |= SN_FINST_H;
5376
5377 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005378 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005379
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005380 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005381 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005382 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005383 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005384 goto hdr_response_bad;
5385 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005386
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005387 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005388 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005389 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005390 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005391 else if (txn->flags & TX_NOT_FIRST)
5392 goto abort_keep_alive;
Willy Tarreau4076a152009-04-02 15:18:36 +02005393
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005394 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005395 if (objt_server(s->target)) {
5396 objt_server(s->target)->counters.failed_resp++;
5397 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005398 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005399
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005400 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005401 rep->analysers = 0;
5402 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005403 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005404 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005405 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005406
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005407 if (!(s->flags & SN_ERR_MASK))
5408 s->flags |= SN_ERR_SRVCL;
5409 if (!(s->flags & SN_FINST_MASK))
5410 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005411 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005412 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005413
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005414 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005415 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005416 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005417 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005418 else if (txn->flags & TX_NOT_FIRST)
5419 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005420
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005421 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005422 if (objt_server(s->target)) {
5423 objt_server(s->target)->counters.failed_resp++;
5424 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005425 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005426
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005427 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005428 rep->analysers = 0;
5429 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005430 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005431 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005432 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005433
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005434 if (!(s->flags & SN_ERR_MASK))
5435 s->flags |= SN_ERR_SRVTO;
5436 if (!(s->flags & SN_FINST_MASK))
5437 s->flags |= SN_FINST_H;
5438 return 0;
5439 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005440
Willy Tarreauf003d372012-11-26 13:35:37 +01005441 /* client abort with an abortonclose */
5442 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5443 s->fe->fe_counters.cli_aborts++;
5444 s->be->be_counters.cli_aborts++;
5445 if (objt_server(s->target))
5446 objt_server(s->target)->counters.cli_aborts++;
5447
5448 rep->analysers = 0;
5449 channel_auto_close(rep);
5450
5451 txn->status = 400;
5452 bi_erase(rep);
5453 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5454
5455 if (!(s->flags & SN_ERR_MASK))
5456 s->flags |= SN_ERR_CLICL;
5457 if (!(s->flags & SN_FINST_MASK))
5458 s->flags |= SN_FINST_H;
5459
5460 /* process_session() will take care of the error */
5461 return 0;
5462 }
5463
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005464 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005465 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005466 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005467 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005468 else if (txn->flags & TX_NOT_FIRST)
5469 goto abort_keep_alive;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005470
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005471 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005472 if (objt_server(s->target)) {
5473 objt_server(s->target)->counters.failed_resp++;
5474 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005475 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005476
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005477 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005478 rep->analysers = 0;
5479 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005480 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005481 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005482 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005483
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005484 if (!(s->flags & SN_ERR_MASK))
5485 s->flags |= SN_ERR_SRVCL;
5486 if (!(s->flags & SN_FINST_MASK))
5487 s->flags |= SN_FINST_H;
5488 return 0;
5489 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005490
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005491 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005492 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005493 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005494 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005495 else if (txn->flags & TX_NOT_FIRST)
5496 goto abort_keep_alive;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005497
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005498 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005499 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005500 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005501
5502 if (!(s->flags & SN_ERR_MASK))
5503 s->flags |= SN_ERR_CLICL;
5504 if (!(s->flags & SN_FINST_MASK))
5505 s->flags |= SN_FINST_H;
5506
5507 /* process_session() will take care of the error */
5508 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005509 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005510
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005511 channel_dont_close(rep);
Willy Tarreau3f3997e2013-12-15 15:21:32 +01005512 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005513 return 0;
5514 }
5515
5516 /* More interesting part now : we know that we have a complete
5517 * response which at least looks like HTTP. We have an indicator
5518 * of each header's length, so we can parse them quickly.
5519 */
5520
5521 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005522 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005523
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005524 /*
5525 * 1: get the status code
5526 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005527 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005528 if (n < 1 || n > 5)
5529 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005530 /* when the client triggers a 4xx from the server, it's most often due
5531 * to a missing object or permission. These events should be tracked
5532 * because if they happen often, it may indicate a brute force or a
5533 * vulnerability scan.
5534 */
5535 if (n == 4)
5536 session_inc_http_err_ctr(s);
5537
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005538 if (objt_server(s->target))
5539 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005540
Willy Tarreau5b154472009-12-21 20:11:07 +01005541 /* check if the response is HTTP/1.1 or above */
5542 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005543 ((rep->buf->p[5] > '1') ||
5544 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005545 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005546
5547 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005548 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 +01005549
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005550 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005551 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005552
Willy Tarreau9b28e032012-10-12 23:49:43 +02005553 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005554
Willy Tarreau39650402010-03-15 19:44:39 +01005555 /* Adjust server's health based on status code. Note: status codes 501
5556 * and 505 are triggered on demand by client request, so we must not
5557 * count them as server failures.
5558 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005559 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005560 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005561 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005562 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005563 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005564 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005565
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005566 /*
5567 * 2: check for cacheability.
5568 */
5569
5570 switch (txn->status) {
5571 case 200:
5572 case 203:
5573 case 206:
5574 case 300:
5575 case 301:
5576 case 410:
5577 /* RFC2616 @13.4:
5578 * "A response received with a status code of
5579 * 200, 203, 206, 300, 301 or 410 MAY be stored
5580 * by a cache (...) unless a cache-control
5581 * directive prohibits caching."
5582 *
5583 * RFC2616 @9.5: POST method :
5584 * "Responses to this method are not cacheable,
5585 * unless the response includes appropriate
5586 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005587 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005588 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005589 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005590 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5591 break;
5592 default:
5593 break;
5594 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005595
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005596 /*
5597 * 3: we may need to capture headers
5598 */
5599 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005600 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005601 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005602 txn->rsp.cap, s->fe->rsp_cap);
5603
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005604 /* 4: determine the transfer-length.
5605 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5606 * the presence of a message-body in a RESPONSE and its transfer length
5607 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005608 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005609 * All responses to the HEAD request method MUST NOT include a
5610 * message-body, even though the presence of entity-header fields
5611 * might lead one to believe they do. All 1xx (informational), 204
5612 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5613 * message-body. All other responses do include a message-body,
5614 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005615 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005616 * 1. Any response which "MUST NOT" include a message-body (such as the
5617 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5618 * always terminated by the first empty line after the header fields,
5619 * regardless of the entity-header fields present in the message.
5620 *
5621 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5622 * the "chunked" transfer-coding (Section 6.2) is used, the
5623 * transfer-length is defined by the use of this transfer-coding.
5624 * If a Transfer-Encoding header field is present and the "chunked"
5625 * transfer-coding is not present, the transfer-length is defined by
5626 * the sender closing the connection.
5627 *
5628 * 3. If a Content-Length header field is present, its decimal value in
5629 * OCTETs represents both the entity-length and the transfer-length.
5630 * If a message is received with both a Transfer-Encoding header
5631 * field and a Content-Length header field, the latter MUST be ignored.
5632 *
5633 * 4. If the message uses the media type "multipart/byteranges", and
5634 * the transfer-length is not otherwise specified, then this self-
5635 * delimiting media type defines the transfer-length. This media
5636 * type MUST NOT be used unless the sender knows that the recipient
5637 * can parse it; the presence in a request of a Range header with
5638 * multiple byte-range specifiers from a 1.1 client implies that the
5639 * client can parse multipart/byteranges responses.
5640 *
5641 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005642 */
5643
5644 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005645 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005646 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005647 * FIXME: should we parse anyway and return an error on chunked encoding ?
5648 */
5649 if (txn->meth == HTTP_METH_HEAD ||
5650 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005651 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005652 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005653 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005654 goto skip_content_length;
5655 }
5656
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005657 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005658 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005659 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005660 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005661 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005662 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5663 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005664 /* bad transfer-encoding (chunked followed by something else) */
5665 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005666 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005667 break;
5668 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005669 }
5670
5671 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5672 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005673 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005674 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005675 signed long long cl;
5676
Willy Tarreauad14f752011-09-02 20:33:27 +02005677 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005678 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005679 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005680 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005681
Willy Tarreauad14f752011-09-02 20:33:27 +02005682 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005683 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005684 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005685 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005686
Willy Tarreauad14f752011-09-02 20:33:27 +02005687 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005688 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005689 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005690 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005691
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005692 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005693 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005694 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005695 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005696
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005697 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005698 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005699 }
5700
William Lallemand82fe75c2012-10-23 10:25:10 +02005701 if (s->fe->comp || s->be->comp)
5702 select_compression_response_header(s, rep->buf);
5703
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005704 /* FIXME: we should also implement the multipart/byterange method.
5705 * For now on, we resort to close mode in this case (unknown length).
5706 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005707skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005708
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005709 /* end of job, return OK */
5710 rep->analysers &= ~an_bit;
5711 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005712 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005713 return 1;
Willy Tarreau6b726ad2013-12-15 19:31:37 +01005714
5715 abort_keep_alive:
5716 /* A keep-alive request to the server failed on a network error.
5717 * The client is required to retry. We need to close without returning
5718 * any other information so that the client retries.
5719 */
5720 txn->status = 0;
5721 rep->analysers = 0;
5722 s->req->analysers = 0;
5723 channel_auto_close(rep);
5724 s->logs.logwait = 0;
5725 s->logs.level = 0;
5726 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
5727 bi_erase(rep);
5728 stream_int_retnclose(rep->cons, NULL);
5729 return 0;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005730}
5731
5732/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005733 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5734 * and updates t->rep->analysers. It might make sense to explode it into several
5735 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005736 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005737int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005738{
5739 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005740 struct http_msg *msg = &txn->rsp;
5741 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005742 struct cond_wordlist *wl;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005743 struct http_res_rule *http_res_last_rule = NULL;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005744
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005745 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005746 now_ms, __FUNCTION__,
5747 t,
5748 rep,
5749 rep->rex, rep->wex,
5750 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005751 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005752 rep->analysers);
5753
Willy Tarreau655dce92009-11-08 13:10:58 +01005754 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005755 return 0;
5756
5757 rep->analysers &= ~an_bit;
5758 rep->analyse_exp = TICK_ETERNITY;
5759
Willy Tarreau5b154472009-12-21 20:11:07 +01005760 /* Now we have to check if we need to modify the Connection header.
5761 * This is more difficult on the response than it is on the request,
5762 * because we can have two different HTTP versions and we don't know
5763 * how the client will interprete a response. For instance, let's say
5764 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5765 * HTTP/1.1 response without any header. Maybe it will bound itself to
5766 * HTTP/1.0 because it only knows about it, and will consider the lack
5767 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5768 * the lack of header as a keep-alive. Thus we will use two flags
5769 * indicating how a request MAY be understood by the client. In case
5770 * of multiple possibilities, we'll fix the header to be explicit. If
5771 * ambiguous cases such as both close and keepalive are seen, then we
5772 * will fall back to explicit close. Note that we won't take risks with
5773 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005774 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005775 */
5776
Willy Tarreaudc008c52010-02-01 16:20:08 +01005777 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5778 txn->status == 101)) {
5779 /* Either we've established an explicit tunnel, or we're
5780 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005781 * to understand the next protocols. We have to switch to tunnel
5782 * mode, so that we transfer the request and responses then let
5783 * this protocol pass unmodified. When we later implement specific
5784 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005785 * header which contains information about that protocol for
5786 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005787 */
5788 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5789 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005790 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5791 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01005792 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5793 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005794 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005795
Willy Tarreau70dffda2014-01-30 03:07:23 +01005796 /* this situation happens when combining pretend-keepalive with httpclose. */
5797 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
5798 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
5799 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))
5800 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
5801
Willy Tarreau60466522010-01-18 19:08:45 +01005802 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005803 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005804 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5805 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005806
Willy Tarreau60466522010-01-18 19:08:45 +01005807 /* now adjust header transformations depending on current state */
5808 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5809 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5810 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005811 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005812 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005813 }
Willy Tarreau60466522010-01-18 19:08:45 +01005814 else { /* SCL / KAL */
5815 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005816 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005817 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005818 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005819
Willy Tarreau60466522010-01-18 19:08:45 +01005820 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005821 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005822
Willy Tarreau60466522010-01-18 19:08:45 +01005823 /* Some keep-alive responses are converted to Server-close if
5824 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005825 */
Willy Tarreau60466522010-01-18 19:08:45 +01005826 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5827 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005828 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005829 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005830 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005831 }
5832
Willy Tarreau7959a552013-09-23 16:44:27 +02005833 /* we want to have the response time before we start processing it */
5834 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
5835
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005836 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005837 /*
5838 * 3: we will have to evaluate the filters.
5839 * As opposed to version 1.2, now they will be evaluated in the
5840 * filters order and not in the header order. This means that
5841 * each filter has to be validated among all headers.
5842 *
5843 * Filters are tried with ->be first, then with ->fe if it is
5844 * different from ->be.
5845 */
5846
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005847 cur_proxy = t->be;
5848 while (1) {
5849 struct proxy *rule_set = cur_proxy;
5850
Willy Tarreaue365c0b2013-06-11 16:06:12 +02005851 /* evaluate http-response rules */
5852 if (!http_res_last_rule)
5853 http_res_last_rule = http_res_get_intercept_rule(cur_proxy, &cur_proxy->http_res_rules, t, txn);
5854
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005855 /* try headers filters */
5856 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005857 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005858 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005859 if (objt_server(t->target)) {
5860 objt_server(t->target)->counters.failed_resp++;
5861 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005862 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005863 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005864 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005865 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005866 txn->status = 502;
Willy Tarreau7959a552013-09-23 16:44:27 +02005867 t->logs.t_data = -1; /* was not a valid response */
Willy Tarreauc88ea682009-12-29 14:56:36 +01005868 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005869 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005870 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005871 if (!(t->flags & SN_ERR_MASK))
5872 t->flags |= SN_ERR_PRXCOND;
5873 if (!(t->flags & SN_FINST_MASK))
5874 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005875 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005876 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005877 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005878
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005879 /* has the response been denied ? */
5880 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005881 if (objt_server(t->target))
5882 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005883
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005884 t->be->be_counters.denied_resp++;
5885 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005886 if (t->listener->counters)
5887 t->listener->counters->denied_resp++;
5888
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005889 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005890 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005891
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005892 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005893 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005894 if (txn->status < 200)
5895 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005896 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005897 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005898 ret = acl_pass(ret);
5899 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5900 ret = !ret;
5901 if (!ret)
5902 continue;
5903 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005904 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005905 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005906 }
5907
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005908 /* check whether we're already working on the frontend */
5909 if (cur_proxy == t->fe)
5910 break;
5911 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005912 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005913
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005914 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005915 * We may be facing a 100-continue response, in which case this
5916 * is not the right response, and we're waiting for the next one.
5917 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005918 * next one.
5919 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005920 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005921 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005922 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005923 msg->msg_state = HTTP_MSG_RPBEFORE;
5924 txn->status = 0;
Willy Tarreau7959a552013-09-23 16:44:27 +02005925 t->logs.t_data = -1; /* was not a response yet */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005926 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5927 return 1;
5928 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005929 else if (unlikely(txn->status < 200))
5930 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005931
5932 /* we don't have any 1xx status code now */
5933
5934 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005935 * 4: check for server cookie.
5936 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005937 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5938 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005939 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005940
Willy Tarreaubaaee002006-06-26 02:48:02 +02005941
Willy Tarreaua15645d2007-03-18 16:22:39 +01005942 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005943 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005944 */
Willy Tarreau67402132012-05-31 20:40:20 +02005945 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005946 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005947
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005948 /*
5949 * 6: add server cookie in the response if needed
5950 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005951 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005952 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005953 (!(t->flags & SN_DIRECT) ||
5954 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5955 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5956 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5957 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005958 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005959 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005960 /* the server is known, it's not the one the client requested, or the
5961 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005962 * insert a set-cookie here, except if we want to insert only on POST
5963 * requests and this one isn't. Note that servers which don't have cookies
5964 * (eg: some backup servers) will return a full cookie removal request.
5965 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005966 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005967 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005968 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5969 t->be->cookie_name);
5970 }
5971 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005972 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005973
5974 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5975 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005976 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5977 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5978 trash.len += 5;
5979
Willy Tarreauef4f3912010-10-07 21:00:29 +02005980 if (t->be->cookie_maxlife) {
5981 /* emit first_date, which is either the original one or
5982 * the current date.
5983 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005984 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005985 s30tob64(txn->cookie_first_date ?
5986 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005987 (date.tv_sec+3) >> 2, trash.str + trash.len);
5988 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005989 }
5990 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005991 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005992 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005993
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005994 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005995 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005996
Willy Tarreau4992dd22012-05-31 21:02:17 +02005997 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005998 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005999
6000 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006001 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02006002
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006003 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006004 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02006005
Willy Tarreauf1348312010-10-07 15:54:11 +02006006 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006007 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02006008 /* the server did not change, only the date was updated */
6009 txn->flags |= TX_SCK_UPDATED;
6010 else
6011 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02006012
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006013 /* Here, we will tell an eventual cache on the client side that we don't
6014 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
6015 * Some caches understand the correct form: 'no-cache="set-cookie"', but
6016 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
6017 */
Willy Tarreau67402132012-05-31 20:40:20 +02006018 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02006019
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006020 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6021
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006022 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01006023 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006024 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006025 }
6026 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02006027
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006028 /*
6029 * 7: check if result will be cacheable with a cookie.
6030 * We'll block the response if security checks have caught
6031 * nasty things such as a cacheable cookie.
6032 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006033 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
6034 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01006035 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006036
6037 /* we're in presence of a cacheable response containing
6038 * a set-cookie header. We'll block it as requested by
6039 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006040 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006041 if (objt_server(t->target))
6042 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006043
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006044 t->be->be_counters.denied_resp++;
6045 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006046 if (t->listener->counters)
6047 t->listener->counters->denied_resp++;
6048
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006049 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006050 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006051 send_log(t->be, LOG_ALERT,
6052 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006053 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006054 goto return_srv_prx_502;
6055 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006056
6057 /*
Willy Tarreau60466522010-01-18 19:08:45 +01006058 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01006059 * If an "Upgrade" token is found, the header is left untouched in order
6060 * not to have to deal with some client bugs : some of them fail an upgrade
6061 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006062 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01006063 if (!(txn->flags & TX_HDR_CONN_UPG) &&
6064 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Willy Tarreau02bce8b2014-01-30 00:15:28 +01006065 ((t->fe->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL ||
6066 (t->be->options & PR_O_HTTP_MODE) == PR_O_HTTP_PCL))) {
Willy Tarreau60466522010-01-18 19:08:45 +01006067 unsigned int want_flags = 0;
6068
6069 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6070 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
6071 /* we want a keep-alive response here. Keep-alive header
6072 * required if either side is not 1.1.
6073 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006074 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01006075 want_flags |= TX_CON_KAL_SET;
6076 }
6077 else {
6078 /* we want a close response here. Close header required if
6079 * the server is 1.1, regardless of the client.
6080 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006081 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01006082 want_flags |= TX_CON_CLO_SET;
6083 }
6084
6085 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01006086 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006087 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006088
Willy Tarreau5843d1a2010-02-01 15:13:32 +01006089 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01006090 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01006091 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006092 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01006093
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006094 /*************************************************************
6095 * OK, that's finished for the headers. We have done what we *
6096 * could. Let's switch to the DATA state. *
6097 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02006098
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006099 /* if the user wants to log as soon as possible, without counting
6100 * bytes from the server, then this is the right moment. We have
6101 * to temporarily assign bytes_out to log what we currently have.
6102 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01006103 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006104 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
6105 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01006106 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006107 t->logs.bytes_out = 0;
6108 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006109
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006110 /* Note: we must not try to cheat by jumping directly to DATA,
6111 * otherwise we would not let the client side wake up.
6112 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006113
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006114 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006115 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01006116 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02006117}
Willy Tarreaua15645d2007-03-18 16:22:39 +01006118
Willy Tarreaud98cf932009-12-27 22:54:55 +01006119/* This function is an analyser which forwards response body (including chunk
6120 * sizes if any). It is called as soon as we must forward, even if we forward
6121 * zero byte. The only situation where it must not be called is when we're in
6122 * tunnel mode and we want to forward till the close. It's used both to forward
6123 * remaining data and to resync after end of body. It expects the msg_state to
6124 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
6125 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreaud3510212014-04-21 11:24:13 +02006126 *
6127 * It is capable of compressing response data both in content-length mode and
6128 * in chunked mode. The state machines follows different flows depending on
6129 * whether content-length and chunked modes are used, since there are no
6130 * trailers in content-length :
6131 *
6132 * chk-mode cl-mode
6133 * ,----- BODY -----.
6134 * / \
6135 * V size > 0 V chk-mode
6136 * .--> SIZE -------------> DATA -------------> CRLF
6137 * | | size == 0 | last byte |
6138 * | v final crlf v inspected |
6139 * | TRAILERS -----------> DONE |
6140 * | |
6141 * `----------------------------------------------'
6142 *
6143 * Compression only happens in the DATA state, and must be flushed in final
6144 * states (TRAILERS/DONE) or when leaving on missing data. Normal forwarding
6145 * is performed at once on final states for all bytes parsed, or when leaving
6146 * on missing data.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006147 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006148int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006149{
6150 struct http_txn *txn = &s->txn;
6151 struct http_msg *msg = &s->txn.rsp;
William Lallemand82fe75c2012-10-23 10:25:10 +02006152 static struct buffer *tmpbuf = NULL;
6153 int compressing = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006154 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006155
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006156 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
6157 return 0;
6158
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006159 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02006160 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01006161 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02006162 /* Output closed while we were sending data. We must abort and
6163 * wake the other side up.
6164 */
6165 msg->msg_state = HTTP_MSG_ERROR;
6166 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01006167 return 1;
6168 }
6169
Willy Tarreau4fe41902010-06-07 22:27:41 +02006170 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006171 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01006172
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006173 if (msg->sov) {
6174 /* we have msg->sov which points to the first byte of message
6175 * body, and res->buf.p still points to the beginning of the
6176 * message. We forward the headers now, as we don't need them
6177 * anymore, and we want to flush them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006178 */
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006179 b_adv(res->buf, msg->sov);
6180 msg->next -= msg->sov;
6181 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01006182
Willy Tarreaub59c7bf2014-04-22 14:29:58 +02006183 /* The previous analysers guarantee that the state is somewhere
6184 * between MSG_BODY and the first MSG_DATA. So msg->sol and
6185 * msg->next are always correct.
6186 */
6187 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
6188 if (msg->flags & HTTP_MSGF_TE_CHNK)
6189 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
6190 else
6191 msg->msg_state = HTTP_MSG_DATA;
6192 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006193 }
6194
Willy Tarreau32b5ab22014-04-21 11:27:29 +02006195 if (unlikely(s->comp_algo != NULL) && msg->msg_state < HTTP_MSG_TRAILERS) {
6196 /* We need a compression buffer in the DATA state to put the
6197 * output of compressed data, and in CRLF state to let the
6198 * TRAILERS state finish the job of removing the trailing CRLF.
6199 */
6200 if (unlikely(tmpbuf == NULL)) {
6201 /* this is the first time we need the compression buffer */
6202 tmpbuf = pool_alloc2(pool2_buffer);
6203 if (tmpbuf == NULL)
6204 goto aborted_xfer; /* no memory */
6205 }
6206
6207 ret = http_compression_buffer_init(s, res->buf, tmpbuf);
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006208 if (ret < 0) {
6209 res->flags |= CF_WAKE_WRITE;
William Lallemand82fe75c2012-10-23 10:25:10 +02006210 goto missing_data; /* not enough spaces in buffers */
Willy Tarreau4afd70a2014-01-25 02:26:39 +01006211 }
William Lallemand82fe75c2012-10-23 10:25:10 +02006212 compressing = 1;
6213 }
6214
Willy Tarreaud98cf932009-12-27 22:54:55 +01006215 while (1) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006216 switch (msg->msg_state - HTTP_MSG_DATA) {
6217 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
Willy Tarreauc623c172014-04-18 09:53:50 +02006218 /* we may have some pending data starting at res->buf->p */
6219 if (unlikely(s->comp_algo)) {
Willy Tarreau7f2f8d52014-04-18 00:20:14 +02006220 ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
William Lallemandbf3ae612012-11-19 12:35:37 +01006221 if (ret < 0)
6222 goto aborted_xfer;
Willy Tarreauc623c172014-04-18 09:53:50 +02006223
Willy Tarreaud5a67832014-04-21 10:54:27 +02006224 if (msg->chunk_len) {
6225 /* input empty or output full */
6226 if (res->buf->i > msg->next)
6227 res->flags |= CF_WAKE_WRITE;
Willy Tarreauc623c172014-04-18 09:53:50 +02006228 goto missing_data;
6229 }
William Lallemandbf3ae612012-11-19 12:35:37 +01006230 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006231 else {
Willy Tarreaud5a67832014-04-21 10:54:27 +02006232 if (msg->chunk_len > res->buf->i - msg->next) {
6233 /* output full */
6234 res->flags |= CF_WAKE_WRITE;
6235 goto missing_data;
6236 }
Willy Tarreauc623c172014-04-18 09:53:50 +02006237 msg->next += msg->chunk_len;
6238 msg->chunk_len = 0;
6239 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01006240
6241 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01006242 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02006243 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01006244 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01006245 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006246 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01006247 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006248 /* fall through for HTTP_MSG_CHUNK_CRLF */
6249
6250 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
6251 /* we want the CRLF after the data */
6252
6253 ret = http_skip_chunk_crlf(msg);
6254 if (ret == 0)
6255 goto missing_data;
6256 else if (ret < 0) {
6257 if (msg->err_pos >= 0)
6258 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
6259 goto return_bad_res;
6260 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006261 /* we're in MSG_CHUNK_SIZE now, fall through */
6262
6263 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01006264 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreauc24715e2014-04-17 15:21:20 +02006265 * set ->next to point to the body and switch to DATA or
Willy Tarreaua458b672012-03-05 11:17:50 +01006266 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01006267 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01006268
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006269 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02006270 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01006271 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006272 else if (ret < 0) {
6273 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006274 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006275 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006276 }
Willy Tarreau0161d622013-04-02 01:26:55 +02006277 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006278 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01006279
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006280 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
Willy Tarreau168ebc52014-04-18 00:53:43 +02006281 if (unlikely(compressing)) {
6282 /* we need to flush output contents before syncing FSMs */
6283 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6284 compressing = 0;
6285 }
6286
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006287 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006288 if (ret == 0)
6289 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006290 else if (ret < 0) {
6291 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01006292 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006293 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006294 }
Willy Tarreau168ebc52014-04-18 00:53:43 +02006295 /* we're in HTTP_MSG_DONE now, fall through */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006296
6297 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01006298 /* other states, DONE...TUNNEL */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006299 if (unlikely(compressing)) {
6300 /* we need to flush output contents before syncing FSMs */
6301 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
6302 compressing = 0;
6303 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006304
Willy Tarreauc623c172014-04-18 09:53:50 +02006305 /* we may have some pending data starting at res->buf->p
6306 * such as a last chunk of data or trailers.
6307 */
6308 b_adv(res->buf, msg->next);
6309 msg->next = 0;
6310
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006311 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02006312 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006313 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6314 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006315 channel_dont_close(res);
Willy Tarreau3ce10ff2014-04-22 08:24:38 +02006316
Willy Tarreau610ecce2010-01-04 21:15:02 +01006317 if (http_resync_states(s)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01006318 /* some state changes occurred, maybe the analyser
6319 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01006320 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006321 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006322 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006323 /* response errors are most likely due to
6324 * the client aborting the transfer.
6325 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006326 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006327 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01006328 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02006329 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01006330 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01006331 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006332 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01006333 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01006334 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006335 }
6336 }
6337
Willy Tarreaud98cf932009-12-27 22:54:55 +01006338 missing_data:
Willy Tarreauc623c172014-04-18 09:53:50 +02006339 /* we may have some pending data starting at res->buf->p */
Willy Tarreau168ebc52014-04-18 00:53:43 +02006340 if (unlikely(compressing)) {
6341 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
William Lallemand82fe75c2012-10-23 10:25:10 +02006342 compressing = 0;
6343 }
Willy Tarreauf003d372012-11-26 13:35:37 +01006344
Willy Tarreauc623c172014-04-18 09:53:50 +02006345 if ((s->comp_algo == NULL || msg->msg_state >= HTTP_MSG_TRAILERS)) {
6346 b_adv(res->buf, msg->next);
6347 msg->next = 0;
6348 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6349 }
6350
Willy Tarreauf003d372012-11-26 13:35:37 +01006351 if (res->flags & CF_SHUTW)
6352 goto aborted_xfer;
6353
6354 /* stop waiting for data if the input is closed before the end. If the
6355 * client side was already closed, it means that the client has aborted,
6356 * so we don't want to count this as a server abort. Otherwise it's a
6357 * server abort.
6358 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006359 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006360 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6361 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006362 if (!(s->flags & SN_ERR_MASK))
6363 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006364 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006365 if (objt_server(s->target))
6366 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006367 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006368 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006369
Willy Tarreau40dba092010-03-04 18:14:51 +01006370 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006371 if (!s->req->analysers)
6372 goto return_bad_res;
6373
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006374 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006375 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006376 * Similarly, with keep-alive on the client side, we don't want to forward a
6377 * close.
6378 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006379 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006380 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6381 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006382 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006383
Willy Tarreau5c620922011-05-11 19:56:11 +02006384 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006385 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006386 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006387 * modes are already handled by the stream sock layer. We must not do
6388 * this in content-length mode because it could present the MSG_MORE
6389 * flag with the last block of forwarded data, which would cause an
6390 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006391 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006392 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006393 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006394
Willy Tarreaud98cf932009-12-27 22:54:55 +01006395 /* the session handler will take care of timeouts and errors */
6396 return 0;
6397
Willy Tarreau40dba092010-03-04 18:14:51 +01006398 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006399 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006400 if (objt_server(s->target))
6401 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006402
6403 return_bad_res_stats_ok:
Willy Tarreaud01f4262014-04-21 11:00:13 +02006404 if (unlikely(compressing)) {
Willy Tarreau168ebc52014-04-18 00:53:43 +02006405 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
Willy Tarreaud01f4262014-04-21 11:00:13 +02006406 compressing = 0;
6407 }
6408
Willy Tarreauc623c172014-04-18 09:53:50 +02006409 /* we may have some pending data starting at res->buf->p */
6410 if (s->comp_algo == NULL) {
6411 b_adv(res->buf, msg->next);
6412 msg->next = 0;
6413 }
6414
Willy Tarreaud98cf932009-12-27 22:54:55 +01006415 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006416 /* don't send any error message as we're in the body */
6417 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006418 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006419 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006420 if (objt_server(s->target))
6421 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006422
6423 if (!(s->flags & SN_ERR_MASK))
6424 s->flags |= SN_ERR_PRXCOND;
6425 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006426 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006427 return 0;
6428
6429 aborted_xfer:
Willy Tarreau6fef8ae2014-04-22 21:22:06 +02006430 if (unlikely(compressing)) {
6431 http_compression_buffer_end(s, &res->buf, &tmpbuf, msg->msg_state >= HTTP_MSG_TRAILERS);
6432 compressing = 0;
6433 }
6434
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006435 txn->rsp.msg_state = HTTP_MSG_ERROR;
6436 /* don't send any error message as we're in the body */
6437 stream_int_retnclose(res->cons, NULL);
6438 res->analysers = 0;
6439 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6440
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006441 s->fe->fe_counters.cli_aborts++;
6442 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006443 if (objt_server(s->target))
6444 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006445
6446 if (!(s->flags & SN_ERR_MASK))
6447 s->flags |= SN_ERR_CLICL;
6448 if (!(s->flags & SN_FINST_MASK))
6449 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006450 return 0;
6451}
6452
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006453/* Iterate the same filter through all request headers.
6454 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006455 * Since it can manage the switch to another backend, it updates the per-proxy
6456 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006457 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006458int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006459{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006460 char term;
6461 char *cur_ptr, *cur_end, *cur_next;
6462 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006463 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006464 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006465 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006466
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006467 last_hdr = 0;
6468
Willy Tarreau9b28e032012-10-12 23:49:43 +02006469 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006470 old_idx = 0;
6471
6472 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006473 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006474 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006475 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006476 (exp->action == ACT_ALLOW ||
6477 exp->action == ACT_DENY ||
6478 exp->action == ACT_TARPIT))
6479 return 0;
6480
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006481 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006482 if (!cur_idx)
6483 break;
6484
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006485 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006486 cur_ptr = cur_next;
6487 cur_end = cur_ptr + cur_hdr->len;
6488 cur_next = cur_end + cur_hdr->cr + 1;
6489
6490 /* Now we have one header between cur_ptr and cur_end,
6491 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006492 */
6493
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006494 /* The annoying part is that pattern matching needs
6495 * that we modify the contents to null-terminate all
6496 * strings before testing them.
6497 */
6498
6499 term = *cur_end;
6500 *cur_end = '\0';
6501
6502 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6503 switch (exp->action) {
6504 case ACT_SETBE:
6505 /* It is not possible to jump a second time.
6506 * FIXME: should we return an HTTP/500 here so that
6507 * the admin knows there's a problem ?
6508 */
6509 if (t->be != t->fe)
6510 break;
6511
6512 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006513 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006514 last_hdr = 1;
6515 break;
6516
6517 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006518 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006519 last_hdr = 1;
6520 break;
6521
6522 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006523 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006524 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006525 break;
6526
6527 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006528 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006529 last_hdr = 1;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006530 break;
6531
6532 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006533 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6534 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006535 /* FIXME: if the user adds a newline in the replacement, the
6536 * index will not be recalculated for now, and the new line
6537 * will not be counted as a new header.
6538 */
6539
6540 cur_end += delta;
6541 cur_next += delta;
6542 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006543 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006544 break;
6545
6546 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006547 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006548 cur_next += delta;
6549
Willy Tarreaufa355d42009-11-29 18:12:29 +01006550 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006551 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6552 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006553 cur_hdr->len = 0;
6554 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006555 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006556 break;
6557
6558 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006559 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006560 if (cur_end)
6561 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006562
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006563 /* keep the link from this header to next one in case of later
6564 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006565 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006566 old_idx = cur_idx;
6567 }
6568 return 0;
6569}
6570
6571
6572/* Apply the filter to the request line.
6573 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6574 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006575 * Since it can manage the switch to another backend, it updates the per-proxy
6576 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006577 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006578int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006579{
6580 char term;
6581 char *cur_ptr, *cur_end;
6582 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006583 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006584 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006585
Willy Tarreau3d300592007-03-18 18:34:41 +01006586 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006587 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006588 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006589 (exp->action == ACT_ALLOW ||
6590 exp->action == ACT_DENY ||
6591 exp->action == ACT_TARPIT))
6592 return 0;
6593 else if (exp->action == ACT_REMOVE)
6594 return 0;
6595
6596 done = 0;
6597
Willy Tarreau9b28e032012-10-12 23:49:43 +02006598 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006599 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006600
6601 /* Now we have the request line between cur_ptr and cur_end */
6602
6603 /* The annoying part is that pattern matching needs
6604 * that we modify the contents to null-terminate all
6605 * strings before testing them.
6606 */
6607
6608 term = *cur_end;
6609 *cur_end = '\0';
6610
6611 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6612 switch (exp->action) {
6613 case ACT_SETBE:
6614 /* It is not possible to jump a second time.
6615 * FIXME: should we return an HTTP/500 here so that
6616 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006617 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006618 if (t->be != t->fe)
6619 break;
6620
6621 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006622 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006623 done = 1;
6624 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006625
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006626 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006627 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006628 done = 1;
6629 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006630
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006631 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006632 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006633 done = 1;
6634 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006635
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006636 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006637 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006638 done = 1;
6639 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006640
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006641 case ACT_REPLACE:
6642 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006643 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6644 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006645 /* FIXME: if the user adds a newline in the replacement, the
6646 * index will not be recalculated for now, and the new line
6647 * will not be counted as a new header.
6648 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006649
Willy Tarreaufa355d42009-11-29 18:12:29 +01006650 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006651 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006652 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006653 HTTP_MSG_RQMETH,
6654 cur_ptr, cur_end + 1,
6655 NULL, NULL);
6656 if (unlikely(!cur_end))
6657 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006658
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006659 /* we have a full request and we know that we have either a CR
6660 * or an LF at <ptr>.
6661 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006662 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6663 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006664 /* there is no point trying this regex on headers */
6665 return 1;
6666 }
6667 }
6668 *cur_end = term; /* restore the string terminator */
6669 return done;
6670}
Willy Tarreau97de6242006-12-27 17:18:38 +01006671
Willy Tarreau58f10d72006-12-04 02:26:12 +01006672
Willy Tarreau58f10d72006-12-04 02:26:12 +01006673
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006674/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006675 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006676 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006677 * unparsable request. Since it can manage the switch to another backend, it
6678 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006679 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006680int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006681{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006682 struct http_txn *txn = &s->txn;
6683 struct hdr_exp *exp;
6684
6685 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006686 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006687
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006688 /*
6689 * The interleaving of transformations and verdicts
6690 * makes it difficult to decide to continue or stop
6691 * the evaluation.
6692 */
6693
Willy Tarreau6c123b12010-01-28 20:22:06 +01006694 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6695 break;
6696
Willy Tarreau3d300592007-03-18 18:34:41 +01006697 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006698 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006699 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006700 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006701
6702 /* if this filter had a condition, evaluate it now and skip to
6703 * next filter if the condition does not match.
6704 */
6705 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006706 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006707 ret = acl_pass(ret);
6708 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6709 ret = !ret;
6710
6711 if (!ret)
6712 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006713 }
6714
6715 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006716 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006717 if (unlikely(ret < 0))
6718 return -1;
6719
6720 if (likely(ret == 0)) {
6721 /* The filter did not match the request, it can be
6722 * iterated through all headers.
6723 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006724 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006725 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006726 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006727 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006728}
6729
6730
Willy Tarreaua15645d2007-03-18 16:22:39 +01006731
Willy Tarreau58f10d72006-12-04 02:26:12 +01006732/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006733 * Try to retrieve the server associated to the appsession.
6734 * If the server is found, it's assigned to the session.
6735 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006736void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006737 struct http_txn *txn = &t->txn;
6738 appsess *asession = NULL;
6739 char *sessid_temp = NULL;
6740
Cyril Bontéb21570a2009-11-29 20:04:48 +01006741 if (len > t->be->appsession_len) {
6742 len = t->be->appsession_len;
6743 }
6744
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006745 if (t->be->options2 & PR_O2_AS_REQL) {
6746 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006747 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006748 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006749 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006750 }
6751
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006752 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006753 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6754 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6755 return;
6756 }
6757
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006758 memcpy(txn->sessid, buf, len);
6759 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006760 }
6761
6762 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6763 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6764 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6765 return;
6766 }
6767
Cyril Bontéb21570a2009-11-29 20:04:48 +01006768 memcpy(sessid_temp, buf, len);
6769 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006770
6771 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6772 /* free previously allocated memory */
6773 pool_free2(apools.sessid, sessid_temp);
6774
6775 if (asession != NULL) {
6776 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6777 if (!(t->be->options2 & PR_O2_AS_REQL))
6778 asession->request_count++;
6779
6780 if (asession->serverid != NULL) {
6781 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006782
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006783 while (srv) {
6784 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006785 if ((srv->state & SRV_RUNNING) ||
6786 (t->be->options & PR_O_PERSIST) ||
6787 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006788 /* we found the server and it's usable */
6789 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006790 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006791 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006792 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006793
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006794 break;
6795 } else {
6796 txn->flags &= ~TX_CK_MASK;
6797 txn->flags |= TX_CK_DOWN;
6798 }
6799 }
6800 srv = srv->next;
6801 }
6802 }
6803 }
6804}
6805
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006806/* Find the end of a cookie value contained between <s> and <e>. It works the
6807 * same way as with headers above except that the semi-colon also ends a token.
6808 * See RFC2965 for more information. Note that it requires a valid header to
6809 * return a valid result.
6810 */
6811char *find_cookie_value_end(char *s, const char *e)
6812{
6813 int quoted, qdpair;
6814
6815 quoted = qdpair = 0;
6816 for (; s < e; s++) {
6817 if (qdpair) qdpair = 0;
6818 else if (quoted) {
6819 if (*s == '\\') qdpair = 1;
6820 else if (*s == '"') quoted = 0;
6821 }
6822 else if (*s == '"') quoted = 1;
6823 else if (*s == ',' || *s == ';') return s;
6824 }
6825 return s;
6826}
6827
6828/* Delete a value in a header between delimiters <from> and <next> in buffer
6829 * <buf>. The number of characters displaced is returned, and the pointer to
6830 * the first delimiter is updated if required. The function tries as much as
6831 * possible to respect the following principles :
6832 * - replace <from> delimiter by the <next> one unless <from> points to a
6833 * colon, in which case <next> is simply removed
6834 * - set exactly one space character after the new first delimiter, unless
6835 * there are not enough characters in the block being moved to do so.
6836 * - remove unneeded spaces before the previous delimiter and after the new
6837 * one.
6838 *
6839 * It is the caller's responsibility to ensure that :
6840 * - <from> points to a valid delimiter or the colon ;
6841 * - <next> points to a valid delimiter or the final CR/LF ;
6842 * - there are non-space chars before <from> ;
6843 * - there is a CR/LF at or after <next>.
6844 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006845int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006846{
6847 char *prev = *from;
6848
6849 if (*prev == ':') {
6850 /* We're removing the first value, preserve the colon and add a
6851 * space if possible.
6852 */
6853 if (!http_is_crlf[(unsigned char)*next])
6854 next++;
6855 prev++;
6856 if (prev < next)
6857 *prev++ = ' ';
6858
6859 while (http_is_spht[(unsigned char)*next])
6860 next++;
6861 } else {
6862 /* Remove useless spaces before the old delimiter. */
6863 while (http_is_spht[(unsigned char)*(prev-1)])
6864 prev--;
6865 *from = prev;
6866
6867 /* copy the delimiter and if possible a space if we're
6868 * not at the end of the line.
6869 */
6870 if (!http_is_crlf[(unsigned char)*next]) {
6871 *prev++ = *next++;
6872 if (prev + 1 < next)
6873 *prev++ = ' ';
6874 while (http_is_spht[(unsigned char)*next])
6875 next++;
6876 }
6877 }
6878 return buffer_replace2(buf, prev, next, NULL, 0);
6879}
6880
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006881/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006882 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006883 * desirable to call it only when needed. This code is quite complex because
6884 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6885 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006886 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006887void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006888{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006889 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006890 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006891 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006892 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6893 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006894
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006895 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006896 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006897 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006898
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006899 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006900 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006901 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006902
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006903 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006904 hdr_beg = hdr_next;
6905 hdr_end = hdr_beg + cur_hdr->len;
6906 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006907
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006908 /* We have one full header between hdr_beg and hdr_end, and the
6909 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006910 * "Cookie:" headers.
6911 */
6912
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006913 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006914 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006915 old_idx = cur_idx;
6916 continue;
6917 }
6918
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006919 del_from = NULL; /* nothing to be deleted */
6920 preserve_hdr = 0; /* assume we may kill the whole header */
6921
Willy Tarreau58f10d72006-12-04 02:26:12 +01006922 /* Now look for cookies. Conforming to RFC2109, we have to support
6923 * attributes whose name begin with a '$', and associate them with
6924 * the right cookie, if we want to delete this cookie.
6925 * So there are 3 cases for each cookie read :
6926 * 1) it's a special attribute, beginning with a '$' : ignore it.
6927 * 2) it's a server id cookie that we *MAY* want to delete : save
6928 * some pointers on it (last semi-colon, beginning of cookie...)
6929 * 3) it's an application cookie : we *MAY* have to delete a previous
6930 * "special" cookie.
6931 * At the end of loop, if a "special" cookie remains, we may have to
6932 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006933 * *MUST* delete it.
6934 *
6935 * Note: RFC2965 is unclear about the processing of spaces around
6936 * the equal sign in the ATTR=VALUE form. A careful inspection of
6937 * the RFC explicitly allows spaces before it, and not within the
6938 * tokens (attrs or values). An inspection of RFC2109 allows that
6939 * too but section 10.1.3 lets one think that spaces may be allowed
6940 * after the equal sign too, resulting in some (rare) buggy
6941 * implementations trying to do that. So let's do what servers do.
6942 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6943 * allowed quoted strings in values, with any possible character
6944 * after a backslash, including control chars and delimitors, which
6945 * causes parsing to become ambiguous. Browsers also allow spaces
6946 * within values even without quotes.
6947 *
6948 * We have to keep multiple pointers in order to support cookie
6949 * removal at the beginning, middle or end of header without
6950 * corrupting the header. All of these headers are valid :
6951 *
6952 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6953 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6954 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6955 * | | | | | | | | |
6956 * | | | | | | | | hdr_end <--+
6957 * | | | | | | | +--> next
6958 * | | | | | | +----> val_end
6959 * | | | | | +-----------> val_beg
6960 * | | | | +--------------> equal
6961 * | | | +----------------> att_end
6962 * | | +---------------------> att_beg
6963 * | +--------------------------> prev
6964 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006965 */
6966
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006967 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6968 /* Iterate through all cookies on this line */
6969
6970 /* find att_beg */
6971 att_beg = prev + 1;
6972 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6973 att_beg++;
6974
6975 /* find att_end : this is the first character after the last non
6976 * space before the equal. It may be equal to hdr_end.
6977 */
6978 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006979
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006980 while (equal < hdr_end) {
6981 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006982 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006983 if (http_is_spht[(unsigned char)*equal++])
6984 continue;
6985 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006986 }
6987
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006988 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6989 * is between <att_beg> and <equal>, both may be identical.
6990 */
6991
6992 /* look for end of cookie if there is an equal sign */
6993 if (equal < hdr_end && *equal == '=') {
6994 /* look for the beginning of the value */
6995 val_beg = equal + 1;
6996 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6997 val_beg++;
6998
6999 /* find the end of the value, respecting quotes */
7000 next = find_cookie_value_end(val_beg, hdr_end);
7001
7002 /* make val_end point to the first white space or delimitor after the value */
7003 val_end = next;
7004 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7005 val_end--;
7006 } else {
7007 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01007008 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007009
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007010 /* We have nothing to do with attributes beginning with '$'. However,
7011 * they will automatically be removed if a header before them is removed,
7012 * since they're supposed to be linked together.
7013 */
7014 if (*att_beg == '$')
7015 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007016
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007017 /* Ignore cookies with no equal sign */
7018 if (equal == next) {
7019 /* This is not our cookie, so we must preserve it. But if we already
7020 * scheduled another cookie for removal, we cannot remove the
7021 * complete header, but we can remove the previous block itself.
7022 */
7023 preserve_hdr = 1;
7024 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007025 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007026 val_end += delta;
7027 next += delta;
7028 hdr_end += delta;
7029 hdr_next += delta;
7030 cur_hdr->len += delta;
7031 http_msg_move_end(&txn->req, delta);
7032 prev = del_from;
7033 del_from = NULL;
7034 }
7035 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01007036 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007037
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007038 /* if there are spaces around the equal sign, we need to
7039 * strip them otherwise we'll get trouble for cookie captures,
7040 * or even for rewrites. Since this happens extremely rarely,
7041 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007042 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007043 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7044 int stripped_before = 0;
7045 int stripped_after = 0;
7046
7047 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007048 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007049 equal += stripped_before;
7050 val_beg += stripped_before;
7051 }
7052
7053 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007054 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007055 val_beg += stripped_after;
7056 stripped_before += stripped_after;
7057 }
7058
7059 val_end += stripped_before;
7060 next += stripped_before;
7061 hdr_end += stripped_before;
7062 hdr_next += stripped_before;
7063 cur_hdr->len += stripped_before;
7064 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007065 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007066 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007067
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007068 /* First, let's see if we want to capture this cookie. We check
7069 * that we don't already have a client side cookie, because we
7070 * can only capture one. Also as an optimisation, we ignore
7071 * cookies shorter than the declared name.
7072 */
7073 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
7074 (val_end - att_beg >= t->fe->capture_namelen) &&
7075 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7076 int log_len = val_end - att_beg;
7077
7078 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
7079 Alert("HTTP logging : out of memory.\n");
7080 } else {
7081 if (log_len > t->fe->capture_len)
7082 log_len = t->fe->capture_len;
7083 memcpy(txn->cli_cookie, att_beg, log_len);
7084 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007085 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007086 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007087
Willy Tarreaubca99692010-10-06 19:25:55 +02007088 /* Persistence cookies in passive, rewrite or insert mode have the
7089 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007090 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007091 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007092 *
Willy Tarreaubca99692010-10-06 19:25:55 +02007093 * For cookies in prefix mode, the form is :
7094 *
7095 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007096 */
7097 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7098 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
7099 struct server *srv = t->be->srv;
7100 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007101
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007102 /* if we're in cookie prefix mode, we'll search the delimitor so that we
7103 * have the server ID between val_beg and delim, and the original cookie between
7104 * delim+1 and val_end. Otherwise, delim==val_end :
7105 *
7106 * Cookie: NAME=SRV; # in all but prefix modes
7107 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
7108 * | || || | |+-> next
7109 * | || || | +--> val_end
7110 * | || || +---------> delim
7111 * | || |+------------> val_beg
7112 * | || +-------------> att_end = equal
7113 * | |+-----------------> att_beg
7114 * | +------------------> prev
7115 * +-------------------------> hdr_beg
7116 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007117
Willy Tarreau67402132012-05-31 20:40:20 +02007118 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007119 for (delim = val_beg; delim < val_end; delim++)
7120 if (*delim == COOKIE_DELIM)
7121 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02007122 } else {
7123 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007124 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02007125 /* Now check if the cookie contains a date field, which would
7126 * appear after a vertical bar ('|') just after the server name
7127 * and before the delimiter.
7128 */
7129 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
7130 if (vbar1) {
7131 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02007132 * right is the last seen date. It is a base64 encoded
7133 * 30-bit value representing the UNIX date since the
7134 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02007135 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02007136 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02007137 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02007138 if (val_end - vbar1 >= 5) {
7139 val = b64tos30(vbar1);
7140 if (val > 0)
7141 txn->cookie_last_date = val << 2;
7142 }
7143 /* look for a second vertical bar */
7144 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
7145 if (vbar1 && (val_end - vbar1 > 5)) {
7146 val = b64tos30(vbar1 + 1);
7147 if (val > 0)
7148 txn->cookie_first_date = val << 2;
7149 }
Willy Tarreaubca99692010-10-06 19:25:55 +02007150 }
7151 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007152
Willy Tarreauf64d1412010-10-07 20:06:11 +02007153 /* if the cookie has an expiration date and the proxy wants to check
7154 * it, then we do that now. We first check if the cookie is too old,
7155 * then only if it has expired. We detect strict overflow because the
7156 * time resolution here is not great (4 seconds). Cookies with dates
7157 * in the future are ignored if their offset is beyond one day. This
7158 * allows an admin to fix timezone issues without expiring everyone
7159 * and at the same time avoids keeping unwanted side effects for too
7160 * long.
7161 */
7162 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007163 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
7164 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007165 txn->flags &= ~TX_CK_MASK;
7166 txn->flags |= TX_CK_OLD;
7167 delim = val_beg; // let's pretend we have not found the cookie
7168 txn->cookie_first_date = 0;
7169 txn->cookie_last_date = 0;
7170 }
7171 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02007172 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
7173 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02007174 txn->flags &= ~TX_CK_MASK;
7175 txn->flags |= TX_CK_EXPIRED;
7176 delim = val_beg; // let's pretend we have not found the cookie
7177 txn->cookie_first_date = 0;
7178 txn->cookie_last_date = 0;
7179 }
7180
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007181 /* Here, we'll look for the first running server which supports the cookie.
7182 * This allows to share a same cookie between several servers, for example
7183 * to dedicate backup servers to specific servers only.
7184 * However, to prevent clients from sticking to cookie-less backup server
7185 * when they have incidentely learned an empty cookie, we simply ignore
7186 * empty cookies and mark them as invalid.
7187 * The same behaviour is applied when persistence must be ignored.
7188 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02007189 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007190 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007191
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007192 while (srv) {
7193 if (srv->cookie && (srv->cklen == delim - val_beg) &&
7194 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
7195 if ((srv->state & SRV_RUNNING) ||
7196 (t->be->options & PR_O_PERSIST) ||
7197 (t->flags & SN_FORCE_PRST)) {
7198 /* we found the server and we can use it */
7199 txn->flags &= ~TX_CK_MASK;
7200 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
7201 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007202 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007203 break;
7204 } else {
7205 /* we found a server, but it's down,
7206 * mark it as such and go on in case
7207 * another one is available.
7208 */
7209 txn->flags &= ~TX_CK_MASK;
7210 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007211 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007212 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007213 srv = srv->next;
7214 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007215
Willy Tarreauf64d1412010-10-07 20:06:11 +02007216 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007217 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007218 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02007219 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
7220 txn->flags |= TX_CK_UNUSED;
7221 else
7222 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007223 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007224
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007225 /* depending on the cookie mode, we may have to either :
7226 * - delete the complete cookie if we're in insert+indirect mode, so that
7227 * the server never sees it ;
7228 * - remove the server id from the cookie value, and tag the cookie as an
7229 * application cookie so that it does not get accidentely removed later,
7230 * if we're in cookie prefix mode
7231 */
Willy Tarreau67402132012-05-31 20:40:20 +02007232 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007233 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007234
Willy Tarreau9b28e032012-10-12 23:49:43 +02007235 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007236 val_end += delta;
7237 next += delta;
7238 hdr_end += delta;
7239 hdr_next += delta;
7240 cur_hdr->len += delta;
7241 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007242
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007243 del_from = NULL;
7244 preserve_hdr = 1; /* we want to keep this cookie */
7245 }
7246 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02007247 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007248 del_from = prev;
7249 }
7250 } else {
7251 /* This is not our cookie, so we must preserve it. But if we already
7252 * scheduled another cookie for removal, we cannot remove the
7253 * complete header, but we can remove the previous block itself.
7254 */
7255 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007256
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007257 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007258 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01007259 if (att_beg >= del_from)
7260 att_beg += delta;
7261 if (att_end >= del_from)
7262 att_end += delta;
7263 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007264 val_end += delta;
7265 next += delta;
7266 hdr_end += delta;
7267 hdr_next += delta;
7268 cur_hdr->len += delta;
7269 http_msg_move_end(&txn->req, delta);
7270 prev = del_from;
7271 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007272 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007273 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007274
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007275 /* Look for the appsession cookie unless persistence must be ignored */
7276 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
7277 int cmp_len, value_len;
7278 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007279
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007280 if (t->be->options2 & PR_O2_AS_PFX) {
7281 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7282 value_begin = att_beg + t->be->appsession_name_len;
7283 value_len = val_end - att_beg - t->be->appsession_name_len;
7284 } else {
7285 cmp_len = att_end - att_beg;
7286 value_begin = val_beg;
7287 value_len = val_end - val_beg;
7288 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007289
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007290 /* let's see if the cookie is our appcookie */
7291 if (cmp_len == t->be->appsession_name_len &&
7292 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
7293 manage_client_side_appsession(t, value_begin, value_len);
7294 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007295 }
7296
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007297 /* continue with next cookie on this header line */
7298 att_beg = next;
7299 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007300
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007301 /* There are no more cookies on this line.
7302 * We may still have one (or several) marked for deletion at the
7303 * end of the line. We must do this now in two ways :
7304 * - if some cookies must be preserved, we only delete from the
7305 * mark to the end of line ;
7306 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01007307 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007308 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007309 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007310 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007311 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007312 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007313 cur_hdr->len += delta;
7314 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007315 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007316
7317 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007318 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7319 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007320 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007321 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007322 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007323 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007324 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007325 }
7326
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007327 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01007328 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02007329 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007330}
7331
7332
Willy Tarreaua15645d2007-03-18 16:22:39 +01007333/* Iterate the same filter through all response headers contained in <rtr>.
7334 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7335 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007336int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007337{
7338 char term;
7339 char *cur_ptr, *cur_end, *cur_next;
7340 int cur_idx, old_idx, last_hdr;
7341 struct http_txn *txn = &t->txn;
7342 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007343 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007344
7345 last_hdr = 0;
7346
Willy Tarreau9b28e032012-10-12 23:49:43 +02007347 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007348 old_idx = 0;
7349
7350 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007351 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007352 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007353 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007354 (exp->action == ACT_ALLOW ||
7355 exp->action == ACT_DENY))
7356 return 0;
7357
7358 cur_idx = txn->hdr_idx.v[old_idx].next;
7359 if (!cur_idx)
7360 break;
7361
7362 cur_hdr = &txn->hdr_idx.v[cur_idx];
7363 cur_ptr = cur_next;
7364 cur_end = cur_ptr + cur_hdr->len;
7365 cur_next = cur_end + cur_hdr->cr + 1;
7366
7367 /* Now we have one header between cur_ptr and cur_end,
7368 * and the next header starts at cur_next.
7369 */
7370
7371 /* The annoying part is that pattern matching needs
7372 * that we modify the contents to null-terminate all
7373 * strings before testing them.
7374 */
7375
7376 term = *cur_end;
7377 *cur_end = '\0';
7378
7379 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7380 switch (exp->action) {
7381 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007382 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007383 last_hdr = 1;
7384 break;
7385
7386 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007387 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007388 last_hdr = 1;
7389 break;
7390
7391 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007392 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7393 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007394 /* FIXME: if the user adds a newline in the replacement, the
7395 * index will not be recalculated for now, and the new line
7396 * will not be counted as a new header.
7397 */
7398
7399 cur_end += delta;
7400 cur_next += delta;
7401 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007402 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007403 break;
7404
7405 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007406 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007407 cur_next += delta;
7408
Willy Tarreaufa355d42009-11-29 18:12:29 +01007409 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007410 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7411 txn->hdr_idx.used--;
7412 cur_hdr->len = 0;
7413 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007414 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007415 break;
7416
7417 }
7418 }
7419 if (cur_end)
7420 *cur_end = term; /* restore the string terminator */
7421
7422 /* keep the link from this header to next one in case of later
7423 * removal of next header.
7424 */
7425 old_idx = cur_idx;
7426 }
7427 return 0;
7428}
7429
7430
7431/* Apply the filter to the status line in the response buffer <rtr>.
7432 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7433 * or -1 if a replacement resulted in an invalid status line.
7434 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007435int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007436{
7437 char term;
7438 char *cur_ptr, *cur_end;
7439 int done;
7440 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007441 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007442
7443
Willy Tarreau3d300592007-03-18 18:34:41 +01007444 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007445 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007446 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007447 (exp->action == ACT_ALLOW ||
7448 exp->action == ACT_DENY))
7449 return 0;
7450 else if (exp->action == ACT_REMOVE)
7451 return 0;
7452
7453 done = 0;
7454
Willy Tarreau9b28e032012-10-12 23:49:43 +02007455 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007456 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007457
7458 /* Now we have the status line between cur_ptr and cur_end */
7459
7460 /* The annoying part is that pattern matching needs
7461 * that we modify the contents to null-terminate all
7462 * strings before testing them.
7463 */
7464
7465 term = *cur_end;
7466 *cur_end = '\0';
7467
7468 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7469 switch (exp->action) {
7470 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007471 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007472 done = 1;
7473 break;
7474
7475 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007476 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007477 done = 1;
7478 break;
7479
7480 case ACT_REPLACE:
7481 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007482 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7483 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007484 /* FIXME: if the user adds a newline in the replacement, the
7485 * index will not be recalculated for now, and the new line
7486 * will not be counted as a new header.
7487 */
7488
Willy Tarreaufa355d42009-11-29 18:12:29 +01007489 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007490 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007491 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007492 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007493 cur_ptr, cur_end + 1,
7494 NULL, NULL);
7495 if (unlikely(!cur_end))
7496 return -1;
7497
7498 /* we have a full respnse and we know that we have either a CR
7499 * or an LF at <ptr>.
7500 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007501 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007502 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007503 /* there is no point trying this regex on headers */
7504 return 1;
7505 }
7506 }
7507 *cur_end = term; /* restore the string terminator */
7508 return done;
7509}
7510
7511
7512
7513/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007514 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007515 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7516 * unparsable response.
7517 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007518int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007519{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007520 struct http_txn *txn = &s->txn;
7521 struct hdr_exp *exp;
7522
7523 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007524 int ret;
7525
7526 /*
7527 * The interleaving of transformations and verdicts
7528 * makes it difficult to decide to continue or stop
7529 * the evaluation.
7530 */
7531
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007532 if (txn->flags & TX_SVDENY)
7533 break;
7534
Willy Tarreau3d300592007-03-18 18:34:41 +01007535 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007536 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7537 exp->action == ACT_PASS)) {
7538 exp = exp->next;
7539 continue;
7540 }
7541
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007542 /* if this filter had a condition, evaluate it now and skip to
7543 * next filter if the condition does not match.
7544 */
7545 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007546 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007547 ret = acl_pass(ret);
7548 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7549 ret = !ret;
7550 if (!ret)
7551 continue;
7552 }
7553
Willy Tarreaua15645d2007-03-18 16:22:39 +01007554 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007555 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007556 if (unlikely(ret < 0))
7557 return -1;
7558
7559 if (likely(ret == 0)) {
7560 /* The filter did not match the response, it can be
7561 * iterated through all headers.
7562 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007563 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007564 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007565 }
7566 return 0;
7567}
7568
7569
Willy Tarreaua15645d2007-03-18 16:22:39 +01007570/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007571 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007572 * desirable to call it only when needed. This function is also used when we
7573 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007574 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007575void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007576{
7577 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007578 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007579 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007580 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007581 char *hdr_beg, *hdr_end, *hdr_next;
7582 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007583
Willy Tarreaua15645d2007-03-18 16:22:39 +01007584 /* Iterate through the headers.
7585 * we start with the start line.
7586 */
7587 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007588 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007589
7590 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7591 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007592 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007593
7594 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007595 hdr_beg = hdr_next;
7596 hdr_end = hdr_beg + cur_hdr->len;
7597 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007598
Willy Tarreau24581ba2010-08-31 22:39:35 +02007599 /* We have one full header between hdr_beg and hdr_end, and the
7600 * next header starts at hdr_next. We're only interested in
7601 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007602 */
7603
Willy Tarreau24581ba2010-08-31 22:39:35 +02007604 is_cookie2 = 0;
7605 prev = hdr_beg + 10;
7606 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007607 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007608 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7609 if (!val) {
7610 old_idx = cur_idx;
7611 continue;
7612 }
7613 is_cookie2 = 1;
7614 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007615 }
7616
Willy Tarreau24581ba2010-08-31 22:39:35 +02007617 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7618 * <prev> points to the colon.
7619 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007620 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007621
Willy Tarreau24581ba2010-08-31 22:39:35 +02007622 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7623 * check-cache is enabled) and we are not interested in checking
7624 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007625 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007626 if (t->be->cookie_name == NULL &&
7627 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007628 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007629 return;
7630
Willy Tarreau24581ba2010-08-31 22:39:35 +02007631 /* OK so now we know we have to process this response cookie.
7632 * The format of the Set-Cookie header is slightly different
7633 * from the format of the Cookie header in that it does not
7634 * support the comma as a cookie delimiter (thus the header
7635 * cannot be folded) because the Expires attribute described in
7636 * the original Netscape's spec may contain an unquoted date
7637 * with a comma inside. We have to live with this because
7638 * many browsers don't support Max-Age and some browsers don't
7639 * support quoted strings. However the Set-Cookie2 header is
7640 * clean.
7641 *
7642 * We have to keep multiple pointers in order to support cookie
7643 * removal at the beginning, middle or end of header without
7644 * corrupting the header (in case of set-cookie2). A special
7645 * pointer, <scav> points to the beginning of the set-cookie-av
7646 * fields after the first semi-colon. The <next> pointer points
7647 * either to the end of line (set-cookie) or next unquoted comma
7648 * (set-cookie2). All of these headers are valid :
7649 *
7650 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7651 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7652 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7653 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7654 * | | | | | | | | | |
7655 * | | | | | | | | +-> next hdr_end <--+
7656 * | | | | | | | +------------> scav
7657 * | | | | | | +--------------> val_end
7658 * | | | | | +--------------------> val_beg
7659 * | | | | +----------------------> equal
7660 * | | | +------------------------> att_end
7661 * | | +----------------------------> att_beg
7662 * | +------------------------------> prev
7663 * +-----------------------------------------> hdr_beg
7664 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007665
Willy Tarreau24581ba2010-08-31 22:39:35 +02007666 for (; prev < hdr_end; prev = next) {
7667 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007668
Willy Tarreau24581ba2010-08-31 22:39:35 +02007669 /* find att_beg */
7670 att_beg = prev + 1;
7671 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7672 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007673
Willy Tarreau24581ba2010-08-31 22:39:35 +02007674 /* find att_end : this is the first character after the last non
7675 * space before the equal. It may be equal to hdr_end.
7676 */
7677 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007678
Willy Tarreau24581ba2010-08-31 22:39:35 +02007679 while (equal < hdr_end) {
7680 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7681 break;
7682 if (http_is_spht[(unsigned char)*equal++])
7683 continue;
7684 att_end = equal;
7685 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007686
Willy Tarreau24581ba2010-08-31 22:39:35 +02007687 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7688 * is between <att_beg> and <equal>, both may be identical.
7689 */
7690
7691 /* look for end of cookie if there is an equal sign */
7692 if (equal < hdr_end && *equal == '=') {
7693 /* look for the beginning of the value */
7694 val_beg = equal + 1;
7695 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7696 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007697
Willy Tarreau24581ba2010-08-31 22:39:35 +02007698 /* find the end of the value, respecting quotes */
7699 next = find_cookie_value_end(val_beg, hdr_end);
7700
7701 /* make val_end point to the first white space or delimitor after the value */
7702 val_end = next;
7703 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7704 val_end--;
7705 } else {
7706 /* <equal> points to next comma, semi-colon or EOL */
7707 val_beg = val_end = next = equal;
7708 }
7709
7710 if (next < hdr_end) {
7711 /* Set-Cookie2 supports multiple cookies, and <next> points to
7712 * a colon or semi-colon before the end. So skip all attr-value
7713 * pairs and look for the next comma. For Set-Cookie, since
7714 * commas are permitted in values, skip to the end.
7715 */
7716 if (is_cookie2)
7717 next = find_hdr_value_end(next, hdr_end);
7718 else
7719 next = hdr_end;
7720 }
7721
7722 /* Now everything is as on the diagram above */
7723
7724 /* Ignore cookies with no equal sign */
7725 if (equal == val_end)
7726 continue;
7727
7728 /* If there are spaces around the equal sign, we need to
7729 * strip them otherwise we'll get trouble for cookie captures,
7730 * or even for rewrites. Since this happens extremely rarely,
7731 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007732 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007733 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7734 int stripped_before = 0;
7735 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007736
Willy Tarreau24581ba2010-08-31 22:39:35 +02007737 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007738 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007739 equal += stripped_before;
7740 val_beg += stripped_before;
7741 }
7742
7743 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007744 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007745 val_beg += stripped_after;
7746 stripped_before += stripped_after;
7747 }
7748
7749 val_end += stripped_before;
7750 next += stripped_before;
7751 hdr_end += stripped_before;
7752 hdr_next += stripped_before;
7753 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007754 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007755 }
7756
7757 /* First, let's see if we want to capture this cookie. We check
7758 * that we don't already have a server side cookie, because we
7759 * can only capture one. Also as an optimisation, we ignore
7760 * cookies shorter than the declared name.
7761 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007762 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007763 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007764 (val_end - att_beg >= t->fe->capture_namelen) &&
7765 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7766 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007767 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007768 Alert("HTTP logging : out of memory.\n");
7769 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007770 else {
7771 if (log_len > t->fe->capture_len)
7772 log_len = t->fe->capture_len;
7773 memcpy(txn->srv_cookie, att_beg, log_len);
7774 txn->srv_cookie[log_len] = 0;
7775 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007776 }
7777
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007778 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007779 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007780 if (!(t->flags & SN_IGNORE_PRST) &&
7781 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7782 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007783 /* assume passive cookie by default */
7784 txn->flags &= ~TX_SCK_MASK;
7785 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007786
7787 /* If the cookie is in insert mode on a known server, we'll delete
7788 * this occurrence because we'll insert another one later.
7789 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007790 * a direct access.
7791 */
Willy Tarreau67402132012-05-31 20:40:20 +02007792 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007793 /* The "preserve" flag was set, we don't want to touch the
7794 * server's cookie.
7795 */
7796 }
Willy Tarreau67402132012-05-31 20:40:20 +02007797 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7798 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007799 /* this cookie must be deleted */
7800 if (*prev == ':' && next == hdr_end) {
7801 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007802 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007803 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7804 txn->hdr_idx.used--;
7805 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007806 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007807 hdr_next += delta;
7808 http_msg_move_end(&txn->rsp, delta);
7809 /* note: while both invalid now, <next> and <hdr_end>
7810 * are still equal, so the for() will stop as expected.
7811 */
7812 } else {
7813 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007814 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007815 next = prev;
7816 hdr_end += delta;
7817 hdr_next += delta;
7818 cur_hdr->len += delta;
7819 http_msg_move_end(&txn->rsp, delta);
7820 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007821 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007822 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007823 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007824 }
Willy Tarreau67402132012-05-31 20:40:20 +02007825 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007826 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007827 * with this server since we know it.
7828 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007829 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007830 next += delta;
7831 hdr_end += delta;
7832 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007833 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007834 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007835
Willy Tarreauf1348312010-10-07 15:54:11 +02007836 txn->flags &= ~TX_SCK_MASK;
7837 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007838 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007839 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007840 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007841 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007842 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007843 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007844 next += delta;
7845 hdr_end += delta;
7846 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007847 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007848 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007849
Willy Tarreau827aee92011-03-10 16:55:02 +01007850 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007851 txn->flags &= ~TX_SCK_MASK;
7852 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007853 }
7854 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007855 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7856 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007857 int cmp_len, value_len;
7858 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007859
Cyril Bontéb21570a2009-11-29 20:04:48 +01007860 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007861 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7862 value_begin = att_beg + t->be->appsession_name_len;
7863 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007864 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007865 cmp_len = att_end - att_beg;
7866 value_begin = val_beg;
7867 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007868 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007869
Cyril Bonté17530c32010-04-06 21:11:10 +02007870 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007871 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7872 /* free a possibly previously allocated memory */
7873 pool_free2(apools.sessid, txn->sessid);
7874
Cyril Bontéb21570a2009-11-29 20:04:48 +01007875 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007876 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007877 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7878 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7879 return;
7880 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007881 memcpy(txn->sessid, value_begin, value_len);
7882 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007883 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007884 }
7885 /* that's done for this cookie, check the next one on the same
7886 * line when next != hdr_end (only if is_cookie2).
7887 */
7888 }
7889 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007890 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007891 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007892
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007893 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007894 appsess *asession = NULL;
7895 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007896 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007897 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007898 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007899 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7900 Alert("Not enough Memory process_srv():asession:calloc().\n");
7901 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7902 return;
7903 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007904 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7905
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007906 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7907 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7908 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007909 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007910 return;
7911 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007912 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007913 asession->sessid[t->be->appsession_len] = 0;
7914
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007915 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007916 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007917 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007918 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007919 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007920 return;
7921 }
7922 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007923 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007924
7925 asession->request_count = 0;
7926 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7927 }
7928
7929 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7930 asession->request_count++;
7931 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007932}
7933
7934
Willy Tarreaua15645d2007-03-18 16:22:39 +01007935/*
7936 * Check if response is cacheable or not. Updates t->flags.
7937 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007938void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007939{
7940 struct http_txn *txn = &t->txn;
7941 char *p1, *p2;
7942
7943 char *cur_ptr, *cur_end, *cur_next;
7944 int cur_idx;
7945
Willy Tarreau5df51872007-11-25 16:20:08 +01007946 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007947 return;
7948
7949 /* Iterate through the headers.
7950 * we start with the start line.
7951 */
7952 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007953 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007954
7955 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7956 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007957 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007958
7959 cur_hdr = &txn->hdr_idx.v[cur_idx];
7960 cur_ptr = cur_next;
7961 cur_end = cur_ptr + cur_hdr->len;
7962 cur_next = cur_end + cur_hdr->cr + 1;
7963
7964 /* We have one full header between cur_ptr and cur_end, and the
7965 * next header starts at cur_next. We're only interested in
7966 * "Cookie:" headers.
7967 */
7968
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007969 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7970 if (val) {
7971 if ((cur_end - (cur_ptr + val) >= 8) &&
7972 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7973 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7974 return;
7975 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007976 }
7977
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007978 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7979 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007980 continue;
7981
7982 /* OK, right now we know we have a cache-control header at cur_ptr */
7983
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007984 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007985
7986 if (p1 >= cur_end) /* no more info */
7987 continue;
7988
7989 /* p1 is at the beginning of the value */
7990 p2 = p1;
7991
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007992 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007993 p2++;
7994
7995 /* we have a complete value between p1 and p2 */
7996 if (p2 < cur_end && *p2 == '=') {
7997 /* we have something of the form no-cache="set-cookie" */
7998 if ((cur_end - p1 >= 21) &&
7999 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
8000 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01008001 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008002 continue;
8003 }
8004
8005 /* OK, so we know that either p2 points to the end of string or to a comma */
8006 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
Willy Tarreau5b15f902013-07-04 12:46:56 +02008007 ((p2 - p1 == 8) && strncasecmp(p1, "no-cache", 8) == 0) ||
Willy Tarreaua15645d2007-03-18 16:22:39 +01008008 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
8009 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
8010 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008011 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008012 return;
8013 }
8014
8015 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01008016 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01008017 continue;
8018 }
8019 }
8020}
8021
8022
Willy Tarreau58f10d72006-12-04 02:26:12 +01008023/*
8024 * Try to retrieve a known appsession in the URI, then the associated server.
8025 * If the server is found, it's assigned to the session.
8026 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008027void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01008028{
Cyril Bontéb21570a2009-11-29 20:04:48 +01008029 char *end_params, *first_param, *cur_param, *next_param;
8030 char separator;
8031 int value_len;
8032
8033 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008034
Willy Tarreaue2e27a52007-04-01 00:01:37 +02008035 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02008036 (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST && t->txn.meth != HTTP_METH_HEAD)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008037 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008038 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008039
Cyril Bontéb21570a2009-11-29 20:04:48 +01008040 first_param = NULL;
8041 switch (mode) {
8042 case PR_O2_AS_M_PP:
8043 first_param = memchr(begin, ';', len);
8044 break;
8045 case PR_O2_AS_M_QS:
8046 first_param = memchr(begin, '?', len);
8047 break;
8048 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008049
Cyril Bontéb21570a2009-11-29 20:04:48 +01008050 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01008051 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01008052 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008053
Cyril Bontéb21570a2009-11-29 20:04:48 +01008054 switch (mode) {
8055 case PR_O2_AS_M_PP:
8056 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
8057 end_params = (char *) begin + len;
8058 }
8059 separator = ';';
8060 break;
8061 case PR_O2_AS_M_QS:
8062 end_params = (char *) begin + len;
8063 separator = '&';
8064 break;
8065 default:
8066 /* unknown mode, shouldn't happen */
8067 return;
8068 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008069
Cyril Bontéb21570a2009-11-29 20:04:48 +01008070 cur_param = next_param = end_params;
8071 while (cur_param > first_param) {
8072 cur_param--;
8073 if ((cur_param[0] == separator) || (cur_param == first_param)) {
8074 /* let's see if this is the appsession parameter */
8075 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
8076 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
8077 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
8078 /* Cool... it's the right one */
8079 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
8080 value_len = MIN(t->be->appsession_len, next_param - cur_param);
8081 if (value_len > 0) {
8082 manage_client_side_appsession(t, cur_param, value_len);
8083 }
8084 break;
8085 }
8086 next_param = cur_param;
8087 }
8088 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01008089#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02008090 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02008091 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008092#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01008093}
8094
Willy Tarreaub2513902006-12-17 14:52:38 +01008095/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02008096 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008097 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01008098 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02008099 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01008100 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01008101 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008102 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01008103 */
Willy Tarreau295a8372011-03-10 11:25:07 +01008104int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01008105{
8106 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008107 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008108 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreaub2513902006-12-17 14:52:38 +01008109
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008110 if (!uri_auth)
8111 return 0;
8112
Cyril Bonté70be45d2010-10-12 00:14:35 +02008113 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01008114 return 0;
8115
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01008116 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008117 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01008118 return 0;
8119
Willy Tarreau414e9bb2013-11-23 00:30:38 +01008120 if (memcmp(uri, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01008121 return 0;
8122
Willy Tarreaub2513902006-12-17 14:52:38 +01008123 return 1;
8124}
8125
Willy Tarreau4076a152009-04-02 15:18:36 +02008126/*
8127 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008128 * By default it tries to report the error position as msg->err_pos. However if
8129 * this one is not set, it will then report msg->next, which is the last known
8130 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008131 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02008132 */
8133void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008134 struct http_msg *msg,
Willy Tarreau3770f232013-12-07 00:01:53 +01008135 enum ht_state state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02008136{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008137 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008138 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01008139
Willy Tarreau9b28e032012-10-12 23:49:43 +02008140 es->len = MIN(chn->buf->i, sizeof(es->buf));
8141 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008142 len1 = MIN(len1, es->len);
8143 len2 = es->len - len1; /* remaining data if buffer wraps */
8144
Willy Tarreau9b28e032012-10-12 23:49:43 +02008145 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008146 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02008147 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008148
Willy Tarreau4076a152009-04-02 15:18:36 +02008149 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008150 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008151 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02008152 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01008153
Willy Tarreau4076a152009-04-02 15:18:36 +02008154 es->when = date; // user-visible date
8155 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008156 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02008157 es->oe = other_end;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008158 if (objt_conn(s->req->prod->end))
8159 es->src = __objt_conn(s->req->prod->end)->addr.from;
8160 else
8161 memset(&es->src, 0, sizeof(es->src));
8162
Willy Tarreau078272e2010-12-12 12:46:33 +01008163 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01008164 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008165 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008166 es->s_flags = s->flags;
8167 es->t_flags = s->txn.flags;
8168 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008169 es->b_out = chn->buf->o;
8170 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02008171 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02008172 es->m_clen = msg->chunk_len;
8173 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02008174}
Willy Tarreaub2513902006-12-17 14:52:38 +01008175
Willy Tarreau294c4732011-12-16 21:35:50 +01008176/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8177 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8178 * performed over the whole headers. Otherwise it must contain a valid header
8179 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8180 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8181 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8182 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008183 * -1. The value fetch stops at commas, so this function is suited for use with
8184 * list headers.
Willy Tarreau294c4732011-12-16 21:35:50 +01008185 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02008186 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008187unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01008188 struct hdr_idx *idx, int occ,
8189 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02008190{
Willy Tarreau294c4732011-12-16 21:35:50 +01008191 struct hdr_ctx local_ctx;
8192 char *ptr_hist[MAX_HDR_HISTORY];
8193 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02008194 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01008195 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02008196
Willy Tarreau294c4732011-12-16 21:35:50 +01008197 if (!ctx) {
8198 local_ctx.idx = 0;
8199 ctx = &local_ctx;
8200 }
8201
Willy Tarreaubce70882009-09-07 11:51:47 +02008202 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008203 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008204 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02008205 occ--;
8206 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008207 *vptr = ctx->line + ctx->val;
8208 *vlen = ctx->vlen;
8209 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008210 }
8211 }
Willy Tarreau294c4732011-12-16 21:35:50 +01008212 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02008213 }
8214
8215 /* negative occurrence, we scan all the list then walk back */
8216 if (-occ > MAX_HDR_HISTORY)
8217 return 0;
8218
Willy Tarreau294c4732011-12-16 21:35:50 +01008219 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008220 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01008221 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8222 len_hist[hist_ptr] = ctx->vlen;
8223 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02008224 hist_ptr = 0;
8225 found++;
8226 }
8227 if (-occ > found)
8228 return 0;
8229 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
Willy Tarreau67dad272013-06-12 22:27:44 +02008230 * find occurrence -occ. 0 <= hist_ptr < MAX_HDR_HISTORY, and we have
8231 * -10 <= occ <= -1. So we have to check [hist_ptr%MAX_HDR_HISTORY+occ]
8232 * to remain in the 0..9 range.
Willy Tarreaubce70882009-09-07 11:51:47 +02008233 */
Willy Tarreau67dad272013-06-12 22:27:44 +02008234 hist_ptr += occ + MAX_HDR_HISTORY;
Willy Tarreaubce70882009-09-07 11:51:47 +02008235 if (hist_ptr >= MAX_HDR_HISTORY)
8236 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01008237 *vptr = ptr_hist[hist_ptr];
8238 *vlen = len_hist[hist_ptr];
8239 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02008240}
8241
Willy Tarreau04ff9f12013-06-10 18:39:42 +02008242/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
8243 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
8244 * performed over the whole headers. Otherwise it must contain a valid header
8245 * context, initialised with ctx->idx=0 for the first lookup in a series. If
8246 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
8247 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
8248 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
8249 * -1. This function differs from http_get_hdr() in that it only returns full
8250 * line header values and does not stop at commas.
8251 * The return value is 0 if nothing was found, or non-zero otherwise.
8252 */
8253unsigned int http_get_fhdr(const struct http_msg *msg, const char *hname, int hlen,
8254 struct hdr_idx *idx, int occ,
8255 struct hdr_ctx *ctx, char **vptr, int *vlen)
8256{
8257 struct hdr_ctx local_ctx;
8258 char *ptr_hist[MAX_HDR_HISTORY];
8259 int len_hist[MAX_HDR_HISTORY];
8260 unsigned int hist_ptr;
8261 int found;
8262
8263 if (!ctx) {
8264 local_ctx.idx = 0;
8265 ctx = &local_ctx;
8266 }
8267
8268 if (occ >= 0) {
8269 /* search from the beginning */
8270 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8271 occ--;
8272 if (occ <= 0) {
8273 *vptr = ctx->line + ctx->val;
8274 *vlen = ctx->vlen;
8275 return 1;
8276 }
8277 }
8278 return 0;
8279 }
8280
8281 /* negative occurrence, we scan all the list then walk back */
8282 if (-occ > MAX_HDR_HISTORY)
8283 return 0;
8284
8285 found = hist_ptr = 0;
8286 while (http_find_full_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
8287 ptr_hist[hist_ptr] = ctx->line + ctx->val;
8288 len_hist[hist_ptr] = ctx->vlen;
8289 if (++hist_ptr >= MAX_HDR_HISTORY)
8290 hist_ptr = 0;
8291 found++;
8292 }
8293 if (-occ > found)
8294 return 0;
8295 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
8296 * find occurrence -occ, so we have to check [hist_ptr+occ].
8297 */
8298 hist_ptr += occ;
8299 if (hist_ptr >= MAX_HDR_HISTORY)
8300 hist_ptr -= MAX_HDR_HISTORY;
8301 *vptr = ptr_hist[hist_ptr];
8302 *vlen = len_hist[hist_ptr];
8303 return 1;
8304}
8305
Willy Tarreaubaaee002006-06-26 02:48:02 +02008306/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02008307 * Print a debug line with a header. Always stop at the first CR or LF char,
8308 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
8309 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01008310 */
8311void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
8312{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008313 int max;
8314 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaub363a1f2013-10-01 10:45:07 +02008315 dir,
8316 objt_conn(t->req->prod->end) ? (unsigned short)objt_conn(t->req->prod->end)->t.sock.fd : -1,
8317 objt_conn(t->req->cons->end) ? (unsigned short)objt_conn(t->req->cons->end)->t.sock.fd : -1);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008318
8319 for (max = 0; start + max < end; max++)
8320 if (start[max] == '\r' || start[max] == '\n')
8321 break;
8322
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008323 UBOUND(max, trash.size - trash.len - 3);
8324 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8325 trash.str[trash.len++] = '\n';
Willy Tarreau89efaed2013-12-13 15:14:55 +01008326 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau58f10d72006-12-04 02:26:12 +01008327}
8328
Willy Tarreau0937bc42009-12-22 15:03:09 +01008329/*
8330 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8331 * the required fields are properly allocated and that we only need to (re)init
8332 * them. This should be used before processing any new request.
8333 */
8334void http_init_txn(struct session *s)
8335{
8336 struct http_txn *txn = &s->txn;
8337 struct proxy *fe = s->fe;
8338
8339 txn->flags = 0;
8340 txn->status = -1;
8341
Willy Tarreauf64d1412010-10-07 20:06:11 +02008342 txn->cookie_first_date = 0;
8343 txn->cookie_last_date = 0;
8344
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008345 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008346 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008347 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008348 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008349 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008350 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008351 txn->req.chunk_len = 0LL;
8352 txn->req.body_len = 0LL;
8353 txn->rsp.chunk_len = 0LL;
8354 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008355 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8356 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008357 txn->req.chn = s->req;
8358 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008359
8360 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008361
8362 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8363 if (fe->options2 & PR_O2_REQBUG_OK)
8364 txn->req.err_pos = -1; /* let buggy requests pass */
8365
Willy Tarreau46023632010-01-07 22:51:47 +01008366 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008367 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8368
Willy Tarreau46023632010-01-07 22:51:47 +01008369 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008370 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8371
8372 if (txn->hdr_idx.v)
8373 hdr_idx_init(&txn->hdr_idx);
8374}
8375
8376/* to be used at the end of a transaction */
8377void http_end_txn(struct session *s)
8378{
8379 struct http_txn *txn = &s->txn;
8380
Willy Tarreau75195602014-03-11 15:48:55 +01008381 /* release any possible compression context */
8382 if (s->flags & SN_COMP_READY)
8383 s->comp_algo->end(&s->comp_ctx);
8384 s->comp_algo = NULL;
8385 s->flags &= ~SN_COMP_READY;
8386
Willy Tarreau0937bc42009-12-22 15:03:09 +01008387 /* these ones will have been dynamically allocated */
8388 pool_free2(pool2_requri, txn->uri);
8389 pool_free2(pool2_capture, txn->cli_cookie);
8390 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008391 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008392 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008393
William Lallemanda73203e2012-03-12 12:48:57 +01008394 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008395 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008396 txn->uri = NULL;
8397 txn->srv_cookie = NULL;
8398 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008399
8400 if (txn->req.cap) {
8401 struct cap_hdr *h;
8402 for (h = s->fe->req_cap; h; h = h->next)
8403 pool_free2(h->pool, txn->req.cap[h->index]);
8404 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8405 }
8406
8407 if (txn->rsp.cap) {
8408 struct cap_hdr *h;
8409 for (h = s->fe->rsp_cap; h; h = h->next)
8410 pool_free2(h->pool, txn->rsp.cap[h->index]);
8411 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8412 }
8413
Willy Tarreau0937bc42009-12-22 15:03:09 +01008414}
8415
8416/* to be used at the end of a transaction to prepare a new one */
8417void http_reset_txn(struct session *s)
8418{
8419 http_end_txn(s);
8420 http_init_txn(s);
8421
8422 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008423 s->logs.logwait = s->fe->to_log;
Willy Tarreauabcd5142013-06-11 17:18:02 +02008424 s->logs.level = 0;
Simon Hormanaf514952011-06-21 14:34:57 +09008425 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008426 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008427 /* re-init store persistence */
8428 s->store_count = 0;
Willy Tarreau1f0da242014-01-25 11:01:50 +01008429 s->uniq_id = global.req_count++;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008430
Willy Tarreau0937bc42009-12-22 15:03:09 +01008431 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008432
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008433 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008434
Willy Tarreau739cfba2010-01-25 23:11:14 +01008435 /* We must trim any excess data from the response buffer, because we
8436 * may have blocked an invalid response from a server that we don't
8437 * want to accidentely forward once we disable the analysers, nor do
8438 * we want those data to come along with next response. A typical
8439 * example of such data would be from a buggy server responding to
8440 * a HEAD with some data, or sending more than the advertised
8441 * content-length.
8442 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008443 if (unlikely(s->rep->buf->i))
8444 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008445
Willy Tarreau0937bc42009-12-22 15:03:09 +01008446 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008447 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008448
Willy Tarreaud04e8582010-05-31 12:31:35 +02008449 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008450 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008451
8452 s->req->rex = TICK_ETERNITY;
8453 s->req->wex = TICK_ETERNITY;
8454 s->req->analyse_exp = TICK_ETERNITY;
8455 s->rep->rex = TICK_ETERNITY;
8456 s->rep->wex = TICK_ETERNITY;
8457 s->rep->analyse_exp = TICK_ETERNITY;
8458}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008459
Willy Tarreauff011f22011-01-06 17:51:27 +01008460void free_http_req_rules(struct list *r) {
8461 struct http_req_rule *tr, *pr;
8462
8463 list_for_each_entry_safe(pr, tr, r, list) {
8464 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008465 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008466 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008467
8468 free(pr);
8469 }
8470}
8471
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008472/* parse an "http-request" rule */
Willy Tarreauff011f22011-01-06 17:51:27 +01008473struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8474{
8475 struct http_req_rule *rule;
8476 int cur_arg;
8477
8478 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8479 if (!rule) {
8480 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008481 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008482 }
8483
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008484 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008485 rule->action = HTTP_REQ_ACT_ALLOW;
8486 cur_arg = 1;
8487 } else if (!strcmp(args[0], "deny")) {
8488 rule->action = HTTP_REQ_ACT_DENY;
8489 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008490 } else if (!strcmp(args[0], "tarpit")) {
8491 rule->action = HTTP_REQ_ACT_TARPIT;
8492 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008493 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008494 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008495 cur_arg = 1;
8496
8497 while(*args[cur_arg]) {
8498 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008499 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008500 cur_arg+=2;
8501 continue;
8502 } else
8503 break;
8504 }
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008505 } else if (!strcmp(args[0], "set-nice")) {
8506 rule->action = HTTP_REQ_ACT_SET_NICE;
8507 cur_arg = 1;
8508
8509 if (!*args[cur_arg] ||
8510 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8511 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer value).\n",
8512 file, linenum, args[0]);
8513 goto out_err;
8514 }
8515 rule->arg.nice = atoi(args[cur_arg]);
8516 if (rule->arg.nice < -1024)
8517 rule->arg.nice = -1024;
8518 else if (rule->arg.nice > 1024)
8519 rule->arg.nice = 1024;
8520 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008521 } else if (!strcmp(args[0], "set-tos")) {
8522#ifdef IP_TOS
8523 char *err;
8524 rule->action = HTTP_REQ_ACT_SET_TOS;
8525 cur_arg = 1;
8526
8527 if (!*args[cur_arg] ||
8528 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8529 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8530 file, linenum, args[0]);
8531 goto out_err;
8532 }
8533
8534 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8535 if (err && *err != '\0') {
8536 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8537 file, linenum, err, args[0]);
8538 goto out_err;
8539 }
8540 cur_arg++;
8541#else
8542 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8543 goto out_err;
8544#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008545 } else if (!strcmp(args[0], "set-mark")) {
8546#ifdef SO_MARK
8547 char *err;
8548 rule->action = HTTP_REQ_ACT_SET_MARK;
8549 cur_arg = 1;
8550
8551 if (!*args[cur_arg] ||
8552 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8553 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (integer/hex value).\n",
8554 file, linenum, args[0]);
8555 goto out_err;
8556 }
8557
8558 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8559 if (err && *err != '\0') {
8560 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-request %s' (integer/hex value expected).\n",
8561 file, linenum, err, args[0]);
8562 goto out_err;
8563 }
8564 cur_arg++;
8565 global.last_checks |= LSTCHK_NETADM;
8566#else
8567 Alert("parsing [%s:%d]: 'http-request %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8568 goto out_err;
8569#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008570 } else if (!strcmp(args[0], "set-log-level")) {
8571 rule->action = HTTP_REQ_ACT_SET_LOGL;
8572 cur_arg = 1;
8573
8574 if (!*args[cur_arg] ||
8575 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8576 bad_log_level:
8577 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument (log level name or 'silent').\n",
8578 file, linenum, args[0]);
8579 goto out_err;
8580 }
8581 if (strcmp(args[cur_arg], "silent") == 0)
8582 rule->arg.loglevel = -1;
8583 else if ((rule->arg.loglevel = get_log_level(args[cur_arg]) + 1) == 0)
8584 goto bad_log_level;
8585 cur_arg++;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008586 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8587 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8588 cur_arg = 1;
8589
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008590 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8591 (*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 +01008592 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8593 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008594 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008595 }
8596
8597 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8598 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8599 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008600
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008601 proxy->conf.args.ctx = ARGC_HRQ;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008602 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008603 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8604 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008605 free(proxy->conf.lfs_file);
8606 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8607 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008608 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008609 } else if (strcmp(args[0], "del-header") == 0) {
8610 rule->action = HTTP_REQ_ACT_DEL_HDR;
8611 cur_arg = 1;
8612
8613 if (!*args[cur_arg] ||
8614 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8615 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 1 argument.\n",
8616 file, linenum, args[0]);
8617 goto out_err;
8618 }
8619
8620 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8621 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8622
8623 proxy->conf.args.ctx = ARGC_HRQ;
8624 free(proxy->conf.lfs_file);
8625 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8626 proxy->conf.lfs_line = proxy->conf.args.line;
8627 cur_arg += 1;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008628 } else if (strcmp(args[0], "redirect") == 0) {
8629 struct redirect_rule *redir;
Willy Tarreau6d4890c2013-11-18 18:04:25 +01008630 char *errmsg = NULL;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008631
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008632 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg, 1)) == NULL) {
Willy Tarreau81499eb2012-12-27 12:19:02 +01008633 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8634 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8635 goto out_err;
8636 }
8637
8638 /* this redirect rule might already contain a parsed condition which
8639 * we'll pass to the http-request rule.
8640 */
8641 rule->action = HTTP_REQ_ACT_REDIR;
8642 rule->arg.redir = redir;
8643 rule->cond = redir->cond;
8644 redir->cond = NULL;
8645 cur_arg = 2;
8646 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008647 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008648 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 +01008649 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008650 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008651 }
8652
8653 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8654 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008655 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008656
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008657 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8658 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8659 file, linenum, args[0], errmsg);
8660 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008661 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008662 }
8663 rule->cond = cond;
8664 }
8665 else if (*args[cur_arg]) {
8666 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8667 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8668 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008669 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008670 }
8671
8672 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008673 out_err:
8674 free(rule);
8675 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008676}
8677
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008678/* parse an "http-respose" rule */
8679struct http_res_rule *parse_http_res_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8680{
8681 struct http_res_rule *rule;
8682 int cur_arg;
8683
8684 rule = calloc(1, sizeof(*rule));
8685 if (!rule) {
8686 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8687 goto out_err;
8688 }
8689
8690 if (!strcmp(args[0], "allow")) {
8691 rule->action = HTTP_RES_ACT_ALLOW;
8692 cur_arg = 1;
8693 } else if (!strcmp(args[0], "deny")) {
8694 rule->action = HTTP_RES_ACT_DENY;
8695 cur_arg = 1;
Willy Tarreauf4c43c12013-06-11 17:01:13 +02008696 } else if (!strcmp(args[0], "set-nice")) {
8697 rule->action = HTTP_RES_ACT_SET_NICE;
8698 cur_arg = 1;
8699
8700 if (!*args[cur_arg] ||
8701 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8702 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer value).\n",
8703 file, linenum, args[0]);
8704 goto out_err;
8705 }
8706 rule->arg.nice = atoi(args[cur_arg]);
8707 if (rule->arg.nice < -1024)
8708 rule->arg.nice = -1024;
8709 else if (rule->arg.nice > 1024)
8710 rule->arg.nice = 1024;
8711 cur_arg++;
Willy Tarreau42cf39e2013-06-11 18:51:32 +02008712 } else if (!strcmp(args[0], "set-tos")) {
8713#ifdef IP_TOS
8714 char *err;
8715 rule->action = HTTP_RES_ACT_SET_TOS;
8716 cur_arg = 1;
8717
8718 if (!*args[cur_arg] ||
8719 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8720 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8721 file, linenum, args[0]);
8722 goto out_err;
8723 }
8724
8725 rule->arg.tos = strtol(args[cur_arg], &err, 0);
8726 if (err && *err != '\0') {
8727 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8728 file, linenum, err, args[0]);
8729 goto out_err;
8730 }
8731 cur_arg++;
8732#else
8733 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (IP_TOS undefined).\n", file, linenum, args[0]);
8734 goto out_err;
8735#endif
Willy Tarreau51347ed2013-06-11 19:34:13 +02008736 } else if (!strcmp(args[0], "set-mark")) {
8737#ifdef SO_MARK
8738 char *err;
8739 rule->action = HTTP_RES_ACT_SET_MARK;
8740 cur_arg = 1;
8741
8742 if (!*args[cur_arg] ||
8743 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8744 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (integer/hex value).\n",
8745 file, linenum, args[0]);
8746 goto out_err;
8747 }
8748
8749 rule->arg.mark = strtoul(args[cur_arg], &err, 0);
8750 if (err && *err != '\0') {
8751 Alert("parsing [%s:%d]: invalid character starting at '%s' in 'http-response %s' (integer/hex value expected).\n",
8752 file, linenum, err, args[0]);
8753 goto out_err;
8754 }
8755 cur_arg++;
8756 global.last_checks |= LSTCHK_NETADM;
8757#else
8758 Alert("parsing [%s:%d]: 'http-response %s' is not supported on this platform (SO_MARK undefined).\n", file, linenum, args[0]);
8759 goto out_err;
8760#endif
Willy Tarreau9a355ec2013-06-11 17:45:46 +02008761 } else if (!strcmp(args[0], "set-log-level")) {
8762 rule->action = HTTP_RES_ACT_SET_LOGL;
8763 cur_arg = 1;
8764
8765 if (!*args[cur_arg] ||
8766 (*args[cur_arg + 1] && strcmp(args[cur_arg + 1], "if") != 0 && strcmp(args[cur_arg + 1], "unless") != 0)) {
8767 bad_log_level:
8768 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument (log level name or 'silent').\n",
8769 file, linenum, args[0]);
8770 goto out_err;
8771 }
8772 if (strcmp(args[cur_arg], "silent") == 0)
8773 rule->arg.loglevel = -1;
8774 else if ((rule->arg.loglevel = get_log_level(args[cur_arg] + 1)) == 0)
8775 goto bad_log_level;
8776 cur_arg++;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008777 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8778 rule->action = *args[0] == 'a' ? HTTP_RES_ACT_ADD_HDR : HTTP_RES_ACT_SET_HDR;
8779 cur_arg = 1;
8780
8781 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8782 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
8783 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 2 arguments.\n",
8784 file, linenum, args[0]);
8785 goto out_err;
8786 }
8787
8788 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8789 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8790 LIST_INIT(&rule->arg.hdr_add.fmt);
8791
Thierry FOURNIER1c0054f2013-11-20 15:09:52 +01008792 proxy->conf.args.ctx = ARGC_HRS;
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008793 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008794 (proxy->cap & PR_CAP_BE) ? SMP_VAL_BE_HRS_HDR : SMP_VAL_FE_HRS_HDR,
8795 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008796 free(proxy->conf.lfs_file);
8797 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8798 proxy->conf.lfs_line = proxy->conf.args.line;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008799 cur_arg += 2;
Thierry FOURNIERdad3d1d2014-04-22 18:07:25 +02008800 } else if (strcmp(args[0], "del-header") == 0) {
8801 rule->action = HTTP_RES_ACT_DEL_HDR;
8802 cur_arg = 1;
8803
8804 if (!*args[cur_arg] ||
8805 (*args[cur_arg+1] && strcmp(args[cur_arg+1], "if") != 0 && strcmp(args[cur_arg+1], "unless") != 0)) {
8806 Alert("parsing [%s:%d]: 'http-response %s' expects exactly 1 argument.\n",
8807 file, linenum, args[0]);
8808 goto out_err;
8809 }
8810
8811 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8812 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8813
8814 proxy->conf.args.ctx = ARGC_HRS;
8815 free(proxy->conf.lfs_file);
8816 proxy->conf.lfs_file = strdup(proxy->conf.args.file);
8817 proxy->conf.lfs_line = proxy->conf.args.line;
8818 cur_arg += 1;
Willy Tarreaue365c0b2013-06-11 16:06:12 +02008819 } else {
Willy Tarreau51347ed2013-06-11 19:34:13 +02008820 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 +02008821 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
8822 goto out_err;
8823 }
8824
8825 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8826 struct acl_cond *cond;
8827 char *errmsg = NULL;
8828
8829 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8830 Alert("parsing [%s:%d] : error detected while parsing an 'http-response %s' condition : %s.\n",
8831 file, linenum, args[0], errmsg);
8832 free(errmsg);
8833 goto out_err;
8834 }
8835 rule->cond = cond;
8836 }
8837 else if (*args[cur_arg]) {
8838 Alert("parsing [%s:%d]: 'http-response %s' expects"
8839 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8840 file, linenum, args[0], args[cur_arg]);
8841 goto out_err;
8842 }
8843
8844 return rule;
8845 out_err:
8846 free(rule);
8847 return NULL;
8848}
8849
Willy Tarreau4baae242012-12-27 12:00:31 +01008850/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008851 * with <err> filled with the error message. If <use_fmt> is not null, builds a
8852 * dynamic log-format rule instead of a static string.
Willy Tarreau4baae242012-12-27 12:00:31 +01008853 */
8854struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008855 const char **args, char **errmsg, int use_fmt)
Willy Tarreau4baae242012-12-27 12:00:31 +01008856{
8857 struct redirect_rule *rule;
8858 int cur_arg;
8859 int type = REDIRECT_TYPE_NONE;
8860 int code = 302;
8861 const char *destination = NULL;
8862 const char *cookie = NULL;
8863 int cookie_set = 0;
8864 unsigned int flags = REDIRECT_FLAG_NONE;
8865 struct acl_cond *cond = NULL;
8866
8867 cur_arg = 0;
8868 while (*(args[cur_arg])) {
8869 if (strcmp(args[cur_arg], "location") == 0) {
8870 if (!*args[cur_arg + 1])
8871 goto missing_arg;
8872
8873 type = REDIRECT_TYPE_LOCATION;
8874 cur_arg++;
8875 destination = args[cur_arg];
8876 }
8877 else if (strcmp(args[cur_arg], "prefix") == 0) {
8878 if (!*args[cur_arg + 1])
8879 goto missing_arg;
8880
8881 type = REDIRECT_TYPE_PREFIX;
8882 cur_arg++;
8883 destination = args[cur_arg];
8884 }
8885 else if (strcmp(args[cur_arg], "scheme") == 0) {
8886 if (!*args[cur_arg + 1])
8887 goto missing_arg;
8888
8889 type = REDIRECT_TYPE_SCHEME;
8890 cur_arg++;
8891 destination = args[cur_arg];
8892 }
8893 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8894 if (!*args[cur_arg + 1])
8895 goto missing_arg;
8896
8897 cur_arg++;
8898 cookie = args[cur_arg];
8899 cookie_set = 1;
8900 }
8901 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8902 if (!*args[cur_arg + 1])
8903 goto missing_arg;
8904
8905 cur_arg++;
8906 cookie = args[cur_arg];
8907 cookie_set = 0;
8908 }
8909 else if (strcmp(args[cur_arg], "code") == 0) {
8910 if (!*args[cur_arg + 1])
8911 goto missing_arg;
8912
8913 cur_arg++;
8914 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008915 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008916 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008917 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008918 args[cur_arg - 1], args[cur_arg]);
8919 return NULL;
8920 }
8921 }
8922 else if (!strcmp(args[cur_arg],"drop-query")) {
8923 flags |= REDIRECT_FLAG_DROP_QS;
8924 }
8925 else if (!strcmp(args[cur_arg],"append-slash")) {
8926 flags |= REDIRECT_FLAG_APPEND_SLASH;
8927 }
8928 else if (strcmp(args[cur_arg], "if") == 0 ||
8929 strcmp(args[cur_arg], "unless") == 0) {
8930 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8931 if (!cond) {
8932 memprintf(errmsg, "error in condition: %s", *errmsg);
8933 return NULL;
8934 }
8935 break;
8936 }
8937 else {
8938 memprintf(errmsg,
8939 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8940 args[cur_arg]);
8941 return NULL;
8942 }
8943 cur_arg++;
8944 }
8945
8946 if (type == REDIRECT_TYPE_NONE) {
8947 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8948 return NULL;
8949 }
8950
8951 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8952 rule->cond = cond;
Willy Tarreau60e08382013-12-03 00:48:45 +01008953 LIST_INIT(&rule->rdr_fmt);
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008954
8955 if (!use_fmt) {
8956 /* old-style static redirect rule */
8957 rule->rdr_str = strdup(destination);
8958 rule->rdr_len = strlen(destination);
8959 }
8960 else {
8961 /* log-format based redirect rule */
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008962
8963 /* Parse destination. Note that in the REDIRECT_TYPE_PREFIX case,
8964 * if prefix == "/", we don't want to add anything, otherwise it
8965 * makes it hard for the user to configure a self-redirection.
8966 */
8967 proxy->conf.args.ctx = ARGC_RDR;
8968 if (!(type == REDIRECT_TYPE_PREFIX && destination[0] == '/' && destination[1] == '\0')) {
Thierry FOURNIERd048d8b2014-03-13 16:46:18 +01008969 parse_logformat_string(destination, curproxy, &rule->rdr_fmt, LOG_OPT_HTTP,
Thierry FOURNIEReeaa9512014-02-11 14:00:19 +01008970 (curproxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR,
8971 file, linenum);
Willy Tarreau59ad1a22014-01-29 14:39:58 +01008972 free(curproxy->conf.lfs_file);
8973 curproxy->conf.lfs_file = strdup(curproxy->conf.args.file);
8974 curproxy->conf.lfs_line = curproxy->conf.args.line;
Thierry FOURNIERd18cd0f2013-11-29 12:15:45 +01008975 }
8976 }
8977
Willy Tarreau4baae242012-12-27 12:00:31 +01008978 if (cookie) {
8979 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8980 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8981 */
8982 rule->cookie_len = strlen(cookie);
8983 if (cookie_set) {
8984 rule->cookie_str = malloc(rule->cookie_len + 10);
8985 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8986 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8987 rule->cookie_len += 9;
8988 } else {
8989 rule->cookie_str = malloc(rule->cookie_len + 21);
8990 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8991 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8992 rule->cookie_len += 20;
8993 }
8994 }
8995 rule->type = type;
8996 rule->code = code;
8997 rule->flags = flags;
8998 LIST_INIT(&rule->list);
8999 return rule;
9000
9001 missing_arg:
9002 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
9003 return NULL;
9004}
9005
Willy Tarreau8797c062007-05-07 00:55:35 +02009006/************************************************************************/
9007/* The code below is dedicated to ACL parsing and matching */
9008/************************************************************************/
9009
9010
Willy Tarreau14174bc2012-04-16 14:34:04 +02009011/* This function ensures that the prerequisites for an L7 fetch are ready,
9012 * which means that a request or response is ready. If some data is missing,
9013 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02009014 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
9015 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02009016 *
9017 * The function returns :
Willy Tarreau506d0502013-07-06 13:29:24 +02009018 * 0 with SMP_F_MAY_CHANGE in the sample flags if some data is missing to
9019 * decide whether or not an HTTP message is present ;
9020 * 0 if the requested data cannot be fetched or if it is certain that
9021 * we'll never have any HTTP message there ;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009022 * 1 if an HTTP message is ready
9023 */
9024static int
Willy Tarreau506d0502013-07-06 13:29:24 +02009025smp_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009026 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02009027{
9028 struct http_txn *txn = l7;
9029 struct http_msg *msg = &txn->req;
9030
9031 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9032 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9033 */
9034
9035 if (unlikely(!s || !txn))
9036 return 0;
9037
9038 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02009039 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009040
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009041 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009042 if (unlikely(!s->req))
9043 return 0;
9044
Willy Tarreauaae75e32013-03-29 12:31:49 +01009045 /* If the buffer does not leave enough free space at the end,
9046 * we must first realign it.
9047 */
9048 if (s->req->buf->p > s->req->buf->data &&
9049 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
9050 buffer_slow_realign(s->req->buf);
9051
Willy Tarreau14174bc2012-04-16 14:34:04 +02009052 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau472b1ee2013-10-14 22:41:30 +02009053 if (msg->msg_state == HTTP_MSG_ERROR)
Willy Tarreau506d0502013-07-06 13:29:24 +02009054 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009055
9056 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02009057 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02009058 http_msg_analyzer(msg, &txn->hdr_idx);
9059
9060 /* Still no valid request ? */
9061 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02009062 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02009063 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau506d0502013-07-06 13:29:24 +02009064 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009065 }
9066 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02009067 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009068 return 0;
9069 }
9070
9071 /* OK we just got a valid HTTP request. We have some minor
9072 * preparation to perform so that further checks can rely
9073 * on HTTP tests.
9074 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01009075
9076 /* If the request was parsed but was too large, we must absolutely
9077 * return an error so that it is not processed. At the moment this
9078 * cannot happen, but if the parsers are to change in the future,
9079 * we want this check to be maintained.
9080 */
9081 if (unlikely(s->req->buf->i + s->req->buf->p >
9082 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
9083 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau506d0502013-07-06 13:29:24 +02009084 smp->data.uint = 1;
Willy Tarreauaae75e32013-03-29 12:31:49 +01009085 return 1;
9086 }
9087
Willy Tarreau9b28e032012-10-12 23:49:43 +02009088 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02009089 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
9090 s->flags |= SN_REDIRECTABLE;
9091
Willy Tarreau506d0502013-07-06 13:29:24 +02009092 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
9093 return 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009094 }
9095
Willy Tarreau506d0502013-07-06 13:29:24 +02009096 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02009097 return 0; /* data might have moved and indexes changed */
Willy Tarreau506d0502013-07-06 13:29:24 +02009098 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009099
9100 /* otherwise everything's ready for the request */
9101 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02009102 else {
9103 /* Check for a dependency on a response */
Willy Tarreau506d0502013-07-06 13:29:24 +02009104 if (txn->rsp.msg_state < HTTP_MSG_BODY) {
9105 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009106 return 0;
Willy Tarreau506d0502013-07-06 13:29:24 +02009107 }
Willy Tarreau14174bc2012-04-16 14:34:04 +02009108 }
9109
9110 /* everything's OK */
Willy Tarreau506d0502013-07-06 13:29:24 +02009111 smp->data.uint = 1;
Willy Tarreau14174bc2012-04-16 14:34:04 +02009112 return 1;
9113}
Willy Tarreau8797c062007-05-07 00:55:35 +02009114
Willy Tarreauc0239e02012-04-16 14:42:55 +02009115#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009116 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 +02009117
Willy Tarreau24e32d82012-04-23 23:55:44 +02009118#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau506d0502013-07-06 13:29:24 +02009119 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 +02009120
Willy Tarreau8797c062007-05-07 00:55:35 +02009121
9122/* 1. Check on METHOD
9123 * We use the pre-parsed method if it is known, and store its number as an
9124 * integer. If it is unknown, we use the pointer and the length.
9125 */
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009126static int pat_parse_meth(const char *text, struct pattern *pattern, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02009127{
9128 int len, meth;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009129 struct chunk *trash;
Willy Tarreau8797c062007-05-07 00:55:35 +02009130
Thierry FOURNIER580c32c2014-01-24 10:58:12 +01009131 len = strlen(text);
9132 meth = find_http_meth(text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02009133
9134 pattern->val.i = meth;
9135 if (meth == HTTP_METH_OTHER) {
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009136 trash = get_trash_chunk();
9137 if (trash->size < len) {
9138 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
9139 len, trash->size);
9140 return 0;
Thierry FOURNIER0b2fe4a2013-12-06 20:33:50 +01009141 }
Thierry FOURNIERedc15c32013-12-13 15:36:59 +01009142 pattern->ptr.str = trash->str;
Willy Tarreau8797c062007-05-07 00:55:35 +02009143 pattern->len = len;
9144 }
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009145 else {
9146 pattern->ptr.str = NULL;
9147 pattern->len = 0;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009148 }
Willy Tarreau8797c062007-05-07 00:55:35 +02009149 return 1;
9150}
9151
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009152/* This function fetches the method of current HTTP request and stores
9153 * it in the global pattern struct as a chunk. There are two possibilities :
9154 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
9155 * in <len> and <ptr> is NULL ;
9156 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
9157 * <len> to its length.
Thierry FOURNIERa65b3432013-11-28 18:22:00 +01009158 * This is intended to be used with pat_match_meth() only.
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009159 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009160static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009161smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009162 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009163{
9164 int meth;
9165 struct http_txn *txn = l7;
9166
Willy Tarreau24e32d82012-04-23 23:55:44 +02009167 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009168
Willy Tarreau8797c062007-05-07 00:55:35 +02009169 meth = txn->meth;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009170 smp->type = SMP_T_METH;
9171 smp->data.meth.meth = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02009172 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02009173 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
9174 /* ensure the indexes are not affected */
9175 return 0;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009176 smp->flags |= SMP_F_CONST;
Thierry FOURNIERd4373142013-12-17 01:10:10 +01009177 smp->data.meth.str.len = txn->req.sl.rq.m_l;
9178 smp->data.meth.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009179 }
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009180 smp->flags |= SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009181 return 1;
9182}
9183
Willy Tarreau8e5e9552011-12-16 15:38:49 +01009184/* See above how the method is stored in the global pattern */
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009185static struct pattern *pat_match_meth(struct sample *smp, struct pattern_expr *expr, int fill)
Willy Tarreau8797c062007-05-07 00:55:35 +02009186{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009187 int icase;
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009188 struct pattern_list *lst;
9189 struct pattern *pattern;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009190
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009191 list_for_each_entry(lst, &expr->patterns, list) {
9192 pattern = &lst->pat;
Willy Tarreau8797c062007-05-07 00:55:35 +02009193
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009194 /* well-known method */
9195 if (pattern->val.i != HTTP_METH_OTHER) {
9196 if (smp->data.meth.meth == pattern->val.i)
9197 return pattern;
9198 else
9199 continue;
9200 }
Willy Tarreauc8d7c962007-06-17 08:20:33 +02009201
Thierry FOURNIER5338eea2013-12-16 14:22:13 +01009202 /* Other method, we must compare the strings */
9203 if (pattern->len != smp->data.meth.str.len)
9204 continue;
9205
9206 icase = pattern->flags & PAT_F_IGNORE_CASE;
9207 if ((icase && strncasecmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0) ||
9208 (!icase && strncmp(pattern->ptr.str, smp->data.meth.str.str, smp->data.meth.str.len) != 0))
9209 return pattern;
9210 }
9211 return NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02009212}
9213
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009214static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009215smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009216 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009217{
9218 struct http_txn *txn = l7;
9219 char *ptr;
9220 int len;
9221
Willy Tarreauc0239e02012-04-16 14:42:55 +02009222 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009223
Willy Tarreau8797c062007-05-07 00:55:35 +02009224 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009225 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02009226
9227 while ((len-- > 0) && (*ptr++ != '/'));
9228 if (len <= 0)
9229 return 0;
9230
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009231 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009232 smp->data.str.str = ptr;
9233 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009234
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009235 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009236 return 1;
9237}
9238
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009239static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009240smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009241 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009242{
9243 struct http_txn *txn = l7;
9244 char *ptr;
9245 int len;
9246
Willy Tarreauc0239e02012-04-16 14:42:55 +02009247 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009248
Willy Tarreauf26b2522012-12-14 08:33:14 +01009249 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9250 return 0;
9251
Willy Tarreau8797c062007-05-07 00:55:35 +02009252 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009253 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02009254
9255 while ((len-- > 0) && (*ptr++ != '/'));
9256 if (len <= 0)
9257 return 0;
9258
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009259 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009260 smp->data.str.str = ptr;
9261 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02009262
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009263 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009264 return 1;
9265}
9266
9267/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009268static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009269smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009270 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009271{
9272 struct http_txn *txn = l7;
9273 char *ptr;
9274 int len;
9275
Willy Tarreauc0239e02012-04-16 14:42:55 +02009276 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009277
Willy Tarreauf26b2522012-12-14 08:33:14 +01009278 if (txn->rsp.msg_state < HTTP_MSG_BODY)
9279 return 0;
9280
Willy Tarreau8797c062007-05-07 00:55:35 +02009281 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009282 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02009283
Willy Tarreauf853c462012-04-23 18:53:56 +02009284 smp->type = SMP_T_UINT;
9285 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02009286 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009287 return 1;
9288}
9289
9290/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02009291static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009292smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009293 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau8797c062007-05-07 00:55:35 +02009294{
9295 struct http_txn *txn = l7;
9296
Willy Tarreauc0239e02012-04-16 14:42:55 +02009297 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009298
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009299 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009300 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009301 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009302 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau8797c062007-05-07 00:55:35 +02009303 return 1;
9304}
9305
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009306static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009307smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009308 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009309{
9310 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009311 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009312
Willy Tarreauc0239e02012-04-16 14:42:55 +02009313 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009314
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009315 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 +02009316 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01009317 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009318
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009319 smp->type = SMP_T_IPV4;
9320 smp->data.ipv4 = ((struct sockaddr_in *)&addr)->sin_addr;
Willy Tarreau37406352012-04-23 16:16:37 +02009321 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009322 return 1;
9323}
9324
9325static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009326smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009327 const struct arg *args, struct sample *smp, const char *kw)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009328{
9329 struct http_txn *txn = l7;
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009330 struct sockaddr_storage addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009331
Willy Tarreauc0239e02012-04-16 14:42:55 +02009332 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009333
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01009334 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 +02009335 if (((struct sockaddr_in *)&addr)->sin_family != AF_INET)
9336 return 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009337
Willy Tarreau4c804ec2013-09-30 14:37:14 +02009338 smp->type = SMP_T_UINT;
9339 smp->data.uint = ntohs(((struct sockaddr_in *)&addr)->sin_port);
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02009340 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01009341 return 1;
9342}
9343
Willy Tarreau185b5c42012-04-26 15:11:51 +02009344/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9345 * Accepts an optional argument of type string containing the header field name,
9346 * and an optional argument of type signed or unsigned integer to request an
9347 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009348 * headers are considered from the first one. It does not stop on commas and
9349 * returns full lines instead (useful for User-Agent or Date for example).
9350 */
9351static int
9352smp_fetch_fhdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009353 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009354{
9355 struct http_txn *txn = l7;
9356 struct hdr_idx *idx = &txn->hdr_idx;
9357 struct hdr_ctx *ctx = smp->ctx.a[0];
9358 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9359 int occ = 0;
9360 const char *name_str = NULL;
9361 int name_len = 0;
9362
9363 if (!ctx) {
9364 /* first call */
9365 ctx = &static_hdr_ctx;
9366 ctx->idx = 0;
9367 smp->ctx.a[0] = ctx;
9368 }
9369
9370 if (args) {
9371 if (args[0].type != ARGT_STR)
9372 return 0;
9373 name_str = args[0].data.str.str;
9374 name_len = args[0].data.str.len;
9375
9376 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9377 occ = args[1].data.uint;
9378 }
9379
9380 CHECK_HTTP_MESSAGE_FIRST();
9381
9382 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
9383 /* search for header from the beginning */
9384 ctx->idx = 0;
9385
9386 if (!occ && !(opt & SMP_OPT_ITERATE))
9387 /* no explicit occurrence and single fetch => last header by default */
9388 occ = -1;
9389
9390 if (!occ)
9391 /* prepare to report multiple occurrences for ACL fetches */
9392 smp->flags |= SMP_F_NOT_LAST;
9393
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009394 smp->type = SMP_T_STR;
9395 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009396 if (http_get_fhdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
9397 return 1;
9398
9399 smp->flags &= ~SMP_F_NOT_LAST;
9400 return 0;
9401}
9402
9403/* 6. Check on HTTP header count. The number of occurrences is returned.
9404 * Accepts exactly 1 argument of type string. It does not stop on commas and
9405 * returns full lines instead (useful for User-Agent or Date for example).
9406 */
9407static int
9408smp_fetch_fhdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009409 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04ff9f12013-06-10 18:39:42 +02009410{
9411 struct http_txn *txn = l7;
9412 struct hdr_idx *idx = &txn->hdr_idx;
9413 struct hdr_ctx ctx;
9414 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
9415 int cnt;
9416
9417 if (!args || args->type != ARGT_STR)
9418 return 0;
9419
9420 CHECK_HTTP_MESSAGE_FIRST();
9421
9422 ctx.idx = 0;
9423 cnt = 0;
9424 while (http_find_full_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
9425 cnt++;
9426
9427 smp->type = SMP_T_UINT;
9428 smp->data.uint = cnt;
9429 smp->flags = SMP_F_VOL_HDR;
9430 return 1;
9431}
9432
9433/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
9434 * Accepts an optional argument of type string containing the header field name,
9435 * and an optional argument of type signed or unsigned integer to request an
9436 * explicit occurrence of the header. Note that in the event of a missing name,
Willy Tarreau185b5c42012-04-26 15:11:51 +02009437 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009438 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02009439static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009440smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009441 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009442{
9443 struct http_txn *txn = l7;
9444 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009445 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009446 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009447 int occ = 0;
9448 const char *name_str = NULL;
9449 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009450
Willy Tarreaua890d072013-04-02 12:01:06 +02009451 if (!ctx) {
9452 /* first call */
9453 ctx = &static_hdr_ctx;
9454 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02009455 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009456 }
9457
Willy Tarreau185b5c42012-04-26 15:11:51 +02009458 if (args) {
9459 if (args[0].type != ARGT_STR)
9460 return 0;
9461 name_str = args[0].data.str.str;
9462 name_len = args[0].data.str.len;
9463
9464 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
9465 occ = args[1].data.uint;
9466 }
Willy Tarreau34db1082012-04-19 17:16:54 +02009467
Willy Tarreaue333ec92012-04-16 16:26:40 +02009468 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02009469
Willy Tarreau185b5c42012-04-26 15:11:51 +02009470 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02009471 /* search for header from the beginning */
9472 ctx->idx = 0;
9473
Willy Tarreau185b5c42012-04-26 15:11:51 +02009474 if (!occ && !(opt & SMP_OPT_ITERATE))
9475 /* no explicit occurrence and single fetch => last header by default */
9476 occ = -1;
9477
9478 if (!occ)
9479 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02009480 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01009481
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009482 smp->type = SMP_T_STR;
9483 smp->flags |= SMP_F_VOL_HDR | SMP_F_CONST;
Willy Tarreau185b5c42012-04-26 15:11:51 +02009484 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 +02009485 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009486
Willy Tarreau37406352012-04-23 16:16:37 +02009487 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009488 return 0;
9489}
9490
Willy Tarreauc11416f2007-06-17 16:58:38 +02009491/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02009492 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02009493 */
9494static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009495smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009496 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009497{
9498 struct http_txn *txn = l7;
9499 struct hdr_idx *idx = &txn->hdr_idx;
9500 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009501 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009502 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02009503
Willy Tarreau24e32d82012-04-23 23:55:44 +02009504 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009505 return 0;
9506
Willy Tarreaue333ec92012-04-16 16:26:40 +02009507 CHECK_HTTP_MESSAGE_FIRST();
9508
Willy Tarreau33a7e692007-06-10 19:45:56 +02009509 ctx.idx = 0;
9510 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02009511 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 +02009512 cnt++;
9513
Willy Tarreauf853c462012-04-23 18:53:56 +02009514 smp->type = SMP_T_UINT;
9515 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009516 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009517 return 1;
9518}
9519
Willy Tarreau185b5c42012-04-26 15:11:51 +02009520/* Fetch an HTTP header's integer value. The integer value is returned. It
9521 * takes a mandatory argument of type string and an optional one of type int
9522 * to designate a specific occurrence. It returns an unsigned integer, which
9523 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02009524 */
9525static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009526smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009527 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau33a7e692007-06-10 19:45:56 +02009528{
Willy Tarreauef38c392013-07-22 16:29:32 +02009529 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw);
Willy Tarreaue333ec92012-04-16 16:26:40 +02009530
Willy Tarreauf853c462012-04-23 18:53:56 +02009531 if (ret > 0) {
9532 smp->type = SMP_T_UINT;
9533 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9534 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02009535
Willy Tarreaud53e2422012-04-16 17:21:11 +02009536 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009537}
9538
Cyril Bonté69fa9922012-10-25 00:01:06 +02009539/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
9540 * and an optional one of type int to designate a specific occurrence.
9541 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02009542 */
9543static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02009544smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009545 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau106f9792009-09-19 07:54:16 +02009546{
Willy Tarreaud53e2422012-04-16 17:21:11 +02009547 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009548
Willy Tarreauef38c392013-07-22 16:29:32 +02009549 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp, kw)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02009550 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
9551 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02009552 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02009553 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01009554 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02009555 if (smp->data.str.len < temp->size - 1) {
9556 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
9557 temp->str[smp->data.str.len] = '\0';
9558 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
9559 smp->type = SMP_T_IPV6;
9560 break;
9561 }
9562 }
9563 }
9564
Willy Tarreaud53e2422012-04-16 17:21:11 +02009565 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02009566 if (!(smp->flags & SMP_F_NOT_LAST))
9567 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02009568 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02009569 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02009570}
9571
Willy Tarreau737b0c12007-06-10 21:28:46 +02009572/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
9573 * the first '/' after the possible hostname, and ends before the possible '?'.
9574 */
9575static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009576smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009577 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009578{
9579 struct http_txn *txn = l7;
9580 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02009581
Willy Tarreauc0239e02012-04-16 14:42:55 +02009582 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02009583
Willy Tarreau9b28e032012-10-12 23:49:43 +02009584 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01009585 ptr = http_get_path(txn);
9586 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02009587 return 0;
9588
9589 /* OK, we got the '/' ! */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009590 smp->type = SMP_T_STR;
Willy Tarreauf853c462012-04-23 18:53:56 +02009591 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009592
9593 while (ptr < end && *ptr != '?')
9594 ptr++;
9595
Willy Tarreauf853c462012-04-23 18:53:56 +02009596 smp->data.str.len = ptr - smp->data.str.str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009597 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02009598 return 1;
9599}
9600
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009601/* This produces a concatenation of the first occurrence of the Host header
9602 * followed by the path component if it begins with a slash ('/'). This means
9603 * that '*' will not be added, resulting in exactly the first Host entry.
9604 * If no Host header is found, then the path is returned as-is. The returned
9605 * value is stored in the trash so it does not need to be marked constant.
Willy Tarreaub169eba2013-12-16 15:14:43 +01009606 * The returned sample is of type string.
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009607 */
9608static int
9609smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009610 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009611{
9612 struct http_txn *txn = l7;
9613 char *ptr, *end, *beg;
9614 struct hdr_ctx ctx;
9615
9616 CHECK_HTTP_MESSAGE_FIRST();
9617
9618 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02009619 if (!http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx) || !ctx.vlen)
Willy Tarreauef38c392013-07-22 16:29:32 +02009620 return smp_fetch_path(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009621
9622 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009623 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009624 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01009625 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009626 smp->data.str.len = ctx.vlen;
9627
9628 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +02009629 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009630 beg = http_get_path(txn);
9631 if (!beg)
9632 beg = end;
9633
9634 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9635
9636 if (beg < ptr && *beg == '/') {
9637 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
9638 smp->data.str.len += ptr - beg;
9639 }
9640
9641 smp->flags = SMP_F_VOL_1ST;
9642 return 1;
9643}
9644
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009645/* This produces a 32-bit hash of the concatenation of the first occurrence of
9646 * the Host header followed by the path component if it begins with a slash ('/').
9647 * This means that '*' will not be added, resulting in exactly the first Host
9648 * entry. If no Host header is found, then the path is used. The resulting value
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009649 * is hashed using the path hash followed by a full avalanche hash and provides a
9650 * 32-bit integer value. This fetch is useful for tracking per-path activity on
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009651 * high-traffic sites without having to store whole paths.
9652 */
9653static int
9654smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009655 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009656{
9657 struct http_txn *txn = l7;
9658 struct hdr_ctx ctx;
9659 unsigned int hash = 0;
9660 char *ptr, *beg, *end;
9661 int len;
9662
9663 CHECK_HTTP_MESSAGE_FIRST();
9664
9665 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +02009666 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009667 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
9668 ptr = ctx.line + ctx.val;
9669 len = ctx.vlen;
9670 while (len--)
9671 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
9672 }
9673
9674 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +02009675 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009676 beg = http_get_path(txn);
9677 if (!beg)
9678 beg = end;
9679
9680 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
9681
9682 if (beg < ptr && *beg == '/') {
9683 while (beg < ptr)
9684 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
9685 }
9686 hash = full_hash(hash);
9687
9688 smp->type = SMP_T_UINT;
9689 smp->data.uint = hash;
9690 smp->flags = SMP_F_VOL_1ST;
9691 return 1;
9692}
9693
Willy Tarreau4a550602012-12-09 14:53:32 +01009694/* This concatenates the source address with the 32-bit hash of the Host and
Neil - HAProxy List39c63c52013-11-04 13:48:42 +00009695 * path as returned by smp_fetch_base32(). The idea is to have per-source and
9696 * per-path counters. The result is a binary block from 8 to 20 bytes depending
9697 * on the source address length. The path hash is stored before the address so
Willy Tarreau4a550602012-12-09 14:53:32 +01009698 * that in environments where IPv6 is insignificant, truncating the output to
9699 * 8 bytes would still work.
9700 */
9701static int
9702smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009703 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a550602012-12-09 14:53:32 +01009704{
Willy Tarreau47ca5452012-12-23 20:22:19 +01009705 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009706 struct connection *cli_conn = objt_conn(l4->si[0].end);
9707
9708 if (!cli_conn)
9709 return 0;
Willy Tarreau4a550602012-12-09 14:53:32 +01009710
Willy Tarreauef38c392013-07-22 16:29:32 +02009711 if (!smp_fetch_base32(px, l4, l7, opt, args, smp, kw))
Willy Tarreau4a550602012-12-09 14:53:32 +01009712 return 0;
9713
Willy Tarreau47ca5452012-12-23 20:22:19 +01009714 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01009715 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
9716 temp->len += sizeof(smp->data.uint);
9717
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009718 switch (cli_conn->addr.from.ss_family) {
Willy Tarreau4a550602012-12-09 14:53:32 +01009719 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009720 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Willy Tarreau4a550602012-12-09 14:53:32 +01009721 temp->len += 4;
9722 break;
9723 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +02009724 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Willy Tarreau4a550602012-12-09 14:53:32 +01009725 temp->len += 16;
9726 break;
9727 default:
9728 return 0;
9729 }
9730
9731 smp->data.str = *temp;
9732 smp->type = SMP_T_BIN;
9733 return 1;
9734}
9735
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009736static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009737smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009738 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009739{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009740 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9741 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9742 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009743
Willy Tarreau24e32d82012-04-23 23:55:44 +02009744 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009745
Willy Tarreauf853c462012-04-23 18:53:56 +02009746 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009747 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009748 return 1;
9749}
9750
Willy Tarreau7f18e522010-10-22 20:04:13 +02009751/* return a valid test if the current request is the first one on the connection */
9752static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009753smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009754 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009755{
9756 if (!s)
9757 return 0;
9758
Willy Tarreauf853c462012-04-23 18:53:56 +02009759 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009760 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009761 return 1;
9762}
9763
Willy Tarreau34db1082012-04-19 17:16:54 +02009764/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009765static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009766smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009767 const struct arg *args, struct sample *smp, const char *kw)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009768{
9769
Willy Tarreau24e32d82012-04-23 23:55:44 +02009770 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009771 return 0;
9772
Willy Tarreauc0239e02012-04-16 14:42:55 +02009773 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009774
Willy Tarreauc0239e02012-04-16 14:42:55 +02009775 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009776 return 0;
9777
Willy Tarreauf853c462012-04-23 18:53:56 +02009778 smp->type = SMP_T_BOOL;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009779 smp->data.uint = check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009780 return 1;
9781}
Willy Tarreau8797c062007-05-07 00:55:35 +02009782
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009783/* Accepts exactly 1 argument of type userlist */
9784static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009785smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +02009786 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009787{
9788
9789 if (!args || args->type != ARGT_USR)
9790 return 0;
9791
9792 CHECK_HTTP_MESSAGE_FIRST();
9793
9794 if (!get_http_auth(l4))
9795 return 0;
9796
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009797 /* if the user does not belong to the userlist or has a wrong password,
9798 * report that it unconditionally does not match. Otherwise we return
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009799 * a string containing the username.
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009800 */
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009801 if (!check_user(args->data.usr, l4->txn.auth.user, l4->txn.auth.pass))
9802 return 0;
9803
9804 /* pat_match_auth() will need the user list */
9805 smp->ctx.a[0] = args->data.usr;
9806
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009807 smp->type = SMP_T_STR;
9808 smp->flags = SMP_F_CONST;
Thierry FOURNIER9eec0a62014-01-22 18:38:02 +01009809 smp->data.str.str = l4->txn.auth.user;
9810 smp->data.str.len = strlen(l4->txn.auth.user);
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009811
9812 return 1;
9813}
9814
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009815/* Try to find the next occurrence of a cookie name in a cookie header value.
9816 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9817 * the cookie value is returned into *value and *value_l, and the function
9818 * returns a pointer to the next pointer to search from if the value was found.
9819 * Otherwise if the cookie was not found, NULL is returned and neither value
9820 * nor value_l are touched. The input <hdr> string should first point to the
9821 * header's value, and the <hdr_end> pointer must point to the first character
9822 * not part of the value. <list> must be non-zero if value may represent a list
9823 * of values (cookie headers). This makes it faster to abort parsing when no
9824 * list is expected.
9825 */
9826static char *
9827extract_cookie_value(char *hdr, const char *hdr_end,
9828 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009829 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009830{
9831 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9832 char *next;
9833
9834 /* we search at least a cookie name followed by an equal, and more
9835 * generally something like this :
9836 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9837 */
9838 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9839 /* Iterate through all cookies on this line */
9840
9841 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9842 att_beg++;
9843
9844 /* find att_end : this is the first character after the last non
9845 * space before the equal. It may be equal to hdr_end.
9846 */
9847 equal = att_end = att_beg;
9848
9849 while (equal < hdr_end) {
9850 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9851 break;
9852 if (http_is_spht[(unsigned char)*equal++])
9853 continue;
9854 att_end = equal;
9855 }
9856
9857 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9858 * is between <att_beg> and <equal>, both may be identical.
9859 */
9860
9861 /* look for end of cookie if there is an equal sign */
9862 if (equal < hdr_end && *equal == '=') {
9863 /* look for the beginning of the value */
9864 val_beg = equal + 1;
9865 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9866 val_beg++;
9867
9868 /* find the end of the value, respecting quotes */
9869 next = find_cookie_value_end(val_beg, hdr_end);
9870
9871 /* make val_end point to the first white space or delimitor after the value */
9872 val_end = next;
9873 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9874 val_end--;
9875 } else {
9876 val_beg = val_end = next = equal;
9877 }
9878
9879 /* We have nothing to do with attributes beginning with '$'. However,
9880 * they will automatically be removed if a header before them is removed,
9881 * since they're supposed to be linked together.
9882 */
9883 if (*att_beg == '$')
9884 continue;
9885
9886 /* Ignore cookies with no equal sign */
9887 if (equal == next)
9888 continue;
9889
9890 /* Now we have the cookie name between att_beg and att_end, and
9891 * its value between val_beg and val_end.
9892 */
9893
9894 if (att_end - att_beg == cookie_name_l &&
9895 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9896 /* let's return this value and indicate where to go on from */
9897 *value = val_beg;
9898 *value_l = val_end - val_beg;
9899 return next + 1;
9900 }
9901
9902 /* Set-Cookie headers only have the name in the first attr=value part */
9903 if (!list)
9904 break;
9905 }
9906
9907 return NULL;
9908}
9909
William Lallemanda43ba4e2014-01-28 18:14:25 +01009910/* Fetch a captured HTTP request header. The index is the position of
9911 * the "capture" option in the configuration file
9912 */
9913static int
9914smp_fetch_capture_header_req(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9915 const struct arg *args, struct sample *smp, const char *kw)
9916{
9917 struct proxy *fe = l4->fe;
9918 struct http_txn *txn = l7;
9919 int idx;
9920
9921 if (!args || args->type != ARGT_UINT)
9922 return 0;
9923
9924 idx = args->data.uint;
9925
9926 if (idx > (fe->nb_req_cap - 1) || txn->req.cap == NULL || txn->req.cap[idx] == NULL)
9927 return 0;
9928
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009929 smp->type = SMP_T_STR;
9930 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +01009931 smp->data.str.str = txn->req.cap[idx];
9932 smp->data.str.len = strlen(txn->req.cap[idx]);
9933
9934 return 1;
9935}
9936
9937/* Fetch a captured HTTP response header. The index is the position of
9938 * the "capture" option in the configuration file
9939 */
9940static int
9941smp_fetch_capture_header_res(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9942 const struct arg *args, struct sample *smp, const char *kw)
9943{
9944 struct proxy *fe = l4->fe;
9945 struct http_txn *txn = l7;
9946 int idx;
9947
9948 if (!args || args->type != ARGT_UINT)
9949 return 0;
9950
9951 idx = args->data.uint;
9952
9953 if (idx > (fe->nb_rsp_cap - 1) || txn->rsp.cap == NULL || txn->rsp.cap[idx] == NULL)
9954 return 0;
9955
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009956 smp->type = SMP_T_STR;
9957 smp->flags |= SMP_F_CONST;
William Lallemanda43ba4e2014-01-28 18:14:25 +01009958 smp->data.str.str = txn->rsp.cap[idx];
9959 smp->data.str.len = strlen(txn->rsp.cap[idx]);
9960
9961 return 1;
9962}
9963
William Lallemand65ad6e12014-01-31 15:08:02 +01009964/* Extracts the METHOD in the HTTP request, the txn->uri should be filled before the call */
9965static int
9966smp_fetch_capture_req_method(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9967 const struct arg *args, struct sample *smp, const char *kw)
9968{
9969 struct chunk *temp;
9970 struct http_txn *txn = l7;
William Lallemand96a77852014-02-05 00:30:02 +01009971 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +01009972
9973 if (!txn->uri)
9974 return 0;
9975
William Lallemand96a77852014-02-05 00:30:02 +01009976 ptr = txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009977
William Lallemand96a77852014-02-05 00:30:02 +01009978 while (*ptr != ' ' && *ptr != '\0') /* find first space */
9979 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +01009980
William Lallemand96a77852014-02-05 00:30:02 +01009981 temp = get_trash_chunk();
9982 temp->str = txn->uri;
9983 temp->len = ptr - txn->uri;
William Lallemand65ad6e12014-01-31 15:08:02 +01009984 smp->data.str = *temp;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +01009985 smp->type = SMP_T_STR;
9986 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +01009987
9988 return 1;
9989
9990}
9991
9992/* Extracts the path in the HTTP request, the txn->uri should be filled before the call */
9993static int
9994smp_fetch_capture_req_uri(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9995 const struct arg *args, struct sample *smp, const char *kw)
9996{
9997 struct chunk *temp;
9998 struct http_txn *txn = l7;
9999 char *ptr;
William Lallemand65ad6e12014-01-31 15:08:02 +010010000
10001 if (!txn->uri)
10002 return 0;
William Lallemand96a77852014-02-05 00:30:02 +010010003
William Lallemand65ad6e12014-01-31 15:08:02 +010010004 ptr = txn->uri;
10005
10006 while (*ptr != ' ' && *ptr != '\0') /* find first space */
10007 ptr++;
William Lallemand96a77852014-02-05 00:30:02 +010010008
William Lallemand65ad6e12014-01-31 15:08:02 +010010009 if (!*ptr)
10010 return 0;
10011
10012 ptr++; /* skip the space */
10013
10014 temp = get_trash_chunk();
William Lallemand96a77852014-02-05 00:30:02 +010010015 ptr = temp->str = http_get_path_from_string(ptr);
William Lallemand65ad6e12014-01-31 15:08:02 +010010016 if (!ptr)
10017 return 0;
10018 while (*ptr != ' ' && *ptr != '\0') /* find space after URI */
10019 ptr++;
William Lallemand65ad6e12014-01-31 15:08:02 +010010020
10021 smp->data.str = *temp;
William Lallemand96a77852014-02-05 00:30:02 +010010022 smp->data.str.len = ptr - temp->str;
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010023 smp->type = SMP_T_STR;
10024 smp->flags = SMP_F_CONST;
William Lallemand65ad6e12014-01-31 15:08:02 +010010025
10026 return 1;
10027}
10028
10029
Willy Tarreaue333ec92012-04-16 16:26:40 +020010030/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +020010031 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +020010032 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +020010033 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +020010034 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +020010035 * Accepts exactly 1 argument of type string. If the input options indicate
10036 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreaub169eba2013-12-16 15:14:43 +010010037 * The returned sample is of type CSTR.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010038 */
10039static int
Willy Tarreau51539362012-05-08 12:46:28 +020010040smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010041 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010042{
10043 struct http_txn *txn = l7;
10044 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +020010045 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +020010046 const struct http_msg *msg;
10047 const char *hdr_name;
10048 int hdr_name_len;
10049 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +020010050 int occ = 0;
10051 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010052
Willy Tarreau24e32d82012-04-23 23:55:44 +020010053 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010054 return 0;
10055
Willy Tarreaua890d072013-04-02 12:01:06 +020010056 if (!ctx) {
10057 /* first call */
10058 ctx = &static_hdr_ctx;
10059 ctx->idx = 0;
10060 smp->ctx.a[2] = ctx;
10061 }
10062
Willy Tarreaue333ec92012-04-16 16:26:40 +020010063 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010064
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010065 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010066 msg = &txn->req;
10067 hdr_name = "Cookie";
10068 hdr_name_len = 6;
10069 } else {
10070 msg = &txn->rsp;
10071 hdr_name = "Set-Cookie";
10072 hdr_name_len = 10;
10073 }
10074
Willy Tarreau28376d62012-04-26 21:26:10 +020010075 if (!occ && !(opt & SMP_OPT_ITERATE))
10076 /* no explicit occurrence and single fetch => last cookie by default */
10077 occ = -1;
10078
10079 /* OK so basically here, either we want only one value and it's the
10080 * last one, or we want to iterate over all of them and we fetch the
10081 * next one.
10082 */
10083
Willy Tarreau9b28e032012-10-12 23:49:43 +020010084 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +020010085 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010086 /* search for the header from the beginning, we must first initialize
10087 * the search parameters.
10088 */
Willy Tarreau37406352012-04-23 16:16:37 +020010089 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010090 ctx->idx = 0;
10091 }
10092
Willy Tarreau28376d62012-04-26 21:26:10 +020010093 smp->flags |= SMP_F_VOL_HDR;
10094
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010095 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +020010096 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
10097 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010098 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
10099 goto out;
10100
Willy Tarreau24e32d82012-04-23 23:55:44 +020010101 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010102 continue;
10103
Willy Tarreau37406352012-04-23 16:16:37 +020010104 smp->ctx.a[0] = ctx->line + ctx->val;
10105 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010106 }
10107
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010108 smp->type = SMP_T_STR;
10109 smp->flags |= SMP_F_CONST;
Willy Tarreau37406352012-04-23 16:16:37 +020010110 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +020010111 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010112 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010113 &smp->data.str.str,
10114 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +020010115 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +020010116 found = 1;
10117 if (occ >= 0) {
10118 /* one value was returned into smp->data.str.{str,len} */
10119 smp->flags |= SMP_F_NOT_LAST;
10120 return 1;
10121 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010122 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010123 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010124 }
Willy Tarreau28376d62012-04-26 21:26:10 +020010125 /* all cookie headers and values were scanned. If we're looking for the
10126 * last occurrence, we may return it now.
10127 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010128 out:
Willy Tarreau37406352012-04-23 16:16:37 +020010129 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +020010130 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010131}
10132
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010133/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +020010134 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreaub169eba2013-12-16 15:14:43 +010010135 * multiple cookies may be parsed on the same line. The returned sample is of
10136 * type UINT. Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010137 */
10138static int
Willy Tarreau409bcde2013-01-08 00:31:00 +010010139smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010140 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010141{
10142 struct http_txn *txn = l7;
10143 struct hdr_idx *idx = &txn->hdr_idx;
10144 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010145 const struct http_msg *msg;
10146 const char *hdr_name;
10147 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010148 int cnt;
10149 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +020010150 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010151
Willy Tarreau24e32d82012-04-23 23:55:44 +020010152 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +020010153 return 0;
10154
Willy Tarreaue333ec92012-04-16 16:26:40 +020010155 CHECK_HTTP_MESSAGE_FIRST();
10156
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010157 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +020010158 msg = &txn->req;
10159 hdr_name = "Cookie";
10160 hdr_name_len = 6;
10161 } else {
10162 msg = &txn->rsp;
10163 hdr_name = "Set-Cookie";
10164 hdr_name_len = 10;
10165 }
10166
Willy Tarreau9b28e032012-10-12 23:49:43 +020010167 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +020010168 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010169 ctx.idx = 0;
10170 cnt = 0;
10171
10172 while (1) {
10173 /* Note: val_beg == NULL every time we need to fetch a new header */
10174 if (!val_beg) {
10175 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
10176 break;
10177
Willy Tarreau24e32d82012-04-23 23:55:44 +020010178 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010179 continue;
10180
10181 val_beg = ctx.line + ctx.val;
10182 val_end = val_beg + ctx.vlen;
10183 }
10184
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010185 smp->type = SMP_T_STR;
10186 smp->flags |= SMP_F_CONST;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010187 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +020010188 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020010189 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +020010190 &smp->data.str.str,
10191 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010192 cnt++;
10193 }
10194 }
10195
Willy Tarreaub169eba2013-12-16 15:14:43 +010010196 smp->type = SMP_T_UINT;
Willy Tarreauf853c462012-04-23 18:53:56 +020010197 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +020010198 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +020010199 return 1;
10200}
10201
Willy Tarreau51539362012-05-08 12:46:28 +020010202/* Fetch an cookie's integer value. The integer value is returned. It
10203 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
10204 */
10205static int
10206smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010207 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreau51539362012-05-08 12:46:28 +020010208{
Willy Tarreauef38c392013-07-22 16:29:32 +020010209 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp, kw);
Willy Tarreau51539362012-05-08 12:46:28 +020010210
10211 if (ret > 0) {
10212 smp->type = SMP_T_UINT;
10213 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10214 }
10215
10216 return ret;
10217}
10218
Willy Tarreau8797c062007-05-07 00:55:35 +020010219/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +020010220/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +020010221/************************************************************************/
10222
David Cournapeau16023ee2010-12-23 20:55:41 +090010223/*
10224 * Given a path string and its length, find the position of beginning of the
10225 * query string. Returns NULL if no query string is found in the path.
10226 *
10227 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
10228 *
10229 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
10230 */
bedis4c75cca2012-10-05 08:38:24 +020010231static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010232{
10233 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +020010234
bedis4c75cca2012-10-05 08:38:24 +020010235 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +090010236 return p ? p + 1 : NULL;
10237}
10238
bedis4c75cca2012-10-05 08:38:24 +020010239static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010240{
bedis4c75cca2012-10-05 08:38:24 +020010241 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +090010242}
10243
10244/*
10245 * Given a url parameter, find the starting position of the first occurence,
10246 * or NULL if the parameter is not found.
10247 *
10248 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
10249 * the function will return query_string+8.
10250 */
10251static char*
10252find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +020010253 char* url_param_name, size_t url_param_name_l,
10254 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010255{
10256 char *pos, *last;
10257
10258 pos = query_string;
10259 last = query_string + query_string_l - url_param_name_l - 1;
10260
10261 while (pos <= last) {
10262 if (pos[url_param_name_l] == '=') {
10263 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
10264 return pos;
10265 pos += url_param_name_l + 1;
10266 }
bedis4c75cca2012-10-05 08:38:24 +020010267 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010268 pos++;
10269 pos++;
10270 }
10271 return NULL;
10272}
10273
10274/*
10275 * Given a url parameter name, returns its value and size into *value and
10276 * *value_l respectively, and returns non-zero. If the parameter is not found,
10277 * zero is returned and value/value_l are not touched.
10278 */
10279static int
10280find_url_param_value(char* path, size_t path_l,
10281 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +020010282 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +090010283{
10284 char *query_string, *qs_end;
10285 char *arg_start;
10286 char *value_start, *value_end;
10287
bedis4c75cca2012-10-05 08:38:24 +020010288 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010289 if (!query_string)
10290 return 0;
10291
10292 qs_end = path + path_l;
10293 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +020010294 url_param_name, url_param_name_l,
10295 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +090010296 if (!arg_start)
10297 return 0;
10298
10299 value_start = arg_start + url_param_name_l + 1;
10300 value_end = value_start;
10301
bedis4c75cca2012-10-05 08:38:24 +020010302 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010303 value_end++;
10304
10305 *value = value_start;
10306 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +010010307 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +090010308}
10309
10310static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010311smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010312 const struct arg *args, struct sample *smp, const char *kw)
David Cournapeau16023ee2010-12-23 20:55:41 +090010313{
bedis4c75cca2012-10-05 08:38:24 +020010314 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +090010315 struct http_txn *txn = l7;
10316 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010317
bedis4c75cca2012-10-05 08:38:24 +020010318 if (!args || args[0].type != ARGT_STR ||
10319 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010320 return 0;
10321
10322 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +090010323
bedis4c75cca2012-10-05 08:38:24 +020010324 if (args[1].type)
10325 delim = *args[1].data.str.str;
10326
Willy Tarreau9b28e032012-10-12 23:49:43 +020010327 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +020010328 args->data.str.str, args->data.str.len,
10329 &smp->data.str.str, &smp->data.str.len,
10330 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +090010331 return 0;
10332
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010333 smp->type = SMP_T_STR;
10334 smp->flags = SMP_F_VOL_1ST | SMP_F_CONST;
David Cournapeau16023ee2010-12-23 20:55:41 +090010335 return 1;
10336}
10337
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010338/* Return the signed integer value for the specified url parameter (see url_param
10339 * above).
10340 */
10341static int
10342smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreauef38c392013-07-22 16:29:32 +020010343 const struct arg *args, struct sample *smp, const char *kw)
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010344{
Willy Tarreauef38c392013-07-22 16:29:32 +020010345 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp, kw);
Willy Tarreaua9fddca2012-07-31 07:51:48 +020010346
10347 if (ret > 0) {
10348 smp->type = SMP_T_UINT;
10349 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
10350 }
10351
10352 return ret;
10353}
10354
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010355/* This produces a 32-bit hash of the concatenation of the first occurrence of
10356 * the Host header followed by the path component if it begins with a slash ('/').
10357 * This means that '*' will not be added, resulting in exactly the first Host
10358 * entry. If no Host header is found, then the path is used. The resulting value
10359 * is hashed using the url hash followed by a full avalanche hash and provides a
10360 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
10361 * high-traffic sites without having to store whole paths.
10362 * this differs from the base32 functions in that it includes the url parameters
10363 * as well as the path
10364 */
10365static int
10366smp_fetch_url32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010367 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010368{
10369 struct http_txn *txn = l7;
10370 struct hdr_ctx ctx;
10371 unsigned int hash = 0;
10372 char *ptr, *beg, *end;
10373 int len;
10374
10375 CHECK_HTTP_MESSAGE_FIRST();
10376
10377 ctx.idx = 0;
Willy Tarreau877e78d2013-04-07 18:48:08 +020010378 if (http_find_header2("Host", 4, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010379 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
10380 ptr = ctx.line + ctx.val;
10381 len = ctx.vlen;
10382 while (len--)
10383 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
10384 }
10385
10386 /* now retrieve the path */
Willy Tarreau877e78d2013-04-07 18:48:08 +020010387 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 +000010388 beg = http_get_path(txn);
10389 if (!beg)
10390 beg = end;
10391
10392 for (ptr = beg; ptr < end ; ptr++);
10393
10394 if (beg < ptr && *beg == '/') {
10395 while (beg < ptr)
10396 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
10397 }
10398 hash = full_hash(hash);
10399
10400 smp->type = SMP_T_UINT;
10401 smp->data.uint = hash;
10402 smp->flags = SMP_F_VOL_1ST;
10403 return 1;
10404}
10405
10406/* This concatenates the source address with the 32-bit hash of the Host and
10407 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
10408 * per-url counters. The result is a binary block from 8 to 20 bytes depending
10409 * on the source address length. The URL hash is stored before the address so
10410 * that in environments where IPv6 is insignificant, truncating the output to
10411 * 8 bytes would still work.
10412 */
10413static int
10414smp_fetch_url32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreaue155ec22013-11-18 18:33:22 +010010415 const struct arg *args, struct sample *smp, const char *kw)
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010416{
10417 struct chunk *temp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010418 struct connection *cli_conn = objt_conn(l4->si[0].end);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010419
Willy Tarreaue155ec22013-11-18 18:33:22 +010010420 if (!smp_fetch_url32(px, l4, l7, opt, args, smp, kw))
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010421 return 0;
10422
10423 temp = get_trash_chunk();
10424 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
10425 temp->len += sizeof(smp->data.uint);
10426
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010427 switch (cli_conn->addr.from.ss_family) {
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010428 case AF_INET:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010429 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&cli_conn->addr.from)->sin_addr, 4);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010430 temp->len += 4;
10431 break;
10432 case AF_INET6:
Willy Tarreaub363a1f2013-10-01 10:45:07 +020010433 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)&cli_conn->addr.from)->sin6_addr, 16);
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010434 temp->len += 16;
10435 break;
10436 default:
10437 return 0;
10438 }
10439
10440 smp->data.str = *temp;
10441 smp->type = SMP_T_BIN;
10442 return 1;
10443}
10444
Willy Tarreau185b5c42012-04-26 15:11:51 +020010445/* This function is used to validate the arguments passed to any "hdr" fetch
10446 * keyword. These keywords support an optional positive or negative occurrence
10447 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
10448 * is assumed that the types are already the correct ones. Returns 0 on error,
10449 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
10450 * error message in case of error, that the caller is responsible for freeing.
10451 * The initial location must either be freeable or NULL.
10452 */
10453static int val_hdr(struct arg *arg, char **err_msg)
10454{
10455 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +020010456 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +020010457 return 0;
10458 }
10459 return 1;
10460}
10461
Willy Tarreau276fae92013-07-25 14:36:01 +020010462/* takes an UINT value on input supposed to represent the time since EPOCH,
10463 * adds an optional offset found in args[0] and emits a string representing
10464 * the date in RFC-1123/5322 format.
10465 */
10466static int sample_conv_http_date(const struct arg *args, struct sample *smp)
10467{
10468 const char day[7][4] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
10469 const char mon[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
10470 struct chunk *temp;
10471 struct tm *tm;
10472 time_t curr_date = smp->data.uint;
10473
10474 /* add offset */
10475 if (args && (args[0].type == ARGT_SINT || args[0].type == ARGT_UINT))
10476 curr_date += args[0].data.sint;
10477
10478 tm = gmtime(&curr_date);
10479
10480 temp = get_trash_chunk();
10481 temp->len = snprintf(temp->str, temp->size - temp->len,
10482 "%s, %02d %s %04d %02d:%02d:%02d GMT",
10483 day[tm->tm_wday], tm->tm_mday, mon[tm->tm_mon], 1900+tm->tm_year,
10484 tm->tm_hour, tm->tm_min, tm->tm_sec);
10485
10486 smp->data.str = *temp;
10487 smp->type = SMP_T_STR;
10488 return 1;
10489}
10490
Thierry FOURNIERad903512014-04-11 17:51:01 +020010491/* Match language range with language tag. RFC2616 14.4:
10492 *
10493 * A language-range matches a language-tag if it exactly equals
10494 * the tag, or if it exactly equals a prefix of the tag such
10495 * that the first tag character following the prefix is "-".
10496 *
10497 * Return 1 if the strings match, else return 0.
10498 */
10499static inline int language_range_match(const char *range, int range_len,
10500 const char *tag, int tag_len)
10501{
10502 const char *end = range + range_len;
10503 const char *tend = tag + tag_len;
10504 while (range < end) {
10505 if (*range == '-' && tag == tend)
10506 return 1;
10507 if (*range != *tag || tag == tend)
10508 return 0;
10509 range++;
10510 tag++;
10511 }
10512 /* Return true only if the last char of the tag is matched. */
10513 return tag == tend;
10514}
10515
10516/* Arguments: The list of expected value, the number of parts returned and the separator */
10517static int sample_conv_q_prefered(const struct arg *args, struct sample *smp)
10518{
10519 const char *al = smp->data.str.str;
10520 const char *end = al + smp->data.str.len;
10521 const char *token;
10522 int toklen;
10523 int qvalue;
10524 const char *str;
10525 const char *w;
10526 int best_q = 0;
10527
10528 /* Set the constant to the sample, because the output of the
10529 * function will be peek in the constant configuration string.
10530 */
10531 smp->flags |= SMP_F_CONST;
10532 smp->data.str.size = 0;
10533 smp->data.str.str = "";
10534 smp->data.str.len = 0;
10535
10536 /* Parse the accept language */
10537 while (1) {
10538
10539 /* Jump spaces, quit if the end is detected. */
10540 while (al < end && isspace(*al))
10541 al++;
10542 if (al >= end)
10543 break;
10544
10545 /* Start of the fisrt word. */
10546 token = al;
10547
10548 /* Look for separator: isspace(), ',' or ';'. Next value if 0 length word. */
10549 while (al < end && *al != ';' && *al != ',' && !isspace(*al))
10550 al++;
10551 if (al == token)
10552 goto expect_comma;
10553
10554 /* Length of the token. */
10555 toklen = al - token;
10556 qvalue = 1000;
10557
10558 /* Check if the token exists in the list. If the token not exists,
10559 * jump to the next token.
10560 */
10561 str = args[0].data.str.str;
10562 w = str;
10563 while (1) {
10564 if (*str == ';' || *str == '\0') {
10565 if (language_range_match(token, toklen, w, str-w))
10566 goto look_for_q;
10567 if (*str == '\0')
10568 goto expect_comma;
10569 w = str + 1;
10570 }
10571 str++;
10572 }
10573 goto expect_comma;
10574
10575look_for_q:
10576
10577 /* Jump spaces, quit if the end is detected. */
10578 while (al < end && isspace(*al))
10579 al++;
10580 if (al >= end)
10581 goto process_value;
10582
10583 /* If ',' is found, process the result */
10584 if (*al == ',')
10585 goto process_value;
10586
10587 /* If the character is different from ';', look
10588 * for the end of the header part in best effort.
10589 */
10590 if (*al != ';')
10591 goto expect_comma;
10592
10593 /* Assumes that the char is ';', now expect "q=". */
10594 al++;
10595
10596 /* Jump spaces, process value if the end is detected. */
10597 while (al < end && isspace(*al))
10598 al++;
10599 if (al >= end)
10600 goto process_value;
10601
10602 /* Expect 'q'. If no 'q', continue in best effort */
10603 if (*al != 'q')
10604 goto process_value;
10605 al++;
10606
10607 /* Jump spaces, process value if the end is detected. */
10608 while (al < end && isspace(*al))
10609 al++;
10610 if (al >= end)
10611 goto process_value;
10612
10613 /* Expect '='. If no '=', continue in best effort */
10614 if (*al != '=')
10615 goto process_value;
10616 al++;
10617
10618 /* Jump spaces, process value if the end is detected. */
10619 while (al < end && isspace(*al))
10620 al++;
10621 if (al >= end)
10622 goto process_value;
10623
10624 /* Parse the q value. */
10625 qvalue = parse_qvalue(al, &al);
10626
10627process_value:
10628
10629 /* If the new q value is the best q value, then store the associated
10630 * language in the response. If qvalue is the biggest value (1000),
10631 * break the process.
10632 */
10633 if (qvalue > best_q) {
10634 smp->data.str.str = (char *)w;
10635 smp->data.str.len = str - w;
10636 if (qvalue >= 1000)
10637 break;
10638 best_q = qvalue;
10639 }
10640
10641expect_comma:
10642
10643 /* Expect comma or end. If the end is detected, quit the loop. */
10644 while (al < end && *al != ',')
10645 al++;
10646 if (al >= end)
10647 break;
10648
10649 /* Comma is found, jump it and restart the analyzer. */
10650 al++;
10651 }
10652
10653 /* Set default value if required. */
10654 if (smp->data.str.len == 0 && args[1].type == ARGT_STR) {
10655 smp->data.str.str = args[1].data.str.str;
10656 smp->data.str.len = args[1].data.str.len;
10657 }
10658
10659 /* Return true only if a matching language was found. */
10660 return smp->data.str.len != 0;
10661}
10662
Willy Tarreau4a568972010-05-12 08:08:50 +020010663/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010664/* All supported ACL keywords must be declared here. */
10665/************************************************************************/
10666
10667/* Note: must not be declared <const> as its list will be overwritten.
10668 * Please take care of keeping this list alphabetically sorted.
10669 */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010670static struct acl_kw_list acl_kws = {ILH, {
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010671 { "base", "base", PAT_MATCH_STR },
10672 { "base_beg", "base", PAT_MATCH_BEG },
10673 { "base_dir", "base", PAT_MATCH_DIR },
10674 { "base_dom", "base", PAT_MATCH_DOM },
10675 { "base_end", "base", PAT_MATCH_END },
10676 { "base_len", "base", PAT_MATCH_LEN },
10677 { "base_reg", "base", PAT_MATCH_REG },
10678 { "base_sub", "base", PAT_MATCH_SUB },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +020010679
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010680 { "cook", "req.cook", PAT_MATCH_STR },
10681 { "cook_beg", "req.cook", PAT_MATCH_BEG },
10682 { "cook_dir", "req.cook", PAT_MATCH_DIR },
10683 { "cook_dom", "req.cook", PAT_MATCH_DOM },
10684 { "cook_end", "req.cook", PAT_MATCH_END },
10685 { "cook_len", "req.cook", PAT_MATCH_LEN },
10686 { "cook_reg", "req.cook", PAT_MATCH_REG },
10687 { "cook_sub", "req.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010688
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010689 { "hdr", "req.hdr", PAT_MATCH_STR },
10690 { "hdr_beg", "req.hdr", PAT_MATCH_BEG },
10691 { "hdr_dir", "req.hdr", PAT_MATCH_DIR },
10692 { "hdr_dom", "req.hdr", PAT_MATCH_DOM },
10693 { "hdr_end", "req.hdr", PAT_MATCH_END },
10694 { "hdr_len", "req.hdr", PAT_MATCH_LEN },
10695 { "hdr_reg", "req.hdr", PAT_MATCH_REG },
10696 { "hdr_sub", "req.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010697
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010698 /* these two declarations uses strings with list storage (in place
10699 * of tree storage). The basic match is PAT_MATCH_STR, but the indexation
10700 * and delete functions are relative to the list management. The parse
10701 * and match method are related to the corresponding fetch methods. This
10702 * is very particular ACL declaration mode.
10703 */
10704 { "http_auth_group", NULL, PAT_MATCH_STR, NULL, pat_idx_list_str, pat_del_list_ptr, NULL, pat_match_auth },
10705 { "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 +020010706
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010707 { "path", "path", PAT_MATCH_STR },
10708 { "path_beg", "path", PAT_MATCH_BEG },
10709 { "path_dir", "path", PAT_MATCH_DIR },
10710 { "path_dom", "path", PAT_MATCH_DOM },
10711 { "path_end", "path", PAT_MATCH_END },
10712 { "path_len", "path", PAT_MATCH_LEN },
10713 { "path_reg", "path", PAT_MATCH_REG },
10714 { "path_sub", "path", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010715
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010716 { "req_ver", "req.ver", PAT_MATCH_STR },
10717 { "resp_ver", "res.ver", PAT_MATCH_STR },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010718
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010719 { "scook", "res.cook", PAT_MATCH_STR },
10720 { "scook_beg", "res.cook", PAT_MATCH_BEG },
10721 { "scook_dir", "res.cook", PAT_MATCH_DIR },
10722 { "scook_dom", "res.cook", PAT_MATCH_DOM },
10723 { "scook_end", "res.cook", PAT_MATCH_END },
10724 { "scook_len", "res.cook", PAT_MATCH_LEN },
10725 { "scook_reg", "res.cook", PAT_MATCH_REG },
10726 { "scook_sub", "res.cook", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010727
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010728 { "shdr", "res.hdr", PAT_MATCH_STR },
10729 { "shdr_beg", "res.hdr", PAT_MATCH_BEG },
10730 { "shdr_dir", "res.hdr", PAT_MATCH_DIR },
10731 { "shdr_dom", "res.hdr", PAT_MATCH_DOM },
10732 { "shdr_end", "res.hdr", PAT_MATCH_END },
10733 { "shdr_len", "res.hdr", PAT_MATCH_LEN },
10734 { "shdr_reg", "res.hdr", PAT_MATCH_REG },
10735 { "shdr_sub", "res.hdr", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010736
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010737 { "url", "url", PAT_MATCH_STR },
10738 { "url_beg", "url", PAT_MATCH_BEG },
10739 { "url_dir", "url", PAT_MATCH_DIR },
10740 { "url_dom", "url", PAT_MATCH_DOM },
10741 { "url_end", "url", PAT_MATCH_END },
10742 { "url_len", "url", PAT_MATCH_LEN },
10743 { "url_reg", "url", PAT_MATCH_REG },
10744 { "url_sub", "url", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010745
Thierry FOURNIERc5a4e982014-03-05 16:07:08 +010010746 { "urlp", "urlp", PAT_MATCH_STR },
10747 { "urlp_beg", "urlp", PAT_MATCH_BEG },
10748 { "urlp_dir", "urlp", PAT_MATCH_DIR },
10749 { "urlp_dom", "urlp", PAT_MATCH_DOM },
10750 { "urlp_end", "urlp", PAT_MATCH_END },
10751 { "urlp_len", "urlp", PAT_MATCH_LEN },
10752 { "urlp_reg", "urlp", PAT_MATCH_REG },
10753 { "urlp_sub", "urlp", PAT_MATCH_SUB },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010754
Willy Tarreau8ed669b2013-01-11 15:49:37 +010010755 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +020010756}};
10757
10758/************************************************************************/
10759/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +020010760/************************************************************************/
10761/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreaudc13c112013-06-21 23:16:39 +020010762static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010763 { "base", smp_fetch_base, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010764 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10765 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
10766
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010767 { "capture.req.uri", smp_fetch_capture_req_uri, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
10768 { "capture.req.method", smp_fetch_capture_req_method, 0, NULL, SMP_T_STR, SMP_USE_HRQHP },
William Lallemand65ad6e12014-01-31 15:08:02 +010010769
William Lallemanda43ba4e2014-01-28 18:14:25 +010010770 /* capture are allocated and are permanent in the session */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010771 { "capture.req.hdr", smp_fetch_capture_header_req, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRQHP },
10772 { "capture.res.hdr", smp_fetch_capture_header_res, ARG1(1, UINT), NULL, SMP_T_STR, SMP_USE_HRSHP },
William Lallemanda43ba4e2014-01-28 18:14:25 +010010773
Willy Tarreau409bcde2013-01-08 00:31:00 +010010774 /* cookie is valid in both directions (eg: for "stick ...") but cook*
10775 * are only here to match the ACL's name, are request-only and are used
10776 * for ACL compatibility only.
10777 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010778 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
10779 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV|SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010780 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10781 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10782
10783 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
10784 * only here to match the ACL's name, are request-only and are used for
10785 * ACL compatibility only.
10786 */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010787 { "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 +010010788 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10789 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10790 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10791
Willy Tarreau0a0daec2013-04-02 22:44:58 +020010792 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010793 { "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 +010010794 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Thierry FOURNIERd4373142013-12-17 01:10:10 +010010795 { "method", smp_fetch_meth, 0, NULL, SMP_T_METH, SMP_USE_HRQHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010796 { "path", smp_fetch_path, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010797
10798 /* HTTP protocol on the request path */
10799 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010800 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010801
10802 /* HTTP version on the request path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010803 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
10804 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010805
10806 /* HTTP version on the response path */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010807 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
10808 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010809
Willy Tarreau18ed2562013-01-14 15:56:36 +010010810 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010811 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010812 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10813 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10814
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010815 { "req.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010816 { "req.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010817 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010818 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10819 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
10820 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
10821
10822 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010823 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010824 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10825 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10826
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010827 { "res.fhdr", smp_fetch_fhdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau04ff9f12013-06-10 18:39:42 +020010828 { "res.fhdr_cnt", smp_fetch_fhdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010829 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +010010830 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10831 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10832 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10833
Willy Tarreau409bcde2013-01-08 00:31:00 +010010834 /* scook is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010835 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010836 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10837 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010838 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_STR, SMP_USE_HRSHV }, /* deprecated */
Willy Tarreau409bcde2013-01-08 00:31:00 +010010839
10840 /* shdr is valid only on the response and is used for ACL compatibility */
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010841 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_STR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010842 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
10843 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
10844 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
10845
10846 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010847 { "url", smp_fetch_url, 0, NULL, SMP_T_STR, SMP_USE_HRQHV },
Neil - HAProxy List39c63c52013-11-04 13:48:42 +000010848 { "url32", smp_fetch_url32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
10849 { "url32+src", smp_fetch_url32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010850 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
10851 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
Thierry FOURNIER7654c9f2013-12-17 00:20:33 +010010852 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
10853 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +010010854 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
10855 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +020010856}};
10857
Willy Tarreau8797c062007-05-07 00:55:35 +020010858
Willy Tarreau276fae92013-07-25 14:36:01 +020010859/* Note: must not be declared <const> as its list will be overwritten */
10860static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Thierry FOURNIERad903512014-04-11 17:51:01 +020010861 { "http_date", sample_conv_http_date, ARG1(0,SINT), NULL, SMP_T_UINT, SMP_T_STR},
10862 { "language", sample_conv_q_prefered, ARG2(1,STR,STR), NULL, SMP_T_STR, SMP_T_STR},
Willy Tarreau276fae92013-07-25 14:36:01 +020010863 { NULL, NULL, 0, 0, 0 },
10864}};
10865
Willy Tarreau8797c062007-05-07 00:55:35 +020010866__attribute__((constructor))
10867static void __http_protocol_init(void)
10868{
10869 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +020010870 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau276fae92013-07-25 14:36:01 +020010871 sample_register_convs(&sample_conv_kws);
Willy Tarreau8797c062007-05-07 00:55:35 +020010872}
10873
10874
Willy Tarreau58f10d72006-12-04 02:26:12 +010010875/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020010876 * Local variables:
10877 * c-indent-level: 8
10878 * c-basic-offset: 8
10879 * End:
10880 */